您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > STM32-实时时钟和闹钟程序
#includeled.h#includedelay.h#includesys.h#includelcd.h#includeusart.h#includertc.hintmain(void){u8t=0;delay_init();//NVIC_Configuration();//NVIC2:22uart_init(9600);//9600LED_Init();//LEDLCD_Init();BEEP_Init();//RTC_Init();//RTCPOINT_COLOR=GREEN;//LCD_ShowString(70,50,200,16,16,^_^andy^_^);POINT_COLOR=BLUE;//LCD_ShowString(60,130,200,16,16,--);LCD_ShowString(60,162,200,16,16,::);while(1){if(t!=calendar.sec){t=calendar.sec;LCD_ShowNum(60,130,calendar.w_year,4,16);LCD_ShowNum(100,130,calendar.w_month,2,16);LCD_ShowNum(124,130,calendar.w_date,2,16);switch(calendar.week){case0:LCD_ShowString(60,148,200,16,16,Sunday);break;case1:LCD_ShowString(60,148,200,16,16,Monday);break;case2:LCD_ShowString(60,148,200,16,16,Tuesday);break;case3:LCD_ShowString(60,148,200,16,16,Wednesday);break;case4:LCD_ShowString(60,148,200,16,16,Thursday);break;case5:LCD_ShowString(60,148,200,16,16,Friday);break;case6:LCD_ShowString(60,148,200,16,16,Saturday);break;}LCD_ShowNum(60,162,calendar.hour,2,16);LCD_ShowNum(84,162,calendar.min,2,16);LCD_ShowNum(108,162,calendar.sec,2,16);LED0=!LED0;}delay_ms(10);}}#includesys.h#includedelay.h#includeusart.h#includertc.h_calendar_objcalendar;//staticvoidRTC_NVIC_Config(void){NVIC_InitTypeDefNVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel=RTC_IRQn;//RTCNVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//1,3NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//0,4NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//NVIC_Init(&NVIC_InitStructure);//NVIC_InitStructNVIC}u8RTC_Init(void){//u8temp=0;u32flag=0;if(BKP_ReadBackupRegister(BKP_DR1)!=0x5050)//:{RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);//PWRBKPPWR_BackupAccessCmd(ENABLE);//BKP_DeInit();//RCC_LSEConfig(RCC_LSE_ON);//(LSE),while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET)//RCC,{temp++;delay_ms(10);}if(temp=250)return1;//,RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//RTC(RTCCLK),LSERTCRCC_RTCCLKCmd(ENABLE);//RTCRTC_WaitForLastTask();//RTCRTC_WaitForSynchro();//RTCRTC_ITConfig(RTC_IT_SEC,ENABLE);//RTCRTC_ITConfig(RTC_IT_ALR,ENABLE);RTC_WaitForLastTask();//RTCRTC_EnterConfigMode();///RTC_SetPrescaler(32767);//RTCRTC_WaitForLastTask();//RTCRTC_Set(2014,12,2,10,0,55);//RTC_ExitConfigMode();//flag=RTC_GetCounter();RTC_WaitForLastTask();RTC_SetAlarm(flag+1);//BKP_WriteBackupRegister(BKP_DR1,0X5050);//}else//{RTC_WaitForSynchro();//RTCRTC_ITConfig(RTC_IT_SEC,ENABLE);//RTCRTC_ITConfig(RTC_IT_ALR,ENABLE);RTC_WaitForLastTask();//RTC}RTC_NVIC_Config();//RCTRTC_Get();//return0;//ok}voidRTC_IRQHandler(void){u32flag=0;if(RTC_GetITStatus(RTC_IT_SEC)!=RESET)//{RTC_Get();//}if(RTC_GetITStatus(RTC_IT_ALR)!=RESET)//{RTC_ClearITPendingBit(RTC_IT_ALR);//LED1=!LED1;//ledflag=RTC_GetCounter();RTC_WaitForLastTask();RTC_SetAlarm(flag+1);//}RTC_ClearITPendingBit(RTC_IT_SEC|RTC_IT_OW);//RTC_WaitForLastTask();}////123456789101112//312931303130313130313031//312831303130313130313031//://:.1,.0,u8Is_Leap_Year(u16year){if(year%4==0)//4{if(year%100==0){if(year%400==0)return1;//00,400elsereturn0;}elsereturn1;}elsereturn0;}//////197011//1970~2099//:0,;:.//u8consttable_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};////constu8mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};u8RTC_Set(u16syear,u8smon,u8sday,u8hour,u8min,u8sec){u16t;u32seccount=0;if(syear1970||syear2099)return1;for(t=1970;tsyear;t++)//{if(Is_Leap_Year(t))seccount+=31622400;//elseseccount+=31536000;//}smon-=1;for(t=0;tsmon;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);//PWRBKPPWR_BackupAccessCmd(ENABLE);//RTCRTC_SetCounter(seccount);//RTCRTC_WaitForLastTask();//RTCreturn0;}////:0,;:.u8RTC_Get(void){staticu16daycnt=0;u32timecount=0;u32temp=0;u16temp1=0;timecount=RTC_GetCounter();temp=timecount/86400;//()if(daycnt!=temp)//{daycnt=temp;temp1=1970;//1970while(temp=365){if(Is_Leap_Year(temp1))//{if(temp=366)temp-=366;//else{temp1++;break;}}elsetemp-=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;//elsebreak;}else{if(temp=mon_table[temp1])temp-=mon_table[temp1];//elsebreak;}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);//return0;}////:(1901-2099)////u8RTC_Get_Week(u16year,u8month,u8day){u16temp2;u8yearH,yearL;yearH=year/100;yearL=year%100;//21,100if(yearH19)yearL+=100;//1900temp2=yearL+yearL/4;temp2=temp2%7;temp2=temp2+day+table_week[month-1];if(yearL%4==0&&month3)temp2--;return(temp2%7);}#includeled.hvoidLED_Init(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);//PB,PEGPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED0--PB.5GPIO_InitStructure.GPIO_Mod
本文标题:STM32-实时时钟和闹钟程序
链接地址:https://www.777doc.com/doc-6438959 .html