STM32 备份域寄存器功能验证代码片段
扫描二维码
随时随地手机看文章
STM32 备份域寄存器功能验证代码片段:
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)//上电复位
{
bkData = BKP_ReadBackupRegister(BKP_DR1);
tuning(1);
}
else
{
BKP_WriteBackupRegister(BKP_DR1,0xCCDD);
}
//清除复位标志
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先亮后灭,结果正确