[DemoCode下载] RTC报警唤醒

[复制链接]
1034|3
 楼主| 643757107 发表于 2020-1-31 22:29 | 显示全部楼层 |阅读模式
RTC, TE, ck, TI
  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: 6 $
  5. * $Date: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Use RTC alarm interrupt event to wake up system.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M451Series.h"

  12. #define PLL_CLOCK           72000000


  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Global Interface Variables Declarations                                                                 */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. extern int IsDebugFifoEmpty(void);
  17. volatile uint8_t g_u8IsRTCAlarmINT = 0;


  18. /**
  19. * @brief       IRQ Handler for RTC Interrupt
  20. *
  21. * @param       None
  22. *
  23. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  24. *
  25. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The RTC_IRQHandler is default IRQ of RTC, declared in startup_M451Series.s.
  26. */
  27. void RTC_IRQHandler(void)
  28. {
  29.     /* To check if RTC alarm interrupt occurred */
  30.     if(RTC_GET_ALARM_INT_FLAG() == 1)
  31.     {
  32.         /* Clear RTC alarm interrupt flag */
  33.         RTC_CLEAR_ALARM_INT_FLAG();

  34.         g_u8IsRTCAlarmINT++;
  35.     }
  36. }

  37. void SYS_Init(void)
  38. {
  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init System Clock                                                                                       */
  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* Enable HIRC clock */
  43.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  44.     /* Waiting for HIRC clock ready */
  45.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  46.     /* Switch HCLK clock source to HIRC */
  47.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  48.     /* Enable HXT and LXT-32KHz */
  49.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk | CLK_PWRCTL_LXTEN_Msk);

  50.     /* Waiting for clock ready */
  51.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk | CLK_STATUS_LXTSTB_Msk);

  52.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  53.     CLK_SetCoreClock(PLL_CLOCK);
  54.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  55.     /* Enable peripheral clock */
  56.     CLK_EnableModuleClock(UART0_MODULE);
  57.     CLK_EnableModuleClock(RTC_MODULE);

  58.     /* Peripheral clock source */
  59.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_PLL, CLK_CLKDIV0_UART(1));

  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Init I/O Multi-function                                                                                 */
  62.     /*---------------------------------------------------------------------------------------------------------*/
  63.     /* Set PD multi-function pins for UART0 RXD, TXD */
  64.     SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
  65. }

  66. void UART0_Init(void)
  67. {
  68.     /*---------------------------------------------------------------------------------------------------------*/
  69.     /* Init UART                                                                                               */
  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Reset UART module */
  72.     SYS_ResetModule(UART0_RST);

  73.     /* Configure UART0 and set UART0 Baudrate */
  74.     UART_Open(UART0, 115200);
  75. }

  76. /*---------------------------------------------------------------------------------------------------------*/
  77. /*  MAIN function                                                                                          */
  78. /*---------------------------------------------------------------------------------------------------------*/
  79. int main(void)
  80. {
  81.     S_RTC_TIME_DATA_T sWriteRTC, sReadRTC;

  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 */
  89.     UART0_Init();

  90.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
  91.     printf("+-------------------------------------+\n");
  92.     printf("|    RTC Alarm Wake-up Sample Code    |\n");
  93.     printf("+-------------------------------------+\n\n");

  94.     /* Enable RTC NVIC */
  95.     NVIC_EnableIRQ(RTC_IRQn);

  96.     /* Open RTC */
  97.     sWriteRTC.u32Year       = 2014;
  98.     sWriteRTC.u32Month      = 5;
  99.     sWriteRTC.u32Day        = 15;
  100.     sWriteRTC.u32DayOfWeek  = RTC_THURSDAY;
  101.     sWriteRTC.u32Hour       = 23;
  102.     sWriteRTC.u32Minute     = 59;
  103.     sWriteRTC.u32Second     = 50;
  104.     sWriteRTC.u32TimeScale  = RTC_CLOCK_24;
  105.     RTC_Open(&sWriteRTC);

  106.     /* Set RTC alarm date/time */
  107.     sWriteRTC.u32Year       = 2014;
  108.     sWriteRTC.u32Month      = 5;
  109.     sWriteRTC.u32Day        = 15;
  110.     sWriteRTC.u32DayOfWeek  = RTC_THURSDAY;
  111.     sWriteRTC.u32Hour       = 23;
  112.     sWriteRTC.u32Minute     = 59;
  113.     sWriteRTC.u32Second     = 55;
  114.     RTC_SetAlarmDateAndTime(&sWriteRTC);

  115.     /* Enable RTC alarm interrupt and wake-up function will be enabled also */
  116.     RTC_EnableInt(RTC_INTEN_ALMIEN_Msk);

  117.     printf("# Set RTC current date/time: 2014/05/15 23:59:50.\n");
  118.     printf("# Set RTC alarm date/time:   2014/05/15 23:59:55.\n");
  119.     printf("# Wait system waken-up by RTC alarm interrupt event.\n");

  120.     g_u8IsRTCAlarmINT = 0;

  121.     /* System enter to Power-down */
  122.     /* To program PWRCTL register, it needs to disable register protection first. */
  123.     SYS_UnlockReg();
  124.     printf("\nSystem enter to power-down mode ...\n");
  125.     /* To check if all the debug messages are finished */
  126.     while(IsDebugFifoEmpty() == 0);
  127.     CLK_PowerDown();

  128.     while(g_u8IsRTCAlarmINT == 0);

  129.     /* Read current RTC date/time */
  130.     RTC_GetDateAndTime(&sReadRTC);
  131.     printf("System has been waken-up and current date/time is:\n");
  132.     printf("    %d/%02d/%02d %02d:%02d:%02d\n",
  133.            sReadRTC.u32Year, sReadRTC.u32Month, sReadRTC.u32Day, sReadRTC.u32Hour, sReadRTC.u32Minute, sReadRTC.u32Second);


  134.     printf("\n\n");
  135.     printf("# Set next RTC alarm date/time: 2014/05/16 00:00:05.\n");
  136.     printf("# Wait system waken-up by RTC alarm interrupt event.\n");
  137.     RTC_SetAlarmDate(2014, 05, 16);
  138.     RTC_SetAlarmTime(0, 0, 5, RTC_CLOCK_24, 0);

  139.     g_u8IsRTCAlarmINT = 0;

  140.     /* System enter to Power-down */
  141.     /* To program PWRCTL register, it needs to disable register protection first. */
  142.     SYS_UnlockReg();
  143.     printf("\nSystem enter to power-down mode ...\n");
  144.     /* To check if all the debug messages are finished */
  145.     while(IsDebugFifoEmpty() == 0);
  146.     CLK_PowerDown();

  147.     while(g_u8IsRTCAlarmINT == 0);

  148.     /* Read current RTC date/time */
  149.     RTC_GetDateAndTime(&sReadRTC);
  150.     printf("System has been waken-up and current date/time is:\n");
  151.     printf("    %d/%02d/%02d %02d:%02d:%02d\n",
  152.            sReadRTC.u32Year, sReadRTC.u32Month, sReadRTC.u32Day, sReadRTC.u32Hour, sReadRTC.u32Minute, sReadRTC.u32Second);

  153.     while(1);
  154. }

  155. /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/


 楼主| 643757107 发表于 2020-1-31 22:29 | 显示全部楼层
其实类似闹铃唤醒。
 楼主| 643757107 发表于 2020-1-31 22:30 | 显示全部楼层
年月日,时分秒。
734774645 发表于 2020-1-31 22:33 | 显示全部楼层
分享的资料非常好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3972

帖子

11

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