<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>RTC on 吾思 梦阁</title><link>https://wstek.win/tags/rtc/</link><description>Recent content from 吾思 梦阁</description><generator>Hugo</generator><language>zh-cn</language><managingEditor>wusitech@qq.com (WuSi09)</managingEditor><webMaster>wusitech@qq.com (WuSi09)</webMaster><copyright>本博客所有文章除特别声明外，均采用 BY-NC-SA 许可协议。转载请注明出处！</copyright><lastBuildDate>Fri, 06 Nov 2026 23:59:00 +0800</lastBuildDate><atom:link href="https://wstek.win/tags/rtc/index.xml" rel="self" type="application/rss+xml"/><item><title>STM32 RTC 与 BKP：Unix 时间戳、备份域与掉电走时</title><link>https://wstek.win/posts/stm32-rtc/</link><pubDate>Thu, 05 Nov 2026 19:30:00 +0800</pubDate><author>wusitech@qq.com (WuSi09)</author><guid>https://wstek.win/posts/stm32-rtc/</guid><description>
<![CDATA[<h1>STM32 RTC 与 BKP：Unix 时间戳、备份域与掉电走时</h1><p>作者：WuSi09（wusitech@qq.com）</p>
        
          <p>总结自：<a href="https://www.bilibili.com/video/BV1th411z7sn">STM32入门教程-2023版</a> 第 12 章（12-1 ~ 12-3）。</p>
<p>代码基于 <strong>STM32F103C8T6 + STM32F10x 标准外设库 3.5.0</strong>（API 已按 STM32F10x StdPeriph 3.5 头文件核对；Delay/OLED/Key 为课程配套驱动），不包含新建工程和接线步骤。</p>
<table>
	<thead>
			<tr>
					<th>视频</th>
					<th>视频里讲了什么</th>
					<th>本文对应</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>12-1</td>
					<td>Unix 时间戳、时间换算、2038 问题</td>
					<td>0x10</td>
			</tr>
			<tr>
					<td>12-2</td>
					<td>BKP 简介、RTC 简介、时钟源、预分频、计数器、闹钟、框图、硬件电路</td>
					<td>0x20 ~ 0x50</td>
			</tr>
			<tr>
					<td>12-3</td>
					<td>读写备份寄存器、RTC 初始化、设置/读取时间、显示</td>
					<td>0x60 ~ 0x70</td>
			</tr>
	</tbody>
</table>
<p>视频之外还补了：电池寿命计算、LSE 起振与负载电容、影子寄存器/同步、秒中断与闹钟、时间精度测量、12 条故障排查。</p>
<h2 id="0x00-时间从哪来">
<a class="header-anchor" href="#0x00-%e6%97%b6%e9%97%b4%e4%bb%8e%e5%93%aa%e6%9d%a5"></a>
0x00 时间从哪来
</h2><p>单片机自己不会“知道”现在是几点。要做时钟，需要三样东西：</p>
<ol>
<li><strong>一个稳定的时间基准</strong>：32.768 kHz 晶振；</li>
<li><strong>一个不断累加的计数器</strong>：RTC 的 32 位 CNT；</li>
<li><strong>掉电后继续供电</strong>：VBAT + 纽扣电池。</li>
</ol>
<p>STM32 的 RTC 就在<strong>备份域</strong>里：主电源 VDD 掉电后，VBAT 继续给 RTC 和 BKP 供电，时间不停、备份寄存器不丢。</p>
<h2 id="0x10-unix-时间戳">
<a class="header-anchor" href="#0x10-unix-%e6%97%b6%e9%97%b4%e6%88%b3"></a>
0x10 Unix 时间戳
</h2><p>Unix 时间戳 = 从 <strong>1970-01-01 00:00:00 UTC</strong> 起经过的秒数，用一个 32 位无符号整数存。</p>
        
        <hr><p>本文2026-11-05首发于<a href='https://wstek.win/'>吾思 梦阁</a>，最后修改于2026-11-06</p>]]></description><category>STM32</category></item></channel></rss>