stm32PVD電源監(jiān)測使用
正確操作步驟:
開時鐘;配置電源檢測;配置中斷優(yōu)先級
/*Includes------------------------------------------------------------------*/
#include"stm32f10x.h"
#include"stm32_eval.h"
void EXTI_Configuration(void);
void NVIC_Configuration(void);
intmain(void)
{
STM_EVAL_LEDInit(LED1);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
/*Enable PWRandBKP clock*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);//電源管理部分時鐘開啟
EXTI_Configuration();
NVIC_Configuration();
PWR_PVDLevelConfig(PWR_PVDLevel_2V9);// 設(shè)定監(jiān)控閥值
/*Enable the PVD Output*/
PWR_PVDCmd(ENABLE);// 使能PVD
while(1)
{
}
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_ClearITPendingBit(EXTI_Line16);
EXTI_InitStructure.EXTI_Line=EXTI_Line16;// PVD連接到中斷線16上
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;//使用中斷模式
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;//電壓上升或下降越過設(shè)定閥值時都產(chǎn)生中斷。
EXTI_InitStructure.EXTI_LineCmd=ENABLE;// 使能中斷線
EXTI_Init(&EXTI_InitStructure);//初始
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//中斷優(yōu)先級配置
//設(shè)置PVD中斷
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);
}
注意:GPIO驅(qū)動LED模式為GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;還是推挽輸出。
中斷處理函數(shù):
void PVD_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line16) != RESET)
{
STM_EVAL_LEDToggle(LED1);
EXTI_ClearITPendingBit(EXTI_Line16);
}
}