RTC.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "includes.h"
  2. #if 1
  3. //如果机子彻底掉过电(如拔掉电池),开机过程,LSE就绪要等1秒
  4. void RTC_ConfigurationSTP1(void)
  5. {
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP时钟
  7. PWR_BackupAccessCmd(ENABLE); //关闭后备寄存器写保护
  8. //BKP_DeInit(); //将后备寄存器复位
  9. //RCC_LSEConfig(RCC_LSE_ON); //打开外部低速晶振
  10. }
  11. void RTC_ConfigurationSTP2(void)
  12. {
  13. //while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //等待外部低速晶振就绪
  14. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //选择外部晶振为RTC的时钟源
  15. RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
  16. RTC_WaitForSynchro(); //等待RTC寄存器与RTC的APB时钟同步
  17. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  18. //RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC中断
  19. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  20. RTC_SetPrescaler(32765); //设置RTC的预分频值,由于晶振选择32768,所以计数器频率=RTCCLK/RTC_PR=(32.768KHz/(32767+1)) 36767
  21. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  22. }
  23. #else
  24. void RTC_Configuration(void)
  25. {
  26. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP时钟
  27. PWR_BackupAccessCmd(ENABLE); //关闭后备寄存器写保护
  28. //BKP_DeInit(); //将后备寄存器复位
  29. RCC_LSEConfig(RCC_LSE_ON); //打开外部低速晶振
  30. while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //等待外部低速晶振就绪
  31. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //选择外部晶振为RTC的时钟源
  32. RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
  33. RTC_WaitForSynchro(); //等待RTC寄存器与RTC的APB时钟同步
  34. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  35. RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC中断
  36. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  37. RTC_SetPrescaler(32765); //设置RTC的预分频值,由于晶振选择32768,所以计数器频率=RTCCLK/RTC_PR=(32.768KHz/(32767+1)) 36767
  38. RTC_WaitForLastTask(); //等待上次对RTC寄存器配置完成
  39. }
  40. #endif
  41. /*********************************
  42. void GuiShowTime()
  43. {
  44. int time,h,m,s;
  45. char buf[30];
  46. time = RTC_GetCounter();
  47. h = time/3600;
  48. m = (time%3600)/60;
  49. s = (time%3600)%60;
  50. //printf("Time=[%2d:%02d:%02d]\r\n",h,m,s);
  51. //while(1){
  52. sprintf(buf,"%2d:%2d",h,m);
  53. GuiShowStr(110,0,buf,0);
  54. //}
  55. }
  56. *********************************/
  57. void SetTime(int h,int m,int s)
  58. {
  59. int time;
  60. time = h*3600 + m*60 + s;
  61. RTC_SetCounter(time);
  62. }
  63. void Delay_Timer_Init(void)
  64. {
  65. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  66. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  67. TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  68. TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  69. TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
  70. TIM_TimeBaseInitStruct.TIM_Period = 100-1;
  71. TIM_TimeBaseInitStruct.TIM_Prescaler = (48-1);
  72. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
  73. while((TIM3->SR & TIM_FLAG_Update)!=SET);
  74. TIM3->SR = (uint16_t)~TIM_FLAG_Update;
  75. }
  76. void Delay_us(int us_cnt)
  77. {
  78. TIM3->CNT = us_cnt-1;
  79. TIM3->CR1 |= TIM_CR1_CEN;
  80. while((TIM3->SR & TIM_FLAG_Update)!=SET);
  81. TIM3->SR = (uint16_t)~TIM_FLAG_Update;
  82. TIM3->CR1 &= ~TIM_CR1_CEN;
  83. }