rtc.c.bak 7.9 KB

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