STM32低功耗模式全面指南

STM32低功耗模式全面指南

代码复制另存为html后打开即可

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>STM32低功耗模式全面指南</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2a6c, #2c3e50);
            color: #f0f0f0;
            line-height: 1.6;
            padding: 20px;
            background-attachment: fixed;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            padding: 40px 20px;
            margin-bottom: 30px;
            background: rgba(0, 0, 0, 0.3);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            position: relative;
            overflow: hidden;
            border: 1px solid rgba(0, 150, 255, 0.3);
        }
        
        header::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
            pointer-events: none;
        }
        
        h1 {
            font-size: 3.2rem;
            margin-bottom: 15px;
            color: #fff;
            text-shadow: 0 0 15px rgba(0, 150, 255, 0.7);
        }
        
        .subtitle {
            font-size: 1.4rem;
            color: #a0d2ff;
            max-width: 800px;
            margin: 0 auto 25px;
        }
        
        .intro {
            max-width: 800px;
            margin: 0 auto 30px;
            font-size: 1.1rem;
            color: #d0e5ff;
            background: rgba(0, 30, 60, 0.5);
            padding: 20px;
            border-radius: 10px;
            border-left: 4px solid #0096ff;
        }
        
        .card-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin-bottom: 40px;
        }
        
        .card {
            background: rgba(20, 30, 48, 0.85);
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            position: relative;
            overflow: hidden;
            border-top: 2px solid #0096ff;
        }
        
        .card:hover {
            transform: translateY(-10px);
            box-shadow: 0 15px 35px rgba(0, 100, 200, 0.3);
        }
        
        .card::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 5px;
            background: linear-gradient(90deg, #0096ff, #00d4ff);
        }
        
        .card h2 {
            color: #00d4ff;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .card h2 i {
            font-size: 1.8rem;
        }
        
        .card-content {
            padding: 10px 0;
        }
        
        .power-table {
            width: 100%;
            border-collapse: collapse;
            margin: 25px 0;
            background: rgba(10, 25, 47, 0.7);
            border-radius: 8px;
            overflow: hidden;
        }
        
        .power-table th {
            background: linear-gradient(135deg, #0072ff, #00c6ff);
            padding: 15px;
            text-align: left;
            font-weight: 600;
        }
        
        .power-table td {
            padding: 12px 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .power-table tr:last-child td {
            border-bottom: none;
        }
        
        .power-table tr:nth-child(even) {
            background: rgba(20, 40, 80, 0.3);
        }
        
        .impact-table {
            width: 100%;
            border-collapse: collapse;
            margin: 40px 0;
            background: rgba(15, 30, 60, 0.8);
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
        }
        
        .impact-table th {
            background: linear-gradient(135deg, #0072ff, #8a2be2);
            padding: 18px 15px;
            text-align: center;
            font-weight: 600;
            font-size: 1.1rem;
        }
        
        .impact-table td {
            padding: 16px 15px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            text-align: center;
        }
        
        .impact-table tr:nth-child(odd) {
            background: rgba(25, 45, 80, 0.6);
        }
        
        .impact-table tr:nth-child(even) {
            background: rgba(35, 55, 90, 0.6);
        }
        
        .impact-table td:first-child {
            font-weight: 600;
            color: #00d4ff;
            text-align: left;
        }
        
        .impact-table tr:hover {
            background: rgba(45, 75, 120, 0.7);
        }
        
        .code-block {
            background: #0f1c2e;
            border-radius: 8px;
            padding: 18px;
            margin: 20px 0;
            font-family: 'Courier New', monospace;
            font-size: 0.95rem;
            overflow-x: auto;
            border-left: 4px solid #0096ff;
        }
        
        .code-keyword {
            color: #ff6ec7;
        }
        
        .code-comment {
            color: #5f9ea0;
            font-style: italic;
        }
        
        .code-function {
            color: #00ffff;
        }
        
        .code-value {
            color: #ffd700;
        }
        
        .best-practices {
            background: rgba(0, 50, 100, 0.5);
            padding: 30px;
            border-radius: 15px;
            margin: 40px 0;
            border: 1px solid rgba(0, 150, 255, 0.3);
        }
        
        .best-practices h2 {
            color: #00d4ff;
            margin-bottom: 25px;
            text-align: center;
            font-size: 2rem;
        }
        
        .practice-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 25px;
        }
        
        .practice-item {
            background: rgba(10, 40, 70, 0.6);
            padding: 25px;
            border-radius: 10px;
            transition: transform 0.3s ease;
        }
        
        .practice-item:hover {
            transform: scale(1.03);
        }
        
        .practice-item h3 {
            color: #00bfff;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .conclusion {
            background: rgba(0, 40, 80, 0.7);
            padding: 30px;
            border-radius: 15px;
            margin: 40px 0;
            text-align: center;
            border: 1px solid rgba(0, 150, 255, 0.3);
        }
        
        .conclusion h2 {
            color: #00d4ff;
            margin-bottom: 25px;
        }
        
        .conclusion p {
            max-width: 800px;
            margin: 0 auto 20px;
            font-size: 1.1rem;
        }
        
        .mode-details {
            margin: 40px 0;
        }
        
        .mode-details h2 {
            color: #00d4ff;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid rgba(0, 150, 255, 0.3);
        }
        
        .pros-cons {
            display: flex;
            gap: 20px;
            margin: 25px 0;
        }
        
        .pros, .cons {
            flex: 1;
            padding: 20px;
            border-radius: 10px;
        }
        
        .pros {
            background: rgba(0, 100, 0, 0.3);
            border-left: 4px solid #0f0;
        }
        
        .cons {
            background: rgba(100, 0, 0, 0.3);
            border-left: 4px solid #f00;
        }
        
        .pros h3, .cons h3 {
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .section-title {
            color: #00d4ff;
            margin: 40px 0 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid rgba(0, 150, 255, 0.3);
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        ul {
            padding-left: 20px;
            margin-bottom: 20px;
        }
        
        li {
            margin-bottom: 8px;
        }
        
        .highlight {
            color: #ffd700;
            font-weight: 600;
        }
        
        .note-box {
            background: rgba(200, 150, 0, 0.2);
            border-left: 4px solid #ffd700;
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
        }
        
        .mode-summary {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin: 30px 0;
        }
        
        .mode-box {
            flex: 1;
            min-width: 250px;
            background: rgba(20, 40, 70, 0.6);
            border-radius: 10px;
            padding: 20px;
            border-top: 3px solid #00d4ff;
        }
        
        .mode-box h3 {
            color: #00d4ff;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .mode-box ul {
            padding-left: 15px;
        }
        
        .mode-box li {
            margin-bottom: 10px;
        }
        
        .footer {
            text-align: center;
            padding: 30px 0;
            color: #a0d2ff;
            border-top: 1px solid rgba(0, 150, 255, 0.3);
            margin-top: 50px;
        }
        
        @media (max-width: 768px) {
            .card-container {
                grid-template-columns: 1fr;
            }
            
            .pros-cons {
                flex-direction: column;
            }
            
            h1 {
                font-size: 2.5rem;
            }
            
            .impact-table {
                font-size: 0.9rem;
            }
            
            .mode-summary {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class="fas fa-battery-quarter"></i> STM32低功耗模式全面指南</h1>
            <p class="subtitle">深入解析睡眠模式、停止模式、待机模式与关机模式</p>
            <div class="intro">
                <p>本指南详细介绍了STM32微控制器的各种低功耗模式,包括如何进入和退出每种模式,各种模式下的功耗特性,唤醒源配置以及最佳设计实践。适用于STM32F0/F1/F4/L0/L1/L4/G0/G4等系列。</p>
            </div>
        </header>

        <section class="card-container">
            <div class="card">
                <h2><i class="fas fa-bed"></i> 睡眠模式 (Sleep)</h2>
                <div class="card-content">
                    <p>仅内核停止运行,外设保持工作状态。功耗降低有限但唤醒速度最快。</p>
                    <p><strong>进入方法:</strong></p>
                    <div class="code-block">
                        <span class="code-comment">/* 通过等待中断进入 */</span><br>
                        <span class="code-keyword">__WFI</span>();  <span class="code-comment">// 等待任意中断唤醒</span><br><br>
                        <span class="code-comment">/* 通过等待事件进入 */</span><br>
                        <span class="code-keyword">__WFE</span>();  <span class="code-comment">// 等待特定事件唤醒</span>
                    </div>
                    <p><strong>典型功耗:</strong> 1-10 mA</p>
                    <p><strong>唤醒时间:</strong> &lt;1 μs</p>
                </div>
            </div>
            
            <div class="card">
                <h2><i class="fas fa-stop-circle"></i> 停止模式 (Stop)</h2>
                <div class="card-content">
                    <p>所有时钟停止,SRAM和寄存器内容保留。实现微安级功耗。</p>
                    <p><strong>进入方法:</strong></p>
                    <div class="code-block">
                        <span class="code-comment">/* 配置唤醒时钟源 */</span><br>
                        <span class="code-keyword">__HAL_RCC_WAKEUPSTOP_CLK_CONFIG</span>(<br>
                        &nbsp;&nbsp;RCC_STOP_WAKEUPCLOCK_HSI);<br><br>
                        <span class="code-comment">/* 进入停止模式 */</span><br>
                        <span class="code-keyword">HAL_PWR_EnterSTOPMode</span>(<br>
                        &nbsp;&nbsp;PWR_MAINREGULATOR_ON, <br>
                        &nbsp;&nbsp;PWR_STOPENTRY_WFI);
                    </div>
                    <p><strong>典型功耗:</strong> 10-50 μA</p>
                    <p><strong>唤醒时间:</strong> 5-20 μs</p>
                </div>
            </div>
            
            <div class="card">
                <h2><i class="fas fa-power-off"></i> 待机模式 (Standby)</h2>
                <div class="card-content">
                    <p>仅备份域保持供电,唤醒后系统复位。实现微安级极低功耗。</p>
                    <p><strong>进入方法:</strong></p>
                    <div class="code-block">
                        <span class="code-comment">/* 配置唤醒源 */</span><br>
                        <span class="code-keyword">HAL_PWR_EnableWakeUpPin</span>(<br>
                        &nbsp;&nbsp;PWR_WAKEUP_PIN1);<br><br>
                        <span class="code-comment">/* 清除待机标志 */</span><br>
                        <span class="code-keyword">__HAL_PWR_CLEAR_FLAG</span>(PWR_FLAG_SB);<br><br>
                        <span class="code-comment">/* 进入待机模式 */</span><br>
                        <span class="code-keyword">HAL_PWR_EnterSTANDBYMode</span>();
                    </div>
                    <p><strong>典型功耗:</strong> 1-5 μA</p>
                    <p><strong>唤醒时间:</strong> 100-500 μs</p>
                </div>
            </div>
            
            <div class="card">
                <h2><i class="fas fa-battery-empty"></i> 关机模式 (Shutdown)</h2>
                <div class="card-content">
                    <p>最低功耗模式,仅备份域工作,唤醒后系统复位。实现纳安级功耗。</p>
                    <p><strong>进入方法:</strong></p>
                    <div class="code-block">
                        <span class="code-comment">/* 配置唤醒源 */</span><br>
                        <span class="code-keyword">HAL_PWREx_EnableWakeUpPin</span>(<br>
                        &nbsp;&nbsp;PWR_WAKEUP_PIN1);<br><br>
                        <span class="code-comment">/* 进入关机模式 */</span><br>
                        <span class="code-keyword">HAL_PWREx_EnterSHUTDOWNMode</span>();
                    </div>
                    <p><strong>典型功耗:</strong> 100-500 nA</p>
                    <p><strong>唤醒时间:</strong> 100-500 μs</p>
                    <p class="highlight">仅支持L4/L5/G0/G4/WB等系列</p>
                </div>
            </div>
        </section>

        <section class="power-table-container">
            <h2 class="section-title">
                <i class="fas fa-table"></i> STM32低功耗模式对比
            </h2>
            <table class="power-table">
                <thead>
                    <tr>
                        <th>模式</th>
                        <th>进入方法</th>
                        <th>典型功耗</th>
                        <th>唤醒时间</th>
                        <th>数据保留</th>
                        <th>唤醒后行为</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>睡眠模式 (Sleep)</td>
                        <td>__WFI() / __WFE()</td>
                        <td>1-10 mA</td>
                        <td>&lt;1 μs</td>
                        <td>所有SRAM和寄存器</td>
                        <td>继续执行后续代码</td>
                    </tr>
                    <tr>
                        <td>停止模式 (Stop)</td>
                        <td>HAL_PWR_EnterSTOPMode()</td>
                        <td>10-50 μA</td>
                        <td>5-20 μs</td>
                        <td>所有SRAM和寄存器</td>
                        <td>需重新配置系统时钟</td>
                    </tr>
                    <tr>
                        <td>待机模式 (Standby)</td>
                        <td>HAL_PWR_EnterSTANDBYMode()</td>
                        <td>1-5 μA</td>
                        <td>100-500 μs</td>
                        <td>仅备份寄存器</td>
                        <td>系统复位,从main开始</td>
                    </tr>
                    <tr>
                        <td>关机模式 (Shutdown)</td>
                        <td>HAL_PWREx_EnterSHUTDOWNMode()</td>
                        <td>100-500 nA</td>
                        <td>100-500 μs</td>
                        <td>仅备份寄存器</td>
                        <td>系统复位,从main开始</td>
                    </tr>
                </tbody>
            </table>
            <p style="text-align: center; margin-top: 15px; color: #a0d2ff;">
                ⚠️ 关机模式仅在STM32L4/L5/G0/G4/WB等新系列中可用
            </p>
        </section>

        <h2 class="section-title">
            <i class="fas fa-bolt"></i> 低功耗模式影响对比
        </h2>
        <table class="impact-table">
            <thead>
                <tr>
                    <th>特性</th>
                    <th>睡眠模式</th>
                    <th>停止模式</th>
                    <th>待机模式</th>
                    <th>关机模式</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>CPU 状态</td>
                    <td>停止运行</td>
                    <td>停止运行</td>
                    <td>停止运行</td>
                    <td>停止运行</td>
                </tr>
                <tr>
                    <td>SRAM 内容</td>
                    <td>保留</td>
                    <td>保留</td>
                    <td>丢失</td>
                    <td>丢失</td>
                </tr>
                <tr>
                    <td>寄存器内容</td>
                    <td>保留</td>
                    <td>保留</td>
                    <td>丢失</td>
                    <td>丢失</td>
                </tr>
                <tr>
                    <td>外设状态</td>
                    <td>继续工作</td>
                    <td>大部分关闭</td>
                    <td>全部关闭</td>
                    <td>全部关闭</td>
                </tr>
                <tr>
                    <td>唤醒时间</td>
                    <td>几微秒</td>
                    <td>几十微秒</td>
                    <td>几毫秒</td>
                    <td>几毫秒</td>
                </tr>
                <tr>
                    <td>唤醒源</td>
                    <td>任何中断或事件</td>
                    <td>特定中断(如 EXTI)</td>
                    <td>WKUP 引脚、RTC 闹钟等</td>
                    <td>WKUP 引脚、RTC 闹钟等</td>
                </tr>
                <tr>
                    <td>功耗(典型值)</td>
                    <td>几毫安</td>
                    <td>几微安</td>
                    <td>几十纳安</td>
                    <td>几纳安</td>
                </tr>
                <tr>
                    <td>系统复位</td>
                    <td>否</td>
                    <td>否</td>
                    <td>是</td>
                    <td>是</td>
                </tr>
                <tr>
                    <td>唤醒后执行位置</td>
                    <td>断点处</td>
                    <td>断点处</td>
                    <td>main 函数</td>
                    <td>main 函数</td>
                </tr>
            </tbody>
        </table>

        <section class="best-practices">
            <h2><i class="fas fa-lightbulb"></i> 低功耗设计最佳实践</h2>
            <div class="practice-grid">
                <div class="practice-item">
                    <h3><i class="fas fa-microchip"></i> 引脚配置优化</h3>
                    <p>将所有未使用的GPIO配置为模拟输入模式以最小化漏电流:</p>
                    <div class="code-block">
                        GPIO_InitTypeDef GPIO_InitStruct = {0};<br>
                        GPIO_InitStruct.Pin = GPIO_PIN_All;<br>
                        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;<br>
                        <span class="code-function">HAL_GPIO_Init</span>(GPIOA, &GPIO_InitStruct);
                    </div>
                </div>
                
                <div class="practice-item">
                    <h3><i class="fas fa-clock"></i> 时钟管理策略</h3>
                    <p>进入低功耗前关闭所有不需要的外设时钟:</p>
                    <div class="code-block">
                        <span class="code-keyword">__HAL_RCC_ADC1_CLK_DISABLE</span>();<br>
                        <span class="code-keyword">__HAL_RCC_TIM2_CLK_DISABLE</span>();<br>
                        <span class="code-keyword">__HAL_RCC_USART1_CLK_DISABLE</span>();
                    </div>
                </div>
                
                <div class="practice-item">
                    <h3><i class="fas fa-bell"></i> 唤醒源配置</h3>
                    <p>配置RTC作为周期性唤醒源:</p>
                    <div class="code-block">
                        <span class="code-comment">// 每10秒唤醒一次</span><br>
                        <span class="code-function">HAL_RTCEx_SetWakeUpTimer_IT</span>(<br>
                        &nbsp;&nbsp;&hrtc, 10000, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
                    </div>
                </div>
                
                <div class="practice-item">
                    <h3><i class="fas fa-plug"></i> 电压调节优化</h3>
                    <p>在停止模式中使用低功耗调节器:</p>
                    <div class="code-block">
                        <span class="code-function">HAL_PWREx_ControlVoltageScaling</span>(<br>
                        &nbsp;&nbsp;PWR_REGULATOR_VOLTAGE_SCALE3);
                    </div>
                </div>
            </div>
        </section>

        <div class="mode-summary">
            <div class="mode-box">
                <h3><i class="fas fa-bed"></i> 睡眠模式总结</h3>
                <ul>
                    <li>仅内核停止,外设保持工作</li>
                    <li>唤醒速度最快(&lt;1μs)</li>
                    <li>所有SRAM和寄存器内容保留</li>
                    <li>通过WFI/WFE指令进入</li>
                    <li>任何中断/事件可唤醒</li>
                    <li>功耗相对较高(mA级)</li>
                </ul>
            </div>
            
            <div class="mode-box">
                <h3><i class="fas fa-stop-circle"></i> 停止模式总结</h3>
                <ul>
                    <li>所有时钟停止</li>
                    <li>SRAM和寄存器内容保留</li>
                    <li>唤醒后需重新配置时钟</li>
                    <li>微安级功耗</li>
                    <li>唤醒时间5-20μs</li>
                    <li>支持多种唤醒源</li>
                </ul>
            </div>
            
            <div class="mode-box">
                <h3><i class="fas fa-power-off"></i> 待机模式总结</h3>
                <ul>
                    <li>仅备份域供电</li>
                    <li>唤醒后系统复位</li>
                    <li>SRAM和寄存器内容丢失</li>
                    <li>微安级极低功耗</li>
                    <li>特定唤醒源(WKUP引脚等)</li>
                    <li>唤醒时间较长(ms级)</li>
                </ul>
            </div>
            
            <div class="mode-box">
                <h3><i class="fas fa-battery-empty"></i> 关机模式总结</h3>
                <ul>
                    <li>最低功耗模式(nA级)</li>
                    <li>仅备份域工作</li>
                    <li>唤醒后系统复位</li>
                    <li>仅特定STM32系列支持</li>
                    <li>唤醒源:WKUP引脚、RTC等</li>
                    <li>需要特殊函数进入</li>
                </ul>
            </div>
        </div>

        <h2 class="section-title">
            <i class="fas fa-power-off"></i> 关机模式 (Shutdown) 详解
        </h2>
        
        <div class="note-box">
            <i class="fas fa-exclamation-triangle"></i> 
            <strong>注意:</strong> 关机模式是STM32中功耗最低的模式(纳安级),仅在STM32L4/L5/G0/G4/WB等新系列中可用。
        </div>
        
        <h3 style="color: #00bfff; margin-top: 20px;">
            <i class="fas fa-sign-in-alt"></i> 进入关机模式
        </h3>
        <div class="code-block">
            <span class="code-comment">/* 配置唤醒源(如WKUP引脚) */</span><br>
            <span class="code-keyword">HAL_PWREx_EnableWakeUpPin</span>(PWR_WAKEUP_PIN1);<br><br>
            
            <span class="code-comment">/* 配置RTC唤醒(可选) */</span><br>
            <span class="code-keyword">HAL_RTCEx_SetWakeUpTimer_IT</span>(&hrtc, 0xFFFF, RTC_WAKEUPCLOCK_RTCCLK_DIV16);<br><br>
            
            <span class="code-comment">/* 确保所有数据已保存 */</span><br>
            <span class="code-keyword">HAL_PWREx_EnableBkUpReg</span>();<br>
            <span class="code-keyword">__HAL_RCC_BKP_CLK_ENABLE</span>();<br>
            <span class="code-keyword">WRITE_REG</span>(BKP->DR1, 0xA5A5); <span class="code-comment">// 保存数据到备份寄存器</span><br><br>
            
            <span class="code-comment">/* 进入关机模式 */</span><br>
            <span class="code-keyword">HAL_PWREx_EnterSHUTDOWNMode</span>();
        </div>
        
        <h3 style="color: #00bfff; margin-top: 30px;">
            <i class="fas fa-sign-out-alt"></i> 退出关机模式
        </h3>
        <div class="code-block">
            <span class="code-comment">/* 在main函数中检测唤醒源 */</span><br>
            <span class="code-keyword">int</span> <span class="code-function">main</span>(<span class="code-keyword">void</span>) {<br>
            &nbsp;&nbsp;<span class="code-comment">// 初始化HAL库</span><br>
            &nbsp;&nbsp;<span class="code-function">HAL_Init</span>();<br><br>
            
            &nbsp;&nbsp;<span class="code-comment">// 检测是否从关机模式唤醒</span><br>
            &nbsp;&nbsp;<span class="code-keyword">if</span> (<span class="code-function">__HAL_PWR_GET_FLAG</span>(PWR_FLAG_SB) != RESET) {<br>
            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-comment">// 处理唤醒事件</span><br>
            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-function">__HAL_PWR_CLEAR_FLAG</span>(PWR_FLAG_SB);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-comment">// 恢复备份域设置</span><br>
            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">HAL_PWREx_EnableBkUpReg</span>();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">__HAL_RCC_BKP_CLK_ENABLE</span>();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-comment">// 从备份寄存器恢复数据</span><br>
            &nbsp;&nbsp;&nbsp;&nbsp;uint32_t savedData = <span class="code-function">READ_REG</span>(BKP->DR1);<br>
            &nbsp;&nbsp;}<br><br>
            
            &nbsp;&nbsp;<span class="code-comment">// 系统时钟配置</span><br>
            &nbsp;&nbsp;<span class="code-function">SystemClock_Config</span>();<br>
            &nbsp;&nbsp;<span class="code-comment">// ... 其他初始化代码</span><br>
            }
        </div>
        
        <div class="pros-cons">
            <div class="pros">
                <h3><i class="fas fa-check-circle"></i> 关机模式优点</h3>
                <ul>
                    <li>极低功耗(纳安级)</li>
                    <li>完全断电安全</li>
                    <li>仅备份域保持供电</li>
                    <li>支持多种唤醒源(WKUP引脚,RTC等)</li>
                </ul>
            </div>
            <div class="cons">
                <h3><i class="fas fa-exclamation-triangle"></i> 关机模式缺点</h3>
                <ul>
                    <li>SRAM和寄存器内容丢失</li>
                    <li>唤醒后系统完全复位</li>
                    <li>唤醒时间相对较长</li>
                    <li>仅部分STM32系列支持</li>
                </ul>
            </div>
        </div>
        
        <section class="conclusion">
            <h2><i class="fas fa-star"></i> 设计建议</h2>
            <p>选择适当的低功耗模式需要根据应用需求平衡功耗、唤醒时间和系统状态保留需求。</p>
            <p>对于需要快速响应且保留状态的应用,停止模式是最佳选择。对于超低功耗需求且可以接受系统复位的场景,待机或关机模式更合适。</p>
            <p>使用STM32CubeMX的"Low Power"视图验证配置,并通过STM32CubeMonitor-Power精确测量实际功耗。</p>
        </section>
        
        <div class="footer">
            <p>STM32低功耗模式全面指南 &copy; 2023 | 嵌入式系统开发参考文档</p>
            <p>基于STM32 HAL库和最新参考手册</p>
        </div>
    </div>

    <script>
        // 卡片悬停效果增强
        document.querySelectorAll('.card').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-10px)';
                this.style.boxShadow = '0 15px 35px rgba(0, 100, 200, 0.3)';
            });
            
            card.addEventListener('mouseleave', function() {
                this.style.transform = 'translateY(0)';
                this.style.boxShadow = '0 8px 25px rgba(0, 0, 0, 0.4)';
            });
        });
        
        // 动态更新页面标题
        const originalTitle = document.title;
        let isBlurred = false;
        
        window.addEventListener('blur', () => {
            document.title = "🔋 返回查看STM32低功耗指南!";
            isBlurred = true;
        });
        
        window.addEventListener('focus', () => {
            if(isBlurred) {
                document.title = originalTitle;
                isBlurred = false;
            }
        });
        
        // 表格行高亮效果
        const tableRows = document.querySelectorAll('.impact-table tr');
        tableRows.forEach(row => {
            row.addEventListener('mouseenter', () => {
                row.style.backgroundColor = 'rgba(60, 100, 160, 0.8)';
            });
            
            row.addEventListener('mouseleave', () => {
                const index = Array.from(row.parentNode.children).indexOf(row);
                row.style.backgroundColor = index % 2 === 0 
                    ? 'rgba(25, 45, 80, 0.6)' 
                    : 'rgba(35, 55, 90, 0.6)';
            });
        });
        
        // 自动更新年份
        document.querySelector('.footer p:first-child').innerHTML = 
            `STM32低功耗模式全面指南 &copy; ${new Date().getFullYear()} | 嵌入式系统开发参考文档`;
    </script>
</body>
</html>
相关推荐
小殷学长19 分钟前
【单片机毕业设计17-基于stm32c8t6的智能倒车监测系统】
stm32·单片机·课程设计
Sally璐璐1 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06271 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台1 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue
灿灿121381 小时前
CSS 文字浮雕效果:巧用 text-shadow 实现 3D 立体文字
前端·css
尘心cx2 小时前
前端-CSS-day1
前端·css
花落已飘3 小时前
STM32中实现shell控制台(shell窗口输入实现)
stm32·单片机·嵌入式硬件
花落已飘3 小时前
STM32中实现shell控制台(命令解析实现)
stm32·shell
全宝3 小时前
🎨前端实现文字渐变的三种方式
前端·javascript·css