In the past few days, I have learned about the STM32's BKP, RTC, and PWR, and my study of microcontrollers is basically coming to an end; the only things left are a watchdog and flash.
Now, let's start today's study notes.
(1) PWR#
We will first discuss PWR, as the subsequent BKP and RTC require the PWR clock to be enabled to call some PWR functions.
What is PWR?
PWR stands for Power Control, which is responsible for managing the power supply part of the STM32. It can implement functions such as programmable voltage monitoring and low-power modes. The Programmable Voltage Detector (PVD) can monitor the VDD power voltage, and when VDD drops below or rises above the PVD threshold, the PVD will trigger an interrupt to perform emergency shutdown tasks. (I haven't used the PVD function yet.)
Low-power modes include Sleep, Stop, and Standby modes, which can reduce the power consumption of the STM32 when the system is idle, extending the device's usage time.
The configuration process is as follows:
The PWR functions in the standard library are quite complete, and operating them is incredibly simple.
You just need to start the PWR clock first (the sleep mode doesn't even require starting the clock):
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
Then we call the following functions to enter the corresponding modes:
Sleep mode: __WFI();
Stop mode: PWR_EnterSTOPMode();
Standby mode: PWR_EnterSTANDBYMode();
It is important to note that after entering low-power mode, you cannot directly program the device; you need to hold down the reset button while programming and release it in time.
(2) BKP#
The video from Jiang University mentioned TAMPER, but we haven't used it in our code, so we won't discuss it here. We only need to write the BKP storage and ensure it doesn't get lost during power-off.
Here is the structure of BKP:
VBAT is the backup power pin, and the reason BKP does not lose data during power-off is because of this pin's power supply.
The code is as follows:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
Enable the clock
PWR_BackupAccessCmd(ENABLE);
Enable backup power
BKP_WriteBackupRegister();
Write
BKP_ReadBackupRegister();
Read
(3) RTC#
To use RTC, we first need to understand Unix timestamps and learn some functions from <time.h>.
After understanding these, we can look at RTC.
The structure of RTC is as follows:
Here, pay attention to the clock source; generally, the LSE external low-speed clock is selected, with a 32.768kHz crystal oscillator. However, this may not oscillate (which can happen often), so I used LSI with a crystal frequency of 40kHz during configuration.
Also, there are a few points to note regarding RTC clock configuration:
Below is the configuration code, which also completes the timestamp conversion:
So, this is all about BKP, RTC, and PWR. It's getting easier to write (the more I write, the lazier I get, sadly).