[DemoCode下载] NUC100的RTC定时器与滴答时钟

[复制链接]
 楼主| 小灵通2018 发表于 2022-6-20 20:39 | 显示全部楼层 |阅读模式
  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: 2 $
  5. * $Date: 15/04/13 10:09a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Get the current RTC data/time per tick.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC100Series.h"


  12. #define PLL_CLOCK           50000000


  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_NUC100Series.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 Internal RC 22.1184MHz clock */
  43.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  44.     /* Waiting for Internal RC clock ready */
  45.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  48.     /* Enable external XTAL 12MHz clock and 32 kHz XTAL */
  49.     CLK_EnableXtalRC((CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_XTL32K_EN_Msk));

  50.     /* Waiting for clock ready */
  51.     CLK_WaitClockReady((CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_XTL32K_STB_Msk));

  52.     /* Set core clock as PLL_CLOCK from PLL */
  53.     CLK_SetCoreClock(PLL_CLOCK);
  54.    
  55.     /* Enable UART module clock */
  56.     CLK_EnableModuleClock(UART0_MODULE);   
  57.    
  58.     /* Select UART module clock source */
  59.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  60.    
  61.     /* Enable RTC module clock */
  62.     CLK_EnableModuleClock(RTC_MODULE);   
  63.         
  64.     /*---------------------------------------------------------------------------------------------------------*/
  65.     /* Init I/O Multi-function                                                                                 */
  66.     /*---------------------------------------------------------------------------------------------------------*/
  67.     /* Set PB multi-function pins for UART0 RXD, TXD */
  68.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  69.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
  70. }

  71. void UART0_Init(void)
  72. {
  73.     /*---------------------------------------------------------------------------------------------------------*/
  74.     /* Init UART                                                                                               */
  75.     /*---------------------------------------------------------------------------------------------------------*/
  76.     /* Reset UART0 */
  77.     SYS_ResetModule(UART0_RST);

  78.     /* Configure UART0 and set UART0 Baudrate */
  79.     UART_Open(UART0, 115200);
  80. }

  81. /*---------------------------------------------------------------------------------------------------------*/
  82. /*  MAIN function                                                                                          */
  83. /*---------------------------------------------------------------------------------------------------------*/
  84. int main(void)
  85. {
  86.     S_RTC_TIME_DATA_T sWriteRTC, sReadRTC;
  87.     uint32_t u32Sec;

  88.     /* Unlock protected registers */
  89.     SYS_UnlockReg();

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

  92.     /* Lock protected registers */
  93.     SYS_LockReg();

  94.     /* Init UART0 for printf */
  95.     UART0_Init();

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

  100.     /* Open RTC and start counting */
  101.     sWriteRTC.u32Year       = 2014;
  102.     sWriteRTC.u32Month      = 2;
  103.     sWriteRTC.u32Day        = 6;
  104.     sWriteRTC.u32DayOfWeek  = RTC_THURSDAY;
  105.     sWriteRTC.u32Hour       = 15;
  106.     sWriteRTC.u32Minute     = 30;
  107.     sWriteRTC.u32Second     = 30;
  108.     sWriteRTC.u32TimeScale  = RTC_CLOCK_24;
  109.     RTC_Open(&sWriteRTC);

  110.     /* Enable RTC tick interrupt, one RTC tick is 1/4 second */
  111.     NVIC_EnableIRQ(RTC_IRQn);
  112.     RTC_EnableInt(RTC_RIER_TIER_Msk);
  113.     RTC_SetTickPeriod(RTC_TICK_1_4_SEC);

  114.     printf("# Showing RTC Date/Time on UART-0.\n\n");
  115.     printf("Date/Time is: (Use PB.8 to check tick period time is 1/4 second) \n");

  116.     /* Use PB.8 to check tick period time */
  117.     PB->PMD = (PB->PMD & ~0x00030000) | (0x00010000);

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

  125.             /* Read current RTC date/time */
  126.             RTC_GetDateAndTime(&sReadRTC);
  127.             printf("    %d/%02d/%02d %02d:%02d:%02d\r",
  128.                    sReadRTC.u32Year, sReadRTC.u32Month, sReadRTC.u32Day, sReadRTC.u32Hour, sReadRTC.u32Minute, sReadRTC.u32Second);

  129.             if(u32Sec == sReadRTC.u32Second)
  130.             {
  131.                 printf("\nRTC tick period time is incorrect.\n");
  132.                 while(1);
  133.             }

  134.             u32Sec = sReadRTC.u32Second;
  135.         }
  136.     }
  137. }

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


 楼主| 小灵通2018 发表于 2022-6-20 20:39 | 显示全部楼层
可以轻松实现时间年月日计时
sparrow054 发表于 2022-7-6 21:50 | 显示全部楼层
区别和功能应用有何种不同
麻花油条 发表于 2022-7-8 15:32 来自手机 | 显示全部楼层
两者有何本质区别
kiwis66 发表于 2022-7-14 21:14 | 显示全部楼层
时钟的应用,基本又重要
sadicy 发表于 2022-7-16 15:50 | 显示全部楼层
是不是应用还是有区别的
tail066 发表于 2022-8-7 14:03 | 显示全部楼层
滴答什么时候用
carpsnow 发表于 2022-9-3 14:55 | 显示全部楼层
不是RTC么?
ghuca 发表于 2022-9-10 21:28 | 显示全部楼层
精度达得到吗  
hearstnorman323 发表于 2022-9-10 21:53 | 显示全部楼层
怎么选择内部时钟
ccook11 发表于 2022-9-11 13:43 | 显示全部楼层
怎么用库函数使用滴答定时器?
10299823 发表于 2022-9-11 15:23 | 显示全部楼层
几种时钟和定时器机制
Stahan 发表于 2022-9-13 21:43 | 显示全部楼层
有什么区别吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

1727

帖子

4

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