• STM32-串口通信基本流程


    串口功能讲解

    串口框图

    对应的板载引脚,我的是STM32F103VET6

    引脚 APB2总线 APB1总线 APB1总线 APB1总线 APB1总线
    串口 USART1 USART2 USART3 USART4 USART5
    TXD PA9 PA2 PB10 PC10 PC12
    RXD PA10 PA3 PB11 PC11 PD2
    SCLK PA8 PA4 PB12
    NCTS PA11 PA0 PB13
    NRTS PA12 PA1 PB14

    TXD:数据发送

    RXD:数据接收

    SCLK:时钟,仅同步通信时使用

    nRTS:请求发送(Request to send)

    nCTS:允许发送(CLear to send)

    注意:STM32F103VET6 系统控制器有三个 USART 和两个 UART,其中 USART1 和时钟来源 于 APB2 总线时钟,其最大频率为 72MHz,其他四个的时钟来源于 APB1 总线时钟,其最 大频率为 36MHz。UART 只是异步传输功能,所以没有 SCLK、nCTS 和 nRTS 功能引脚。

    数据寄存器——USART_DR:9位有效,包含一个发送数据寄存器IDR和一个接收数据寄存器RDR,一个地址对应了两个物理内存。

    数据寄存器

    数据寄存器——USART_DR:只有底9位有效,包含一个发送数据寄存器TDR和一个接收数据寄存器RDR。一个地址对应了两个物理内存。

    数据格式

    (启动位)USART_CR1:M,0:8bit 1:9bit

    (停止位)USART_CR2:STOP

    (校验位)USATR_CR1:PCE(使能校验),PS(选择校验),PEIE

    (校验检测)USART_SR:PE(校验错误)

    具体流程

    数据发送

    UE:USART使能(USART enable) 0:时钟与输出被禁止 1:模块使能

    当该位被清零 .

    TE:发送使能(使能后 串口可以发送数据)

    内存→(读取)CPU或DMA→发送数据寄存器(TDR)→发送移位寄存器→TX

    TXE(发送数据寄存器空)→TXC(发送移位寄存器完成)

    数据接收

    UE:USART使能(USART enable) 0:时钟与输出被禁止 1:模块使能

    当该位被清零 .

    RE:接收使能(使能后 串口可以接收数据).

    RX→接收移位寄存器→接收数据寄存器(RXNE)

    波特率

    波特率:每秒钟要发送多少数据

    USART_BRR:波特率寄存器

    分数波特率的产生

    Tx / Rx 波特率 =fck/16*USARTDIV

    fck:外设的时钟(PCLK1用于USART2、3、4、5,PCLK2用于USART1)

    注意需要区分APB1 APB2 两条总线的时钟

    USARTDIV:无符号的定点数 这12位的值设置在USART_BRR寄存器

    列如:串口1

    USART:USART1 时钟为72M

    波特率:115200

    115200 = 72000000/16*USARTDIV

    USARTDIV = 39.0625

    DIV_Fraction = 0.0625*16 = 1 = 0x01

    DIV_Mantissa = 39 = 0x17

    则:USART_BRR = 0X171

    基本结构体

    上面是编写流程 现在进入实战编写代码

    串口初始化结构体

    typedef struct
    {
     //串口波特率   115200
      uint32_t USART_BaudRate;           
    /*!< This member configures the USART communication baud rate.The baud rate is computed using the following formula:- IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate))- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
     //字长  控制寄存器M位 
      uint16_t USART_WordLength;         
     /*!< Specifies the number of data bits transmitted or received in a frame.
    This parameter can be a value of @ref USART_Word_Length */
    //停止位
      uint16_t USART_StopBits;            
     /*!< Specifies the number of stop bits transmitted.This parameter can be a value of @ref USART_Stop_Bits */
    //校验位
      uint16_t USART_Parity;              
      /*!< Specifies the parity mode.This parameter can be a value of @ref USART_Parity @note When parity is enabled, the computed parity is inserted
    at the MSB position of the transmitted data (9th bit whenthe word length is set to 9 data bits; 8th bit when the word length is set to 8 data bits). */
    //模式(发送与接收)
      uint16_t USART_Mode;                
    /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
    This parameter can be a value of @ref USART_Mode */
     //硬件控制流  中断
      uint16_t USART_HardwareFlowControl;
     /*!< Specifies wether the hardware flow control mode is enabledor disabled.
     This parameter can be a value of @ref USART_Hardware_Flow_Control */
    } USART_InitTypeDef;
    

    串口时钟配置(同步还是异步通讯)

    typedef struct
    {
     
      uint16_t USART_Clock;   /*!< Specifies whether the USART clock is enabled or disabled.
                                   This parameter can be a value of @ref USART_Clock */
    //极性
      uint16_t USART_CPOL;    /*!< Specifies the steady state value of the serial clock.
    //相位                               This parameter can be a value of @ref USART_Clock_Polarity */
    
      uint16_t USART_CPHA;    /*!< Specifies the clock transition on which the bit capture is made.
                                   This parameter can be a value of @ref USART_Clock_Phase */
    
      uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
                                   data bit (MSB) has to be output on the SCLK pin in synchronous mode.
                                   This parameter can be a value of @ref USART_Last_Bit */
    } USART_ClockInitTypeDef;
    
  • 相关阅读:
    pycharm2016.2.3注册码到期, 激活, 破解版
    CSS图片垂直居中方法
    C# 与 SQLite的操作
    C#操作Dataset数据集与SQLite数据库
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法
    从谎言看女性营销心理【转】
    Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.【转】
    新房收房时哪些注意事项--装修史上最细规则【转】
    人际沟通最忌讳一脸死相【转】
    比特币
  • 原文地址:https://www.cnblogs.com/Serendipitychen/p/13912030.html
Copyright © 2020-2023  润新知