当前位置: 首页 > news >正文

HAL库源码移植与使用之RTC时钟

实时时钟(Real Time Clock,RTC),本质是一个计数器,计数频率常为秒,专门用来记录时间。

普通定时器无法掉电运行!但RTC可由VBAT备用电源供电,断电不断时

这里讲F1系列的RTC

可以产生三个中断信号,秒  闹钟  溢出信号

其中闹钟可以唤醒wwdg和iwdg

后备寄存器:

后备寄存器不仅有存数据的地方还有存状态,存配置rtc等后备外设的配置寄存器的存在,这些寄存器全部都复位不重置,所以可以在有备用电池的状态下做点文章

因为rcc内有状态位在不断电情况下可以知道上次复位是因为什么造成的

标准库还有用后备寄存器存数据的  temper引脚防破解  校准时钟输出等实验,正点原子hal教学没有,得自己探索

你选时钟源的时候如果32.768khz坏了,也可以选择rc振荡器40khz和高速内部rc振荡器但不推荐,因为他俩很容易受外部影响

你可以获取分频器的分频计数值来更加精确的处理得出时间 

函数解析:

下面的函数是正点原子自己写的,专门用于处理时间戳的函数,你也可以自己用time.h跟标准库那时候一样

rtc.c

#include "./BSP/RTC/rtc.h"
#include "./BSP/LED/led.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"RTC_HandleTypeDef g_rtc_handle; /* RTC控制句柄 */
_calendar_obj calendar;         /* 时间结构体 *//*** @brief       RTC写入后备区域SRAM* @param       bkrx : 后备区寄存器编号,范围:0~41对应 RTC_BKP_DR1~RTC_BKP_DR42* @param       data : 要写入的数据,16位长度* @retval      无*/
void rtc_write_bkr(uint32_t bkrx, uint16_t data)
{HAL_PWR_EnableBkUpAccess(); /* 取消备份区写保护 */HAL_RTCEx_BKUPWrite(&g_rtc_handle, bkrx + 1, data);
}/*** @brief       RTC读取后备区域SRAM* @param       bkrx : 后备区寄存器编号,范围:0~41对应 RTC_BKP_DR1~RTC_BKP_DR42* @retval      读取到的值*/
uint16_t rtc_read_bkr(uint32_t bkrx)
{uint32_t temp = 0;temp = HAL_RTCEx_BKUPRead(&g_rtc_handle, bkrx + 1);return (uint16_t)temp; /* 返回读取到的值 */
}/*** @brief       RTC初始化*   @note*              默认尝试使用LSE,当LSE启动失败后,切换为LSI.*              通过BKP寄存器0的值,可以判断RTC使用的是LSE/LSI:*              当BKP0==0X5050时,使用的是LSE*              当BKP0==0X5051时,使用的是LSI*              注意:切换LSI/LSE将导致时间/日期丢失,切换后需重新设置.** @param       无* @retval      0,成功*              1,进入初始化模式失败*/
uint8_t rtc_init(void)
{/* 检查是不是第一次配置时钟 */uint16_t bkpflag = 0;__HAL_RCC_PWR_CLK_ENABLE(); /* 使能PWR电源时钟 */__HAL_RCC_BKP_CLK_ENABLE(); /* 使能BKP备份时钟 */HAL_PWR_EnableBkUpAccess(); /* 取消备份区写保护 */g_rtc_handle.Instance = RTC;g_rtc_handle.Init.AsynchPrediv = 32767;     /* 时钟周期设置,理论值:32767, 这里也可以用 RTC_AUTO_1_SECOND */g_rtc_handle.Init.OutPut = RTC_OUTPUTSOURCE_NONE;if (HAL_RTC_Init(&g_rtc_handle) != HAL_OK)  /* 初始化RTC */{return 1;}bkpflag = rtc_read_bkr(0);  /* 读取BKP0的值 */if ((bkpflag != 0X5050) && (bkpflag != 0x5051))         /* 之前未初始化过,重新配置 */{rtc_set_time(2020, 4, 25, 20, 25, 35);              /* 设置时间 */}__HAL_RTC_ALARM_ENABLE_IT(&g_rtc_handle, RTC_IT_SEC);   /* 允许秒中断 */__HAL_RTC_ALARM_ENABLE_IT(&g_rtc_handle, RTC_IT_ALRA);  /* 允许闹钟中断 */HAL_NVIC_SetPriority(RTC_IRQn, 0x2, 0);                 /* 设置RTC中断 */HAL_NVIC_EnableIRQ(RTC_IRQn);                           /* 使能中断 */rtc_get_time(); /* 更新时间 */return 0;
}/*** @brief       RTC初始化*   @note*              RTC底层驱动,时钟配置,此函数会被HAL_RTC_Init()调用* @param       hrtc:RTC句柄* @retval      无*/
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
{uint16_t retry = 200;__HAL_RCC_RTC_ENABLE();     /* RTC时钟使能 */RCC_OscInitTypeDef rcc_oscinitstruct;RCC_PeriphCLKInitTypeDef rcc_periphclkinitstruct;/* 使用寄存器的方式去检测LSE是否可以正常工作 */RCC->BDCR |= 1 << 0;    /* 开启外部低速振荡器LSE */while (retry && ((RCC->BDCR & 0X02) == 0))  /* 等待LSE准备好 */{retry--;delay_ms(5);}if (retry == 0)     /* LSE起振失败 使用LSI */{rcc_oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;  /* 选择要配置的振荡器 */rcc_oscinitstruct.LSIState = RCC_LSI_ON;                    /* LSI状态:开启 */rcc_oscinitstruct.PLL.PLLState = RCC_PLL_NONE;              /* PLL无配置 */HAL_RCC_OscConfig(&rcc_oscinitstruct);                      /* 配置设置的rcc_oscinitstruct */rcc_periphclkinitstruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;   /* 选择要配置的外设 RTC */rcc_periphclkinitstruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;   /* RTC时钟源选择 LSI */HAL_RCCEx_PeriphCLKConfig(&rcc_periphclkinitstruct);                /* 配置设置的rcc_periphClkInitStruct */rtc_write_bkr(0, 0X5051);}else{rcc_oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_LSE ; /* 选择要配置的振荡器 */rcc_oscinitstruct.LSEState = RCC_LSE_ON;                    /* LSE状态:开启 */rcc_oscinitstruct.PLL.PLLState = RCC_PLL_NONE;              /* PLL不配置 */HAL_RCC_OscConfig(&rcc_oscinitstruct);                      /* 配置设置的rcc_oscinitstruct */rcc_periphclkinitstruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;   /* 选择要配置外设 RTC */rcc_periphclkinitstruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;   /* RTC时钟源选择LSE */HAL_RCCEx_PeriphCLKConfig(&rcc_periphclkinitstruct);                /* 配置设置的rcc_periphclkinitstruct */rtc_write_bkr(0, 0X5055);}
}/*** @brief       RTC时钟中断*   @note      秒钟中断 / 闹钟中断 共用同一个中断服务函数*              根据RTC_CRL寄存器的 SECF 和 ALRF 位区分是哪个中断* @param       无* @retval      无*/
void RTC_IRQHandler(void)
{if (__HAL_RTC_ALARM_GET_FLAG(&g_rtc_handle, RTC_FLAG_SEC) != RESET)     /* 秒中断 */{rtc_get_time();     /* 更新时间 */__HAL_RTC_ALARM_CLEAR_FLAG(&g_rtc_handle, RTC_FLAG_SEC);            /* 清除秒中断 *///printf("sec:%d\r\n", calendar.sec);   /* 打印秒钟 */}if (__HAL_RTC_ALARM_GET_FLAG(&g_rtc_handle, RTC_FLAG_ALRAF) != RESET)   /* 闹钟中断 */{__HAL_RTC_ALARM_CLEAR_FLAG(&g_rtc_handle, RTC_FLAG_ALRAF);          /* 清除闹钟中断 */printf("Alarm Time:%d-%d-%d %d:%d:%d\n", calendar.year, calendar.month, calendar.date, calendar.hour, calendar.min, calendar.sec);}__HAL_RTC_ALARM_CLEAR_FLAG(&g_rtc_handle, RTC_FLAG_OW);                 /* 清除溢出中断标志 */while (!__HAL_RTC_ALARM_GET_FLAG(&g_rtc_handle, RTC_FLAG_RTOFF));       /* 等待RTC寄存器操作完成, 即等待RTOFF == 1 */
}/*** @brief       判断年份是否是闰年*   @note      月份天数表:*              月份   1  2  3  4  5  6  7  8  9  10 11 12*              闰年   31 29 31 30 31 30 31 31 30 31 30 31*              非闰年 31 28 31 30 31 30 31 31 30 31 30 31* @param       year : 年份* @retval      0, 非闰年; 1, 是闰年;*/
static uint8_t rtc_is_leap_year(uint16_t year)
{/* 闰年规则: 四年闰百年不闰,四百年又闰 */if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 1;}else{return 0;}
}/*** @brief       设置时间, 包括年月日时分秒*   @note      以1970年1月1日为基准, 往后累加时间*              合法年份范围为: 1970 ~ 2105年HAL默认为年份起点为2000年* @param       syear : 年份* @param       smon  : 月份* @param       sday  : 日期* @param       hour  : 小时* @param       min   : 分钟* @param       sec   : 秒钟* @retval      0, 成功; 1, 失败;*/
uint8_t rtc_set_time(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec)
{uint32_t seccount = 0;seccount = rtc_date2sec(syear, smon, sday, hour, min, sec); /* 将年月日时分秒转换成总秒钟数 */__HAL_RCC_PWR_CLK_ENABLE(); /* 使能电源时钟 */__HAL_RCC_BKP_CLK_ENABLE(); /* 使能备份域时钟 */HAL_PWR_EnableBkUpAccess(); /* 取消备份域写保护 *//* 上面三步是必须的! */RTC->CRL |= 1 << 4;         /* 进入配置模式 */RTC->CNTL = seccount & 0xffff;RTC->CNTH = seccount >> 16;RTC->CRL &= ~(1 << 4);      /* 退出配置模式 */while (!__HAL_RTC_ALARM_GET_FLAG(&g_rtc_handle, RTC_FLAG_RTOFF));       /* 等待RTC寄存器操作完成, 即等待RTOFF == 1 */return 0;
}/*** @brief       设置闹钟, 具体到年月日时分秒*   @note      以1970年1月1日为基准, 往后累加时间*              合法年份范围为: 1970 ~ 2105年* @param       syear : 年份* @param       smon  : 月份* @param       sday  : 日期* @param       hour  : 小时* @param       min   : 分钟* @param       sec   : 秒钟* @retval      0, 成功; 1, 失败;*/
uint8_t rtc_set_alarm(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec)
{uint32_t seccount = 0;seccount = rtc_date2sec(syear, smon, sday, hour, min, sec); /* 将年月日时分秒转换成总秒钟数 */__HAL_RCC_PWR_CLK_ENABLE(); /* 使能电源时钟 */__HAL_RCC_BKP_CLK_ENABLE(); /* 使能备份域时钟 */HAL_PWR_EnableBkUpAccess(); /* 取消备份域写保护 *//* 上面三步是必须的! */RTC->CRL |= 1 << 4;         /* 进入配置模式 */RTC->ALRL = seccount & 0xffff;RTC->ALRH = seccount >> 16;RTC->CRL &= ~(1 << 4);      /* 退出配置模式 */while (!__HAL_RTC_ALARM_GET_FLAG(&g_rtc_handle, RTC_FLAG_RTOFF));       /* 等待RTC寄存器操作完成, 即等待RTOFF == 1 */return 0;
}/*** @brief       得到当前的时间*   @note      该函数不直接返回时间, 时间数据保存在calendar结构体里面* @param       无* @retval      无*/
void rtc_get_time(void)
{static uint16_t daycnt = 0;uint32_t seccount = 0;uint32_t temp = 0;uint16_t temp1 = 0;const uint8_t month_table[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /* 平年的月份日期表 */seccount = RTC->CNTH; /* 得到计数器中的值(秒钟数) */seccount <<= 16;seccount += RTC->CNTL;temp = seccount / 86400; /* 得到天数(秒钟数对应的) */if (daycnt != temp) /* 超过一天了 */{daycnt = temp;temp1 = 1970;   /* 从1970年开始 */while (temp >= 365){if (rtc_is_leap_year(temp1)) /* 是闰年 */{if (temp >= 366){temp -= 366;    /* 闰年的秒钟数 */}else{break;}}else{temp -= 365;        /* 平年 */}temp1++;}calendar.year = temp1;      /* 得到年份 */temp1 = 0;while (temp >= 28)      /* 超过了一个月 */{if (rtc_is_leap_year(calendar.year) && temp1 == 1) /* 当年是不是闰年/2月份 */{if (temp >= 29){temp -= 29; /* 闰年的秒钟数 */}else{break;}}else{if (temp >= month_table[temp1]){temp -= month_table[temp1]; /* 平年 */}else{break;}}temp1++;}calendar.month = temp1 + 1; /* 得到月份 */calendar.date = temp + 1;   /* 得到日期 */}temp = seccount % 86400;                                                    /* 得到秒钟数 */calendar.hour = temp / 3600;                                                /* 小时 */calendar.min = (temp % 3600) / 60;                                          /* 分钟 */calendar.sec = (temp % 3600) % 60;                                          /* 秒钟 */calendar.week = rtc_get_week(calendar.year, calendar.month, calendar.date); /* 获取星期 */
}/*** @brief       将年月日时分秒转换成秒钟数*   @note      输入公历日期得到星期(起始时间为: 公元0年3月1日开始, 输入往后的任何日期, 都可以获取正确的星期)*              使用 基姆拉尔森计算公式 计算, 原理说明见此贴:*              https://www.cnblogs.com/fengbohello/p/3264300.html* @param       syear : 年份* @param       smon  : 月份* @param       sday  : 日期* @retval      0, 星期天; 1 ~ 6: 星期一 ~ 星期六*/
uint8_t rtc_get_week(uint16_t year, uint8_t month, uint8_t day)
{uint8_t week = 0;if (month < 3){month += 12;--year;}week = (day + 1 + 2 * month + 3 * (month + 1) / 5 + year + (year >> 2) - year / 100 + year / 400) % 7;return week;
}/*** @brief       将年月日时分秒转换成秒钟数*   @note      以1970年1月1日为基准, 1970年1月1日, 0时0分0秒, 表示第0秒钟*              最大表示到2105年, 因为uint32_t最大表示136年的秒钟数(不包括闰年)!*              本代码参考只linux mktime函数, 原理说明见此贴:*              http://www.openedv.com/thread-63389-1-1.html* @param       syear : 年份* @param       smon  : 月份* @param       sday  : 日期* @param       hour  : 小时* @param       min   : 分钟* @param       sec   : 秒钟* @retval      转换后的秒钟数*/
static long rtc_date2sec(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec)
{uint32_t Y, M, D, X, T;signed char monx = smon;    /* 将月份转换成带符号的值, 方便后面运算 */if (0 >= (monx -= 2))       /* 1..12 -> 11,12,1..10 */{monx += 12; /* Puts Feb last since it has leap day */syear -= 1;}Y = (syear - 1) * 365 + syear / 4 - syear / 100 + syear / 400; /* 公元元年1到现在的闰年数 */M = 367 * monx / 12 - 30 + 59;D = sday - 1;X = Y + M + D - 719162;                      /* 减去公元元年到1970年的天数 */T = ((X * 24 + hour) * 60 + min) * 60 + sec; /* 总秒钟数 */return T;
}

rtc.h

#ifndef __RTC_H
#define __RTC_H#include "./SYSTEM/sys/sys.h"/* 时间结构体, 包括年月日周时分秒等信息 */
typedef struct
{uint8_t hour;       /* 时 */uint8_t min;        /* 分 */uint8_t sec;        /* 秒 *//* 公历年月日周 */uint16_t year;      /* 年 */uint8_t  month;     /* 月 */uint8_t  date;      /* 日 */uint8_t  week;      /* 周 */
} _calendar_obj;extern _calendar_obj calendar;                      /* 时间结构体 *//* 静态函数 */
static uint8_t rtc_is_leap_year(uint16_t year);     /* 判断当前年份是不是闰年 */
static long rtc_date2sec(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec);   /* 将年月日时分秒转换成秒钟数 *//* 接口函数 */
uint8_t rtc_init(void);                             /* 初始化RTC */
void rtc_get_time(void);                            /* 获取RTC时间信息 */
uint16_t rtc_read_bkr(uint32_t bkrx);               /* 读取后备寄存器 */
void rtc_write_bkr(uint32_t bkrx, uint16_t data);   /* 写后备寄存器 */ 
uint8_t rtc_get_week(uint16_t year, uint8_t month, uint8_t day);    /* 根据年月日获取星期几 */
uint8_t rtc_set_time(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec);   /* 设置时间 */
uint8_t rtc_set_alarm(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec);  /* 设置闹钟时间 */#endif

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 时间和空间复杂度
  • Docker、containerd、CRI-O 和 runc 之间的区别
  • 第1关 -- Linux 基础知识
  • AV1技术学习:Transform Coding
  • LeetCode:x的平方根(C语言)
  • Android 自定义系统版本号
  • 数学建模(5)——逻辑回归
  • 『大模型笔记』LLM秘密:温度、Top-K和Top-P抽样技术解析!
  • 服务器相关总结
  • 2024 中国大数据交易平台发展现状调研简报
  • Leetcode3208. 交替组 II
  • 逆向案例二十八——某高考志愿网异步请求头参数加密,以及webpack
  • Qwen-Agent
  • 【MQTT(2)】开发一个客户端,ubuntu版本
  • 亚信安全终端一体化解决方案入选应用创新典型案例
  • [译] 怎样写一个基础的编译器
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • C++类的相互关联
  • CentOS从零开始部署Nodejs项目
  • fetch 从初识到应用
  • flutter的key在widget list的作用以及必要性
  • jQuery(一)
  • laravel 用artisan创建自己的模板
  • leetcode讲解--894. All Possible Full Binary Trees
  • maya建模与骨骼动画快速实现人工鱼
  • node学习系列之简单文件上传
  • Spring声明式事务管理之一:五大属性分析
  • Theano - 导数
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • VUE es6技巧写法(持续更新中~~~)
  • Webpack入门之遇到的那些坑,系列示例Demo
  • webpack项目中使用grunt监听文件变动自动打包编译
  • windows下mongoDB的环境配置
  • 百度地图API标注+时间轴组件
  • 大主子表关联的性能优化方法
  • 分享一份非常强势的Android面试题
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #laravel部署安装报错loadFactoriesFrom是undefined method #
  • (2024,LoRA,全量微调,低秩,强正则化,缓解遗忘,多样性)LoRA 学习更少,遗忘更少
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (实战篇)如何缓存数据
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • (转)人的集合论——移山之道
  • (自用)交互协议设计——protobuf序列化
  • .a文件和.so文件
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .NET Core 中的路径问题
  • .net6 当连接用户的shell断掉后,dotnet会自动关闭,达不到长期运行的效果。.NET 进程守护
  • .Net程序帮助文档制作