请选择 进入手机版 | 继续访问电脑版
查看: 1442|回复: 3

[原创] 基于LPC54114开发板的RTC电子时钟

[复制链接]
  • TA的每日心情
    奋斗
    2023-2-15 00:12
  • 签到天数: 969 天

    [LV.10]以坛为家III

    175

    主题

    2843

    帖子

    34

    金牌会员

    Rank: 6Rank: 6

    积分
    7090
    最后登录
    2023-4-20
    发表于 2021-2-5 09:46:35 | 显示全部楼层 |阅读模式
    LPC54114的芯片内部配有RTC计时器,使用它与OLED显示屏相配合,可以快速地构成一个RTC电子时钟,其效果如图所示。
    file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg
    6.png
    RTC计时效果

    OLED屏的显示驱动,在前面已经介绍过,可直接加以引用。
    电子时钟的主程序如下:
    1. int main(void)
    2. {
    3. int loop = 1;     /* Used to fix the unreachable statement warning */
    4. uint32_t rtcCount;
    5. /* Starting time for this example used to set the RTC */
    6. struct tm startTime = {
    7. 0,                       /* tm_sec, seconds after the minute, 0 to 59 */
    8. 15,                     /* tm_min, minutes after the hour, 0 to 59 */
    9. 1,                       /* tm_hour, hours since midnight, 0 to 23 */
    10. 5,                       /* tm_mday, day of the month, 1 to 31 */
    11. 1,                       /* tm_mon, months since January, 0 to 11 */
    12. (2021 - TM_YEAR_BASE),       /* tm_year, years since base year (1900) */
    13. /* The following 3 fields are not used by the ConvertTimeRtc() function,
    14. while the ConvertRtcTime() fucntion will generate data in them from an
    15. RTC tick (except tm_isdst) */
    16. 0,                       /* tm_wday, days since Sunday, 0 to 6 */
    17. 0,                       /* tm_yday, days since January 1, 0 to 365 */
    18. 0                        /* tm_isdst, Daylight Savings Time flag */
    19. };
    20. /* Setup SystemCoreClock and any needed board code */
    21. SystemCoreClockUpdate();
    22. Board_Init();
    23. Board_LED_Set(0, false);
    24. /* Turn on the RTC 32K Oscillator */
    25. Chip_RTC_PowerUp(LPC_RTC);
    26. /* Enable the RTC oscillator, oscillator rate can be determined by
    27. calling Chip_Clock_GetRTCOscRate()     */
    28. Chip_Clock_EnableRTCOsc();
    29. /* Initialize RTC driver (enables RTC clocking) */
    30. Chip_RTC_Init(LPC_RTC);
    31. /* RTC reset */
    32. Chip_RTC_Reset(LPC_RTC);
    33. /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you
    34. need to disable it before setting the initial RTC count. */
    35. Chip_RTC_Disable(LPC_RTC);
    36. /* COnvert tm structure to RTC tick count */
    37. ConvertTimeRtc(&startTime, &rtcCount);
    38. Chip_RTC_SetCount(LPC_RTC, rtcCount);
    39. rtcCount += 1;
    40. Chip_RTC_SetAlarm(LPC_RTC, rtcCount);
    41. /* Enable RTC */
    42. Chip_RTC_Enable(LPC_RTC);
    43. /* Clear latched RTC interrupt statuses */
    44. Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));
    45. /* Enable RTC interrupt */
    46. NVIC_EnableIRQ(RTC_IRQn);
    47. /* Enable RTC alarm interrupt */
    48. Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN));
    49. OLED_IO_Init();
    50. delay_us(10);
    51. OLED_Init();
    52. OLED_Clear();
    53. OLED_ShowString(0,0,"LPC54114 TEST",16);
    54. OLED_ShowString(0,2,"OLED & RTC",16);
    55. delay_ms(1000);
    56. OLED_Clear();
    57. OLED_ShowString(16,0,"  -  -",16);
    58. OLED_ShowString(16,2,"  :  :",16);
    59. /* Sleep and do all the work in the RTC interrupt handler */
    60. while (loop) {
    61. if (rtcAlarm) {
    62. rtcCount = Chip_RTC_GetCount(LPC_RTC) + 1;
    63. Chip_RTC_SetAlarm(LPC_RTC, rtcCount);
    64. rtcAlarm = false;
    65. }
    66. /* Show RTC time in UT format */
    67. showTime(Chip_RTC_GetCount(LPC_RTC));
    68. /* Sleep while waiting for alarm */
    69. __WFI();
    70. }
    71. return 0;
    72. }
    复制代码


    相应的RTC显示函数为:
    1. static void showTime(uint32_t rtcCount)
    2. {
    3. struct tm currTime;
    4. ConvertRtcTime(rtcCount, &currTime);
    5. /* Todays date */
    6. OLED_ShowNum(16,0,(currTime.tm_year + TM_YEAR_BASE),2,16);
    7. OLED_ShowNum(40,0,currTime.tm_mon + 1,2,16);
    8. OLED_ShowNum(64,0,currTime.tm_mday,2,16);
    9. /* Current time */
    10. OLED_ShowNum(16,2,currTime.tm_hour,2,16);
    11. OLED_ShowNum(40,2,currTime.tm_min,2,16);
    12. OLED_ShowNum(64,2,currTime.tm_sec,2,16);
    13. }
    复制代码



    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-26 15:16
  • 签到天数: 266 天

    [LV.8]以坛为家I

    3298

    主题

    6545

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    32001
    最后登录
    2024-4-9
    发表于 2021-2-5 11:16:38 | 显示全部楼层
    有没有想法出一期视频教程
    签到签到
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2023-2-15 00:12
  • 签到天数: 969 天

    [LV.10]以坛为家III

    175

    主题

    2843

    帖子

    34

    金牌会员

    Rank: 6Rank: 6

    积分
    7090
    最后登录
    2023-4-20
     楼主| 发表于 2021-2-5 16:24:46 | 显示全部楼层
    本帖最后由 jinglixixi 于 2021-2-5 16:28 编辑
    NXP管管 发表于 2021-2-5 11:16
    有没有想法出一期视频教程

    实现个功能啥的还能行,出视频还真发憷呀!先把LPC54114搞定,为DIY挑战赛做好准备。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-26 15:16
  • 签到天数: 266 天

    [LV.8]以坛为家I

    3298

    主题

    6545

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    32001
    最后登录
    2024-4-9
    发表于 2021-2-5 16:29:51 | 显示全部楼层
    jinglixixi 发表于 2021-2-5 16:24
    实现个功能啥的还能行,出视频还真发憷呀!先把LPC54114搞定,为DIY挑战赛做好准备。 ...

    可以可以
    签到签到
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-19 07:00 , Processed in 0.113820 second(s), 22 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表