本帖最后由 sujingliang 于 2024-9-9 08:16 编辑
目的
通过H7S7L8H6H RTC获取时间,并通过TouchGFX analogClock、digitalClock显示出来。
一、TouchGFX配置
增加一个Screen,放置几个部件,其中包含:analogClock1、digitalClock1
F4->生成代码。
二、STM32CubeMX配置
配置RCC,高低速时钟均为外部时钟
配置RTC:
生成代码
三、代码修改
1、RTC初始化
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_PrivilegeStateTypeDef privilegeState = {0};
//RTC_TimeTypeDef sTime = {0};
//RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
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();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
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_JANUARY;
sDate.Date = 0x1;
sDate.Year = 0x0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
这两个变量设成全局了
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
2、model.cpp
这里一定要加上:HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);,虽然没有用。不加,HAL_RTC_GetTime也无法获得时间。
extern RTC_HandleTypeDef hrtc;
extern RTC_TimeTypeDef sTime;
extern RTC_DateTypeDef sDate;
void Model::tick()
{
static uint8_t tick=0;
tick++;
if(tick%60==0)
{
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
modelListener->updateTime(sTime.Hours,sTime.Minutes,sTime.Seconds);
printf( "%2d:%2d:%2d\r\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
tick=0;
}
}
3、ModelListener中增加virtual void updateTime(unsigned char newHours, unsigned char newMinutes, unsigned char newSeconds);
#ifndef MODELLISTENER_HPP
#define MODELLISTENER_HPP
#include <gui/model/Model.hpp>
class ModelListener
{
public:
ModelListener() : model(0) {}
virtual ~ModelListener() {}
virtual void uart4Updated();
virtual void updateTime(unsigned char newHours, unsigned char newMinutes, unsigned char newSeconds);
void bind(Model* m)
{
model = m;
}
protected:
Model* model;
};
#endif // MODELLISTENER_HPP
4、Screen4Presenter中增加:
void Screen4Presenter::updateTime(unsigned char newHours, unsigned char newMinutes, unsigned char newSeconds)
{
view.updateTime(newHours, newMinutes, newSeconds);
}
5、Screen4View中增加
void Screen4View::updateTime(unsigned char newHours, unsigned char newMinutes, unsigned char newSeconds)
{
analogClock1.setTime24Hour(newHours,newMinutes,newSeconds);
digitalClock1.setTime24Hour(newHours,newMinutes,newSeconds);
}
6、HAL_RTC_MspInit中增加:HAL_PWR_EnableBkUpAccess();
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(hrtc->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
HAL_PWR_EnableBkUpAccess();
/* USER CODE END RTC_MspInit 0 */
四、效果
1、屏幕效果
2、串口输出:
3、视频:
五、总结
1、感谢南来之风 https://bbs.21ic.com/icview-3400954-1-1.html排雷,否则不知道要在RTC初始化卡多久。
2、TouchGFX CLOCK+RTC可以参考:https://www.bilibili.com/video/BV1pp421U7gK/?spm_id_from=333.337.search-card.all.click&vd_source=5b0f94f2f57c38a43471771787964a99
|