STM32的备份寄存器测试
1. 研究STM3的备份寄存器,注意,如果要测试这个例程的话,VBAT不能和VDD接一起,必须分开。
2. 理解,备份寄存器可以有VBAT独立供电,也就是外接电池,备份寄存器在VBAT供电情况下,如果发生系统复位(按键复位)和上电复位,备份寄存器的值可以保持。
3. 代码如下:
1 int main(void) 2 { 3 #ifdef DEBUG 4 debug(); 5 #endif 6 7 /* System Clocks Configuration */ 8 RCC_Configuration(); 9 10 /* NVIC configuration */ 11 NVIC_Configuration(); 12 13 /* GPIO configuration */ 14 GPIO_Configuration(); 15 16 /* Enable PWR and BKP clock */ 17 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); 18 19 /* Enable write access to Backup domain */ 20 PWR_BackupAccessCmd(ENABLE); 21 22 /* Clear Tamper pin Event(TE) pending flag */ 23 BKP_ClearFlag(); 24 25 //首先判断是不是上电复位的 26 if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) 27 { 28 /* Clear reset flags */ 29 RCC_ClearFlag(); 30 31 /* Turn on led connected to GPIO_LED Pin8 */ 32 GPIO_SetBits(GPIO_LED, GPIO_Pin_8); 33 34 /* Check if Backup data registers are programmed */ 35 if(CheckBackupReg(0x3211) == 0x00) 36 { /* Backup data registers values are correct */ 37 38 /* Turn on led connected to GPIO_LED Pin6 */ 39 GPIO_SetBits(GPIO_LED, GPIO_Pin_6); 40 } 41 else 42 { /* Backup data registers values are not correct or they are not yet 43 programmed (when the first time the program is executed) */ 44 45 /* Write data to Backup data registers */ 46 WriteToBackupReg(0x3211); 47 48 /* Turn on led connected to GPIO_LED Pin7 */ 49 GPIO_SetBits(GPIO_LED, GPIO_Pin_7); 50 } 51 } 52 53 /* Turn on led connected to GPIO_LED Pin9 */ 54 GPIO_SetBits(GPIO_LED, GPIO_Pin_9); 55 56 while (1) 57 { 58 } 59 }
4. 测试,第一次上电,亮灯LED7,LED8,LED9,然后VDD断电重新上电,亮灯LED6,LED8,LED9,然后按复位按键,亮灯LED9(代码只检查是不是上电复位)
5. 程序比较简单,主要是备份寄存器的作用。