前面已实现了TFT屏的显示驱动,将它与片内的RTC计时器相配合即可快速实现一个电子时钟的制作,其显示效果如下图所示。
电子时钟显示效果图
利用函数MX_RTC_Init()可完成RTC的初始化,并进而完成起始时间的设置,其具体内容如下:
复制
static void MX_RTC_Init(void)
{
RTC_PrivilegeStateTypeDef privilegeState = {0};
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {0};
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
hrtc.Init.SynchPrediv = RTC_SYNCH_PREDIV;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
hrtc.Init.BinMode = RTC_BINARY_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
if (HAL_RTCEx_PrivilegeModeSet(&hrtc, &privilegeState) != HAL_OK)
{
Error_Handler();
}
sTime.Hours = 0x8;
sTime.Minutes = 0x30;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JULY; //RTC_MONTH_FEBRUARY;
sDate.Date = 0x20;
sDate.Year = 0x23;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sAlarm.AlarmTime.Hours = 0x2;
sAlarm.AlarmTime.Minutes = 0x20;
sAlarm.AlarmTime.Seconds = 0x30;
sAlarm.AlarmTime.SubSeconds = 0x56;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
sAlarm.Alarm = RTC_ALARM_A;
sAlarm.FlagAutoClr = ALARM_FLAG_AUTOCLR_DISABLE;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
}
为了完成计时值的显示,需对RTC的时间显示函数加以修改,其修改后的内容为:
复制
static void RTC_TimeShow(uint8_t *showtime)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
sprintf((char *)showtime, "%02d:%02d:%02d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
LCD_ShowString(50,90,showtime);
HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
sprintf((char *)showtime, "20%02d:%02d:%02d", sdatestructureget.Year, sdatestructureget.Month, sdatestructureget.Date);
LCD_ShowString(50,120,showtime);
}
实现电子时钟显示效果的主程序为:
复制
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_RTC_Init();
MX_ICACHE_Init();
BSP_LED_Init(LED1);
BSP_LED_Init(LED3);
RTCStatus = 1;
tft_gpio();
tft_Init();
BACK_COLOR=BLACK;
LCD_Clear(BLACK);
POINT_COLOR=GREEN;
LCD_ShowString(50,30,"NUCLEO_H563ZI");
POINT_COLOR=YELLOW;
LCD_ShowString(50,60,"RTC & TFT Test");
LCD_ShowString(50,260,"jinglixixi");
while (1)
{
RTC_TimeShow(aShowTime);
}
}
这样,就将TFT屏的显示功能与RTC的计时功能结合了起来,并实现了具体的应用。
---------------------
作者:jinglixixi
链接:https://bbs.21ic.com/icview-3316386-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。
---------------------
作者:jinglixixi
链接:https://bbs.21ic.com/icview-3316386-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。