USART1各项工作正常,按照同样的流程配置USART2,就是不能收发数据。难道还有特殊的地方?请坛友支支招。
配置函数如下:
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;//①串口时钟使能,GPIO 时钟使能,复用时钟使能
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能 USART1,GPIOA 时钟//②串口复位
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能 USART1,GPIOA 时钟//②串口复位
USART_DeInit(USART1); //复位串口 1
USART_DeInit(USART2); //复位串口 2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_10; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);//PA10-RX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_USART1);//PA9-TX
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//PA3-RX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);//PA2-TX
USART_InitStructure.USART_BaudRate = bound; //波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为 8 位
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_Mode_Rx |
USART_Init(USART1, &USART_InitStructure); //初始化串口
USART_Init(USART2, &USART_InitStructure); //初始化串口
//⑤开启中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启中断
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //开启中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //开启中断
//USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //开启中断
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ; //抢占优先级 3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
//⑤初始化 NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ; //抢占优先级 3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
//⑥使能串口*/
USART_Cmd(USART1, ENABLE); //使能串口
USART_Cmd(USART2, ENABLE); //使能串口
}
- void UART2_Init(unsigned int bsp)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE );
- /* Configure USART2 Tx (PA.02) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USART2 Rx (PA.03) as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);//PA2-TX
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//PA3-RX
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
-
- USART_InitStructure.USART_BaudRate = bsp;
- 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_RXNE, ENABLE);
- USART_Cmd(USART2, ENABLE);
- }
- void Sys_NVIC_Init(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- //
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void UART2_Putc(unsigned char data)
- {
- while (!(USART2->SR & USART_FLAG_TXE));
- USART2->DR = (data & (uint16_t)0x01FF);
- }
- void UART2_Puts(char * str)
- {
- while(*str)
- {
- while (!(USART2->SR & USART_FLAG_TXE)){}
- USART2->DR = (*str & (uint16_t)0x01FF);
- str++;
- }
- }
- void USART2_IRQHandler(void)
- {
- unsigned char i;
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- i= USART2->DR;
- UART2_Putc(i);
- }}
复制代码一周热门 更多>