[DemoCode下载] 串口唤醒M051

[复制链接]
671|14
 楼主| xixi2017 发表于 2019-10-16 22:02 | 显示全部楼层 |阅读模式
有人问,串口中断需要清理标志位吗?
这里找个例子给大家看看
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 3 $
  5. * $Date: 15/08/07 1:31p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to wake up system form Power-down mode by UART interrupt.
  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. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define functions prototype                                                                              */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. int32_t main(void);
  19. void UART_CTSWakeUpTest(void);


  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /* ISR to handle UART Channel 1 interrupt event                                                            */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. void UART1_IRQHandler(void)
  24. {
  25.     if(UART_GET_INT_FLAG(UART1, UART_ISR_MODEM_INT_Msk))    /* UART Modem Status interrupt flag */
  26.     {
  27.         printf("UART Modem Status interrupt happen.");
  28.         UART_ClearIntFlag(UART1, UART_ISR_MODEM_INT_Msk);   /* Clear UART Modem Status interrupt flag */
  29.     }
  30. }

  31. /*---------------------------------------------------------------------------------------------------------*/
  32. /*  Function for System Entry to Power Down Mode                                                           */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. void PowerDownFunction(void)
  35. {
  36.     /* Check if all the debug messages are finished */
  37.     UART_WAIT_TX_EMPTY(UART0);

  38.     /* Enter to Power-down mode */
  39.     CLK_PowerDown();

  40. }

  41. /*---------------------------------------------------------------------------------------------------------*/
  42. /*  UART nCTS Wake-up Function                                                                             */
  43. /*---------------------------------------------------------------------------------------------------------*/
  44. void UART_CTSWakeUpTest(void)
  45. {

  46.     printf("+----------------------------------------------------------+\n");
  47.     printf("|   Power-Down and Wake-up by UART interrupt Sample Code   |\n");
  48.     printf("+----------------------------------------------------------+\n\n");   
  49.    
  50.     /* Clear Modem Status interrupt before entering Power-down mode */
  51.     UART_ClearIntFlag(UART1, UART_ISR_MODEM_INT_Msk);

  52.     /* Enable UART Wake-up function and Modem Status interrupt */
  53.     UART_EnableInt(UART1, UART_IER_WAKE_EN_Msk|UART_IER_MODEM_IEN_Msk);

  54.     printf("System enter to Power-down mode.\n");
  55.     printf("Toggle nCTS of UART1 to wake-up system.\n\n");

  56.     /* Unlock protected registers before entering Power-down mode */
  57.     SYS_UnlockReg();

  58.     /* Enter to Power-down mode */
  59.     PowerDownFunction();

  60.     /* Lock protected registers after entering Power-down mode */
  61.     SYS_LockReg();

  62.     /* Disable UART Wake-up function and Modem Status interrupt */
  63.     UART_DisableInt(UART1, UART_IER_WAKE_EN_Msk|UART_IER_MODEM_IEN_Msk);
  64.       
  65.     printf("\nSystem waken-up done.\n");      
  66.     printf("\nUART Sample Program End.\n");   

  67. }

  68. void SYS_Init(void)
  69. {
  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init System Clock                                                                                       */
  72.     /*---------------------------------------------------------------------------------------------------------*/

  73.     /* Enable Internal RC 22.1184MHz clock */
  74.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  75.     /* Waiting for Internal RC clock ready */
  76.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  79.     /* Enable external XTAL 12MHz clock */
  80.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  81.     /* Waiting for external XTAL clock ready */
  82.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  83.     /* Set core clock as PLL_CLOCK from PLL */
  84.     CLK_SetCoreClock(PLL_CLOCK);

  85.     /* Enable UART module clock */
  86.     CLK_EnableModuleClock(UART0_MODULE);
  87.     CLK_EnableModuleClock(UART1_MODULE);   

  88.     /* Select UART module clock source */
  89.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  90.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));   

  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init I/O Multi-function                                                                                 */
  93.     /*---------------------------------------------------------------------------------------------------------*/

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

  97.     /* Set P1 multi-function pins for UART1 RXD and TXD */
  98.     SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  99.     SYS->P1_MFP |= (SYS_MFP_P12_RXD1 | SYS_MFP_P13_TXD1);

  100.     /* Set P0 multi-function pins for UART1 CTS */
  101.     SYS->P0_MFP = SYS->P0_MFP & (~SYS_MFP_P00_Msk) | SYS_MFP_P00_CTS1;

  102. }

  103. void UART0_Init()
  104. {
  105.     /*---------------------------------------------------------------------------------------------------------*/
  106.     /* Init UART                                                                                               */
  107.     /*---------------------------------------------------------------------------------------------------------*/
  108.     /* Reset UART0 */
  109.     SYS_ResetModule(UART0_RST);

  110.     /* Configure UART0 and set UART0 Baudrate */
  111.     UART_Open(UART0, 115200);
  112. }

  113. void UART1_Init()
  114. {
  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Init UART                                                                                               */
  117.     /*---------------------------------------------------------------------------------------------------------*/
  118.     /* Reset UART1 */
  119.     SYS_ResetModule(UART1_RST);

  120.     /* Configure UART1 and set UART1 Baudrate */
  121.     UART_Open(UART1, 115200);
  122. }

  123. /*---------------------------------------------------------------------------------------------------------*/
  124. /* MAIN function                                                                                           */
  125. /*---------------------------------------------------------------------------------------------------------*/

  126. int main(void)
  127. {
  128.     /* Unlock protected registers */
  129.     SYS_UnlockReg();

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

  132.     /* Lock protected registers */
  133.     SYS_LockReg();

  134.     /* Init UART0 for printf */
  135.     UART0_Init();
  136.    
  137.     /* Init UART0 for testing */
  138.     UART1_Init();   

  139.     /*---------------------------------------------------------------------------------------------------------*/
  140.     /* SAMPLE CODE                                                                                             */
  141.     /*---------------------------------------------------------------------------------------------------------*/

  142.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  143.     printf("\n\nUART Sample Program.\n");

  144.     /* UART wake-up sample function */
  145.     UART_CTSWakeUpTest();

  146.     while(1);

  147. }


 楼主| xixi2017 发表于 2019-10-16 22:02 | 显示全部楼层
 楼主| xixi2017 发表于 2019-10-16 22:03 | 显示全部楼层
显然有这个函数,说明可能需要,但是这个是进入中断前做的清场操作。
 楼主| xixi2017 发表于 2019-10-16 22:03 | 显示全部楼层
 楼主| xixi2017 发表于 2019-10-16 22:03 | 显示全部楼层
然后找到这个中断函数,发现需要自己清理,没毛病,记住了。
zhuomuniao110 发表于 2019-10-16 22:32 | 显示全部楼层
总结的很好,原来提供了函数操作了,方便。
zhuomuniao110 发表于 2019-10-16 22:32 | 显示全部楼层
不用再死记那些寄存器了。
zhuomuniao110 发表于 2019-10-16 23:17 | 显示全部楼层
看是不是函数里面帮忙实现了,一般是需要自己做的。
幸福小强 发表于 2019-10-16 23:26 | 显示全部楼层
结合例子学习特别快。
jiekou001 发表于 2019-10-20 23:57 | 显示全部楼层
呵呵,如果能收到数据唤醒就好了。
zhuotuzi 发表于 2019-10-21 23:59 | 显示全部楼层
这个功能很重要,低功耗很需要这个
wowu 发表于 2019-11-19 12:38 | 显示全部楼层
非常感谢楼主分享
wakayi 发表于 2019-11-19 12:43 | 显示全部楼层
非常感谢楼主分享
renzheshengui 发表于 2019-11-19 12:46 | 显示全部楼层
非常感谢楼主分享
paotangsan 发表于 2019-11-19 12:50 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

145

主题

2034

帖子

2

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