您的位置:首页 > 其它

10、RTC实时时钟实验

2014-12-17 20:02 309 查看
1、实时时钟(RTC)是STM32一个独立的定时器,可以提供日历功能;


2、想要对实时时钟进行配置,必须进行相关寄存器的操作,大体的流程包括:通过设置寄存器 RCC_APB1ENR 的 PWREN 和 BKPEN 位来打开电源和后备接口的时钟 及电源控

制寄存器(PWR_CR)的 DBP 位来使能对后备寄存器和 RTC 的访问。

3、RTC分频则用到RTC_PRLH与RTC_PRLL这两个寄存器,其中RTC_PRLH中只用用到0~3位,RTC_PRLL则用到16位,总共20位。外部的输入的时钟频率是32.768K

如果给这个寄存器转载32767,则可以实现一秒钟的计数频率,就可以用这个时钟来实现精确到秒的时钟。

4、几种确定年、月、日、闰年、平年的软件实现方法:

计算是平、闰年代码,闰年一年有366天,它的二月份有29天,平年一年只有365,它的二月份只有28天。口诀是四年一闰,百年不闰,四百年再闰。

其实现代码如下:

//判断是否是闰年函数

//月份 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;

}

设置日期及获取从1970年到设置时间总秒数:

//设置时钟

//把输入的时钟转换为秒钟

//以1970年1月1日为基准

//1970~2099年为合法年份

//返回值:0,成功;其他:错误代码.

//月份数据表

u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表

//平年的月份日期表

const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};

u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)

{

u16 t;

u32 seccount=0;

if(syear<1970||syear>2099)return 1;

for(t=1970;t<syear;t++)
//把所有年份的秒钟相加

{

if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数

else seccount+=31536000;
//平年的秒钟数

}

smon-=1;

for(t=0;t<smon;t++)
//把前面月份的秒钟数相加

{

seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加

if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数

}

seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加

seccount+=(u32)hour*3600;//小时秒钟数

seccount+=(u32)min*60; //分钟秒钟数

seccount+=sec;//最后的秒钟加上去

RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
//使能PWR和BKP外设时钟

PWR_BackupAccessCmd(ENABLE);
//使能RTC和后备寄存器访问

RTC_SetCounter(seccount);
//设置RTC计数器的值

RTC_WaitForLastTask();
//等待最近一次对RTC寄存器的写操作完成

RTC_Get();

return 0;

}

从获取到的总的秒数推导出现在时间数:

/得到当前的时间

//返回值:0,成功;其他:错误代码.

u8 RTC_Get(void)

{

static u16 daycnt=0;

u32 timecount=0;

u32 temp=0;

u16 temp1=0;

timecount=RTC_GetCounter();

temp=timecount/86400; //得到天数(秒钟数对应的)

if(daycnt!=temp)//超过一天了

{

daycnt=temp;

temp1=1970;
//从1970年开始

while(temp>=365)

{

if(Is_Leap_Year(temp1))//是闰年

{

if(temp>=366)temp-=366;//闰年的秒钟数

else {temp1++;break;}

}

else temp-=365;
//平年

temp1++;

}

calendar.w_year=temp1;//得到年份

temp1=0;

while(temp>=28)//超过了一个月

{

if(Is_Leap_Year(calendar.w_year)&&temp1==1)//当年是不是闰年/2月份

{

if(temp>=29)temp-=29;//闰年的秒钟数

else break;

}

else

{

if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年

else break;

}

temp1++;

}

calendar.w_month=temp1+1;
//得到月份

calendar.w_date=temp+1;
//得到日期

}

temp=timecount%86400;
//得到秒钟数

calendar.hour=temp/3600;
//小时

calendar.min=(temp%3600)/60;
//分钟

calendar.sec=(temp%3600)%60;
//秒钟

calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//获取星期

return 0;

}

获取当前的星期数:

//获得现在是星期几

//功能描述:输入公历日期得到星期(只允许1901-2099年)

//输入参数:公历年月日

//返回值:星期号

u8 RTC_Get_Week(u16 year,u8 month,u8 day)

{

u16 temp2;

u8 yearH,yearL;

yearH=year/100;
yearL=year%100;

// 如果为21世纪,年份数加100

if (yearH>19)yearL+=100;

// 所过闰年数只算1900年之后的

temp2=yearL+yearL/4;

temp2=temp2%7;

temp2=temp2+day+table_week[month-1];

if (yearL%4==0&&month<3)temp2--;

return(temp2%7);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: