[单片机芯片] chv32v103C8T6的USART1配置接收中断后,测试没有进入接收中断...

[复制链接]
1864|4
 楼主| lilijin1995 发表于 2022-3-10 00:45 | 显示全部楼层 |阅读模式
本帖最后由 lilijin1995 于 2022-3-10 16:52 编辑

chv32v103C8T6的USART1配置接收中断后,测试没有进入接收中断程序;


  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. /* Global define */
  20. #define TxSize1   (size(TxBuffer1))
  21. #define TxSize2   (size(TxBuffer2))
  22. #define size(a)   (sizeof(a) / sizeof(*(a)))

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

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

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

  32. u8 Rxfinish1=0,Rxfinish2=0;

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

  35. void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  36. void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

  37. /*******************************************************************************
  38. * Function Name  : Buffercmp
  39. * Description    : Compares two buffers
  40. * Input          : Buf1,Buf2:buffers to be compared
  41. *                  BufferLength: buffer's length
  42. * Return         : PASSED: Buf1 identical to Buf2
  43. *                  FAILED: Buf1 differs from Buf2
  44. *******************************************************************************/
  45. TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
  46. {
  47.   while(BufLength--)
  48.   {
  49.     if(*Buf1 != *Buf2)
  50.     {
  51.       return FAILED;
  52.     }
  53.     Buf1++;
  54.     Buf2++;
  55.   }
  56.   return PASSED;
  57. }


  58. /*******************************************************************************
  59. * Function Name  : USARTx_CFG
  60. * Description    : Initializes the USART2 & USART3 peripheral.
  61. * Input          : None
  62. * Return         : None
  63. *******************************************************************************/
  64. void USARTx_CFG(void)
  65. {
  66.   GPIO_InitTypeDef  GPIO_InitStructure;
  67.         USART_InitTypeDef USART_InitStructure;
  68.         NVIC_InitTypeDef  NVIC_InitStructure;

  69.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);
  70.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB , ENABLE);


  71.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  72.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  74.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  75.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  76.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  77.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  78.   /* USART3 TX-->B.10  RX-->B.11 */
  79.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  80.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  81.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  82.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  83.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  84.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  85.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  86.         USART_InitStructure.USART_BaudRate = 115200;
  87.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  88.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  89.         USART_InitStructure.USART_Parity = USART_Parity_No;
  90.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  91.         USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

  92.   USART_Init(USART1, &USART_InitStructure);
  93.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

  94.         USART_Init(USART3, &USART_InitStructure);
  95.   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

  96.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  97.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  98.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  99.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  100.   NVIC_Init(&NVIC_InitStructure);

  101.         NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  102.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
  103.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  104.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  105.   NVIC_Init(&NVIC_InitStructure);

  106.         USART_Cmd(USART1, ENABLE);
  107.         USART_Cmd(USART3, ENABLE);
  108. }
  109. /*******************************************************************************
  110. * Function Name  : main
  111. * Description    : Main program.
  112. * Input          : None
  113. * Return         : None
  114. *******************************************************************************/
  115. int main(void)
  116. {
  117.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  118.   Delay_Init();
  119.         USART_Printf_Init(115200);
  120.         printf("SystemClk:%d\r\n",SystemCoreClock);

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



  123.         while(1)
  124.   {
  125.         }
  126. }


  127. /*******************************************************************************
  128. * Function Name  : USART2_IRQHandler
  129. * Description    : This function handles USART2 global interrupt request.
  130. * Input          : None
  131. * Return         : None
  132. *******************************************************************************/
  133. void USART1_IRQHandler(void)
  134. {
  135.   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  136.   {
  137. //    RxBuffer1[RxCnt1++] = USART_ReceiveData(USART1);
  138.     printf("R");
  139. //    if(RxCnt1 == TxSize2)
  140. //    {
  141. //      USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  142. //                        Rxfinish1=1;
  143. //    }
  144.   }

  145. }

  146. /*******************************************************************************
  147. * Function Name  : USART3_IRQHandler
  148. * Description    : This function handles USART3 global interrupt request.
  149. * Input          : None
  150. * Return         : None
  151. *******************************************************************************/
  152. void USART3_IRQHandler(void)
  153. {
  154.   if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  155.   {
  156.     RxBuffer2[RxCnt2++] = USART_ReceiveData(USART3);

  157.     if(RxCnt2 == TxSize1)
  158.     {
  159.       USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
  160.                         Rxfinish2=1;
  161.     }
  162.   }
  163. }



另外debug中配置usart2打印log,我们main函数中声明了中断;
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
但通过测试,始终无法进入USART1_IRQHandler的接收中断;补充一下,是通过串口调试工具的TX发到USART1的RX的,然后通过USART2的TX打印log

WCHTech2 发表于 2022-3-10 19:04 | 显示全部楼层
您好,附件为串口1接收中断例程,可以参考一下。

CH32V103 串口1接收中断.zip

599.39 KB, 下载次数: 1

七毛钱 发表于 2022-3-11 10:10 来自手机 | 显示全部楼层
参考一下相关例程把
 楼主| lilijin1995 发表于 2022-3-11 14:00 | 显示全部楼层
WCHTech2 发表于 2022-3-10 19:04
您好,附件为串口1接收中断例程,可以参考一下。

感谢大佬
Bowclad 发表于 2022-9-10 21:21 | 显示全部楼层
参考下相关例程
您需要登录后才可以回帖 登录 | 注册

本版积分规则

56

主题

165

帖子

8

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