你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F0系列开发之串口的使用

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
写在前面
芯片型号:stm32f030c8t6
说明:便宜,用量大
要点说明:初始化、主函数调用串口接收callback、串口接收空闲中断,串口接收中断
这种API封装方式可以拓展到其他单片机,但需要注意单片机是否支持接收空闲中断
本文注意介绍空闲中断的应用,这样就不用在定时器中计时来检测接收超时中断了

一、应用
外部定义串口接收回调
当串口数据接收完成后,在该回调中处理串口数据即可
  1. void Uart_recvCallBack(void *buf, int len)
  2. {

  3. }
复制代码

外部初始化串口
  1. myUart_initialize(115200,
  2.                                         USART_WordLength_8b,
  3.                                         USART_StopBits_1,
  4.                                         USART_Parity_No,
  5.                                         Uart_recvCallBack);
复制代码

二、源码
myUart.h
  1. #ifndef __myUart_H
  2. #define        __myUart_H

  3. #include "stm32f0xx.h"
  4. #include "stm32f0xx_usart.h"
  5. #include "stm32f0xx_gpio.h"
  6. #include "stm32f0xx_rcc.h"
  7. #include "stm32f0xx_misc.h"

  8. #define USART_REC_LEN                          200          //定义最大接收字节数

  9. typedef void (*myUart_cb)(void *buf, int len);

  10. void myUart_initialize(   uint32_t USART_BaudRate,
  11.                                                         uint32_t USART_WordLength,
  12.                                                         uint32_t USART_StopBits,
  13.                                                         uint32_t USART_Parity,
  14.                                                         myUart_cb cb);
  15. void myUart_setBaudRate(uint32_t USART_BaudRate);
  16. void myUart_sendByte(uint8_t byte);
  17. void myUart_sendBytes(uint8_t *Buffer, uint32_t Length);
  18. uint8_t myUart_recvByte(void);
  19. int fputc(int ch, FILE *f);

  20. #endif /* __UART_H */
复制代码

myUart.c
  1. #include "myUart.h"

  2. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

  3. uint8_t USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
  4. uint16_t USART_RX_count = 0;       //接收状态标记               
  5. myUart_cb myUart_callBack;
  6. /* Private functions ---------------------------------------------------------*/

  7. USART_InitTypeDef USART_InitStructure;

  8. void myUart_initialize(   uint32_t USART_BaudRate,
  9.                                                         uint32_t USART_WordLength,
  10.                                                         uint32_t USART_StopBits,
  11.                                                         uint32_t USART_Parity,
  12.                                                         myUart_cb cb)
  13. {  

  14.         GPIO_InitTypeDef  GPIO_InitStructure;
  15.         NVIC_InitTypeDef NVIC_InitStructure;

  16.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

  17.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
  18.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);        
  19.         /*
  20.         *  USART1_TX -> PA9 , USART1_RX ->        PA10
  21.         */                                
  22.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;                 
  23.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  24.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  25.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  26.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.         GPIO_Init(GPIOA, &GPIO_InitStructure);        

  28.         //Usart1 NVIC 配置
  29.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  30.         NVIC_InitStructure.NVIC_IRQChannelPriority=1 ;//抢占优先级3
  31.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
  32.         NVIC_Init(&NVIC_InitStructure);        //根据指定的参数初始化VIC寄存器

  33.         USART_InitStructure.USART_BaudRate = USART_BaudRate;//设置串口波特率
  34.         USART_InitStructure.USART_WordLength = USART_WordLength;//设置数据位
  35.         USART_InitStructure.USART_StopBits = USART_StopBits;//设置停止位
  36.         USART_InitStructure.USART_Parity = USART_Parity;//设置效验位
  37.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制
  38.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置工作模式
  39.         USART_Init(USART1, &USART_InitStructure); //配置入结构体

  40.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  41.         USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口接受超时中断
  42.         USART_Cmd(USART1, ENABLE);//使能串口1

  43.         myUart_callBack = cb;
  44. }                        
  45. void myUart_setBaudRate(uint32_t USART_BaudRate)
  46. {
  47.         USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
  48.         USART_ITConfig(USART1, USART_IT_IDLE, DISABLE);
  49.         USART_Cmd(USART1, DISABLE);

  50.         USART_InitStructure.USART_BaudRate = USART_BaudRate;//设置串口波特率
  51.         USART_Init(USART1, &USART_InitStructure); //配置入结构体

  52.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接收中断
  53.         USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口接收超时中断
  54.         USART_Cmd(USART1, ENABLE);//使能串口1
  55. }
  56. void myUart_sendByte(uint8_t byte) //发送1字节数据
  57. {
  58. while(!((USART1->ISR)&(1<<7)));
  59. USART1->TDR=byte;        
  60. }               

  61. void myUart_sendBytes(uint8_t *Buffer, uint32_t Length)
  62. {
  63.         while(Length != 0)
  64.         {
  65.                 while(!((USART1->ISR)&(1<<7)));//等待发送完
  66.                 USART1->TDR= *Buffer;
  67.                 Buffer++;
  68.                 Length--;
  69.         }
  70. }

  71. uint8_t myUart_recvByte(void)
  72. {        
  73.         while(!(USART1->ISR & (1<<5)));//等待接收到数据
  74.         return(USART1->RDR);                         //读出数据
  75. }


  76. PUTCHAR_PROTOTYPE
  77. {
  78. /* 将Printf内容发往串口 */
  79.   USART_SendData(USART1,(uint8_t)  ch);
  80.   while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
  81.         {}

  82.   return (ch);
  83. }

  84. void USART1_IRQHandler(void)                        //串口1中断服务程序
  85. {
  86.         uint8_t Res;

  87.         if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
  88.         {
  89.                 USART_ClearITPendingBit(USART1, USART_IT_IDLE);//USART_IT_IDLE需要手动清除
  90.                 if (USART_RX_count)
  91.                 {
  92.                         if (myUart_callBack)
  93.                         {
  94.                                 myUart_callBack(USART_RX_BUF, USART_RX_count);
  95.                         }
  96.                 }
  97.                 USART_RX_count = 0;
  98.         }
  99.         if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断
  100.         {
  101.                 USART_RX_BUF[USART_RX_count] = USART_ReceiveData(USART1);        //读取接收到的数据,自动清除USART_IT_RXNE中断标志
  102.                 USART_RX_count ++;
  103.         }
  104. }

复制代码

收藏 评论0 发布时间:2021-11-22 22:03

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版