在使用RTC时,将天数转换为年月日,会经常遇到。
在使用EEPROM保存数据时,我们也会用到。比如每天保存一条数据,通常保存的第一个数据是天数值,而不是日期值。为什么呢?
因为记录是按照时间顺序的,其次后面要涉及到历史数据查询,通常是31天的历史数据。由于设备记录有可能是间断的,但是天数是递增的。如果保存的是日期,就需要占用很多内存,才能将保存的信息按照时间顺序排序,也就是说按照时间顺序读取,包括向上查询和向下查询。因此,保存的第一个数据是天数值,才是正确的选择。在保存数据前,先要对EEPROM的存储区进行划分。先划分出一“区域A”,用作保存31天的数据.,然后再将”区域A“划分成31个小块。第1块为1号的数据,第2块为2号的数据,第3块为3号的数据,.......第31块为31号的数据。如果记录器工作,就会根据日期,将数据保存到“区域A”中对应的小分区中。这个“区域A”,我们称之为数据缓存区,掉电不会丢失。
再划分出一“区域B”,用作保存12个月的数据,作为某年的”月分区“。当月份发生变化时,需要将”区域A“中对应的数据复制到对应的”月分区“中。查询时,只要读取”区域A“中的数据即可。如果要查询上月数据,则到“区域B”中去读取数据。
因此,年月日和天数相互转换,还是很重要的。
转换程序:
#include "stm32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#include "stdio.h" //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()
#include "USART1.h"
//注意:"USART1.h"要放在 "stdio.h" 包含文件之后,如果这个位置颠倒了,业不能打印浮点数。
typedef struct
{
u16 w_year;
u8 w_month;
u8 w_date;
}EEPROM_RTC_Data_Def;
EEPROM_RTC_Data_Def EEPROM_RTC_Data;
const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //平年的月份日期表
//函数功能:判断是否是闰年
//测试时间:2018年11月8日
u8 Is_Leap_Year(u16 year)
{
if(year % 4 == 0) //必须能被4整除
{
if(year % 100 == 0)
{
if(year % 400 == 0)return 1; //如果以00结尾,还要能被400整除
else return 0;
}
else return 1;
}
else return 0;
}
//函数功能:将天数转换为日期
//函数功能:将天数转换为日期
void Days_To_Date(u16 day)
{
u16 temp=0;
u16 year=0;
printf("\r\nInput: %u",day);
if(day)temp=day-1;
if(temp)
{
year=2000; //从2000年开始
while(temp>=365)
{
if(Is_Leap_Year(year))//是闰年
{
if(temp>=366)temp-=366;//闰年的天数
else break;
}
else temp-=365; //平年
year++;//年份加一
}
EEPROM_RTC_Data.w_year=year;//保存年份
year=0;//借用来计算月
while(temp>=28)//超过了一个月
{
if(Is_Leap_Year(EEPROM_RTC_Data.w_year)&&year==1)//当年是不是闰年/2月份
{
if(temp>=29)temp-=29;//闰年的天数
else break;
}
else
{
if(temp>=mon_table[year])temp-=mon_table[year];//平年
else break;
}
year++;
}
EEPROM_RTC_Data.w_month=year+1; //得到月份
EEPROM_RTC_Data.w_date=temp+1; //得到日期
}
else
{
EEPROM_RTC_Data.w_year=2000;
EEPROM_RTC_Data.w_month=1; //得到月份
EEPROM_RTC_Data.w_date=1; //得到日期
}
}
//函数功能:将日期转换为天数
//函数功能:将日期转换为天数
u16 MakeDate(EEPROM_RTC_Data_Def *date)
{
u16 days;
u16 year,temp;
u8 month;
month=date->w_month;
year=date->w_year;
temp=0;
if(Is_Leap_Year(year))//是闰年
{
if(month>1)temp=31;//1月有31天
if(month>2)temp=60;//1月和2月有60天
if(month>3)temp=91;//1月,2月和3月有91天
if(month>4)temp=121;//1月,2月,3月和4月有121天
if(month>5)temp=152;//1月,2月,3月,4月和5月有152天
if(month>6)temp=182;//1月,2月,3月,4月,5月和6月有182天
if(month>7)temp=213;//1月,2月,3月,4月,5月,6月和7月有213天
if(month>8)temp=244;//1月,2月,3月,4月,5月,6月,7月和8月有244天
if(month>9)temp=274;//1月,2月,3月,4月,5月,6月,7月,8月和9月有274天
if(month>10)temp=305;//1月,2月,3月,4月,5月,6月,7月,8月,9月和10月有305天
if(month>11)temp=335;//1月,2月,3月,4月,5月,6月,7月,8月,9月,10月和11月有335天
temp=temp+date->w_date;
}
else//平年
{
if(month>1)temp=31;//1月有31天
if(month>2)temp=59;//1月和2月有60天
if(month>3)temp=90;//1月,2月和3月有90天
if(month>4)temp=120;//1月,2月,3月和4月有120天
if(month>5)temp=151;//1月,2月,3月,4月和5月有151天
if(month>6)temp=181;//1月,2月,3月,4月,5月和6月有181天
if(month>7)temp=212;//1月,2月,3月,4月,5月,6月和7月有212天
if(month>8)temp=243;//1月,2月,3月,4月,5月,6月,7月和8月有243天
if(month>9)temp=273;//1月,2月,3月,4月,5月,6月,7月,8月和9月有273天
if(month>10)temp=304;//1月,2月,3月,4月,5月,6月,7月,8月,9月和10月有304天
if(month>11)temp=334;//1月,2月,3月,4月,5月,6月,7月,8月,9月,10月和11月有334天
temp=temp+date->w_date;
}
days=0;
if(month)
{
days=days+temp;
}
while(year>2000)
{
year--;//如果year=2001,则计算2000年有多少天
if(Is_Leap_Year(year))temp=366;//是闰年
else temp=365;//平年
days=days+temp;
}
printf("\r\nOutput: %u",days);
return days;
}
const char RTC_rn_REG[]="\r\n";
void Time_Display(void)
{
printf("%s",RTC_rn_REG);
printf("Date: %0.4d-%0.2d-%0.2d ",EEPROM_RTC_Data.w_year,EEPROM_RTC_Data.w_month,EEPROM_RTC_Data.w_date);
}
int main(void)
{
u16 day;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
USART1_Serial_Interface_Enable(115200);
printf("\r\nCPU reset\r\n");
day=365;//2000年是闰年,一年有366天
while(1)
{
Days_To_Date(day);
Time_Display();
MakeDate(&EEPROM_RTC_Data);
day++;
}
}
测试结果: