股票

STM32串口初始化

使用printf从串口打印输出

  1. #ifdef __GNUC__
  2.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  3.   set to ‘Yes’) calls __io_putchar() */
  4.     #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  5. #else
  6.     #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  7. #endif /* __GNUC__ */
  8.   /*******************************************************************************
  9.   * Function Name   : USART_RCC_Configuration
  10.   * Description     : open USART clock for GPIOA.
  11.   *                   There is no need to open peripheral clock, if it is default
  12.   *                   for some GPIO.If not, it is need to open.
  13.   * Input           : None
  14.   * Output          : None
  15.   * Return          : None
  16.   *******************************************************************************/
  17. void USART_RCC_Configuration(void)
  18. {
  19.     //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);  // start peripheral clock
  20.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  21.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  22. }
  23.   /*******************************************************************************
  24.   * Function Name   : USART_GPIO_Configuration
  25.   * Description     : initialize USART2 GPIO mode
  26.   * Input           : None
  27.   * Output          : None
  28.   * Return          : None
  29.   *******************************************************************************/
  30. void USART_GPIO_Configuration(void)
  31. {
  32.     GPIO_InitTypeDef GPIO_InitStruct;
  33.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  34.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
  35.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  36.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  37.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  38.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
  39.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  40. }
  41.   /*******************************************************************************
  42.   * Function Name   : USART_Configuration
  43.   * Description     : initialize USART2
  44.   * Input           : None
  45.   * Output          : None
  46.   * Return          : None
  47.   *******************************************************************************/
  48. void USART_Configuration(void)
  49. {
  50.     USART_InitTypeDef USART_InitStruct;
  51.     USART_RCC_Configuration();
  52.     USART_InitStruct.USART_BaudRate = 115200;
  53.     USART_InitStruct.USART_StopBits = USART_StopBits_1;
  54.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  55.     USART_InitStruct.USART_Parity = USART_Parity_No;
  56.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  57.     USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  58.     USART_Init(USART2, &USART_InitStruct);
  59.     //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// enable receive interrupt
  60.     USART_Cmd(USART2, ENABLE);  // enable USART1
  61.     USART_GPIO_Configuration();
  62. }
  63.   /*******************************************************************************
  64.   * Function Name   : USART_Configuration
  65.   * Description     : initialize USART1 printf
  66.   * Input           : None
  67.   * Output          : None
  68.   * Return          : None
  69.   *******************************************************************************/
  70. PUTCHAR_PROTOTYPE
  71. {
  72.     /* Place your implemetation of fputc here*/
  73.     /* e.g. wirite a character to the USART */
  74.     USART_SendData(USART2, (uint8_t)ch);
  75.     /* Loop until the end of transmission */
  76.     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  77.         ;
  78.     return ch;
  79. }

简单使用为

  1. /*******************************************************************************
  2.   * Function Name   : USART_Configuration
  3.   * Description     : initialize USART1
  4.   * Input           : None
  5.   * Output          : None
  6.   * Return          : None
  7.   *******************************************************************************/
  8. uint8_t USART_GetChar(void)
  9. {
  10.     uint8_t ch=0;
  11.     while(!(USART2->SR & USART_FLAG_RXNE))
  12.         ;
  13.     ch = USART_ReceiveData(USART2);
  14.     return ch;
  15. }
  16.   /*******************************************************************************
  17.   * Function Name   : USART_SendChar
  18.   * Description     : send char by USART2
  19.   * Input           : None
  20.   * Output          : None
  21.   * Return          : None
  22.   *******************************************************************************/
  23. void USART_SendChar(uint8_t data)
  24. {
  25.     /* Loop until the end of transmission */
  26.     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  27.         ;
  28.     USART_SendData(USART2, data);
  29. }
打赏
原文链接:,转发请注明来源!

发表评论