STM32L151C8的USART2死活不能用,求助!

2019-12-18 18:46发布

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); //使能串口
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
zhcj66
2019-12-18 23:36
下面这段代码是可以用的,如果不能用,只能说明硬件出现问题了

  1. void UART2_Init(unsigned int bsp)
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.         USART_InitTypeDef USART_InitStructure;
  5.        
  6.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE );

  7.         /* Configure USART2 Tx (PA.02) as alternate function push-pull */
  8.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  9.           GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  10.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  11.           GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  12.           GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  13.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  14.         /* Configure USART2 Rx (PA.03) as input floating */
  15.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  16.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  17.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  18.        
  19.         GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);//PA2-TX
  20.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//PA3-RX

  21.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
  22.          
  23.         USART_InitStructure.USART_BaudRate            = bsp;
  24.         USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
  25.         USART_InitStructure.USART_StopBits            = USART_StopBits_1;
  26.         USART_InitStructure.USART_Parity              = USART_Parity_No ;
  27.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  28.         USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
  29.         USART_Init(USART2, &USART_InitStructure);
  30.         USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  31.         USART_Cmd(USART2, ENABLE);
  32. }


  33. void Sys_NVIC_Init(void)
  34. {
  35.         NVIC_InitTypeDef NVIC_InitStructure;
  36. //
  37. #ifdef  VECT_TAB_RAM  
  38.   /* Set the Vector Table base location at 0x20000000 */
  39.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  40. #else  /* VECT_TAB_FLASH  */
  41.   /* Set the Vector Table base location at 0x08000000 */
  42.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  43. #endif
  44.        
  45.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  46.        
  47.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  48.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  49.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  50.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  51.         NVIC_Init(&NVIC_InitStructure);
  52.        
  53.         NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  54.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  55.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  56.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  57.         NVIC_Init(&NVIC_InitStructure);
  58. }


  59. void UART2_Putc(unsigned char data)
  60. {
  61.         while (!(USART2->SR & USART_FLAG_TXE));
  62.         USART2->DR = (data & (uint16_t)0x01FF);
  63. }

  64. void UART2_Puts(char * str)
  65. {
  66.         while(*str)
  67.         {
  68.                 while (!(USART2->SR & USART_FLAG_TXE)){}
  69.                    USART2->DR = (*str & (uint16_t)0x01FF);
  70.                 str++;
  71.         }
  72. }



  73. void USART2_IRQHandler(void)
  74. {
  75. unsigned char i;
  76.         if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  77.         {        
  78.                  i= USART2->DR;
  79.                 UART2_Putc(i);
  80.         }}
复制代码

一周热门 更多>