stm32f103低功耗分为WFI等待中断和WFE等待事件,我只用到等待中断,这里没有细究。
待机模式
最低功耗2uA,只有备份寄存器和待机电路供电,
PLL,HSI,HSE断开,寄存器和SRAM复位,除了唤醒引脚其余都处于高阻态
可以被wake_up上升沿,RTC闹钟上升沿,IWDG复位,NRST外部复位 , 唤醒
进入待机模式
void Sys_Enter_Standby(void)/*复位IO端口,并且进入待机状态*/
{
RCC_APB2PeriphResetCmd(0X01FC,DISABLE); //复位IO口时钟,可选择
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); //使能PWR外设时钟
PWR_WakeUpPinCmd(ENABLE); //使能唤醒管脚功能
PWR_EnterSTANDBYMode(); //进入待命(STANDBY)模式
}
端口配置
void GPIO_INI(void){
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 上拉输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); //GPIOA 0
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//上升沿触发
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //GPIO0 所在的中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
wake_up唤醒,退出待机模式
void EXTI0_IRQHandler(void)
{
delay1ms(10);//消抖
printf("ent exit 0\n");
printf("work");
EXTI_ClearITPendingBit(EXTI_Line0); //清除EXTI0线路挂起位
}
停止模式(STOP)
最低功耗20uA,,
PLL,HSI,HSE断开,
寄存器和SRAM被保存下来,所有I/O引脚都保持他们在运行模式时的状态
可以被任意中断唤醒
进入停机模式
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); //进入STOP模式,任意外部中断都会唤醒
端口配置
void GPIO_INI(void){
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 上拉输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); //GPIOA 0
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//上升沿触发
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //GPIO0 所在的中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
退出停机模式(外部中断0唤醒)
void EXTI0_IRQHandler(void)
{
delay1ms(10);//消抖
SystemInit();
printf("ent exit 0\n");
if(EXTI_GetITStatus(EXTI_Line0) != RESET) //检查指定的EXTI0线路触发请求发生与否
{
EXTI_ClearITPendingBit(EXTI_Line0); //清除EXTI0线路挂起位
EXTI_ClearFlag(EXTI_Line0); //清楚EXTI0线路挂起标志位
}
}