在HC32F448内部配置了RTC计时器,这可是一个标准的RTC,而非目前有些厂家推出的芯片说是有RTC,却是个冒牌货,只能称得上是一个准RTC,必须通过软件来实现日历功能。
因此用其片内的RTC可以轻松地实现电子时钟的功能,以串口进行测试的效果如图1所示。
图1 RTC测试效果
对RTC进行设置的函数为:
static void RTC_CalendarConfig(void)
{
stc_rtc_date_tstcRtcDate;
stc_rtc_time_tstcRtcTime;
stcRtcDate.u8Year = 23U;
stcRtcDate.u8Month = RTC_MONTH_JULY;
stcRtcDate.u8Day = 28U;
stcRtcDate.u8Weekday = RTC_WEEKDAY_WEDNESDAY;
stcRtcTime.u8Hour = 23U;
stcRtcTime.u8Minute = 59U;
stcRtcTime.u8Second = 55U;
stcRtcTime.u8AmPm = RTC_HOUR_12H_AM;
if (LL_OK != RTC_SetDate(RTC_DATA_FMT_DEC, &stcRtcDate)) {
DDL_Printf("Set Date failed!\r\n");
}
if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime)) {
DDL_Printf("Set Time failed!\r\n");
}
}
通过与前面介绍的OLED屏相配合,可完成电子时钟的功能,其主程序为:
int32_t main(void)
{
stc_rtc_date_tstcCurrentDate;
stc_rtc_time_tstcCurrentTime;
LL_PERIPH_WE(EXAMPLE_PERIPH_WE);
BSP_CLK_Init();
BSP_IO_Init();
BSP_LED_Init();
LED_Init();
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"HC32F448",16);
OLED_ShowString(20,2,"RTC & OLED",16);
OLED_ShowString(20,4," : :",16);
OLED_ShowString(20,6," - -",16);
DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_Preinit);
RTC_Config();
LL_PERIPH_WP(EXAMPLE_PERIPH_WP);
for (;;)
{
if (1U == u8SecIntFlag)
{
u8SecIntFlag = 0U;
BSP_LED_Toggle(LED_RED);
if (LL_OK == RTC_GetDate(RTC_DATA_FMT_DEC, &stcCurrentDate))
{
if (LL_OK == RTC_GetTime(RTC_DATA_FMT_DEC, &stcCurrentTime))
{
OLED_ShowNum(20,4,stcCurrentTime.u8Hour,2,16);
OLED_ShowNum(44,4,stcCurrentTime.u8Minute,2,16);
OLED_ShowNum(68,4,stcCurrentTime.u8Second,2,16);
OLED_ShowNum(20,6,stcCurrentDate.u8Year,2,16);
OLED_ShowNum(44,6,stcCurrentDate.u8Month,2,16);
OLED_ShowNum(68,6,stcCurrentDate.u8Day,2,16);
} else {
DDL_Printf("Get time failed!\r\n");
}
} else {
DDL_Printf("Get date failed!\r\n");
}
}
}
}
经程序的编译和下载,其测试结果如图2所示,说明功能正确。
图2 电子时钟效果
|