[DemoCode下载] M051串口中断唤醒

[复制链接]
984|6
 楼主| 21mengnan 发表于 2019-7-14 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. }


 楼主| 21mengnan 发表于 2019-7-14 22:03 | 显示全部楼层
请记住,进入中断后,如果想继续使用中断,一定要清理掉中断标志位。这样才能正确响应下次中断,否则会不听的进入中断(因为本次的中断事件)
 楼主| 21mengnan 发表于 2019-7-14 22:04 | 显示全部楼层
不明白的看看串口的头文件。
dongnanxibei 发表于 2019-7-14 23:59 | 显示全部楼层
串口非常容易shangshou
dongnanxibei 发表于 2019-7-14 23:59 | 显示全部楼层
楼主分析的很对。
huangcunxiake 发表于 2019-7-15 08:37 | 显示全部楼层
关于函数的参数说明好像是在.c的文件里。
xinpian101 发表于 2019-7-15 18:00 | 显示全部楼层
多谢各位大侠分享经验。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

88

主题

1151

帖子

1

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