停止模式有三种:停止0,停止1,停止2
```C
Stop 0, Stop 1, and Stop 2 modes
The Stop modes achieve a lowest power consumption while retaining the content of SRAM and registers.
All the clocks in the VCORE domain are stopped, the PLL, the MSI RC, the HSI16 RC and the HSE crystal
oscillators are disabled. The LSE and LSI clocks are still running.
The RTC can remain active (Stop mode with RTC, Stop mode without RTC).
Some peripherals with the wake-up capability can enable the HSI16 RC during Stop mode, to detect their
wake-up condition.
Three Stop modes are available, Stop 0, Stop 1 and Stop2:
–
In Stop2 mode, most of the VCORE domain is put in lower-leakage mode.
–
Stop 1 offers the largest number of active peripherals and wake-up sources, a smaller wake-up time,
but with a higher consumption than Stop 2 mode.
–
In Stop 0 mode, the main regulator remains on, allowing a very fast wake-up time, but with a much
higher consumption.
When exiting from Stop 0, Stop 1 or Stop 2 mode, the system clock can be either the MSI clock (up to
48 MHz) or HSI16, depending on software configuration.
```
我们选择其中一种stop0来进行处理,LED灯闪烁5秒,进入到低功耗停止模式,检查停止模式功耗,然后通过PC13按键唤醒,检查运行功耗。
1:配置按键为外部中断模式:
![image-20240530152846399](https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240530152846399.png)
LED灯闪烁采用systick时钟实现。
![image-20240530153019661](https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240530153019661.png)
2:编写代码
设置外部中断处理:
```C
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == BUTTON_USER_PIN)
{
/* Reconfigure LED4 */
BSP_LED_Init(LED4);
/* Toggle LED4 */
BSP_LED_Toggle(LED4);
TimingDelay = LED_TOGGLE_DELAY;
}
}
```
编写滴答时钟闪烁LED灯处理:
```C
void HAL_SYSTICK_Callback(void)
{
if (TimingDelay != 0)
{
TimingDelay--;
}
else
{
/* Toggle LED4 */
BSP_LED_Toggle(LED4);
TimingDelay = LED_TOGGLE_DELAY;
}
}
```
进入低功耗模式之前,配置GPIO为GPIO_MODE_ANALOG模拟输入模式
```C
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOs clock */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
/* Set all GPIO in analog state to reduce power consumption, */
/* except GPIOC to keep user button interrupt enabled */
/* Note: Debug using ST-Link is not possible during the execution of this */
/* example because communication between ST-link and the device */
/* under test is done through UART. All GPIO pins are disabled (set */
/* to analog input mode) including UART I/O pins. */
GPIO_InitStructure.Pin = GPIO_PIN_ALL;
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Disable GPIOs clock */
__HAL_RCC_GPIOA_CLK_DISABLE();
__HAL_RCC_GPIOB_CLK_DISABLE();
__HAL_RCC_GPIOD_CLK_DISABLE();
__HAL_RCC_GPIOE_CLK_DISABLE();
```
最后进入stop停止模式:
```
HAL_SuspendTick();
HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
SYSCLKConfig_STOP();
```
使用万用表检查运行时候的功耗:4.64mA
![image-20240530154608350](https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240530154608350.png)
停止模式下的功耗:1.81mA
![image-20240530154656337](https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240530154656337.png)
运行视频如下:
停止模式功耗