在LPC54114的芯片内部配有RTC计时器,使用它与OLED显示屏相配合,可以快速地构成一个RTC电子时钟,其效果如图所示。 file:///C:/Users/PC/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg RTC计时效果
OLED屏的显示驱动,在前面已经介绍过,可直接加以引用。 电子时钟的主程序如下: - int main(void)
- {
- int loop = 1; /* Used to fix the unreachable statement warning */
- uint32_t rtcCount;
- /* Starting time for this example used to set the RTC */
- struct tm startTime = {
- 0, /* tm_sec, seconds after the minute, 0 to 59 */
- 15, /* tm_min, minutes after the hour, 0 to 59 */
- 1, /* tm_hour, hours since midnight, 0 to 23 */
- 5, /* tm_mday, day of the month, 1 to 31 */
- 1, /* tm_mon, months since January, 0 to 11 */
- (2021 - TM_YEAR_BASE), /* tm_year, years since base year (1900) */
- /* The following 3 fields are not used by the ConvertTimeRtc() function,
- while the ConvertRtcTime() fucntion will generate data in them from an
- RTC tick (except tm_isdst) */
- 0, /* tm_wday, days since Sunday, 0 to 6 */
- 0, /* tm_yday, days since January 1, 0 to 365 */
- 0 /* tm_isdst, Daylight Savings Time flag */
- };
- /* Setup SystemCoreClock and any needed board code */
- SystemCoreClockUpdate();
- Board_Init();
- Board_LED_Set(0, false);
- /* Turn on the RTC 32K Oscillator */
- Chip_RTC_PowerUp(LPC_RTC);
- /* Enable the RTC oscillator, oscillator rate can be determined by
- calling Chip_Clock_GetRTCOscRate() */
- Chip_Clock_EnableRTCOsc();
- /* Initialize RTC driver (enables RTC clocking) */
- Chip_RTC_Init(LPC_RTC);
- /* RTC reset */
- Chip_RTC_Reset(LPC_RTC);
- /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you
- need to disable it before setting the initial RTC count. */
- Chip_RTC_Disable(LPC_RTC);
- /* COnvert tm structure to RTC tick count */
- ConvertTimeRtc(&startTime, &rtcCount);
- Chip_RTC_SetCount(LPC_RTC, rtcCount);
- rtcCount += 1;
- Chip_RTC_SetAlarm(LPC_RTC, rtcCount);
- /* Enable RTC */
- Chip_RTC_Enable(LPC_RTC);
- /* Clear latched RTC interrupt statuses */
- Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ));
- /* Enable RTC interrupt */
- NVIC_EnableIRQ(RTC_IRQn);
- /* Enable RTC alarm interrupt */
- Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN));
- OLED_IO_Init();
- delay_us(10);
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"LPC54114 TEST",16);
- OLED_ShowString(0,2,"OLED & RTC",16);
- delay_ms(1000);
- OLED_Clear();
- OLED_ShowString(16,0," - -",16);
- OLED_ShowString(16,2," : :",16);
- /* Sleep and do all the work in the RTC interrupt handler */
- while (loop) {
- if (rtcAlarm) {
- rtcCount = Chip_RTC_GetCount(LPC_RTC) + 1;
- Chip_RTC_SetAlarm(LPC_RTC, rtcCount);
- rtcAlarm = false;
- }
- /* Show RTC time in UT format */
- showTime(Chip_RTC_GetCount(LPC_RTC));
- /* Sleep while waiting for alarm */
- __WFI();
- }
- return 0;
- }
复制代码
相应的RTC显示函数为: - static void showTime(uint32_t rtcCount)
- {
- struct tm currTime;
- ConvertRtcTime(rtcCount, &currTime);
- /* Todays date */
- OLED_ShowNum(16,0,(currTime.tm_year + TM_YEAR_BASE),2,16);
- OLED_ShowNum(40,0,currTime.tm_mon + 1,2,16);
- OLED_ShowNum(64,0,currTime.tm_mday,2,16);
- /* Current time */
- OLED_ShowNum(16,2,currTime.tm_hour,2,16);
- OLED_ShowNum(40,2,currTime.tm_min,2,16);
- OLED_ShowNum(64,2,currTime.tm_sec,2,16);
- }
复制代码
|