[DemoCode下载] 定时器中断唤醒系统

[复制链接]
1117|2
 楼主| zhuotuzi 发表于 2024-4-21 19:53 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 6 $
  5. * $Date: 15/01/16 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Use timer0 periodic time-out interrupt event to wake up system.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M071M.h"

  12. #define PLL_CLOCK           50000000


  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Global Interface Variables Declarations                                                                 */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. extern int IsDebugFifoEmpty(void);
  17. volatile uint8_t g_u8IsTMR0WakeupFlag = 0;
  18. volatile uint32_t g_au32TMRINTCount[4] = {0};


  19. /**
  20. * @brief       Timer0 IRQ
  21. *
  22. * @param       None
  23. *
  24. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  25. *
  26. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_NUC1311.s.
  27. */
  28. void TMR0_IRQHandler(void)
  29. {
  30.     if(TIMER_GetIntFlag(TIMER0) == 1)
  31.     {
  32.         /* Clear Timer0 time-out interrupt flag */
  33.         TIMER_ClearIntFlag(TIMER0);

  34.         g_au32TMRINTCount[0]++;
  35.     }

  36.     if(TIMER_GetWakeupFlag(TIMER0) == 1)
  37.     {
  38.         /* Clear Timer0 wake-up flag */
  39.         TIMER_ClearWakeupFlag(TIMER0);

  40.         g_u8IsTMR0WakeupFlag = 1;
  41.     }
  42. }

  43. void SYS_Init(void)
  44. {
  45.     /*---------------------------------------------------------------------------------------------------------*/
  46.     /* Init System Clock                                                                                       */
  47.     /*---------------------------------------------------------------------------------------------------------*/
  48.     /* Enable HIRC clock */
  49.     CLK_EnableXtalRC(CLK_PWRCON_IRC22M_EN_Msk);

  50.     /* Waiting for HIRC clock ready */
  51.     CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

  52.     /* Switch HCLK clock source to HIRC */
  53.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  54.     /* Enable HXT and LIRC */
  55.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_IRC10K_EN_Msk);

  56.     /* Waiting for clock ready */
  57.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_IRC10K_STB_Msk);

  58.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  59.     CLK_SetCoreClock(PLL_CLOCK);
  60.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLK_S_HCLK_DIV2);

  61.     /* Enable peripheral clock */
  62.     CLK_EnableModuleClock(UART0_MODULE);
  63.     CLK_EnableModuleClock(TMR0_MODULE);

  64.     /* Peripheral clock source */
  65.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  66.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_LIRC, 0);

  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init I/O Multi-function                                                                                 */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Set PB multi-function pins for UART0 RXD, TXD */
  71.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  72.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
  73. }

  74. void UART0_Init(void)
  75. {
  76.     /*---------------------------------------------------------------------------------------------------------*/
  77.     /* Init UART                                                                                               */
  78.     /*---------------------------------------------------------------------------------------------------------*/
  79.     /* Reset IP */
  80.     SYS_ResetModule(UART0_RST);

  81.     /* Configure UART0 and set UART0 Baudrate */
  82.     UART_Open(UART0, 115200);
  83. }

  84. /*---------------------------------------------------------------------------------------------------------*/
  85. /*  MAIN function                                                                                          */
  86. /*---------------------------------------------------------------------------------------------------------*/
  87. int main(void)
  88. {
  89.     volatile uint32_t u32InitCount;

  90.     /* Unlock protected registers */
  91.     SYS_UnlockReg();

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

  94.     /* Lock protected registers */
  95.     SYS_LockReg();

  96.     /* Init UART0 for printf */
  97.     UART0_Init();

  98.     printf("CPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  99.     printf("+-------------------------------------------------+\n");
  100.     printf("|    Timer0 Power-down and Wake-up Sample Code    |\n");
  101.     printf("+-------------------------------------------------+\n\n");

  102.     printf("# Timer0 Settings:\n");
  103.     printf("    - Clock source is LIRC          \n");
  104.     printf("    - Time-out frequency is 1 Hz    \n");
  105.     printf("    - Periodic mode                 \n");
  106.     printf("    - Interrupt enable              \n");
  107.     printf("    - Wake-up function enable       \n");
  108.     printf("# System will enter to Power-down mode while Timer0 interrupt counts is reaching 3.\n");
  109.     printf("  And will be wakeup while Timer0 interrupt counts is reaching 4.\n\n");

  110.     /* Enable Timer0 NVIC */
  111.     NVIC_EnableIRQ(TMR0_IRQn);

  112.     /* Open Timer0 time-out frequency to 1 Hz in periodic mode */
  113.     TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);

  114.     /* Enable Timer0 time-out interrupt and wake-up function */
  115.     TIMER_EnableInt(TIMER0);
  116.     TIMER_EnableWakeup(TIMER0);

  117.     /* Start Timer0 counting */
  118.     TIMER_Start(TIMER0);

  119.     u32InitCount = g_u8IsTMR0WakeupFlag = g_au32TMRINTCount[0] = 0;
  120.     while(g_au32TMRINTCount[0] < 10)
  121.     {
  122.         if(g_au32TMRINTCount[0] != u32InitCount)
  123.         {
  124.             printf("Timer0 interrupt counts - %d\n", g_au32TMRINTCount[0]);
  125.             if(g_au32TMRINTCount[0] == 3)
  126.             {
  127.                 /* System enter to Power-down */
  128.                 /* To program PWRCON register, it needs to disable register protection first. */
  129.                 SYS_UnlockReg();
  130.                 printf("\nSystem enter to power-down mode ...\n");
  131.                 /* To check if all the debug messages are finished */
  132.                 while(IsDebugFifoEmpty() == 0);
  133.                 CLK_PowerDown();

  134.                 /* Check if Timer0 time-out interrupt and wake-up flag occurred */
  135.                 while(g_u8IsTMR0WakeupFlag == 0);

  136.                 printf("System has been waken-up done. (Timer0 interrupt counts is %d)\n\n", g_au32TMRINTCount[0]);
  137.             }
  138.             u32InitCount = g_au32TMRINTCount[0];
  139.         }
  140.     }

  141.     /* Stop Timer0 counting */
  142.     TIMER_Stop(TIMER0);

  143.     printf("*** PASS ***\n");

  144.     while(1);
  145. }

  146. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


598330983 发表于 2024-4-21 20:53 | 显示全部楼层
除了定时器可以唤醒,IO中断也可以唤醒吧。唤醒后是接着运行吗
lidi911 发表于 2024-4-22 08:33 来自手机 | 显示全部楼层
大部分低功耗应用都是外部事件唤醒
您需要登录后才可以回帖 登录 | 注册

本版积分规则

217

主题

3393

帖子

7

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