STM32F042F6P 串口2问题
最近用STM32F042F6P用于双串口通信,串口1通信正常,串口2一直调试不成功,不知道是什么问题,故求助大神们;串口2的配置信息如下:void RCC_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//设置A端口时钟使能
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);//设置B端口时钟使能
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);//设置F端口时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //设置串口1时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//设置串口2时钟使能
}
void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//gpio配置
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);//PA2复用功能打开
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); //PA3复用功能打开
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART2_TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.3) as input mode */
GPIO_InitStructure.GPIO_Pin = USART2_RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART2 mode config */
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
//打开空闲中断
//USART_ITConfig(USART2,USART_IT_IDLE, ENABLE);
//打开接收中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC);
//允许UART2中断
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Uart2PutChar(unsigned char ch)
{
USART_SendData(USART2,ch);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
}
void Send2_Mesg(void)
{
Uart2PutChar('F');
Uart2PutChar('I');
Uart2PutChar('S');
Uart2PutChar('R');
Uart2PutChar('T');
Uart2PutChar('0');
Uart2PutChar('2');
Uart2PutChar('\n');
}
楼主应该先检查一下USART2的硬件连接是否正常,然后才进入软件的故障排除步骤。 楼主需要描述问题更清晰,不成功是接收发送么有数据吗?如实是没有数据中间检查硬件,可以先吧PA2和PA2设置为GPIO,设置高低输出,查看GPIO驱动是否正常,如果正常使用示波器查看串口波形,是否与串口数据一致。如果是接收或者发送乱码,重点查看波特率设置是否一致或者晶振使用是否正确。 串口二引脚宏定义部分是否修改一致了? GPIO_InitStructure.GPIO_Pin = USART2_TX; GPIO_InitStructure.GPIO_Pin = USART2_RX;
页:
[1]