×
单片机 > 单片机程序设计 > 详情

STM32单片机串口功能实现

发布时间:2020-06-16 发布时间:
|

STM32学习笔记心得四:


=============串口通信================

1.通信方式

  处理器和外围设备进行通信的方式:并行通信和串行通信

  

  并行通信:

  传输原理:数据的每一个位都在同时传输

  优点:    速度快

  缺点:    占用很多引脚资源



  

  串行通信:

  传输原理:数据按位的顺序传输

  优点:    占用引脚少

  缺点:    传输速度慢

  

--------------------------------------------------

 对于大部分的设备来讲,引脚资源是有限的,所以在通信方面要节约IO口

 串口作为MCU的重要外接设备,同时也作为软件调试手段

 

串行通信按照传送的方向分几种:

单工  :数据在传输的时候只支持一个方向的传输

半双工:允许数据在两个方向传输,但是在同一时刻只允许数据一个方向传输

全双工:允许数据在两个方向传输,但是设备要具有收发功能


----------------------------------------------------

串行通信又分为同步通信和异步通信


同步通信:带时钟的同步信号去传输,比如IIC和SPI


异步通信:不带时钟的同步信号,如UART(通用的异步收发设备)


   通信标准     引脚        通信方式    通信方向

    UART      TXD:发送端     异步通信    全双工

              RXD:接收端

    IIC       SCL:同步时钟   同步通信    半双工

              SDA:数据IO口


===================================================

串口应该怎么使用(七步曲)

1.使能串口时钟+使能GPIO时钟

2.设置引脚的复位映射

3.配置GPIO引脚初始化,GPIO引脚的模式改为复用模式

4.配置串口的参数

5.中断优先级+配置NVIC初始化

6.使能串口

7.编写中断服务函数


---------------------------------

1.硬件图   (day04/串口.png)


2.

  a.使能串口时钟 RCC_APB2PeriphClockCmd

   void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)

   参数1:uint32_t RCC_APB2Periph   串口号

          RCC_APB2Periph_USART1

   参数2:FunctionalState NewState

          ENABLE 开启使能  DISABLE 关闭使能

          

  b.设置引脚的复位映射 GPIO_PinAFConfig()

   void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)

   参数1:GPIO_TypeDef* GPIOx     组别

          GPIOA  ---GPIOG

   参数2:uint16_t GPIO_PinSource  引脚号

          GPIO_PinSourcex where x can be (0..15).

   参数3:uint8_t GPIO_AF

          @arg GPIO_AF_USART1: Connect USART1 pins to AF7

          @arg GPIO_AF_USART2: Connect USART2 pins to AF7

          @arg GPIO_AF_USART3: Connect USART3 pins to AF7

          @arg GPIO_AF_UART4: Connect UART4 pins to AF8

          @arg GPIO_AF_UART5: Connect UART5 pins to AF8

          @arg GPIO_AF_USART6: Connect USART6 pins to AF8

          @arg GPIO_AF_UART7: Connect UART7 pins to AF8

          @arg GPIO_AF_UART8: Connect UART8 pins to AF8

        等下配置复用功能 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;//复用模式

    

  c.配置串口的参数  USART_Init

    void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)

    参数1:USART_TypeDef* USARTx    串口号

           USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8

    参数2:USART_InitTypeDef* USART_InitStruct

        typedef struct

        {

          uint32_t USART_BaudRate;            /*!< 波特率 */


          uint16_t USART_WordLength;          /*!< 字节长度 */


          uint16_t USART_StopBits;            /*!< 停止位 */


          uint16_t USART_Parity;              /*!< 奇偶校验位 */

         

          uint16_t USART_Mode;                /*!< 模式配置*/


          uint16_t USART_HardwareFlowControl; /*!< 硬件控制流 */

        } USART_InitTypeDef; 

        

        1.常用的波特率 是9600 115200

        

        2.

        /** @defgroup USART_Word_Length 

          * @{

          */       

          #define USART_WordLength_8b  ((uint16_t)0x0000)//8位

          #define USART_WordLength_9b  ((uint16_t)0x1000)//9位


        3.

        /** @defgroup USART_Stop_Bits 

          * @{

          */ 

        #define USART_StopBits_1   ((uint16_t)0x0000)

        #define USART_StopBits_0_5 ((uint16_t)0x1000)

        #define USART_StopBits_2   ((uint16_t)0x2000)

        #define USART_StopBits_1_5 ((uint16_t)0x3000)


        4./** @defgroup USART_Parity 

          * @{

          */ 

          

        #define USART_Parity_No  ((uint16_t)0x0000)//常用

        #define USART_Parity_Even((uint16_t)0x0400)//奇校验

        #define USART_Parity_Odd ((uint16_t)0x0600)//偶校验

        

        5.

        /** @defgroup USART_Mode 

          * @{

          */ 

          

        #define USART_Mode_Rx ((uint16_t)0x0004)//接收

        #define USART_Mode_Tx ((uint16_t)0x0008)//发送

 

        6.

        /** @defgroup USART_Hardware_Flow_Control 

          * @{

          */ 

        #define USART_HardwareFlowControl_None       ((uint16_t)0x0000)//无流控

        #define USART_HardwareFlowControl_RTS        ((uint16_t)0x0100)

        #define USART_HardwareFlowControl_CTS        ((uint16_t)0x0200)

        #define USART_HardwareFlowControl_RTS_CTS    ((uint16_t)0x0300)


   d.中断优先级+配置NVIC初始化

     void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)

     参数:uint32_t NVIC_PriorityGroup

     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority

                                4 bits for subpriority

     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority

                                3 bits for subpriority

     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority

                                2 bits for subpriority

     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority

                                1 bits for subpriority

     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority

                               0 bits for subpriority


    配置中断分组  NVIC_Init()

   void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)

    typedef struct

    {

      uint8_t NVIC_IRQChannel;                    /*!


      uint8_t NVIC_IRQChannelPreemptionPriority;  /*!< 抢占优先级 */


      uint8_t NVIC_IRQChannelSubPriority;         /*!< 响应优先级*/


      FunctionalState NVIC_IRQChannelCmd;         /*!

    } NVIC_InitTypeDef;

 

    1.

      USART1_IRQn                 = 37,     /*!< USART1 global Interrupt                                           */

USART2_IRQn = 38, /*!< USART2 global Interrupt */


关键字:STM32  单片机  串口功能 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
mu;C/OS-Ⅱ在ARM系列单片机S3C44B0x上的移植