虽然F4硬件自带了日历功能,可以直接读年月日,但有些场合使用计数值存储还是更方便。
这里根据正点原子F1中RTC例程的年月日转换,写了适用于F4的RTC日历转计数值。
两个函数的声明:
- uint32_t RTC_DataToCnt(RTC_TimeTypeDef RTC_TimeStruct,RTC_DateTypeDef RTC_DateStruct);
- void RTC_CntToData(uint32_t RTC_Count,RTC_TimeTypeDef* RTC_TimeStruct,RTC_DateTypeDef* RTC_DateStruct);
复制代码
函数实现内容:
- const u8 mon_table[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
- //判断是否是闰年函数
- //月份 1 2 3 4 5 6 7 8 9 10 11 12
- //闰年 31 29 31 30 31 30 31 31 30 31 30 31
- //非闰年 31 28 31 30 31 30 31 31 30 31 30 31
- //输入:年份
- //输出:该年份是不是闰年.1,是.0,不是
- 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;
- }
- //
- uint32_t RTC_DataToCnt(RTC_TimeTypeDef RTC_TimeStruct,RTC_DateTypeDef RTC_DateStruct)
- {
- u16 t;
- u32 seccount=0;
- for(t=0; t<RTC_DateStruct.RTC_Year; t++) //把所有年份的秒钟相加
- {
- if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数
- else seccount+=31536000; //平年的秒钟数
- }
- RTC_DateStruct.RTC_Month-=1;
- for(t=0; t<RTC_DateStruct.RTC_Month; t++) //把前面月份的秒钟数相加
- {
- seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加
- if(Is_Leap_Year(RTC_DateStruct.RTC_Year)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数
- }
- seccount+=(u32)(RTC_DateStruct.RTC_Date-1)*86400;//把前面日期的秒钟数相加
- seccount+=(u32)RTC_TimeStruct.RTC_Hours*3600;//小时秒钟数
- seccount+=(u32)RTC_TimeStruct.RTC_Minutes*60; //分钟秒钟数
- seccount+=RTC_TimeStruct.RTC_Seconds;//最后的秒钟加上去
- return seccount;
- }
- //
- void RTC_CntToData(uint32_t RTC_Count,RTC_TimeTypeDef* RTC_TimeStruct,RTC_DateTypeDef* RTC_DateStruct)
- {
- static u16 daycnt=0;
- u32 timecount=0;
- u32 temp=0;
- u16 temp1=0;
- timecount=RTC_Count;
- temp=timecount/86400; //得到天数(秒钟数对应的)
- if(daycnt!=temp)//超过一天了
- {
- daycnt=temp;
- temp1=0; //从1970年开始 //从0年开始
- while(temp>=365)
- {
- if(Is_Leap_Year(temp1))//是闰年
- {
- if(temp>=366)temp-=366;//闰年的秒钟数
- else {
- temp1++;
- break;
- }
- }
- else temp-=365; //平年
- temp1++;
- }
- RTC_DateStruct->RTC_Year=temp1;//得到年份
- temp1=0;
- while(temp>=28)//超过了一个月
- {
- if(Is_Leap_Year(RTC_DateStruct->RTC_Year)&&temp1==1)//当年是不是闰年/2月份
- {
- if(temp>=29)temp-=29;//闰年的秒钟数
- else break;
- }
- else
- {
- if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年
- else break;
- }
- temp1++;
- }
- RTC_DateStruct->RTC_Month=temp1+1; //得到月份
- RTC_DateStruct->RTC_Date=temp+1; //得到日期
- }
- temp=timecount%86400; //得到秒钟数
- RTC_TimeStruct->RTC_Hours=temp/3600; //小时
- RTC_TimeStruct->RTC_Minutes=(temp%3600)/60; //分钟
- RTC_TimeStruct->RTC_Seconds=(temp%3600)%60; //秒钟
- return ;
- }
复制代码
简单的测试程序,其中rtc_update_flag在RTC中断中赋值的,用于更新后打印。
- while(1)
- {
- if(rtc_update_flag==1)
- {
- rtc_update_flag=0;
- RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
- RTC_GetDate(RTC_Format_BIN,&RTC_DateStruct);
- sprintf((char*)tbuf,"read :%02d:%02d:%02d-%02d:%02d:%02d\r\n",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date,
- RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
- printf("%s",tbuf);
- rtc_cnt=RTC_DataToCnt(RTC_TimeStruct,RTC_DateStruct);
- RTC_CntToData(rtc_cnt,&RTC_TimeStruct1,&RTC_DateStruct1);
- sprintf((char*)tbuf,"update:%02d:%02d:%02d-%02d:%02d:%02d\r\n",RTC_DateStruct1.RTC_Year,RTC_DateStruct1.RTC_Month,RTC_DateStruct1.RTC_Date,
- RTC_TimeStruct1.RTC_Hours,RTC_TimeStruct1.RTC_Minutes,RTC_TimeStruct1.RTC_Seconds);
- printf("%s",tbuf);
- }
- }
复制代码
打印结果如下:
————————————————
版权声明:小盼你最萌哒
如有侵权请联系删除
|