在开发板的例程中,配有RTC计时的程序,将它与前面介绍的OLED屏相配合可轻松实现一个RTC电子时钟。 RTC例程的功能是先读取RTC开始计时,在5秒后再按重新设置的时间来计时,其运行效果如图1和图2所示。 图1 读取RTC时间 图2 设置新时间并计时 图3 OLED屏显示RTC 实现图3所示计时效果的主程序为: int main(void)
{
S_RTC_TIME_DATA_T sInitTime, sReadRTC;
uint32_t u32Sec;
uint8_t u8IsNewDateTime = 0;
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Init UART for printf */
UART_Init();
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
GPIO_SetMode(PC, BIT1, GPIO_MODE_OUTPUT);
GPIO_SetMode(PC, BIT0, GPIO_MODE_OUTPUT);
GPIO_SetMode(PH, BIT4, GPIO_MODE_INPUT);
PB14 = 1;
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"M471 RTC",16);
OLED_ShowString(0,2,"OLED DISPLAY",16);
TIMER_Delay(TIMER0, 2000000);
OLED_Clear();
/* Enable RTC NVIC */
NVIC_EnableIRQ(RTC_IRQn);
/* Open RTC and start counting */
sInitTime.u32Year = 2021;
sInitTime.u32Month =8;
sInitTime.u32Day = 13;
sInitTime.u32Hour = 12;
sInitTime.u32Minute = 30;
sInitTime.u32Second = 0;
sInitTime.u32DayOfWeek = 5;
sInitTime.u32TimeScale = RTC_CLOCK_24;
RTC_Open(&sInitTime);
/* Enable RTC tick interrupt, one RTC tick is 1 second */
RTC_EnableInt(RTC_INTEN_TICKIEN_Msk);
RTC_SetTickPeriod(RTC_TICK_1_SEC);
/* Use PA.2 to check tick period time */
PA->MODE = (PA->MODE & ~GPIO_MODE_MODE2_Msk) | (GPIO_MODE_OUTPUT << GPIO_MODE_MODE2_Pos);
PA2 = 1;
u32Sec = 0;
g_u32RTCTInt = 0;
OLED_ShowString(0,0,"00:00:00",16);
while(1)
{
if(g_u32RTCTInt == 1)
{
g_u32RTCTInt = 0;
/* Read current RTC date/time */
RTC_GetDateAndTime(&sReadRTC);
OLED_ShowNum(0,0,sReadRTC.u32Hour,2,16);
OLED_ShowNum(24,0,sReadRTC.u32Minute,2,16);
OLED_ShowNum(48,0,sReadRTC.u32Second,2,16);
if(u32Sec == sReadRTC.u32Second)
{
while(1) {}
}
u32Sec = sReadRTC.u32Second;
if(u8IsNewDateTime == 0)
{
if(u32Sec == (sInitTime.u32Second + 5))
{
u8IsNewDateTime = 1;
RTC_SetDate(2021, 7, 30, RTC_FRIDAY);
RTC_SetTime(11, 12, 13, RTC_CLOCK_24, RTC_AM);
}
}
}
}
}
|