RTC

可由 Vbat 供电 
不会被复位键复位

日历 无法断电保存
rtc.h 自己添加
MX_RTC_Init();需要自己添加

RTC.c 中的MX_RTC_Init(void)函数有:
1
2
3
4
5
6
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1)!=0x5050)    //判断是否时第一次上电 (默认是0xffff) 如果不是0x5050 则是第一次 就给初始化数据 否则就继承之前的
{
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x5050);

最后在后面加个}

存放数据 先定义一个RTC_DateTypeDef rtcTime ;
然后调用
HAL_RTC_GetTime(&hrtc, &rtcTime, RTC_FORMAT_BIN);
读取数据
printf("%02d:%02d:%02d\r\n",GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
1
2
3
4
5
HAL_PWR_EnableBkUpAccess();         //取消备份区写保护

__HAL_RCC_BKP_CLK_ENABLE(); //使能电源始终pwr

__HAL_RCC_RTC_ENABLE();
需要先读出 时间 再读出日期

BKP

10个16位寄存器
可保存20个字节用户应用数据

后备寄存器 可由 Vbat 供电 
不会被复位键复位

HAL_RTCEx_BKUPRead()

1
2
3
4
5
6
7
  HAL_RTCEx_BKUPWrite
* @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
* the configuration information for RTC.
* @param BackupRegister: RTC Backup data Register number.
* This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
* specify the register (depending devices).
* @retval Read value

HAL_RTCEx_BKUPWrite

1
2
3
4
5
6
7
8
  HAL_RTCEx_BKUPWrite
* @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
* the configuration information for RTC.
* @param BackupRegister: RTC Backup data Register number.
* This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
* specify the register (depending devices).
* @param Data: Data to be written in the specified RTC Backup data register.
* @retval None
例子:
    HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1)!=0x5050) 

    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x5050);