[RISC-V MCU 应用开发] 第六十四章、CH32V103应用教程——USART-中断

[复制链接]
 楼主| RISCVLAR 发表于 2021-1-26 16:59 | 显示全部楼层 |阅读模式
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)等等。
下图为中断和对应的使能位的关系:
图片1.png
注意:EIE使能位只有在DMA接收数据时使用。
关于CH32V103 USART具体信息,可参考CH32V103应用手册。USART标准库函数在第三章节已介绍,在此不再赘述。

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

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

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

  15.     /* USART2 TX-->A.2   RX-->A.3 */
  16.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  17.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  19.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  20.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  21.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  22.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  23.     /* USART3 TX-->B.10  RX-->B.11 */
  24.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  25.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  27.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  28.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  29.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  30.     GPIO_Init(GPIOB, &GPIO_InitStructure);

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

  37.     USART_Init(USART2, &USART_InitStructure);
  38.     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

  39.     USART_Init(USART3, &USART_InitStructure);
  40.     USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

  41.     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  42.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  43.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  44.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  45.     NVIC_Init(&NVIC_InitStructure);

  46.     NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  47.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
  48.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  49.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  50.     NVIC_Init(&NVIC_InitStructure);

  51.     USART_Cmd(USART2, ENABLE);
  52.     USART_Cmd(USART3, ENABLE);
  53. }

  54. /*******************************************************************************
  55. * Function Name  : Buffercmp
  56. * Description    : Compares two buffers
  57. * Input          : Buf1,Buf2:buffers to be compared
  58. *                  BufferLength: buffer's length
  59. * Return         : PASSED: Buf1 identical to Buf2
  60. *                  FAILED: Buf1 differs from Buf2
  61. *******************************************************************************/
  62. TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
  63. {
  64.   while(BufLength--)
  65.   {
  66.     if(*Buf1 != *Buf2)
  67.     {
  68.       return FAILED;
  69.     }
  70.     Buf1++;
  71.     Buf2++;
  72.   }
  73.   return PASSED;
  74. }

usart.c文件主要包括两个函数:USARTx_CFG函数和Buffercmp函数。其中,USARTx_CFG函数主要进行USART2和USART3的初始化配置:包括USART2和USART3对应GPIO的初始化配置以及串口和NVIC相关配置;Buffercmp函数主要对发送数据和接收数据进行比较。
main.c文件
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name          : main.c
  3. * Author             : WCH
  4. * Version            : V1.0.0
  5. * Date               : 2020/04/30
  6. * Description        : Main program body.
  7. *******************************************************************************/

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

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

  17. */

  18. #include "debug.h"
  19. #include "usart.h"

  20. /* Global define */
  21. #define TxSize1   (size(TxBuffer1))
  22. #define TxSize2   (size(TxBuffer2))
  23. #define size(a)   (sizeof(a) / sizeof(*(a)))

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

  29. u8 TxCnt1 = 0, RxCnt1 = 0;
  30. u8 TxCnt2 = 0, RxCnt2 = 0;

  31. u8 Rxfinish1=0,Rxfinish2=0;

  32. TestStatus TransferStatus1 = FAILED;
  33. TestStatus TransferStatus2 = FAILED;

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

  36. /*******************************************************************************
  37. * Function Name  : main
  38. * Description    : Main program.
  39. * Input          : None
  40. * Return         : None
  41. *******************************************************************************/
  42. int main(void)
  43. {
  44.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  45.     Delay_Init();
  46.     USART_Printf_Init(115200);
  47.     printf("SystemClk:%d\r\n",SystemCoreClock);

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

  50.     while(TxCnt2<TxSize2)                                         /* USART3--->USART2 */
  51.     {
  52.         USART_SendData(USART3, TxBuffer2[TxCnt2++]);
  53.         while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
  54.         {
  55.         }
  56.     }
  57.     while(TxCnt1<TxSize1)                                         /* USART2--->USART3 */
  58.     {
  59.         USART_SendData(USART2, TxBuffer1[TxCnt1++]);
  60.         while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
  61.         {
  62.         }
  63.     }

  64.     Delay_Ms(100);

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

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

  70.     if(TransferStatus1&&TransferStatus2)
  71.     {
  72.       printf("\r\nSend Success!\r\n");
  73.     }
  74.     else
  75.     {
  76.       printf("\r\nSend Fail!\r\n");
  77.     }
  78.     printf("TxBuffer1---->RxBuffer2     TxBuffer2---->RxBuffer1\r\n");
  79.     printf("TxBuffer1:%s\r\n",TxBuffer1);
  80.     printf("RxBuffer1:%s\r\n",RxBuffer1);
  81.     printf("TxBuffer2:%s\r\n",TxBuffer2);
  82.     printf("RxBuffer2:%s\r\n",RxBuffer2);

  83.     while(1)
  84.     {
  85.     }
  86. }

  87. /*******************************************************************************
  88. * Function Name  : USART2_IRQHandler
  89. * Description    : This function handles USART2 global interrupt request.
  90. * Input          : None
  91. * Return         : None
  92. *******************************************************************************/
  93. void USART2_IRQHandler(void)
  94. {
  95.   if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  96.   {
  97.     RxBuffer1[RxCnt1++] = USART_ReceiveData(USART2);

  98.     if(RxCnt1 == TxSize2)
  99.     {
  100.       USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  101.       Rxfinish1=1;
  102.     }
  103.   }
  104. }

  105. /*******************************************************************************
  106. * Function Name  : USART3_IRQHandler
  107. * Description    : This function handles USART3 global interrupt request.
  108. * Input          : None
  109. * Return         : None
  110. *******************************************************************************/
  111. void USART3_IRQHandler(void)
  112. {
  113.   if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  114.   {
  115.     RxBuffer2[RxCnt2++] = USART_ReceiveData(USART3);

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

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

63、USART-中断请求.rar

484.11 KB, 下载次数: 248

一刀一级 发表于 2021-1-26 17:23 | 显示全部楼层
很不错的知识贴,了解一下
qminiup 发表于 2022-5-23 09:55 | 显示全部楼层
有没有中断发送的例子(不需要DMA)?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

133

主题

296

帖子

44

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

133

主题

296

帖子

44

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