欢迎光临
我们一直在努力

发现STM32之BKP和RTC

备份寄存器(BKP)在大容量STM32芯片中通常是42个16位寄存器(共84字节),用于存储用户数据。它们位于备份域中,当VDD电源切断时,可由VBAT引脚连接的备用电池维持供电保证数据不丢失。当系统从待机模式唤醒、或发生系统复位/电源复位时,BKP数据不会被清除(除非主动进行备份域复位或VBAT断电)。

• 注意:BKP特殊的一点就是他们是由两个电源控制的(VDD和VBAT),当VDD不工作了还有VBAT给BKP供电。但是,BKP也不是掉电不丢失数据的。当两个电源都没了,那数据可能也就没了。

• 此外, BKP控制寄存器用来管理侵入检测(TAMPWE)和RTC校准功能。

• 作用:

        • 存储RTC的配置参数(如校准值、时钟源选择)。

        •  保存关键数据(如时间戳、闹钟设置),避免复位或掉电后丢失。

• 复位后,对备份寄存器(BKP)和RTC的访问被禁止,并且备份域被保护以防止可能存在的意外的写操作。执行以下操作可以使能对备份寄存器(BKP)和RTC的访问:

• 通过设置寄存器RCC_APB1ENR的PWREN和BKPEN位来打开电源和后备接口的时钟,如图:

• 电源控制寄存器(PWR_CR)的DBP位来使能对后备寄存器(BKP)和RTC的访问,如图:

• 用户数据存储容量:20字节(中容量和小容量)/ 84字节(大容量和互联型)。

2. BKP框图

• 侵入检测的作用:防止别人恶意读取数据,保护数据,如果一旦发生别人读取数据,数据自动销毁。

3. 实操,读写BKP
3.1 在rtc.c
#include "rtc.h"
 
RTC_HandleTypeDef rtc_handle = {0};
void rtc_init(){
 
    __HAL_RCC_PWR_CLK_ENABLE();//打开电源时钟
    __HAL_RCC_BKP_CLK_ENABLE();//打开备份接口时钟
    HAL_PWR_EnableBkUpAccess();//使能允许写入RTC和BKP
    
    rtc_handle.Instance = RTC;
    rtc_handle.Init.AsynchPrediv = 32767;
    rtc_handle.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
    HAL_RTC_Init(&rtc_handle);//rtc和bkp是紧密的,都在一个区域(后备域),并且还共用一个VBAT
}
 
uint16_t rtc_read_bkr(uint8_t bkrx){//读
    uint32_t recv_data = 0;
    recv_data = HAL_RTCEx_BKUPRead(&rtc_handle,bkrx);
    return (uint16_t) recv_data;
}
 
void rtc_write_bkr(uint8_t bkrx,uint16_t data){//写
    HAL_RTCEx_BKUPWrite(&rtc_handle,bkrx,data);
}
AI写代码
cpp
运行

3.2 在main.c
#include "sys.h"
#include "uart1.h"
#include "delay.h"
#include "led.h"
#include "rtc.h"
 
int main(void)
{
    HAL_Init();                         /* 初始化HAL库 */
    stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟, 72Mhz */
    led_init();                         /* LED初始化 */
    uart1_init(115200);
    rtc_init();
    printf("hello world\\r\\n");
    rtc_write_bkr(1,0x11CD);
    printf("读到的数据是:%X\\r\\n",rtc_read_bkr(1));
    while(1)//流水灯实验
    { 
      
    }
}
AI写代码
cpp
运行

4. 什么是RTC
• 实时时钟是一个独立的定时器。 RTC模块拥有一组连续计数的计数器(32位的可编程计数器),在相应软件配置下,可提供时钟日历的功能(F1系列是没有的)。修改计数器的值可以重新设置系统当前的时间和日期。

• RTC模块和时钟配置系统(RCC_BDCR寄存器)处于后备区域,即在系统复位或从待机模式唤醒后, RTC的设置和时间维持不变。

• 复位后,对备份寄存器和RTC的访问被禁止,并且备份域被保护以防止可能存在的意外的写操作。执行以下操作可以使能对备份寄存器和RTC的访问:

        • 通过设置寄存器RCC_APB1ENR的PWREN和BKPEN位来打开电源和后备接口的时钟。

        • 电源控制寄存器(PWR_CR)的DBP位来使能对后备寄存器和RTC的访问。

• 32位的可编程计数器(RTC):保存的是可对应的UNIX时间戳

• 20位的可编程预分频器,可以适配不同速率的输入时钟
https://avg.163.com/topic/detail/10634983
https://avg.163.com/topic/detail/10635023
https://avg.163.com/topic/detail/10635059
https://avg.163.com/topic/detail/10634986
https://avg.163.com/topic/detail/10634989
https://avg.163.com/topic/detail/10634977
https://avg.163.com/topic/detail/10634980
https://avg.163.com/topic/detail/10634981
https://avg.163.com/topic/detail/10635104
https://avg.163.com/topic/detail/10635028
https://avg.163.com/topic/detail/10635029
https://avg.163.com/topic/detail/10634988
https://avg.163.com/topic/detail/10634974
https://avg.163.com/topic/detail/10635026
https://avg.163.com/topic/detail/10635062
https://avg.163.com/topic/detail/10635025
https://avg.163.com/topic/detail/10634984
https://avg.163.com/topic/detail/10635098
https://avg.163.com/topic/detail/10635027
https://avg.163.com/topic/detail/10635022
https://avg.163.com/topic/detail/10635060
https://avg.163.com/topic/detail/10634979
https://avg.163.com/topic/detail/10635132
https://avg.163.com/topic/detail/10635021
https://avg.163.com/topic/detail/10635058
https://avg.163.com/topic/detail/10635100
https://avg.163.com/topic/detail/10635139
https://avg.163.com/topic/detail/10635019
https://avg.163.com/topic/detail/10634982
https://avg.163.com/topic/detail/10635061
https://avg.163.com/topic/detail/10635063
https://avg.163.com/topic/detail/10635056
https://avg.163.com/topic/detail/10635024
https://avg.163.com/topic/detail/10635092
https://avg.163.com/topic/detail/10635064
https://avg.163.com/topic/detail/10635129
https://avg.163.com/topic/detail/10635096
https://avg.163.com/topic/detail/10635133
https://avg.163.com/topic/detail/10635065
https://avg.163.com/topic/detail/10635102
https://avg.163.com/topic/detail/10635142
https://avg.163.com/topic/detail/10635017
https://avg.163.com/topic/detail/10635101
https://avg.163.com/topic/detail/10635094
https://avg.163.com/topic/detail/10635053
https://avg.163.com/topic/detail/10635055
https://avg.163.com/topic/detail/10635138
https://avg.163.com/topic/detail/10635095
https://avg.163.com/topic/detail/10635099
https://avg.163.com/topic/detail/10634975
https://avg.163.com/topic/detail/10635008
https://avg.163.com/topic/detail/10635126
https://avg.163.com/topic/detail/10635049
https://avg.163.com/topic/detail/10634987
https://avg.163.com/topic/detail/10635128
https://avg.163.com/topic/detail/10635136
https://avg.163.com/topic/detail/10635103
https://avg.163.com/topic/detail/10635089
https://avg.163.com/topic/detail/10635140
https://avg.163.com/topic/detail/10635135
https://avg.163.com/topic/detail/10634985
https://avg.163.com/topic/detail/10635018
https://avg.163.com/topic/detail/10635054
https://avg.163.com/topic/detail/10635093
https://avg.163.com/topic/detail/10635143
https://avg.163.com/topic/detail/10635020
https://avg.163.com/topic/detail/10635057
https://avg.163.com/topic/detail/10635097
https://avg.163.com/topic/detail/10634970
https://avg.163.com/topic/detail/10635134
https://avg.163.com/topic/detail/10635131
https://avg.163.com/topic/detail/10634973
https://avg.163.com/topic/detail/10635047
https://avg.163.com/topic/detail/10634978
https://avg.163.com/topic/detail/10635013
https://avg.163.com/topic/detail/10635051
https://avg.163.com/topic/detail/10635091
https://avg.163.com/topic/detail/10635137
https://avg.163.com/topic/detail/10634972
https://avg.163.com/topic/detail/10634964
https://avg.163.com/topic/detail/10635006
https://avg.163.com/topic/detail/10635043
https://avg.163.com/topic/detail/10635087
https://avg.163.com/topic/detail/10635090
https://avg.163.com/topic/detail/10635127
https://avg.163.com/topic/detail/10635010
https://avg.163.com/topic/detail/10635045
https://avg.163.com/topic/detail/10635085
https://avg.163.com/topic/detail/10635123
https://avg.163.com/topic/detail/10635009
https://avg.163.com/topic/detail/10635124
https://avg.163.com/topic/detail/10634962
https://avg.163.com/topic/detail/10635012
https://avg.163.com/topic/detail/10635050
https://avg.163.com/topic/detail/10635086
https://avg.163.com/topic/detail/10635117
https://avg.163.com/topic/detail/10634965
https://avg.163.com/topic/detail/10635003
https://avg.163.com/topic/detail/10635052
https://avg.163.com/topic/detail/10635088
https://avg.163.com/topic/detail/10635125
https://avg.163.com/topic/detail/10635048
https://avg.163.com/topic/detail/10635005
https://avg.163.com/topic/detail/10635046
https://avg.163.com/topic/detail/10635080
https://avg.163.com/topic/detail/10635083
https://avg.163.com/topic/detail/10635115
https://avg.163.com/topic/detail/10635118
https://avg.163.com/topic/detail/10634960
https://avg.163.com/topic/detail/10635001
https://avg.163.com/topic/detail/10635044
https://avg.163.com/topic/detail/10635078
https://avg.163.com/topic/detail/10635122
https://avg.163.com/topic/detail/10634971
https://avg.163.com/topic/detail/10635016
https://avg.163.com/topic/detail/10635042
https://avg.163.com/topic/detail/10635084
https://avg.163.com/topic/detail/10635121
https://avg.163.com/topic/detail/10634969
https://avg.163.com/topic/detail/10635014
https://avg.163.com/topic/detail/10635041
https://avg.163.com/topic/detail/10635082
https://avg.163.com/topic/detail/10635120
https://avg.163.com/topic/detail/10634967
https://avg.163.com/topic/detail/10635011
https://avg.163.com/topic/detail/10635040
https://avg.163.com/topic/detail/10635079
https://avg.163.com/topic/detail/10635116
https://avg.163.com/topic/detail/10634966
https://avg.163.com/topic/detail/10635004
https://avg.163.com/topic/detail/10635039
https://avg.163.com/topic/detail/10635077
https://avg.163.com/topic/detail/10635113
https://avg.163.com/topic/detail/10634961
https://avg.163.com/topic/detail/10635002
https://avg.163.com/topic/detail/10635036
https://avg.163.com/topic/detail/10634963
https://avg.163.com/topic/detail/10635007
https://avg.163.com/topic/detail/10635038
https://avg.163.com/topic/detail/10635076
https://avg.163.com/topic/detail/10635119
https://avg.163.com/topic/detail/10635075
https://avg.163.com/topic/detail/10635114
https://avg.163.com/topic/detail/10634959
https://avg.163.com/topic/detail/10634999
https://avg.163.com/topic/detail/10635035
https://avg.163.com/topic/detail/10635074
https://avg.163.com/topic/detail/10635111
https://avg.163.com/topic/detail/10634958
https://avg.163.com/topic/detail/10635000
https://avg.163.com/topic/detail/10635037
https://avg.163.com/topic/detail/10635072
https://avg.163.com/topic/detail/10635109
https://avg.163.com/topic/detail/10634956
https://avg.163.com/topic/detail/10634957
https://avg.163.com/topic/detail/10634998
https://avg.163.com/topic/detail/10634996
https://avg.163.com/topic/detail/10635033
https://avg.163.com/topic/detail/10635032
https://avg.163.com/topic/detail/10635112
https://avg.163.com/topic/detail/10635071
https://avg.163.com/topic/detail/10635110
https://avg.163.com/topic/detail/10634955
https://avg.163.com/topic/detail/10634997
https://avg.163.com/topic/detail/10635034
https://avg.163.com/topic/detail/10635073
https://avg.163.com/topic/detail/10635108
 

赞(0)
未经允许不得转载:171主机测评 » 发现STM32之BKP和RTC
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址