PVD (Programmable Votage Detector) ,即可编程电压监测器
PVD可以检测电压变化并触发中断,一般用于判断断电并进行数据保存
工程是使用STM32CubeMx生成的,在STM32CubeMx找了大半天,没找到可以PVD配置的地方
找到官方例程:Repository\STM32Cube_FW_F1_V1.8.0\Projects\STM3210E_EVAL\Examples\PWR\PWR_PVD
直接搬砖
/** PVD配置函数,从例程直接复制过来,在Main函数中调用配置PVD
* @brief Configures the PVD resources.
* @param None
* @retval None
*/
void PVD_Config(void)
{
/*##-1- Enable Power Clock #################################################*/
__HAL_RCC_PWR_CLK_ENABLE();
/*##-2- Configure the NVIC for PVD #########################################*/
HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(PVD_IRQn);
/* Configure the PVD Level to 3 and generate an interrupt on rising and falling
edges(PVD detection level set to 2.5V, refer to the electrical characteristics
of you device datasheet for more details) */
PWR_PVDTypeDef sConfigPVD;
sConfigPVD.PVDLevel = PWR_PVDLEVEL_7;
sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING;
HAL_PWR_ConfigPVD(&sConfigPVD);
/* Enable the PVD Output */
HAL_PWR_EnablePVD();
}
/** PVD (Programmable Votage Detector) ,即可编程电压监测器,PVD中断回调,在这个函数中添加自己需要的断电时处理的内容
* @brief PWR PVD interrupt callback
* @retval None
*/
void HAL_PWR_PVDCallback(void)
{
/* NOTE : This function Should not be modified, when the callback is needed,
the HAL_PWR_PVDCallback could be implemented in the user file
*/
// 断电时点亮一下LED,会亮一瞬间
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
}
/** 由于没有STM32CubeMx找到配置PVD的地方,这里需要手动添加PVD_IRQHandler的实现
* @brief This function handles the PVD Output interrupt request.
* @param None
* @retval None
*/
void PVD_IRQHandler(void)
{
HAL_PWR_PVD_IRQHandler();
}
PVD_IRQHandler是必须要添加的,如果STM32CubeMx能配置PVD,这个函数应该能自动添加的
太依赖STM32CubeMx了,开始时写了PVD_Config()和HAL_PWR_PVDCallback(),然后测试一直进不去中断,最后在例程的stm32f1xx_it.c文件中看到PVD_IRQHandler的实现,而自己的工程里却没有,加上后就一切搞定了
然后在HAL_PWR_PVDCallback()函数中写入flash,发现擦除完section后没有写入成功,时间不太够,估计要加个电容
=============================================================
找到STM32CubeMx配置PVD的地方了,在NVIC中,PVD interrupt through EXTI line 16,之前之所找不到因为我之前勾选了show only enable interrupts选项,没显示出来,重新生成工程,打开stm32f1xx_it.c可以看到PVD_IRQHandler()函数代码已经自动生成了