STM32 備份域寄存器功能驗(yàn)證代碼片段
STM32 備份域寄存器功能驗(yàn)證代碼片段:
void tuning(unsigned char On_Off)
{
if(On_Off)
GPIOA -> BSRR = GPIO_Pin_6;//亮燈
else
GPIOA -> BRR = GPIO_Pin_6;//滅
}
unsigned char CheckBkp()
{
unsigned short bkData = 0;
unsigned char temp;
/* 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();
temp = (RCC->CSR>>26)&0x3f;
if( (temp&0x02) != 0)//上電復(fù)位
{
bkData = BKP_ReadBackupRegister(BKP_DR1);
tuning(1);
}
else
{
BKP_WriteBackupRegister(BKP_DR1,0xCCDD);
}
//清除復(fù)位標(biāo)志
RCC_ClearFlag();
return (bkData == 0xCCDD ? 1:0);
}
void main()
{
unsigned int i;
if(CheckBkp() == 1)
{
for(i = 0; i < 5000000; i++)
__asm("nop");
tuning(0);
}
while(1)
{
......
}
}
斷電后,再上電,LED先亮后滅,結(jié)果正確