STM32 掉电检测程序
当VDD下降到PVD阀值以下或当VDD上升到PVD阀值之上时,根据外部中断第16线的上升/下降边沿触发设置,就会产生PVD中断
void PVD_IRQHandler(void)
{
led_ctrl(LED1, ON);
if(PWR_GetFlagStatus(PWR_FLAG_PVDO))
{
MSD0_WriteSingleBlock(1, buf);
}
EXTI_ClearITPendingBit(EXTI_Line16);
}
void PVD_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_PVDLevelConfig(PWR_PVDLevel_2V9); //可设置范围:2.2-2.9V
PWR_PVDCmd(ENABLE);
EXTI_DeInit();
EXTI_StructInit(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line16; //PVD连接到外部中断16
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
当检测到掉电以后,led会亮一下,然后进行掉电保存操作。
注意:
- 掉电时间有限,如果耗时较长的操作,可能会操作失败;
- 时钟频率设置也会有影响。(SysTick_Config)