STM32 study notes (twenty)

STM32F103ZET6 standby wake-up experiment



Preface

The learning of STM32 can be divided into 3 versions.
1. Register version
2. Library function version
3. HAL library version
Due to personal reasons, I choose the library function version to learn STM32.


Tip: Problems such as software installation will not be explained! ! !

1. Introduction

Many microcontrollers have low power consumption modes, and STM32 is no exception. After the system or power is reset, the microcontroller is in operation. The HCLK in the running state provides the clock for the CPU, and the kernel executes the program code. When the CPU does not need to continue to run, multiple low-power modes can be used to save power, such as when waiting for an external event. The user needs to select an optimal low power consumption mode based on the lowest power consumption, fastest startup time, and available wake-up sources.

3 low power modes
Insert picture description here
Insert picture description here

Second, the standby mode configuration process

1. Description

Insert picture description here

2. Related registers

Insert picture description here
Insert picture description here

3. Configuration steps

Insert picture description here

Three, program source code

1.wkup.h

code show as below:

#ifndef __WKUP_H
#define __WKUP_H	 
#include "sys.h"
				    
#define WKUP_KD PAin(0)			//PA0 检测是否外部WK_UP按键按下
	 
u8 Check_WKUP(void);  			//检测WKUP脚的信号
void WKUP_Init(void); 			//PA0 WKUP唤醒初始化
void Sys_Enter_Standby(void);	//系统进入待机模式
#endif

2.wkup.c

code show as below:

#include "wkup.h"
#include "led.h"
#include "delay.h"

void Sys_Standby(void)
{
    
      
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);	//使能PWR外设时钟
	PWR_WakeUpPinCmd(ENABLE);  //使能唤醒管脚功能
	PWR_EnterSTANDBYMode();	  //进入待命(STANDBY)模式 		 
}
//系统进入待机模式
void Sys_Enter_Standby(void)
{
    
    			 
	RCC_APB2PeriphResetCmd(0X01FC,DISABLE);	//复位所有IO口
	Sys_Standby();
}
//检测WKUP脚的信号
//返回值1:连续按下3s以上
//      0:错误的触发	
u8 Check_WKUP(void) 
{
    
    
	u8 t=0;	//记录按下的时间
	LED0=0; //亮灯DS0 
	while(1)
	{
    
    
		if(WKUP_KD)
		{
    
    
			t++;			//已经按下了 
			delay_ms(30);
			if(t>=100)		//按下超过3秒钟
			{
    
    
				LED0=0;	 	//点亮DS0 
				return 1; 	//按下3s以上了
			}
		}else 
		{
    
     
			LED0=1;
			return 0; //按下不足3秒
		}
	}
} 
//中断,检测到PA0脚的一个上升沿.	  
//中断线0线上的中断检测


void EXTI0_IRQHandler(void)
{
    
     		    		    				     		    
	EXTI_ClearITPendingBit(EXTI_Line0); // 清除LINE10上的中断标志位		  
	if(Check_WKUP())//关机?
	{
    
    		  
		Sys_Enter_Standby();  
	}
} 
//PA0 WKUP唤醒初始化
void WKUP_Init(void)
{
    
    	
  GPIO_InitTypeDef  GPIO_InitStructure;  		  
	NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//使能GPIOA和复用功能时钟

	GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;	 //PA.0
	GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPD;//上拉输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);	//初始化IO
    //使用外部中断方式
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);	//中断线0连接GPIOA.0

  EXTI_InitStructure.EXTI_Line = EXTI_Line0;	//设置按键所有的外部线路
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;			//设外外部中断模式:EXTI线路为中断请求
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  //上升沿触发
 	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);	// 初始化外部中断

	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键所在的外部中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从优先级2级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
	NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

	if(Check_WKUP()==0) Sys_Standby();    //不是开机,进入待机模式  
	
}

3.main.c

code show as below:

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"	 
#include "wkup.h"
 int main(void)
 {
    
    	 
  
	delay_init();	    	 //延时函数初始化	  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
	uart_init(115200);	 	//串口初始化为115200
 	LED_Init();			     //LED端口初始化	 	
	WKUP_Init(); //待机唤醒初始化
	LCD_Init();	 //LCD初始化
	POINT_COLOR=RED;
	 
	LCD_ShowString(30,50,200,16,16,"Elite STM32");	
	LCD_ShowString(30,70,200,16,16,"WKUP TEST");	
	LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
	LCD_ShowString(30,110,200,16,16,"2015/1/14");
	 
	while(1)
	{
    
    
		LED0=!LED0;
		delay_ms(250);
	}
 }

Experimental results

The experimental procedure is more complicated. The main reason is to realize the same pin PAO pin (Wake_Up pin), long press for 3 seconds to enter standby mode, and in standby mode, long press for 3 seconds to wake up.


to sum up

1. After watching the video, be sure to write the program yourself.
2. Before programming the program, analyze the program and reason about the experimental phenomenon.
3. If the experimental phenomenon is inconsistent with the reasoning, the program must be carefully analyzed.

Guess you like

Origin blog.csdn.net/weixin_44935259/article/details/113267941