rtc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "delay.h"
  2. #include "usart.h"
  3. #include "led.h"
  4. #include "rtc.h"
  5. #include "sys.h"
  6. //////////////////////////////////////////////////////////////////////////////////
  7. //本程序只供内部使用,未经作者许可,不得用于其它任何用途
  8. //STM32开发板V3
  9. //RTC驱动代码
  10. //修改日期:2017/5/27
  11. //版本:V1.0
  12. //版权所有,盗版必究。
  13. //Copyright(C) 济南鲁泰电气有限公司 2009-2019
  14. //All rights reserved
  15. //////////////////////////////////////////////////////////////////////////////////
  16. RTC_HandleTypeDef RTC_Handler; //RTC句柄
  17. _calendar_obj calendar;//时钟结构体
  18. //实时时钟配置
  19. //初始化RTC时钟,同时检测时钟是否工作正常
  20. //BKP->DR1用于保存是否第一次配置的设置
  21. //返回0:正常
  22. //其他:错误代码
  23. u8 RTC_Init(void)
  24. {
  25. RTC_Handler.Instance=RTC;
  26. RTC_Handler.Init.AsynchPrediv=32767; //时钟周期设置(有待观察,看是否跑慢了?)理论值:32767
  27. if(HAL_RTC_Init(&RTC_Handler)!=HAL_OK) return 1;
  28. if(HAL_RTCEx_BKUPRead(&RTC_Handler,RTC_BKP_DR1)!=0X5050)//是否第一次配置
  29. {
  30. RTC_Set(2017,5,27,17,7,0); //设置日期和时间,2017年5月27日,17点02分0秒
  31. HAL_RTCEx_BKUPWrite(&RTC_Handler,RTC_BKP_DR1,0X5050);//标记已经初始化过了
  32. printf("FIRST TIME\n");
  33. }
  34. __HAL_RTC_ALARM_ENABLE_IT(&RTC_Handler,RTC_IT_SEC); //允许秒中断
  35. __HAL_RTC_ALARM_ENABLE_IT(&RTC_Handler,RTC_IT_ALRA); //允许闹钟中断
  36. HAL_NVIC_SetPriority(RTC_IRQn,0x01,0x02); //抢占优先级1,子优先级2
  37. HAL_NVIC_EnableIRQ(RTC_IRQn);
  38. RTC_Get();//更新时间
  39. return 0; //ok
  40. }
  41. //RTC底层驱动,时钟配置
  42. //此函数会被HAL_RTC_Init()调用
  43. //hrtc:RTC句柄
  44. void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  45. {
  46. RCC_OscInitTypeDef RCC_OscInitStruct;
  47. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  48. __HAL_RCC_PWR_CLK_ENABLE(); //使能电源时钟PWR
  49. HAL_PWR_EnableBkUpAccess(); //取消备份区域写保护
  50. __HAL_RCC_BKP_CLK_ENABLE(); //使能BSP时钟
  51. RCC_OscInitStruct.OscillatorType=RCC_OSCILLATORTYPE_LSE;//LSE配置
  52. RCC_OscInitStruct.PLL.PLLState=RCC_PLL_NONE;
  53. RCC_OscInitStruct.LSEState=RCC_LSE_ON; //RTC使用LSE
  54. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  55. PeriphClkInitStruct.PeriphClockSelection=RCC_PERIPHCLK_RTC;//外设为RTC
  56. PeriphClkInitStruct.RTCClockSelection=RCC_RTCCLKSOURCE_LSE;//RTC时钟源为LSE
  57. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  58. __HAL_RCC_RTC_ENABLE();//RTC时钟使能
  59. }
  60. //RTC时钟中断
  61. //每秒触发一次
  62. void RTC_IRQHandler(void)
  63. {
  64. if(__HAL_RTC_ALARM_GET_FLAG(&RTC_Handler,RTC_FLAG_SEC)!=RESET) //秒中断
  65. {
  66. __HAL_RTC_ALARM_CLEAR_FLAG(&RTC_Handler,RTC_FLAG_SEC); //清除秒中断
  67. RTC_Get(); //更新时间
  68. // LED1=!LED1; //LED1翻转
  69. }
  70. if(__HAL_RTC_ALARM_GET_FLAG(&RTC_Handler,RTC_FLAG_SEC)!=RESET) //闹钟中断
  71. {
  72. __HAL_RTC_ALARM_CLEAR_FLAG(&RTC_Handler,RTC_FLAG_ALRAF); //清除闹钟中断
  73. RTC_Get(); //更新时间
  74. printf("ALARM A!\r\n");
  75. }
  76. __HAL_RTC_ALARM_CLEAR_FLAG(&RTC_Handler,RTC_FLAG_OW); //清除溢出
  77. }
  78. //判断是否是闰年函数
  79. //月份 1 2 3 4 5 6 7 8 9 10 11 12
  80. //闰年 31 29 31 30 31 30 31 31 30 31 30 31
  81. //非闰年 31 28 31 30 31 30 31 31 30 31 30 31
  82. //year:年份
  83. //返回值:该年份是不是闰年.1,是.0,不是
  84. u8 Is_Leap_Year(u16 year)
  85. {
  86. if(year%4==0) //必须能被4整除
  87. {
  88. if(year%100==0)
  89. {
  90. if(year%400==0)return 1;//如果以00结尾,还要能被400整除
  91. else return 0;
  92. }else return 1;
  93. }else return 0;
  94. }
  95. //设置时钟
  96. //把输入的时钟转换为秒钟
  97. //以1970年1月1日为基准
  98. //1970~2099年为合法年份
  99. //返回值:0,成功;其他:错误代码.
  100. //月份数据表
  101. u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表
  102. //平年的月份日期表
  103. const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  104. //syear,smon,sday,hour,min,sec:年月日时分秒
  105. //返回值:设置结果。0,成功;1,失败。
  106. u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
  107. {
  108. u16 t;
  109. u32 seccount=0;
  110. // RTC_DateTypeDef RTC_DateStructure;
  111. // RTC_TimeTypeDef RTC_TimeStructure;
  112. if(syear<1970||syear>2099)return 1;
  113. for(t=1970;t<syear;t++) //把所有年份的秒钟相加
  114. {
  115. if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数
  116. else seccount+=31536000; //平年的秒钟数
  117. }
  118. smon-=1;
  119. for(t=0;t<smon;t++) //把前面月份的秒钟数相加
  120. {
  121. seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加
  122. if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数
  123. }
  124. seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加
  125. seccount+=(u32)hour*3600;//小时秒钟数
  126. seccount+=(u32)min*60; //分钟秒钟数
  127. seccount+=sec;//最后的秒钟加上去
  128. // RTC_DateStructure.Year=syear;
  129. // RTC_DateStructure.Month=smon;
  130. // RTC_DateStructure.Date=sday;
  131. // HAL_RTC_SetDate(&RTC_Handler,&RTC_DateStructure,RTC_FORMAT_BIN);
  132. //
  133. // RTC_TimeStructure.Hours=hour;
  134. // RTC_TimeStructure.Minutes=min;
  135. // RTC_TimeStructure.Seconds=sec;
  136. // HAL_RTC_SetTime(&RTC_Handler,&RTC_TimeStructure,RTC_FORMAT_BIN);
  137. //设置时钟
  138. RCC->APB1ENR|=1<<28;//使能电源时钟
  139. RCC->APB1ENR|=1<<27;//使能备份时钟
  140. PWR->CR|=1<<8; //取消备份区写保护
  141. //上面三步是必须的!
  142. RTC->CRL|=1<<4; //允许配置
  143. RTC->CNTL=seccount&0xffff;
  144. RTC->CNTH=seccount>>16;
  145. RTC->CRL&=~(1<<4);//配置更新
  146. while(!(RTC->CRL&(1<<5)));//等待RTC寄存器操作完成
  147. RTC_Get();//设置完之后更新一下数据
  148. return 0;
  149. }
  150. //初始化闹钟
  151. //以1970年1月1日为基准
  152. //1970~2099年为合法年份
  153. //syear,smon,sday,hour,min,sec:闹钟的年月日时分秒
  154. //返回值:0,成功;其他:错误代码.
  155. u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
  156. {
  157. u16 t;
  158. u32 seccount=0;
  159. if(syear<1970||syear>2099)return 1;
  160. for(t=1970;t<syear;t++) //把所有年份的秒钟相加
  161. {
  162. if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数
  163. else seccount+=31536000; //平年的秒钟数
  164. }
  165. smon-=1;
  166. for(t=0;t<smon;t++) //把前面月份的秒钟数相加
  167. {
  168. seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加
  169. if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数
  170. }
  171. seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加
  172. seccount+=(u32)hour*3600;//小时秒钟数
  173. seccount+=(u32)min*60; //分钟秒钟数
  174. seccount+=sec;//最后的秒钟加上去
  175. //设置时钟
  176. RCC->APB1ENR|=1<<28;//使能电源时钟
  177. RCC->APB1ENR|=1<<27;//使能备份时钟
  178. PWR->CR|=1<<8; //取消备份区写保护
  179. //上面三步是必须的!
  180. RTC->CRL|=1<<4; //允许配置
  181. RTC->ALRL=seccount&0xffff;
  182. RTC->ALRH=seccount>>16;
  183. RTC->CRL&=~(1<<4);//配置更新
  184. while(!(RTC->CRL&(1<<5)));//等待RTC寄存器操作完成
  185. return 0;
  186. }
  187. //得到当前的时间,结果保存在calendar结构体里面
  188. //返回值:0,成功;其他:错误代码.
  189. u8 RTC_Get(void)
  190. {
  191. static u16 daycnt=0;
  192. u32 timecount=0;
  193. u32 temp=0;
  194. u16 temp1=0;
  195. timecount=RTC->CNTH;//得到计数器中的值(秒钟数)
  196. timecount<<=16;
  197. timecount+=RTC->CNTL;
  198. temp=timecount/86400; //得到天数(秒钟数对应的)
  199. if(daycnt!=temp)//超过一天了
  200. {
  201. daycnt=temp;
  202. temp1=1970; //从1970年开始
  203. while(temp>=365)
  204. {
  205. if(Is_Leap_Year(temp1))//是闰年
  206. {
  207. if(temp>=366)temp-=366;//闰年的秒钟数
  208. else break;
  209. }
  210. else temp-=365; //平年
  211. temp1++;
  212. }
  213. calendar.w_year=temp1;//得到年份
  214. temp1=0;
  215. while(temp>=28)//超过了一个月
  216. {
  217. if(Is_Leap_Year(calendar.w_year)&&temp1==1)//当年是不是闰年/2月份
  218. {
  219. if(temp>=29)temp-=29;//闰年的秒钟数
  220. else break;
  221. }
  222. else
  223. {
  224. if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年
  225. else break;
  226. }
  227. temp1++;
  228. }
  229. calendar.w_month=temp1+1; //得到月份
  230. calendar.w_date=temp+1; //得到日期
  231. }
  232. temp=timecount%86400; //得到秒钟数
  233. calendar.hour=temp/3600; //小时
  234. calendar.min=(temp%3600)/60; //分钟
  235. calendar.sec=(temp%3600)%60; //秒钟
  236. calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//获取星期
  237. return 0;
  238. }
  239. //获得现在是星期几
  240. //功能描述:输入公历日期得到星期(只允许1901-2099年)
  241. //year,month,day:公历年月日
  242. //返回值:星期号
  243. u8 RTC_Get_Week(u16 year,u8 month,u8 day)
  244. {
  245. u16 temp2;
  246. u8 yearH,yearL;
  247. yearH=year/100; yearL=year%100;
  248. // 如果为21世纪,年份数加100
  249. if (yearH>19)yearL+=100;
  250. // 所过闰年数只算1900年之后的
  251. temp2=yearL+yearL/4;
  252. temp2=temp2%7;
  253. temp2=temp2+day+table_week[month-1];
  254. if (yearL%4==0&&month<3)temp2--;
  255. return(temp2%7);
  256. }