mirror of
https://github.com/No-Chicken/Power-Pico.git
synced 2026-04-03 13:02:36 +08:00
add rtc
This commit is contained in:
@@ -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 */
|
||||
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user