From 13cd1e65b3d0dcb47cec84fbad06db5682b9a1e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=90=83=E6=B2=B9=E7=82=B8=E9=B8=A1?= <1425962791@qq.com> Date: Sat, 27 Sep 2025 19:11:39 +0800 Subject: [PATCH] add rtc --- Power_Pico/Core/Inc/rtc.h | 2 + Power_Pico/Core/Src/rtc.c | 55 +++++++++++++++++++ Power_Pico/User/GUI/screens/ui_mainPage.c | 36 +++++++++++- .../User/Tasks/Src/user_HardwareInitTask.c | 4 ++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/Power_Pico/Core/Inc/rtc.h b/Power_Pico/Core/Inc/rtc.h index 2b87ae5..90c3910 100644 --- a/Power_Pico/Core/Inc/rtc.h +++ b/Power_Pico/Core/Inc/rtc.h @@ -41,6 +41,8 @@ extern RTC_HandleTypeDef hrtc; void MX_RTC_Init(void); /* USER CODE BEGIN Prototypes */ +void SetReferenceTime(void); +void GetElapsedTime_HMS(uint8_t *hours, uint8_t *minutes, uint8_t *seconds); /* USER CODE END Prototypes */ diff --git a/Power_Pico/Core/Src/rtc.c b/Power_Pico/Core/Src/rtc.c index 927856b..3690f98 100644 --- a/Power_Pico/Core/Src/rtc.c +++ b/Power_Pico/Core/Src/rtc.c @@ -102,4 +102,59 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) /* USER CODE BEGIN 1 */ +// 存储参考时间的时间戳(秒数) +static uint32_t s_ReferenceTimestamp = 0; + +/** + * @brief 设定参考时间:从现在开始计时 + * 调用此函数后,计时器清零,重新开始 + */ +void SetReferenceTime(void) +{ + RTC_TimeTypeDef sTime = {0}; + RTC_DateTypeDef sDate = {0}; + + // 获取当前RTC时间 + HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); + HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); + + // 转换为总秒数(简化计算,仅用于计时差) + s_ReferenceTimestamp = + (sDate.Year * 365 + sDate.Month * 31 + sDate.Date) * 24 * 3600 + + sTime.Hours * 3600 + + sTime.Minutes * 60 + + sTime.Seconds; +} + +/** + * @brief 获取从参考时间到现在经过的时间(时:分:秒) + * @param hours [out] 经过的小时数 + * @param minutes [out] 经过的分钟数 + * @param seconds [out] 经过的秒数 + */ +void GetElapsedTime_HMS(uint8_t *hours, uint8_t *minutes, uint8_t *seconds) +{ + RTC_TimeTypeDef sTime = {0}; + RTC_DateTypeDef sDate = {0}; + uint32_t currentTimestamp, elapsed; + + // 获取当前时间并转为秒数 + HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); + HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); + + uint32_t nowSeconds = + (sDate.Year * 365 + sDate.Month * 31 + sDate.Date) * 24 * 3600 + + sTime.Hours * 3600 + + sTime.Minutes * 60 + + sTime.Seconds; + + // 计算经过的秒数 + elapsed = nowSeconds - s_ReferenceTimestamp; + + // 转换为 时:分:秒 + *seconds = elapsed % 60; + *minutes = (elapsed / 60) % 60; + *hours = (elapsed / 3600); +} + /* USER CODE END 1 */ diff --git a/Power_Pico/User/GUI/screens/ui_mainPage.c b/Power_Pico/User/GUI/screens/ui_mainPage.c index f41180e..e2451a3 100644 --- a/Power_Pico/User/GUI/screens/ui_mainPage.c +++ b/Power_Pico/User/GUI/screens/ui_mainPage.c @@ -33,7 +33,7 @@ static void full_screen_refresh(void) { static void set_val_cur_label(float voltage, float current) { - char buf[10]; + char buf[16]; sprintf(buf, "%.2f", voltage); lv_label_set_text(ui_LabelValt, buf); if(current >= 1000000.0) { @@ -63,6 +63,40 @@ static void set_val_cur_label(float voltage, float current) lv_label_set_text(ui_LabelCur, buf); lv_label_set_text(ui_LabelUnitCur, "uA"); } + // set power + float power = voltage * current; // in uW + if(power >= 1000000.0) { + power = power / 1000000.0; + sprintf(buf, "%.2f", power); + lv_label_set_text(ui_LabelEnerge, buf); + lv_label_set_text(ui_LabelUnitEnerge, "W"); + } + else if(power >= 1000.0) { + power = power / 1000.0; + if(power >= 100.0) { + sprintf(buf, "%.1f", power); + } + else { + sprintf(buf, "%.2f", power); + } + lv_label_set_text(ui_LabelEnerge, buf); + lv_label_set_text(ui_LabelUnitEnerge, "mW"); + } + else { + if(power >= 100.0) { + sprintf(buf, "%.1f", power); + } + else { + sprintf(buf, "%.2f", power); + } + lv_label_set_text(ui_LabelEnerge, buf); + lv_label_set_text(ui_LabelUnitEnerge, "uW"); + } + // set time + uint8_t hours = 0, minutes = 0, seconds = 0; + GetElapsedTime_HMS(&hours, &minutes, &seconds); + sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds); + lv_label_set_text(ui_LabelTime, buf); } // event funtions diff --git a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c index fe44e73..de594dc 100644 --- a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c +++ b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c @@ -6,6 +6,7 @@ #include "tim.h" #include "stm32f4xx_it.h" #include "adc.h" +#include "rtc.h" // user #include "user_TasksInit.h" @@ -82,6 +83,9 @@ void HardwareInitTask(void *argument) // lcd // done in lvgl disp init + // rtc + SetReferenceTime(); + // ui // LVGL and disp init lv_init();