RTC.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "includes.h"
  2. void RTC_Configuration(void)
  3. {
  4. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP时钟
  5. PWR_BackupAccessCmd(ENABLE); //关闭后备寄存器写保护
  6. //BKP_DeInit(); //将后备寄存器复位
  7. RCC_LSEConfig(RCC_LSE_ON); //打开外部低速晶振
  8. while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //等待外部低速晶振就绪
  9. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //选择外部晶振为RTC的时钟源
  10. RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
  11. RTC_WaitForSynchro(); //等待RTC寄存器与RTC的APB时钟同步
  12. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  13. RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC中断
  14. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  15. RTC_SetPrescaler(32765); //设置RTC的预分频值,由于晶振选择32768,所以计数器频率=RTCCLK/RTC_PR=(32.768KHz/(32767+1)) 36767
  16. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  17. }
  18. /*********************************
  19. void GuiShowTime()
  20. {
  21. int time,h,m,s;
  22. char buf[30];
  23. time = RTC_GetCounter();
  24. h = time/3600;
  25. m = (time%3600)/60;
  26. s = (time%3600)%60;
  27. //printf("Time=[%2d:%02d:%02d]\r\n",h,m,s);
  28. //while(1){
  29. sprintf(buf,"%2d:%2d",h,m);
  30. GuiShowStr(110,0,buf,0);
  31. //}
  32. }
  33. *********************************/
  34. void SetTime(int h,int m,int s)
  35. {
  36. int time;
  37. time = h*3600 + m*60 + s;
  38. RTC_SetCounter(time);
  39. }