RTC Configuration and initialization of STM32-rtc.h RTC.C

Source: Internet
Author: User

<rtc.h>#include"stm32f10x.h"#ifndef _rtc_h#define_rtc_htypedefstruct{vu8 hour;    Vu8 Mintue;    Vu8 second;    VU16 year;    Vu8 month;    Vu8 Day; Vu8 Week;} _calendar_obj;//extern _calendar_obj calendar;U8 Rtc_init (void); U8 Is_leap_year (U16 _year); _calendar_obj Rtc_getcalendar (void); U8 Rtc_getweek (U16 _year,u8 _month,u8 _day); U8 Rtc_setcalendar (U16 _year,u8 _month,u8 _day,u8 _hour,u8 _min,u8 _sec ond);#endif<rtc.c>#include"rtc.h"ConstU8 month_table[]={ to, -, to, -, to, -, to, to, -, to, -, to};ConstU8 week_table[]={0,3,3,6,1,4,6,2,5,0,3,5};_calendar_obj Calendar;voidDelay_ms (U16 time) {U16 I=0;  while(time--) {i=12000;  while(i--); }}u8 is_leap_year (U16 _year) {if((_year%4&& _year% -!=0) | (_year% -==0))return 0; Else return 1;} U8 Rtc_init (void) {U16 temp=0; //Enable RTC_CLK outside    if(Bkp_readbackupregister (BKP_DR1)! =0x5050) {rcc_apb1periphclockcmd (RCC_APB1PERIPH_PWR|rcc_apb1periph_bkp,enable);        Pwr_backupaccesscmd (ENABLE);        Bkp_deinit ();        Rcc_lseconfig (rcc_lse_on);  while(Rcc_getflagstatus (rcc_flag_hserdy) = =RESET) {Temp++; Delay_ms (Ten); }        if(temp> -)return 0;        Rcc_rtcclkconfig (Rcc_rtcclksource_lse);        Rcc_rtcclkcmd (ENABLE);        Rtc_waitforlasttask ();        Rtc_itconfig (rtc_it_sec,enable);        Rtc_waitforlasttask (); Rtc_setprescaler (32767);        Rtc_waitforlasttask (); Rtc_setcalendar ( -,Ten,Ten,Ten,Ten,Ten);          Rtc_exitconfigmode (); Bkp_writebackupregister (BKP_DR1,0x5050); }    Else{rtc_waitforsynchro ();        Rtc_itconfig (rtc_it_sec,enable);    Rtc_waitforlasttask (); }    return 1;} _calendar_obj Rtc_getcalendar (void){    StaticU16 daycount=0; U32 Timecount=Rtc_getcounter (); U32 Daynum=timecount/86400;// DayU16 temp=0; //Calendar Year    if(Daycount!=daynum)//>1{Daycount=Daynum; Temp=1970;  while(daynum>365)        {            if(Is_leap_year (temp)) {if(daynum>=366) Daynum=daynum-366; Else                     Break; }            ElseDaynum=daynum-365; Temp++; } calendar.year=temp;//Set Yeartemp=0;//Reset Temp//Calendar Month         while(daynum>= -)        {            if(Is_leap_year (calendar.year) && temp==1)            {                if(daynum>= in) daynum=daynum- in; Else  Break; }            Else            {                if(daynum>=month_table[temp]) daynum=daynum-Month_table[temp]; Else                     Break; } temp++; } calendar.month=temp+1;//Set Monthcalendar.day=daynum+1;//Set Day} daynum=timecount%86400; Calendar.hour=daynum/3600;//Set HourCalendar.mintue= (daynum%3600)/ -;//Set MintueCalendar.second= (daynum%3600)% -;//Set Second        returnCalendar;}    U8 Rtc_setcalendar (U16 _year,u8 _month,u8 _day,u8 _hour,u8 _min,u8 _second) {U16 i; U32 Seccount=0;//Second Total    if(_year<1970|| _year>2099)return 1;//valid Year//Calculate Year     for(i=1970; i<_year;i++)    {        if(Is_leap_year (i)) Seccount=seccount+3600* -*366;//3600*24*366=31622400        ElseSeccount=seccount+3600* -*365;//3600*24*365=31536000    }    //Calculate Month_month=_month-1;  for(i=0; i<_month;i++) {Seccount=seccount+ (U32) month_table[i]*3600* -; if(Is_leap_year (_year) && i==1)//February,add 1 Day{Seccount=seccount+3600* -; }    }        //Calculate Dayseccount=seccount+ (u32) (_day-1)*3600* -; Seccount+ = (u32) _hour*3600; Seccount+ = (u32) _min* -; Seccount+=_second; //Set CalendarRcc_apb1periphclockcmd (rcc_apb1periph_pwr|rcc_apb1periph_bkp,enable);    Pwr_backupaccesscmd (ENABLE);    Rtc_setcounter (Seccount);        Rtc_waitforlasttask (); return 0;}    U8 Rtc_getweek (U16 _year,u8 _month,u8 _day) {U16 temp;    U8 Yearh,yearl; Yearh=_year/ -;//2016/100=20yearl=_year% -;//2016%100=16        if(yearh> +) yearl+= -; Temp=yearl+yearl/4; Temp=temp+_day+week_table[_month-1]; if(yearl%4==0&& _month<3) Temp=temp-1; returntemp%7;}

RTC Configuration and initialization of STM32-rtc.h RTC.C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.