资源描述:
《stm32的rtc实现日历功能程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、STM32的RTC实现日历功能程序STM32RTCisa32bitcounterusedfortiming,didnotregistertosavethedatewhensuch.Youcansetthiscounterby1bysetting,andyoucanprobablytake136yearsfromO-OXFFFFFFFF.Theprogramshouldsetatimestartingpointrepresenting0,andthegeneralsettingstartsat19
2、70-01-010:0:0.ThisistheUNIXtimestamp・IfyouwanttosettheRTCtime,suchas2011-9-3012:00:00,thencalculatehowmanyseconds2011-9-3012:00:00to1970-01-010:0:0,andwritethisvaluetotheRTCcounter・Ifyouwanttoreadthecurrentdatewhen,firstreadthe32bitRTCcountervalue,an
3、dthento1970-01-010:0:0asthetimepoint,calculatethecurrenttime・Whysetthestartingtimeto1970-01-010:0:0,becausethereisaTIME・Hfile,therearewrittensettings,timefunctions,readtimefunction,theuseofparametersisthe32bitcounter・ButIdidn,tfindthefile・Ifthefuncti
4、onofthisfileisnotused,thestartingtimecanbesettoanyvalue.Theadvantageofusing32bitcounterstocalculatetimedirectly:1,youcannotuseRTCsecondsinterrupt,becausewhentheSTM32powerdown,onlythebatterypower,whentheRTCcount,buttheSTM32kernelinterruptshouldnotbeus
5、ed・2,notinthebackupareatosavedays,daysandotherparameters・Hereistheonlinesearchprogram,whichIhaveactuallytestedandiscorrect・//timestructureTypedefstruct{U8hour;U8min;U8sec;TheGregoriancalendaryear/weekU16w_year;U8wjnonth;U8w_date:U8week;}tm;TMtimer;*/
6、VoidRTC_Config(void){U16ul6_WaitFor0scSource;InBKP//wereservearegister1,saveaspecialcharacterof0xA5A5Thefirsttime/powerorbackuppowerafterpoweroff,thelossofdataregister,ShowRTC//dataloss,needtoreconfigureIf(BKP_ReadBackupRegister(BKP_DR1)二二0x5A5A){//r
7、econfigurationRTCEnablePWRandBKPclocks*//*RCC_APBIPeriphClockCmd(RCC_APBlPeriph_PWRRCC_APBlPeriph_BKP,ENABLE);AllowaccesstoBKPDomain*//*PWRBackupAccessCmd(ENABLE);ResetBackupDomain/**/BKP_DeInit();EnableLSE/**/RCC_LSEConfig(RCC_LSE_ON);For(ul6_WaitFo
8、r0scSource=0;ul6_WaitFor0scSource<5000;ul6_WaitFor0scSource++){}WaittillLSEisready*//*而(rcc_getflagstatus(rcc_flag_lserdy)二二重置);/*选择LSE作为RTC吋钟源*rcc_rtcclkconfig(rcc_rtcclksource_lse);/*启用RTC时钟*rcc_rtcclkcmd(使);*等待RTC寄存器同步*rtc_waitforsynchro();*直到RTC寄