Files
stc32g128k/App/clock.h
edisondeng cb37b1deea feat(clock): add day/month/year rollover and Zeller weekday calc
Extends Clock_IncMS()'s existing sec->min->hour cascade to also roll
day->month->year (with leap-year Feb 29 via Days_In_Month()), and adds
Clock_RecalcWeekday() computing current_weekday via Zeller's congruence
whenever the date changes, rather than trusting PCF8563's own weekday
register (which would drift if the date is edited manually). Verified
the formula against 2026-07-31, a known Friday.
2026-07-31 19:32:44 +08:00

37 lines
1.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __CLOCK_H__
#define __CLOCK_H__
#include "config.h"
// 全局软时钟变量声明
extern u8 current_hour;
extern u8 current_min;
extern u8 current_sec;
extern u8 current_day; // 日 (1~31)
extern u8 current_month; // 月 (1~12)
extern u8 current_year; // 年 (0~99代表 2000~2099不处理世纪)
extern u8 current_weekday; // 星期 (0=周日, 1=周一, ..., 6=周六)
extern volatile u8 clock_updated;
extern volatile bit second_tick_flag; // 整秒滴答标志位,每次 current_sec 递增时置 1供主循环转换为事件
/**
* @brief 系统软时钟毫秒递增更新 (在 1ms 中断中调用)
*/
void Clock_IncMS(void);
/**
* @brief 返回指定年月的天数上限 (处理闰年 2 月)
* @param year 年 (0~99代表 2000~2099)
* @param month 月 (1~12)
*/
u8 Days_In_Month(u8 year, u8 month);
/**
* @brief 根据年月日重新计算星期,并写回 current_weekday
* @details 采用 Zeller 公式现算,不依赖 RTC 自身的星期寄存器,
* 避免手动改日期后星期跟着错乱;日期发生变化(自然进位或手动调节)时都要调用一次。
*/
void Clock_RecalcWeekday(void);
#endif // __CLOCK_H__