[DemoCode下载] NUC029的RTC用法

[复制链接]
1003|10
 楼主| zhuomuniao110 发表于 2020-6-20 23:39 | 显示全部楼层 |阅读模式
  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: 2 $
  5. * $Date: 16/10/25 4:30p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show the current RTC data/time per tick.
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC029xGE.h"


  12. #define PLL_CLOCK           72000000


  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Global Interface Variables Declarations                                                                 */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. volatile uint32_t g_u32RTCTickINT;


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

  33.         g_u32RTCTickINT++;

  34.         PB8 ^= 1;
  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 multi-function pins for UART0 RXD and TXD */
  64.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  65.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);
  66. }

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

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

  77. /*---------------------------------------------------------------------------------------------------------*/
  78. /*  MAIN function                                                                                          */
  79. /*---------------------------------------------------------------------------------------------------------*/
  80. int main(void)
  81. {
  82.     S_RTC_TIME_DATA_T sWriteRTC, sReadRTC;
  83.     uint32_t u32Sec;
  84.     uint8_t u8IsNewDateTime = 0;

  85.     /* Unlock protected registers */
  86.     SYS_UnlockReg();

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

  89.     /* Lock protected registers */
  90.     SYS_LockReg();

  91.     /* Init UART0 for printf */
  92.     UART0_Init();

  93.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
  94.     printf("+-----------------------------------------+\n");
  95.     printf("|    RTC Date/Time and Tick Sample Code   |\n");
  96.     printf("+-----------------------------------------+\n\n");

  97.     /* Enable RTC NVIC */
  98.     NVIC_EnableIRQ(RTC_IRQn);

  99.     /* Open RTC and start counting */
  100.     sWriteRTC.u32Year       = 2016;
  101.     sWriteRTC.u32Month      = 5;
  102.     sWriteRTC.u32Day        = 15;
  103.     sWriteRTC.u32DayOfWeek  = RTC_SUNDAY;
  104.     sWriteRTC.u32Hour       = 15;
  105.     sWriteRTC.u32Minute     = 30;
  106.     sWriteRTC.u32Second     = 30;
  107.     sWriteRTC.u32TimeScale  = RTC_CLOCK_24;
  108.     RTC_Open(&sWriteRTC);

  109.     /* Enable RTC tick interrupt, one RTC tick is 1/4 second */
  110.     RTC_EnableInt(RTC_INTEN_TICKIEN_Msk);
  111.     RTC_SetTickPeriod(RTC_TICK_1_4_SEC);

  112.     printf("# Showing RTC date/time on UART0.\n\n");
  113.     printf("1.) Use PB.8 to check tick period time is 1/4 second or not.\n");
  114.     printf("2.) Show RTC date/time and change date/time after 5 seconds:\n");

  115.     /* Use PB.8 to check tick period time */
  116.     PB->MODE = (PB->MODE & ~GPIO_MODE_MODE8_Msk) | (GPIO_MODE_OUTPUT << GPIO_MODE_MODE8_Pos);

  117.     u32Sec = 0;
  118.     g_u32RTCTickINT = 0;
  119.     while(1)
  120.     {
  121.         if(g_u32RTCTickINT == 4)
  122.         {
  123.             g_u32RTCTickINT = 0;

  124.             /* Read current RTC date/time */
  125.             RTC_GetDateAndTime(&sReadRTC);
  126.             if(u8IsNewDateTime == 0)
  127.             {
  128.                 printf("    %d/%02d/%02d %02d:%02d:%02d\n",
  129.                        sReadRTC.u32Year, sReadRTC.u32Month, sReadRTC.u32Day, sReadRTC.u32Hour, sReadRTC.u32Minute, sReadRTC.u32Second);
  130.             }
  131.             else
  132.             {
  133.                 printf("    %d/%02d/%02d %02d:%02d:%02d\r",
  134.                        sReadRTC.u32Year, sReadRTC.u32Month, sReadRTC.u32Day, sReadRTC.u32Hour, sReadRTC.u32Minute, sReadRTC.u32Second);
  135.             }

  136.             if(u32Sec == sReadRTC.u32Second)
  137.             {
  138.                 printf("\nRTC tick period time is incorrect.\n");
  139.                 while(1);
  140.             }

  141.             u32Sec = sReadRTC.u32Second;

  142.             if(u8IsNewDateTime == 0)
  143.             {
  144.                 if(u32Sec == (sWriteRTC.u32Second + 5))
  145.                 {
  146.                     printf("\n");
  147.                     printf("3.) Update new date/time to 2017/05/15 11:12:13.\n");

  148.                     u8IsNewDateTime = 1;
  149.                     RTC_SetDate(2017, 5, 15, RTC_MONDAY);
  150.                     RTC_SetTime(11, 12, 13, RTC_CLOCK_24, RTC_AM);
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }

  156. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


 楼主| zhuomuniao110 发表于 2020-6-20 23:40 | 显示全部楼层
这个例子非常容易理解和上手,官方的库函数非常棒。
 楼主| zhuomuniao110 发表于 2020-6-20 23:41 | 显示全部楼层
主要用到了读写库函数。
 楼主| zhuomuniao110 发表于 2020-6-20 23:42 | 显示全部楼层
在库函数中有rtc.h文件的都可以支持这些功能
wahahaheihei 发表于 2020-6-20 23:56 | 显示全部楼层
看了你的帖,发现这个RTC外设还真有意思,不是所有系列配置了。
xinpian101 发表于 2020-6-21 17:52 | 显示全部楼层
莫非买了那个开发板
xinpian101 发表于 2020-6-21 17:57 | 显示全部楼层
这个可以带电池供电吗,是不是必须使用外部晶振啊。
643757107 发表于 2020-6-22 23:37 | 显示全部楼层
这一套库函数真好用。
稳稳の幸福 发表于 2020-6-23 23:27 | 显示全部楼层
每个例子中都有看到串口,串口真好用。
小灵通2018 发表于 2020-6-24 21:39 | 显示全部楼层
非常棒。
余三水 发表于 2020-6-28 16:49 | 显示全部楼层
楼主的程序很规范呀,赞一个。多多像楼主学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

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