Files
stc32g128k/Docs/60_coding/mod-power.md

59 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 编码实现 - 低功耗电源管理 (Docs/60_coding/mod-power.md)
本文件对应低功耗待机与外部中断唤醒的具体编码实现细节。
## 1. 对应代码源文件
* [App/main.c](file:///c:/workfile/105/stc32g12k128/App/main.c) (休眠控制、外部中断初始化与服务函数)
## 2. 关键代码片段与逻辑实现
### 2.1 屏幕休眠命令与供电切断
在待机检测到达 10秒超时后调用进入睡眠流程
```c
void Enter_Deep_Sleep(void) {
// 1. 发送屏幕休眠指令 (Sleep In)
LCD_WriteCmd(0x10);
Delay_ms(120); // 必须等待 120ms 供屏幕电容放电
// 2. 切断 LCD 供电与背光
SGM_CTRL = 0;
// 3. 配置 KEY 端口的中断触发(配置为下降沿触发)
P0IM0 = 0x00; // 下降沿
P0IM1 = 0x00;
P0INTE = 0x3E; // 开启 P0.1(UP), P0.2(SOS1), P0.3(DOWN), P0.4(CONFIRM), P0.5(SOS2) 的端口中断 (0011 1110)
// 4. 调用单片机 PD 指令挂起主时钟
PCON |= 0x02; // PD = 1
_nop_();
_nop_();
}
```
### 2.2 端口中断唤醒与显示重建
当任意按键被按下P0.1~P0.5 电平下降沿跳变触发外部端口中断CPU 被唤醒并从停机处继续执行:
```c
void Port0_Isr(void) interrupt 37 {
u8 int_src = P0INTF;
P0INTF = 0x00; // 清除中断标志位
if (int_src & 0x3E) { // 检查是否是 5 个按键引发的唤醒
// 1. 恢复 LCD 背光与供电
SGM_CTRL = 1;
Delay_ms(20); // 等待供电稳定
// 2. 发送唤醒屏幕指令 (Sleep Out)
LCD_WriteCmd(0x11);
Delay_ms(120);
// 3. 恢复工作状态与屏幕刷新
current_state = STATE_NORMAL;
clock_updated = 1;
}
}
```
<!-- Checked and verified with bracelet_factory_id and active SOS changes -->
通过中断直接退出 `PD` 模式,恢复手环的常规时钟显示。