ST NUCLEO-C031C6开发板学习笔记09(低功耗之睡眠模式)
<div class='showpostmsg'>## ==12:低功耗模式==### 12.1:硬件设计
查看芯片手册关于低功耗的描述:
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305223716190.png)
包含睡眠模式,停止模式,待机模式,以及关机这四种模式,我们选择最基础的睡眠模式进行应用开发。
```C
In Sleep mode, only the CPU is stopped. All peripherals continue to operate and can wake up the CPU when an interrupt/event occurs.
在睡眠模式下,只有CPU停止。所有外围设备都可以继续运行,当发生中断/事件时唤醒CPU。
```
我们可以使用开发板上的外部中断按钮来实现睡眠模式下的唤醒。
### 12.2:软件设计
1:CubeMX配置
首先配置外部中断引脚PC13
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240306001826132.png)
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240306002029509.png)
使能外部中断:
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240306002122381.png)
2:代码编写
设置外部中断引脚输入:
```C
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
```
唤醒后5秒内LED闪烁,然后自动进去低功耗睡眠模式
```C
void HAL_SYSTICK_Callback(void)
{
if (TimingDelay != 0)
{
TimingDelay--;
}
else
{
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
TimingDelay = LED_TOGGLE_DELAY;
}
}
```
延时5秒后,自动进入低功耗睡眠模式
```
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* Insert 5 seconds delay */
HAL_Delay(5000);
BSP_LED_Off(LED4);
/*Suspend Tick increment to prevent wakeup by Systick interrupt.
Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
HAL_SuspendTick();
/* Enter Sleep Mode , wake up is done once User push-button is pressed */
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
/* Resume Tick interrupt if disabled prior to SLEEP mode entry */
HAL_ResumeTick();
/* Re-configure LED4 */
BSP_LED_Init(LED4);
}
```
### 12.3:低功耗单元测试
具体参考哔哩哔哩:
https://www.bilibili.com/video/BV19j421m7kT/?vd_source=d89384d7551b55d1559dd2bf89310863
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>补充下,睡眠模式唤醒之后,系统无需初始化,如果是停止模式或者待机模式,需要重新进行外设初始化</p>
<p>板载的按键能唤醒吗? </p>
秦天qintian0303 发表于 2024-3-7 17:27
板载的按键能唤醒吗?
<p>就是用板載的按鍵喚醒的</p>
<p> </p>
页:
[1]