硬件条件-----智嵌物联STM32F207VET6开发板
IO配置:PB0——外部中断唤醒引脚,配置为EXTI_Line0
PE15——外部中断引脚,进入Stop Mode模式,配置为EXTI_Line15(键S3)
PE2——外部中断引脚,用于退出Stop Mode后,检查其他的功能是否正常(键S4)
PA4——LED1
PA5——LED2
PB1——LED3
PE11——LED4
功能描述:在进入Stop Mode模式之前,LED4不停闪烁;按下按键S3进入Stop Mode模式,PB0口下降沿来,CPU退出Stop Mode模式,恢复正常;用外部中断S4来点亮LED1观察退出Stop Mode是否正常(亦可观察LED4的闪烁情况)。
本实验主要验证事项:
(1)进入Stop Mode模式条件;
(2)退出Stop Mode程序从哪里运行;
(3)退出后是否需要重新配置时钟;
(4)监测电流消耗情况
(5)进入、退出Stop Mode模式中断优先级
配置流程:LED_Configuration();配置LED的IO
void LED_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB| RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
LED1_OFF;
LED2_OFF;
LED3_OFF;
LED4_OFF;
}
EXTI_Configuration(); 配置外部中断,配置唤醒中断时,特别注意方式:1,边缘触发2,事件。
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_DeInit();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15|EXTI_Line12;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource15); //S3
EXTI_InitStructure.EXTI_Line = EXTI_Line15;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource12);
EXTI_InitStructure.EXTI_Line = EXTI_Line12;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
StopMode_Measure(void);配置Stop Mode,把不需要的管脚配置为模拟输入,这里特别主要不能把外部中断的引脚关闭了,否则无法唤醒。HAL_GPIO_EXTI_Callback()用于判定Stop Mode退出后程序从哪里执行。
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_All;
GPIO_Init(GPIOC,&GPIO_InitStruct);
GPIO_Init(GPIOD,&GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,DISABLE);
EXTI_ClearFlag(EXTI_Line0|EXTI_Line1|EXTI_Line2|EXTI_Line3|EXTI_Line4
|EXTI_Line5|EXTI_Line6|EXTI_Line7|EXTI_Line8|EXTI_Line9
|EXTI_Line10|EXTI_Line11|EXTI_Line12|EXTI_Line13|EXTI_Line14
|EXTI_Line15|EXTI_Line16|EXTI_Line17|EXTI_Line18|EXTI_Line19|
EXTI_Line20|EXTI_Line21|EXTI_Line22);
PWR_FlashPowerDownCmd(ENABLE);
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);
SystemInit();
HAL_GPIO_EXTI_Callback();
}
配置void NVIC_Configuration(void):特别注意,唤醒中断的优先级必须高于进入Stop Mode的优先级,否则无法唤醒。
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //优先级必须高于EXTI15_10_IRQn。
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler(void):配置进入Stop Mode模式
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line12) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line12);
LED1_ON;
LED_Delay(1000000);
LED1_OFF;
}
if(EXTI_GetITStatus(EXTI_Line15) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line15);
StopMode_Measure();
}
}
void EXTI0_IRQHandler(void):配置退出Stop Mode模式
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) !=RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
LED2_ON;
LED_Delay(1000000);
LED2_OFF;
}
}