打印
[RISC-V MCU 应用开发]

第六十四章、CH32V103应用教程——USART-中断

[复制链接]
1831|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
USART, ST, IO, ni, pi
CH32V103应用教程——USART-中断

本章教程使用串口2(USART2)和串口3(USART3)进行查询发送和中断接收。

1、USART简介及相关函数介绍
USART模块支持多种中断源,包括发送数据寄存器空(TXE)、CTS、发送完成(TC)、接收数据就绪(TXNE)、数据溢出(ORE)、线路空闲(IDLE)、奇偶校验出错(PE)、断开标志(LBD)、噪声(NE)、多缓冲通信的溢出(ORT)和帧错误(FE)等等。
下图为中断和对应的使能位的关系:
注意:EIE使能位只有在DMA接收数据时使用。
关于CH32V103 USART具体信息,可参考CH32V103应用手册。USART标准库函数在第三章节已介绍,在此不再赘述。

2、硬件设计
本章教程使用串口2(USART2)和串口3(USART3)进行查询发送和中断接收。将开发板USART2与USART3连接起来即可,具体连接方式如下:
硬件连线:PA2 —— PB11
      PA3 —— PB10

3软件设计
本章教程使用串口2(USART2)和串口3(USART3)进行查询发送和中断接收,具体程序如下:
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);

#endif
usart.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;
    NVIC_InitTypeDef  NVIC_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.10  RX-->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_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    USART_Init(USART3, &USART_InitStructure);
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(USART2, ENABLE);
    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函数主要进行USART2和USART3的初始化配置:包括USART2和USART3对应GPIO的初始化配置以及串口和NVIC相关配置;Buffercmp函数主要对发送数据和接收数据进行比较。
main.c文件
/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2020/04/30
* Description        : Main program body.
*******************************************************************************/

/*
*@Note
USART中断例程:
Master:USART2_Tx(PA2)、USART2_Rx(PA3)。
Slave:USART3_Tx(PB10)、USART3_Rx(PB11)。

本例程演示 UART2 和 USART3 使用查询发送,中断接收。
注:
     硬件连线:PA2 —— PB11
           PA3 —— PB10

*/

#include "debug.h"
#include "usart.h"

/* Global define */
#define TxSize1   (size(TxBuffer1))
#define TxSize2   (size(TxBuffer2))
#define size(a)   (sizeof(a) / sizeof(*(a)))

/* Global Variable */
u8 TxBuffer1[] = "*Buffer1 Send from USART2 to USART3 using Interrupt!";     /* Send by UART2 */
u8 TxBuffer2[] = "#Buffer2 Send from USART3 to USART2 using Interrupt!";     /* Send by UART3 */
u8 RxBuffer1[TxSize1]={0};                                                   /* USART2 Using */
u8 RxBuffer2[TxSize2]={0};                                                   /* USART3 Using  */

u8 TxCnt1 = 0, RxCnt1 = 0;
u8 TxCnt2 = 0, RxCnt2 = 0;

u8 Rxfinish1=0,Rxfinish2=0;

TestStatus TransferStatus1 = FAILED;
TestStatus TransferStatus2 = FAILED;

void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Return         : None
*******************************************************************************/
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    Delay_Init();
    USART_Printf_Init(115200);
    printf("SystemClk:%d\r\n",SystemCoreClock);

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

    while(TxCnt2<TxSize2)                                         /* USART3--->USART2 */
    {
        USART_SendData(USART3, TxBuffer2[TxCnt2++]);
        while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
        {
        }
    }
    while(TxCnt1<TxSize1)                                         /* USART2--->USART3 */
    {
        USART_SendData(USART2, TxBuffer1[TxCnt1++]);
        while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
        {
        }
    }

    Delay_Ms(100);

    while(!Rxfinish1 || !Rxfinish2)                               /* waiting for receiving int finish */
    {
    }

    TransferStatus1=Buffercmp(TxBuffer1,RxBuffer2,TxSize1);
    TransferStatus2=Buffercmp(TxBuffer2,RxBuffer1,TxSize2);

    if(TransferStatus1&&TransferStatus2)
    {
      printf("\r\nSend Success!\r\n");
    }
    else
    {
      printf("\r\nSend Fail!\r\n");
    }
    printf("TxBuffer1---->RxBuffer2     TxBuffer2---->RxBuffer1\r\n");
    printf("TxBuffer1:%s\r\n",TxBuffer1);
    printf("RxBuffer1:%s\r\n",RxBuffer1);
    printf("TxBuffer2:%s\r\n",TxBuffer2);
    printf("RxBuffer2:%s\r\n",RxBuffer2);

    while(1)
    {
    }
}

/*******************************************************************************
* Function Name  : USART2_IRQHandler
* Description    : This function handles USART2 global interrupt request.
* Input          : None
* Return         : None
*******************************************************************************/
void USART2_IRQHandler(void)
{
  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  {
    RxBuffer1[RxCnt1++] = USART_ReceiveData(USART2);

    if(RxCnt1 == TxSize2)
    {
      USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
      Rxfinish1=1;
    }
  }
}

/*******************************************************************************
* Function Name  : USART3_IRQHandler
* Description    : This function handles USART3 global interrupt request.
* Input          : None
* Return         : None
*******************************************************************************/
void USART3_IRQHandler(void)
{
  if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  {
    RxBuffer2[RxCnt2++] = USART_ReceiveData(USART3);

    if(RxCnt2 == TxSize1)
    {
      USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
      Rxfinish2=1;
    }
  }
}
main.c文件主要包括3个函数:main函数、USART2_IRQHandler函数和USART3_IRQHandler函数。main函数主要进行USART2和USART3的查询发送以及进行发送和接收之间的比较,并打印输出发送和接收数据。USART2_IRQHandler函数和USART3_IRQHandler函数主要进行串口在中断情况下的数据接收。

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

63、USART-中断请求.rar

484.11 KB

使用特权

评论回复

相关帖子

沙发
一刀一级| | 2021-1-26 17:23 | 只看该作者
很不错的知识贴,了解一下

使用特权

评论回复
板凳
qminiup| | 2022-5-23 09:55 | 只看该作者
有没有中断发送的例子(不需要DMA)?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

293

帖子

34

粉丝