当前位置:   article > 正文

STM32使用USART基础(含printf重定义)(标准库)_stm32 usart_clearflag

stm32 usart_clearflag

配置USART

  1. void USART1_Configuration(void)
  2. {
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. USART_InitTypeDef USART_InitStructure;
  5. NVIC_InitTypeDef NVICinitStucture;
  6. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
  7. /*
  8. * USART1_TX -> PA9 , USART1_RX -> PA10
  9. */
  10. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(GPIOA, &GPIO_InitStructure);
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  15. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  16. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17. GPIO_Init(GPIOA, &GPIO_InitStructure);
  18. USART_InitStructure.USART_BaudRate = 9600;
  19. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  20. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  21. USART_InitStructure.USART_Parity = USART_Parity_No;
  22. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  23. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  24. USART_Init(USART1, &USART_InitStructure);
  25. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收中断
  26. NVICinitStucture.NVIC_IRQChannel =USART1_IRQn;//中断通道,选择串口中断
  27. NVICinitStucture.NVIC_IRQChannelPreemptionPriority=1;//配置中断优先级
  28. NVICinitStucture.NVIC_IRQChannelCmd =ENABLE;//使能打开
  29. NVICinitStucture.NVIC_IRQChannelSubPriority =0;//配置中断子优先级
  30. NVIC_Init(&NVICinitStucture);//串口结构体初始化
  31. USART_Cmd(USART1, ENABLE);
  32. }

注意:若要使用中断组来设置中断优先级,则需要使用NVIC_Config(),如果只需要实现串口通信,可以把NVIC_Config()注释掉,使用USART_ITConfig(USART1, USART_IT_TXE, ENABLE)。两个函数不能同时使用,否则会出现错误

发送字符串

  1. void USART_sendstring(USART_TypeDef* USARTx, char *String)
  2. {
  3. int i = 0;
  4. USART_ClearFlag(USARTx,USART_FLAG_TC); //清空传输完成标志
  5. while(String[i] != '\0') //字符串结束符
  6. {
  7. USART_SendData(USARTx,String[i]); //每次发送字符串的一个字符
  8. while(USART_GetFlagStatus(USARTx,USART_FLAG_TC) == 0); //等待数据发送成功
  9. i++;
  10. }
  11. }
  12. /*
  13. USART_ClearFlag可选的USART_FLAG
  14. USART_FLAG_CTS: CTS 更改标志(不适用于 UART4 和 UART5)。
  15. USART_FLAG_LBD: LIN中断检测标志
  16. USART_FLAG_TC:传输完成标志
  17. USART_FLAG_RXNE: 接收数据寄存器不为空标志
  18. */

stm32f10x_it.c里编写中断函数,中断函数名要对应startup_stm32f10x_hd.s里定义的中断入口名称,例如使用USRAT1对应的中断函数名就为USART1_IRQHandler

  1. void USART1_IRQHandler()
  2. {
  3. unsigned char ReceivedData;
  4. if (USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
  5. {
  6. ReceivedData = USART_ReceiveData( USART1 );
  7. USART_SendData(USART1,ReceivedData);
  8. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  9. }
  10. }

在首尾加入如下代码即可使用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. ********************************************************
  10. void main()
  11. {
  12. }
  13. ********************************************************
  14. */
  15. /**
  16. * @brief Retargets the C library printf function to the USART.
  17. * @param None
  18. * @retval None
  19. */
  20. PUTCHAR_PROTOTYPE
  21. {
  22. /* Place your implementation of fputc here */
  23. /* e.g. write a character to the USART */
  24. USART_SendData(USART1, (uint8_t) ch);
  25. /* Loop until the end of transmission */
  26. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  27. {}
  28. return ch;
  29. }
  30. #ifdef USE_FULL_ASSERT
  31. /**
  32. * @brief Reports the name of the source file and the source line number
  33. * where the assert_param error has occurred.
  34. * @param file: pointer to the source file name
  35. * @param line: assert_param error line source number
  36. * @retval None
  37. */
  38. void assert_failed(uint8_t* file, uint32_t line)
  39. {
  40. /* User can add his own implementation to report the file name and line number,
  41. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  42. /* Infinite loop */
  43. while (1)
  44. {
  45. }
  46. }
  47. #endif

奉上main.c

  1. void Delay (uint32_t nCount)
  2. {
  3. for(; nCount != 0; nCount--);
  4. }
  5. int main()
  6. {
  7. char wstr[] ="成功了";
  8. char success[]="完成了";
  9. char i=0;
  10. NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4);
  11. USART1_Configuration();
  12. while(i<5)
  13. {
  14. USART_sendstring(USART1,wstr);
  15. Delay (0xffffff);
  16. USART_sendstring(USART1,success);
  17. Delay (0xffffff);
  18. i++;
  19. }
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/281040
推荐阅读
相关标签
  

闽ICP备14008679号