[RISC-V MCU 应用开发] 第六十六章、CH32V103应用教程——USART-轮询收发模式

[复制链接]
 楼主| RISCVLAR 发表于 2021-1-26 18:37 | 显示全部楼层 |阅读模式
本帖最后由 RISCVLAR 于 2021-1-26 18:36 编辑

CH32V103应用教程——USART-轮询收发模式

本章教程主要进行串口轮询收发模式演示,使用USART2进行发送,USART3进行接收。

1、USART简介及相关函数介绍
USART使用轮询收发模式的配置步骤具体如下:

使用轮询方式发送:
1、检查USART状态寄存器(R32_USARTx_STATR)TXE位(发送数据寄存器空标志)是否为1,即数据已经被转移到移位寄存器。若不为1,等待数据发送完成;
2、向USART数据寄存器(USARTx_DATAR)写入数据;
3、继续写入剩下数据。有以下两种方式:
a:检查USART数据寄存器(USARTx_DATAR)是否已满,若未满,则继续写入要发送的数据;
b:检查USART数据寄存器(USARTx_DATAR)是否为空,若为空,则执行第二步。

使用轮询方式接收:
1、检查USART状态寄存器(R32_USARTx_STATR)RXNE位(读数据寄存器非空标志)是否为1,若为1,则数据收到,能够读出。若不为1,则数据还没收到,等待接收数据完成;
2、从数据寄存器读取数据;

关于CH32V103 USART具体信息,可参考CH32V103应用手册。USART标准库函数在第三章节已介绍,在此不再赘述。

2、硬件设计
本章教程主要进行串口轮询收发模式演示,使用USART2进行发送,USART3进行接收。将开发板USART2与USART3连接起来即可,具体连接方式如下:
硬件连线:PA2 —— PB11
      PA3 —— PB10

3软件设计
本章教程主要进行串口轮询收发模式演示,具体程序如下:
usart.h文件
  1. #ifndef __USART_H
  2. #define __USART_H

  3. #include "ch32v10x_conf.h"

  4. /* Global typedef */
  5. typedef enum
  6. {
  7.     FAILED = 0,
  8.     PASSED = !FAILED
  9. } TestStatus;

  10. void USARTx_CFG(void);
  11. TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength);

  12. #endif
usart.h文件主要进行相关定义和函数声明;
usart.c文件

  1. #include "usart.h"

  2. /*******************************************************************************
  3. * Function Name  : USARTx_CFG
  4. * Description    : Initializes the USART2 & USART3 peripheral.
  5. * Input          : None
  6. * Return         : None
  7. *******************************************************************************/
  8. void USARTx_CFG(void)
  9. {
  10.   GPIO_InitTypeDef GPIO_InitStructure;
  11.   USART_InitTypeDef USART_InitStructure;

  12.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);
  13.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB , ENABLE);

  14.   /* USART2 TX-->A.2   RX-->A.3 */
  15.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  16.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  18.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  19.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  20.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  21.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  22.   /* USART3 TX-->B.10  RX-->B.11 */
  23.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  24.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  25.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  26.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  27.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  28.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  29.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  30.   USART_InitStructure.USART_BaudRate = 115200;
  31.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  32.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  33.   USART_InitStructure.USART_Parity = USART_Parity_No;
  34.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  35.   USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

  36.   USART_Init(USART2, &USART_InitStructure);
  37.   USART_Cmd(USART2, ENABLE);

  38.   USART_Init(USART3, &USART_InitStructure);
  39.   USART_Cmd(USART3, ENABLE);
  40. }

  41. /*******************************************************************************
  42. * Function Name  : Buffercmp
  43. * Description    : Compares two buffers
  44. * Input          : Buf1,Buf2:buffers to be compared
  45. *                  BufferLength: buffer's length
  46. * Return         : PASSED: Buf1 identical to Buf2
  47. *                  FAILED: Buf1 differs from Buf2
  48. *******************************************************************************/
  49. TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
  50. {
  51.   while(BufLength--)
  52.   {
  53.     if(*Buf1 != *Buf2)
  54.     {
  55.       return FAILED;
  56.     }
  57.     Buf1++;
  58.     Buf2++;
  59.   }
  60.   return PASSED;
  61. }
usart.c文件主要包括两个函数:USARTx_CFG函数和Buffercmp函数;USARTx_CFG函数主要进行串口2和串口3的初始化配置;Buffercmp函数主要进行发送数据和接收数据的比较。
main.c文件
  1. int main(void)
  2. {
  3.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  4.     Delay_Init();
  5.     USART_Printf_Init(115200);
  6.     printf("SystemClk:%d\r\n",SystemCoreClock);

  7.     printf("USART Polling TEST\r\n");
  8.     USARTx_CFG();                                                 /* USART2 & USART3 INIT */

  9.     while(TxCnt<TxSize)
  10.     {

  11.         while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  12.         {
  13.                                                                       /* waiting for sending finish */
  14.         }
  15.         USART_SendData(USART2, TxBuffer[TxCnt++]);

  16.         while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
  17.         {
  18.                                                                     /* waiting for receiving finish */
  19.         }
  20.         RxBuffer[RxCnt++] = (USART_ReceiveData(USART3));
  21.     }

  22.     TransferStatus=Buffercmp(TxBuffer,RxBuffer,TxSize);

  23.     if(TransferStatus)
  24.     {
  25.         printf("send success!\r\n");
  26.         printf("TXBuffer: %s \r\n",TxBuffer);
  27.         printf("RxBuffer: %s \r\n",RxBuffer);
  28.     }
  29.     else
  30.     {
  31.         printf("send fail!\r\n");
  32.         printf("TXBuffer: %s \r\n",TxBuffer);
  33.         printf("RxBuffer: %s \r\n",RxBuffer);
  34.     }

  35.     while(1)
  36.     {
  37.     }
  38. }
main.c文件主要进行串口2的发送以及串口3的接收。并将串口2发送数据与串口3接收数据进行比较。

4下载验证
将编译好的程序下载到开发版并复位,串口打印如下:
图片1.png

65、USART-轮询收发模式.rar

472.73 KB, 下载次数: 239

gaoyang9992006 发表于 2021-1-26 23:46 | 显示全部楼层
楼主今晚发了不少跟串口通信有关的知识,受益匪浅啊。。。。。。。
htmlme 发表于 2021-2-21 20:29 | 显示全部楼层
使用中断习惯了。    
xinpian101 发表于 2021-2-22 10:15 | 显示全部楼层
写的好,看看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

133

主题

296

帖子

44

粉丝
快速回复 在线客服 返回列表 返回顶部

133

主题

296

帖子

44

粉丝
快速回复 在线客服 返回列表 返回顶部