单片机的休眠唤醒和停止唤醒实验代码示例
单片机源程序如下:
- #include "wkup.h"
- #include "led.h"
- #include "delay.h"
- void Sys_Sleepy(void)
- {
- SCB->SCR |=0X00;
- #if defined ( __CC_ARM )
- __force_stores();
- #endif
- /* Request Wait For Interrupt */
- __WFI();
- }
- //系统进入待机模式
- void Sys_Enter_Sys_Sleepy(void)
- {
- // RCC_APB2PeriphResetCmd(0X01FC,DISABLE); //复位所有IO口
- Sys_Sleepy();
- }
- void EXTI2_IRQHandler(void)
- {
- EXTI_ClearITPendingBit(EXTI_Line2); // 清除LINE10上的中断标志位
- SCB->SCR |=SCB_SCR_SEVONPEND;
- // Sys_Enter_Sys_Sleepy();
- if(E2==0)//关机?
- {
- delay_ms(10000);
- if(E2==0)
- {
- LED0=0;
- Sys_Enter_Sys_Sleepy();
- }
- } else LED1=0;
-
- }
- void EXTI3_IRQHandler(void)
- {
- EXTI_ClearITPendingBit(EXTI_Line3); // 清除LINE10上的中断标志位
- SCB->SCR |=SCB_SCR_SEVONPEND;
- LED1=!LED1;
- /* if(E3!=0)//关机?
- {
- Sys_Enter_Sys_Sleepy();
- } else LED0=1; */
- }
- //PA0 WKUP唤醒初始化
- void WKUP_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);//使能GPIOA和复用功能时钟
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2; //PE.2
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//上拉输入
- GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化IO
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3; //PE.3
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//上拉输入
- GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化IO
- //使用外部中断方式
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2); //中断线2连接GPIOE.2
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);
- EXTI_InitStructure.EXTI_Line = EXTI_Line3|EXTI_Line2; //设置按键所有的外部线路
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设外外部中断模式:EXTI线路为中断请求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //上升沿触发
- EXTI_InitStructure.EXTI_LineCmd =ENABLE;// DISABLE;
- EXTI_Init(&EXTI_InitStructure); // 初始化外部中断
- NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //使能按键所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- /*----------------------------------------*/
- NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //使能按键所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- if(E2!=0) Sys_Sleepy(); //不是开机,进入待机模式
- }
复制代码
所有资料51hei提供下载:
睡眠唤醒和停止唤醒实验.rar
(649.88 KB, 下载次数: 70)
|