[DemoCode下载] 这个串口例子很有深意啊,串口中断是咋回事?

[复制链接]
1437|11
 楼主| 玛尼玛尼哄 发表于 2020-6-9 23:14 | 显示全部楼层 |阅读模式
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 7 $
  5. * $Date: 15/05/22 3:53p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Transmit and receive data from PC terminal through RS232 interface.
  7. *
  8. * @note
  9. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"


  14. #define PLL_CLOCK           50000000
  15. # if defined ( __GNUC__ )
  16. #define RXBUFSIZE 128
  17. #else
  18. #define RXBUFSIZE 1024
  19. #endif

  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /* Global variables                                                                                        */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. uint8_t g_u8RecData[RXBUFSIZE]  = {0};

  24. volatile uint32_t g_u32comRbytes = 0;
  25. volatile uint32_t g_u32comRhead  = 0;
  26. volatile uint32_t g_u32comRtail  = 0;
  27. volatile int32_t g_bWait         = TRUE;

  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* Define functions prototype                                                                              */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. int32_t main(void);
  32. void UART_TEST_HANDLE(void);
  33. void UART_FunctionTest(void);


  34. void SYS_Init(void)
  35. {
  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Init System Clock                                                                                       */
  38.     /*---------------------------------------------------------------------------------------------------------*/

  39.     /* Enable Internal RC 22.1184MHz clock */
  40.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  41.     /* Waiting for Internal RC clock ready */
  42.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  43.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  44.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  45.     /* Enable external XTAL 12MHz clock */
  46.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  47.     /* Waiting for external XTAL clock ready */
  48.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  49.     /* Set core clock as PLL_CLOCK from PLL */
  50.     CLK_SetCoreClock(PLL_CLOCK);

  51.     /* Enable UART module clock */
  52.     CLK_EnableModuleClock(UART0_MODULE);

  53.     /* Select UART module clock source */
  54.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  55.     /*---------------------------------------------------------------------------------------------------------*/
  56.     /* Init I/O Multi-function                                                                                 */
  57.     /*---------------------------------------------------------------------------------------------------------*/

  58.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  59.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  60.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  61. }

  62. void UART0_Init()
  63. {
  64.     /*---------------------------------------------------------------------------------------------------------*/
  65.     /* Init UART                                                                                               */
  66.     /*---------------------------------------------------------------------------------------------------------*/
  67.     /* Reset UART0 */
  68.     SYS_ResetModule(UART0_RST);

  69.     /* Configure UART0 and set UART0 Baudrate */
  70.     UART_Open(UART0, 115200);
  71. }

  72. /*---------------------------------------------------------------------------------------------------------*/
  73. /* UART Test Sample                                                                                        */
  74. /* Test Item                                                                                               */
  75. /* It sends the received data to HyperTerminal.                                                            */
  76. /*---------------------------------------------------------------------------------------------------------*/

  77. /*---------------------------------------------------------------------------------------------------------*/
  78. /* MAIN function                                                                                           */
  79. /*---------------------------------------------------------------------------------------------------------*/

  80. int32_t main(void)
  81. {
  82.     /* Unlock protected registers */
  83.     SYS_UnlockReg();

  84.     /* Init System, peripheral clock and multi-function I/O */
  85.     SYS_Init();

  86.     /* Lock protected registers */
  87.     SYS_LockReg();

  88.     /* Init UART0 for printf and testing */
  89.     UART0_Init();

  90.     /*---------------------------------------------------------------------------------------------------------*/
  91.     /* SAMPLE CODE                                                                                             */
  92.     /*---------------------------------------------------------------------------------------------------------*/
  93. #if !( __GNUC__ )
  94.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
  95. #endif
  96.     printf("\n\nUART Sample Program\n");

  97.     /* UART sample function */
  98.     UART_FunctionTest();
  99.    
  100.     while(1);

  101. }

  102. /*---------------------------------------------------------------------------------------------------------*/
  103. /* ISR to handle UART Channel 0 interrupt event                                                            */
  104. /*---------------------------------------------------------------------------------------------------------*/
  105. void UART0_IRQHandler(void)
  106. {
  107.     UART_TEST_HANDLE();
  108. }

  109. /*---------------------------------------------------------------------------------------------------------*/
  110. /* UART Callback function                                                                                  */
  111. /*---------------------------------------------------------------------------------------------------------*/
  112. void UART_TEST_HANDLE()
  113. {
  114.     uint8_t u8InChar = 0xFF;
  115.     uint32_t u32IntSts = UART0->ISR;

  116.     if(u32IntSts & UART_ISR_RDA_INT_Msk)
  117.     {
  118.         printf("\nInput:");

  119.         /* Get all the input characters */
  120.         while(UART_IS_RX_READY(UART0))
  121.         {
  122.             /* Get the character from UART Buffer */
  123.             u8InChar = UART_READ(UART0);

  124.             printf("%c ", u8InChar);

  125.             if(u8InChar == '0')
  126.             {
  127.                 g_bWait = FALSE;
  128.             }

  129.             /* Check if buffer full */
  130.             if(g_u32comRbytes < RXBUFSIZE)
  131.             {
  132.                 /* Enqueue the character */
  133.                 g_u8RecData[g_u32comRtail] = u8InChar;
  134.                 g_u32comRtail = (g_u32comRtail == (RXBUFSIZE - 1)) ? 0 : (g_u32comRtail + 1);
  135.                 g_u32comRbytes++;
  136.             }
  137.         }
  138.         printf("\nTransmission Test:");
  139.     }

  140.     if(u32IntSts & UART_ISR_THRE_INT_Msk)
  141.     {
  142.         uint16_t tmp;
  143.         tmp = g_u32comRtail;
  144.         if(g_u32comRhead != tmp)
  145.         {
  146.             u8InChar = g_u8RecData[g_u32comRhead];
  147.             while(UART_IS_TX_FULL(UART0));  /* Wait Tx is not full to transmit data */            
  148.             UART_WRITE(UART0, u8InChar);
  149.             g_u32comRhead = (g_u32comRhead == (RXBUFSIZE - 1)) ? 0 : (g_u32comRhead + 1);
  150.             g_u32comRbytes--;
  151.         }
  152.     }
  153. }


  154. /*---------------------------------------------------------------------------------------------------------*/
  155. /*  UART Function Test                                                                                     */
  156. /*---------------------------------------------------------------------------------------------------------*/
  157. void UART_FunctionTest()
  158. {
  159.     printf("+-----------------------------------------------------------+\n");
  160.     printf("|  UART Function Test                                       |\n");
  161.     printf("+-----------------------------------------------------------+\n");
  162.     printf("|  Description :                                            |\n");
  163.     printf("|    The sample code will print input char on terminal      |\n");
  164.     printf("|    Please enter any to start     (Press '0' to exit)      |\n");
  165.     printf("+-----------------------------------------------------------+\n");

  166.     /*
  167.         Using a RS232 cable to connect UART0 and PC.
  168.         UART0 is set to debug port. UART0 is enable RDA and RLS interrupt.
  169.         When inputing char to terminal screen, RDA interrupt will happen and
  170.         UART0 will print the received char on screen.
  171.     */

  172.     /* Enable Interrupt and install the call back function */
  173.     UART_ENABLE_INT(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk));
  174.     NVIC_EnableIRQ(UART0_IRQn);
  175.     while(g_bWait);

  176.     /* Disable Interrupt */
  177.     UART_DISABLE_INT(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk));
  178.     NVIC_DisableIRQ(UART0_IRQn);
  179.     g_bWait = TRUE;
  180.     printf("\nUART Sample Demo End.\n");

  181. }



 楼主| 玛尼玛尼哄 发表于 2020-6-9 23:15 | 显示全部楼层
这里面也有用到中断,没看太懂
598330983 发表于 2020-6-18 22:44 | 显示全部楼层
有种BSP的影子。
zhuomuniao110 发表于 2020-6-19 16:49 | 显示全部楼层
看明白了,在测试函数里面启动的中断。
huangcunxiake 发表于 2020-6-20 09:02 | 显示全部楼层
看明白了。
zljiu 发表于 2020-7-6 16:54 | 显示全部楼层
为什么说有dsp的影子啊
coshi 发表于 2020-7-6 16:55 | 显示全部楼层
代码很有深度
aoyi 发表于 2020-7-6 16:55 | 显示全部楼层
这段代码确实很有意思
drer 发表于 2020-7-6 16:55 | 显示全部楼层
想法很好啊
gwsan 发表于 2020-7-6 16:56 | 显示全部楼层
楼主很热心啊
leadd@sina.com 发表于 2020-11-7 21:20 | 显示全部楼层
原本就是isp代码当中的uart部分代码
chenqianqian 发表于 2020-11-9 08:10 来自手机 | 显示全部楼层
将接受得字符打印出来
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3261

帖子

2

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