• 三.STM32F030C8T6 MCU开发之UART配置例程


    三.STM32F030C8T6 MCU开发之UART配置例程

    0.总体功能概述

    使用STD库–en.stm32f0_stdperiph_lib_v1.6.0。

    1.UART硬件

    USART的全称是universal synchronous asynchronous receiver and transmitte,中文名叫做通用同步异步收发器。USART是一种非常重要的通信协议,单片机和很多的外设的通信都是用的串口通信,这是一种全双工的异步通信,发送端称为TXD(Transmit Data),接收端称为RXD(Receive Data)。对于串口通信最重要的就是五个量,起始位、数据位,停止位,校验位以及波特率。

    2.UART软件配置

    2.1 UART CLK IO配置

    void STM_EVAL_COMInit( USART_InitTypeDef* USART_InitStruct)
    {
      GPIO_InitTypeDef GPIO_InitStructure;
    
      /* Enable GPIO clock */
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
    
      /* Enable USART clock */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
    
      /* Connect PXx to USARTx_Tx */
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_1);
    
      /* Connect PXx to USARTx_Rx */
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_1);
      
      /* Configure USART Tx as alternate function push-pull */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
        
      /* Configure USART Rx as alternate function push-pull */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
      GPIO_Init(GPIOA,&GPIO_InitStructure);
    
      /* USART configuration */
      USART_Init(USART2, USART_InitStruct);
        
      /* Enable USART */
      USART_Cmd(USART2, ENABLE);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2.2 UART 配置

    串口打印

    1.设置串口参数

    2.重定向

    3.勾选use mcrolib

    2.2.1 通信波特率配置–通过波特率计算数据传输时间

    有关波特率9600 bits per second涉及的传输时间计算

    波特率是串口传输速率的关键作用参数,9600bps就是每秒传输9600bit(位)的意思,也就相当于:1/9600=1.041666666666667e-4秒为每个bit的传输时间

    实际项目中,串口通信时数据格式是:起始位+8位数据+奇偶校验位+停止位 ,一般都没有奇偶校验位,所以是10位

    一帧数据包括:

    1个起始位:
    8个数据位;
    1个停止位;
    总共10bits;

    现有12bytes数据要发送因此有12*10bits发送;
    1bits的发送时间 = 1000 / 9600 (bit / ms)
    发送12bytes数据的时间 = 12 * 10 * (1000 / 9600) ms = 12.5ms;

    数据是低位(LSB传输)在前

    2.2.2 UART1/2具体配置
    	/**
      * @brief Configure the USART Device 9600波特率 1位停止位,8位数据,无校验
      * @param  None
      * @retval None
      */
    static void USART_Config(void)
    {
      USART_InitTypeDef USART_InitStructure;
        
    /* USARTx configured as follow:
      - BaudRate = 9600 baud  
      - Word Length = 8 Bits
      - Two Stop Bit
      - Odd parity
      - Hardware flow control disabled (RTS and CTS signals)
      - Receive and transmit enabled
      */
      USART_InitStructure.USART_BaudRate = 9600;
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      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;
      
      STM_EVAL_COMInit(&USART_InitStructure);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    2.2.3 重定向
    int fputc(int ch, FILE *f)  
    {  
      USART_SendData(USART1,ch);  
      while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);  
      return(ch);  
    }  
    
    
    int fgetc(FILE *f)  
    {  
      while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);  
    
      return (int)USART_ReceiveData(USART1);  
    } 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    E *f)
    {
    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    return (int)USART_ReceiveData(USART1);
    }

    
    #### 2.2.4 勾选microLib
    
    • 1
    • 2
  • 相关阅读:
    作为编程的我们,针对我们的职业,初入职场要如何规划呢?
    2022 全球 AI 模型周报
    无人机航测拍摄分类和注意事项
    职场经验分享--接口中按时间戳查数据容易被忽略的细节
    斯伯克CYBELEC触摸屏维修CybTouch12折弯机特点
    autoware之轮式里程计计算
    Git 行尾设置须知
    最简单的tab选项卡
    MySQL——备份和还原
    8┃音视频直播系统之 WebRTC 信令系统实现以及通讯核心并实现视频通话
  • 原文地址:https://blog.csdn.net/xushx_bigbear/article/details/127815787