1. 研究STM3的备份寄存器,注意,如果要测试这个例程的话,VBAT不能和VDD接一起,必须分开。
2. 理解,备份寄存器可以有VBAT独立供电,也就是外接电池,备份寄存器在VBAT供电情况下,如果发生系统复位(按键复位)和上电复位,备份寄存器的值可以保持。
3. 代码如下:
int main(void)
{
#ifdef DEBUG
debug();
#endif /* System Clocks Configuration */
RCC_Configuration(); /* NVIC configuration */
NVIC_Configuration(); /* GPIO configuration */
GPIO_Configuration(); /* Enable PWR and BKP clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Enable write access to Backup domain */
PWR_BackupAccessCmd(ENABLE); /* Clear Tamper pin Event(TE) pending flag */
BKP_ClearFlag(); //首先判断是不是上电复位的
if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
/* Clear reset flags */
RCC_ClearFlag(); /* Turn on led connected to GPIO_LED Pin8 */
GPIO_SetBits(GPIO_LED, GPIO_Pin_8); /* Check if Backup data registers are programmed */
if(CheckBackupReg(0x3211) == 0x00)
{ /* Backup data registers values are correct */ /* Turn on led connected to GPIO_LED Pin6 */
GPIO_SetBits(GPIO_LED, GPIO_Pin_6);
}
else
{ /* Backup data registers values are not correct or they are not yet
programmed (when the first time the program is executed) */ /* Write data to Backup data registers */
WriteToBackupReg(0x3211); /* Turn on led connected to GPIO_LED Pin7 */
GPIO_SetBits(GPIO_LED, GPIO_Pin_7);
}
} /* Turn on led connected to GPIO_LED Pin9 */
GPIO_SetBits(GPIO_LED, GPIO_Pin_9); while ()
{
}
}
4. 测试,第一次上电,亮灯LED7,LED8,LED9,然后VDD断电重新上电,亮灯LED6,LED8,LED9,然后按复位按键,亮灯LED9(代码只检查是不是上电复位)
5. 程序比较简单,主要是备份寄存器的作用。