[DemoCode下载] M4系列的滴答时钟用法

[复制链接]
 楼主| 643757107 发表于 2020-1-31 22:25 | 显示全部楼层 |阅读模式
  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]    Get the current RTC data/time per tick.
  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. 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_M451Series.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 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.     uint32_t u32Sec;
  83.     uint8_t u8IsNewDateTime = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

  140.             u32Sec = sReadRTC.u32Second;

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

  147.                     u8IsNewDateTime = 1;
  148.                     RTC_SetDate(2014, 5, 18, RTC_SUNDAY);
  149.                     RTC_SetTime(11, 12, 13, RTC_CLOCK_24, RTC_AM);
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }

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


 楼主| 643757107 发表于 2020-1-31 22:25 | 显示全部楼层
Scale是形式,这里是24小时制
 楼主| 643757107 发表于 2020-1-31 22:28 | 显示全部楼层
深入了解库函数的参数意义非常重要。
734774645 发表于 2020-1-31 22:33 | 显示全部楼层
734774645 发表于 2020-1-31 22:34 | 显示全部楼层
参考代码非常给力。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

222

主题

3931

帖子

11

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