1 Star 0 Fork 1

yjwpm/ha102m

forked from 海石生风/ha102m 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
calendar.c 4.09 KB
一键复制 编辑 原始数据 按行查看 历史
海石生风 提交于 2018-06-25 23:39 . 完善参数设置
/*
* calendar.c
*
* Created on: 201863
* Author: chenss
*/
#include "calendar.h"
#define IS_LEAP_YEAR(year) (((year) % 400 == 0)||((year) % 4 == 0) && ((year) % 100 != 0))
static Calendar_DateTime_t _dateTime;
static uint8_t _weekOfYear;
static Calendar_UpdateCallback_t _updateCallback;
uint8_t _CalcWeekOfYear(Calendar_Date_t *date);
static void _SetAlarm(void);
void Calendar_Init(Calendar_UpdateCallback_t cb)
{
RTC_InitTypeDef RTC_InitStr;
RTC_TimeTypeDef RTC_TimeStr;
RTC_DateTypeDef RTC_DateStr;
Calendar_Date_t date;
_updateCallback = cb;
RTC_InitStr.RTC_HourFormat = RTC_HourFormat_24;
RTC_InitStr.RTC_AsynchPrediv = 0x7F;
RTC_InitStr.RTC_SynchPrediv = 0x00FF;
RTC_Init(&RTC_InitStr);
RTC_DateStructInit(&RTC_DateStr);
RTC_DateStr.RTC_WeekDay = RTC_Weekday_Friday;
RTC_DateStr.RTC_Date = 1;
RTC_DateStr.RTC_Month = RTC_Month_June;
RTC_DateStr.RTC_Year = 18;
RTC_SetDate(RTC_Format_BIN, &RTC_DateStr);
Calendar_GetDate(&date);
_weekOfYear = _CalcWeekOfYear(&date);
RTC_TimeStructInit(&RTC_TimeStr);
RTC_TimeStr.RTC_Hours = 12;
RTC_TimeStr.RTC_Minutes = 00;
RTC_TimeStr.RTC_Seconds = 00;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStr);
_SetAlarm();
}
static const uint8_t _daysOfYear[12] =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
static const uint8_t _daysOfMonth[12] =
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
uint16_t _CalcDayOfYear(Calendar_Date_t *date)
{
uint16_t days;
uint16_t year;
days = _daysOfYear[date->month - 1] + date->day;
year = (uint16_t) 2000 + date->year;
if((date->month > 2) && IS_LEAP_YEAR(year))
{
days += 1;
}
return days;
}
uint8_t _CalcWeekOfYear(Calendar_Date_t *date)
{
uint16_t d, base, dayPassOfYear;
dayPassOfYear = _CalcDayOfYear(date) - 1;
d = dayPassOfYear % 7;
if(date->weekDay >= d)
{
base = date->weekDay - d;
}
else
{
base = (7 + date->weekDay) - d;
}
return 1 + ((base + dayPassOfYear) / 7);
}
void Calendar_GetDate(Calendar_Date_t *date)
{
RTC_DateTypeDef RTC_DateStruct;
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
date->day = RTC_DateStruct.RTC_Date;
date->month = RTC_DateStruct.RTC_Month;
date->year = RTC_DateStruct.RTC_Year;
date->weekDay = RTC_DateStruct.RTC_WeekDay - RTC_Weekday_Monday; // һΪ0
}
bool Calendar_SetDate(Calendar_Date_t *date)
{
RTC_DateTypeDef RTC_DateStruct;
bool ret;
RTC_DateStruct.RTC_Date = date->day;
RTC_DateStruct.RTC_Month = date->month;
RTC_DateStruct.RTC_Year = date->year;
ret = RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct) == SUCCESS;
if(ret == SUCCESS)
{
_weekOfYear = _CalcWeekOfYear(date);
}
return ret;
}
void Calendar_GetTime(Calendar_Time_t *time)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
time->second = RTC_TimeStruct.RTC_Seconds;
time->minute = RTC_TimeStruct.RTC_Minutes;
time->hour = RTC_TimeStruct.RTC_Hours;
}
void Calendar_SetTime(Calendar_Time_t *time)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_TimeStruct.RTC_Seconds = time->second;
RTC_TimeStruct.RTC_Minutes = time->minute;
RTC_TimeStruct.RTC_Hours = time->hour;
return RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct) == SUCCESS;
}
static void _SetAlarm(void)
{
RTC_AlarmTypeDef RTC_AlarmStr;
RTC_AlarmStructInit(&RTC_AlarmStr);
RTC_AlarmStr.RTC_AlarmTime.RTC_Hours = 00;
RTC_AlarmStr.RTC_AlarmTime.RTC_Minutes = 00;
RTC_AlarmStr.RTC_AlarmTime.RTC_Seconds = 00;
RTC_AlarmStr.RTC_AlarmMask = RTC_AlarmMask_Minutes;
RTC_SetAlarm(RTC_Format_BIN, &RTC_AlarmStr);
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
RTC_AlarmCmd(ENABLE);
}
void Calendar_AlarmIrs(void)
{
_SetAlarm();
TASK_SET(TASK_Id_CalendarUpdate);
}
void Calendar_OnUpdate(void)
{
Calendar_DateTime_t dateTime;
Calendar_GetDate(& dateTime.date);
Calendar_GetTime(& dateTime.time);
if((dateTime.date.weekDay == 0) && (dateTime.time.hour == 0) && (dateTime.time.minute == 0))
{
_weekOfYear = _CalcWeekOfYear(& dateTime.date);
}
dateTime.weekOfYear = _weekOfYear;
_updateCallback(& dateTime);
}
uint8_t Calendar_MaxDayOfMonth(Calendar_Date_t *date)
{
uint8_t day;
day = _daysOfMonth[date->month - 1];
if(IS_LEAP_YEAR(date->year))
{
day ++;
}
return day;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/yjwpm/ha102m.git
[email protected]:yjwpm/ha102m.git
yjwpm
ha102m
ha102m
master

搜索帮助