草帽王子 发表于 2021-4-30 19:19:25

第六十六章:CH32V103应用教程——USART-轮询收发模式

本帖最后由 草帽王子 于 2021-9-10 17:58 编辑

本章教程主要进行串口轮询收发模式演示,使用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文件
#ifndef __USART_H
#define __USART_H

#include "ch32v10x_conf.h"

/* Global typedef */
typedef enum
{
    FAILED = 0,
    PASSED = !FAILED
} TestStatus;

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

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

#include "usart.h"

/*******************************************************************************
* Function Name: USARTx_CFG
* Description    : Initializes the USART2 & USART3 peripheral.
* Input          : None
* Return         : None
*******************************************************************************/
void USARTx_CFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

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

/* USART2 TX-->A.2   RX-->A.3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART3 TX-->B.10RX-->B.11 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 115200;
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_Tx | USART_Mode_Rx;

USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);

USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}

/*******************************************************************************
* Function Name: Buffercmp
* Description    : Compares two buffers
* Input          : Buf1,Buf2:buffers to be compared
*                  BufferLength: buffer's length
* Return         : PASSED: Buf1 identical to Buf2
*                  FAILED: Buf1 differs from Buf2
*******************************************************************************/
TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
{
while(BufLength--)
{
    if(*Buf1 != *Buf2)
    {
      return FAILED;
    }
    Buf1++;
    Buf2++;
}
return PASSED;
}usart.c文件主要包括两个函数:USARTx_CFG函数和Buffercmp函数;USARTx_CFG函数主要进行串口2和串口3的初始化配置;Buffercmp函数主要进行发送数据和接收数据的比较。
main.c文件
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    Delay_Init();
    USART_Printf_Init(115200);
    printf("SystemClk:%d\r\n",SystemCoreClock);

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

    while(TxCnt<TxSize)
    {

      while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
      {
                                                                      /* waiting for sending finish */
      }
      USART_SendData(USART2, TxBuffer);

      while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
      {
                                                                  /* waiting for receiving finish */
      }
      RxBuffer = (USART_ReceiveData(USART3));
    }

    TransferStatus=Buffercmp(TxBuffer,RxBuffer,TxSize);

    if(TransferStatus)
    {
      printf("send success!\r\n");
      printf("TXBuffer: %s \r\n",TxBuffer);
      printf("RxBuffer: %s \r\n",RxBuffer);
    }
    else
    {
      printf("send fail!\r\n");
      printf("TXBuffer: %s \r\n",TxBuffer);
      printf("RxBuffer: %s \r\n",RxBuffer);
    }

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


4、下载验证

将编译好的程序下载到开发版并复位,串口打印如下:

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

链接:https://pan.baidu.com/s/1X6hiYJrpMyQHIfk-h7Zzqw
提取码:0jqf
复制这段内容后打开百度网盘手机App,操作更方便哦



页: [1]
查看完整版本: 第六十六章:CH32V103应用教程——USART-轮询收发模式