为什么串口2发送不了数据

2019-08-20 17:36发布

[mw_shl_code=applescript,true]void uart2_init(u32 bound){
  //GPIO¶Ë¿ÚÉèÖÃ
  GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
         
        RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_GPIOD, ENABLE);        //ʹÄÜUSART1£¬GPIOAʱÖÓ
  
        //USART1_TX   GPIOA.9
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PA.9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //¸´ÓÃÍÆÍìÊä³ö
  GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
   
  //USART1_RX          GPIOA.10³õʼ»¯
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA10
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
  GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIOA.10  

  //Usart1 NVIC ÅäÖÃ
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //×ÓÓÅÏȼ¶3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQͨµÀʹÄÜ
        NVIC_Init(&NVIC_InitStructure);        //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
  
   //USART ³õʼ»¯ÉèÖÃ

        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_Init(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú1
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
  USART_Cmd(USART2, ENABLE);                    //ʹÄÜ´®¿Ú1

}
//初始化

void  USART2SendChar(unsigned char t)
{       
         USART_SendData(USART2,t);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);//µÈ´ý´®¿Ú·¢ËÍÍê±Ï      
}
//发送函数

[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。