STM32F4 RTC日历和Count转换

本文介绍了一种将STM32F4的RTC日历数据转换为计数值的方法,包括两个主要函数:RTC_DataToCnt和RTC_CntToData。通过详细解释代码实现,展示了如何进行年月日到计数值的转换,以及如何逆向操作。

虽然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);

        }
    }

打印结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值