#include "includes.h" #if 1 //如果机子彻底掉过电(如拔掉电池),开机过程,LSE就绪要等1秒 void RTC_ConfigurationSTP1(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP时钟 PWR_BackupAccessCmd(ENABLE); //关闭后备寄存器写保护 //BKP_DeInit(); //将后备寄存器复位 //RCC_LSEConfig(RCC_LSE_ON); //打开外部低速晶振 } void RTC_ConfigurationSTP2(void) { //while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //等待外部低速晶振就绪 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //选择外部晶振为RTC的时钟源 RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 RTC_WaitForSynchro(); //等待RTC寄存器与RTC的APB时钟同步 RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成 //RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC中断 RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成 RTC_SetPrescaler(32765); //设置RTC的预分频值,由于晶振选择32768,所以计数器频率=RTCCLK/RTC_PR=(32.768KHz/(32767+1)) 36767 RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成 } #else void RTC_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP时钟 PWR_BackupAccessCmd(ENABLE); //关闭后备寄存器写保护 //BKP_DeInit(); //将后备寄存器复位 RCC_LSEConfig(RCC_LSE_ON); //打开外部低速晶振 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //等待外部低速晶振就绪 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //选择外部晶振为RTC的时钟源 RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 RTC_WaitForSynchro(); //等待RTC寄存器与RTC的APB时钟同步 RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成 RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC中断 RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成 RTC_SetPrescaler(32765); //设置RTC的预分频值,由于晶振选择32768,所以计数器频率=RTCCLK/RTC_PR=(32.768KHz/(32767+1)) 36767 RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成 } #endif /********************************* void GuiShowTime() { int time,h,m,s; char buf[30]; time = RTC_GetCounter(); h = time/3600; m = (time%3600)/60; s = (time%3600)%60; //printf("Time=[%2d:%02d:%02d]\r\n",h,m,s); //while(1){ sprintf(buf,"%2d:%2d",h,m); GuiShowStr(110,0,buf,0); //} } *********************************/ void SetTime(int h,int m,int s) { int time; time = h*3600 + m*60 + s; RTC_SetCounter(time); } void Delay_Timer_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct); TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down; TIM_TimeBaseInitStruct.TIM_Period = 100-1; TIM_TimeBaseInitStruct.TIM_Prescaler = (48-1); TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct); while((TIM3->SR & TIM_FLAG_Update)!=SET); TIM3->SR = (uint16_t)~TIM_FLAG_Update; } void Delay_us(int us_cnt) { TIM3->CNT = us_cnt-1; TIM3->CR1 |= TIM_CR1_CEN; while((TIM3->SR & TIM_FLAG_Update)!=SET); TIM3->SR = (uint16_t)~TIM_FLAG_Update; TIM3->CR1 &= ~TIM_CR1_CEN; }