低功耗

一,低功耗模式

      低功耗

如电脑

低功耗

低功耗

int main(void)
{ 
    
    LED_Init();        

    //系统定时器初始化,时钟源来自HCLK,且进行8分频,
    //系统定时器时钟频率=168MHz/8=21MHz
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 
    
    //设置中断优先级分组2
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    //串口1,波特率115200bps,开启接收中断
    USART1_Init(115200);

    
    
    while(1)
    {
        printf("cpu run into sleep....
");
        
        //执行WFI指令,让CPU进入睡眠模式,立即停止执行任何的指令
        __WFI();
        
        delay_ms(500);
        
        printf("cpu wake up from sleep...
");

    }
}





void USART1_IRQHandler(void)                                //串口1中断服务程序
{
    uint8_t d;
    

    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)      //接收中断
    {
        //STM32进入串口中断后且读取数据才能唤醒
        //有些芯片是不需要这么做
        d = USART_ReceiveData(USART1);    
    } 
} 
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"
#include "stdio.h"
#include "sys.h"

static GPIO_InitTypeDef      GPIO_InitStructure;
static USART_InitTypeDef     USART_InitStructure;
static NVIC_InitTypeDef     NVIC_InitStructure;        
static EXTI_InitTypeDef      EXTI_InitStructure;



//重定义fputc函数 
int fputc(int ch, FILE *f)
{     
    USART_SendData(USART1,ch);
    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);  
    
    return ch;
}   

void delay_us(uint32_t nus)
{        
    uint32_t temp;             
    SysTick->LOAD =SystemCoreClock/8/1000000*nus;     //时间加载               
    SysTick->VAL  =0x00;                            //清空计数器
    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;         //使能滴答定时器开始倒数      
    do
    {
        temp=SysTick->CTRL;
    }while((temp&0x01)&&!(temp&(1<<16)));            //等待时间到达   
    SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;         //关闭计数器
    SysTick->VAL =0X00;                               //清空计数器 
}

void delay_ms(uint16_t nms)
{                     
    uint32_t temp;           
    SysTick->LOAD=SystemCoreClock/8/1000*nms;        //时间加载(SysTick->LOAD为24bit)
    SysTick->VAL =0x00;                               //清空计数器
    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;        //能滴答定时器开始倒数 
    do
    {
        temp=SysTick->CTRL;
    }while((temp&0x01)&&!(temp&(1<<16)));            //等待时间到达   
    SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;        //关闭计数器
    SysTick->VAL =0X00;                               //清空计数器              
} 

void LED_Init(void)
{         
  
    //使能GPIOE,GPIOF时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF, ENABLE);            

    //GPIOF9,F10初始化设置 
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9 | GPIO_Pin_10;        //LED0和LED1对应IO口
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;                    //普通输出模式,
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                    //推挽输出,驱动LED需要电流驱动
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;                //100MHz
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;                    //上拉
    GPIO_Init(GPIOF, &GPIO_InitStructure);                            //初始化GPIOF,把配置的数据写入寄存器                        


    //GPIOE13,PE14初始化设置 
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_14;        //LED2和LED3对应IO口
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;                    //普通输出模式
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                    //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;                //100MHz
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;                    //上拉
    GPIO_Init(GPIOE, &GPIO_InitStructure);                            //初始化GPIOE,把配置的数据写入寄存器

    GPIO_SetBits(GPIOF,GPIO_Pin_9  | GPIO_Pin_10);                    //GPIOF9,PF10设置高,灯灭
    GPIO_SetBits(GPIOE,GPIO_Pin_13 | GPIO_Pin_14);        
}


void USART1_Init(uint32_t baud)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);                             //使能GPIOA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);                            //使能USART1时钟
 
    //串口1对应引脚复用映射
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);                         //GPIOA9复用为USART1
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);                         //GPIOA10复用为USART1
    
    //USART1端口配置
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;                         //GPIOA9与GPIOA10
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                                    //复用功能
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                                //速度50MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                                     //推挽复用输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                                     //上拉
    GPIO_Init(GPIOA,&GPIO_InitStructure);                                             //初始化PA9,PA10

    //USART1 初始化设置
    USART_InitStructure.USART_BaudRate = baud;                                        //波特率设置
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;                        //字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1;                            //一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No;                                //无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;    //无硬件数据流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                    //收发模式
    USART_Init(USART1, &USART_InitStructure);                                         //初始化串口1
    
    USART_Cmd(USART1, ENABLE);                                                      //使能串口1 
    
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                                    //开启相关中断

    //Usart1 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;                                //串口1中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;                            //抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                                //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                    //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                                                    //根据指定的参数初始化VIC寄存器
}

void exti0_init(void)
{

    /* Enable SYSCFG clock ,使能系统配置时钟*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    /* Connect EXTI Line0 to PA0 pin,将PA0引脚连接到外部中断控制线0 */
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

    /*    PA0 引脚配置为输入模式 */
    GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    
    /* Configure EXTI Line0配置外部中断控制线0*/
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;                        //当前使用外部中断控制线0 2 3 4
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                //中断模式
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;            //上升沿,也意味着按键松开的时候才触发中断 
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                        //使能外部中断控制线0 2 3 4 ,让外部中断控制线0 2 3 4工作
    EXTI_Init(&EXTI_InitStructure);    
    
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;                //允许外部中断控制线0触发中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;    //抢占优先级为0x3,最低优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;            //响应优先级为0x3,最低优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                    //使能
    NVIC_Init(&NVIC_InitStructure);    
}


int main(void)
{ 
    
    LED_Init();        

    //系统定时器初始化,时钟源来自HCLK,且进行8分频,
    //系统定时器时钟频率=168MHz/8=21MHz
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 
    
    //设置中断优先级分组2
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    //串口1,波特率115200bps,开启接收中断
    USART1_Init(115200);

    //外部中断控制线0初始化
    exti0_init();
    
    while(1)
    {
        printf("cpu run into stop....
");
        delay_ms(500);
        
        //让CPU进入停止模式,CPU不会运行任何指令且所有外设时钟都停止
        PWR_EnterSTOPMode(PWR_LowPowerRegulator_ON,PWR_STOPEntry_WFI);
        
        //退出停止模式后,CPU会选择HSI RC振荡电路作为时钟源,这不是我们想要,要重新初始化
        SystemInit();
        
        delay_ms(500);
        printf("cpu wake up from stop...
");

    }
}


void EXTI0_IRQHandler(void)
{
    //检查当前外部中断控制线0是否触发中断
    if(EXTI_GetITStatus(EXTI_Line0) == SET)
    {
        
        //添加用户代码
        printf("EXTI0_IRQHandler
");
     
        /* Clear the EXTI line 0 pending bit,清空外部中断控制线0标志位,告诉CPU我已经完成当前中断请求,可以继续响应下一次的中断请求 */
        EXTI_ClearITPendingBit(EXTI_Line0);
    }
}


void USART1_IRQHandler(void)                                //串口1中断服务程序
{
    uint8_t d;
    

    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)      //接收中断
    {
        //STM32进入串口中断后且读取数据才能唤醒
        //有些芯片是不需要这么做
        d = USART_ReceiveData(USART1);    
    } 
} 
stop