<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>PWM on 吾思 梦阁</title><link>https://wstek.win/tags/pwm/</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:10:00 +0800</lastBuildDate><atom:link href="https://wstek.win/tags/pwm/index.xml" rel="self" type="application/rss+xml"/><item><title>STM32 定时器：时基单元、PWM、输入捕获与编码器</title><link>https://wstek.win/posts/stm32f4-timer/</link><pubDate>Thu, 29 Oct 2026 19:30:00 +0800</pubDate><author>wusitech@qq.com (WuSi09)</author><guid>https://wstek.win/posts/stm32f4-timer/</guid><description>
<![CDATA[<h1>STM32 定时器：时基单元、PWM、输入捕获与编码器</h1><p>作者：WuSi09（wusitech@qq.com）</p>
        
          <p>总结自：<a href="https://www.bilibili.com/video/BV1th411z7sn">STM32入门教程-2023版</a> 第 6 章（6-1 ~ 6-8）。</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>0x21</td>
					<td>定时中断、内外时钟源</td>
					<td>6-1、6-2</td>
			</tr>
			<tr>
					<td>0x22</td>
					<td>输出比较</td>
					<td>6-3</td>
			</tr>
			<tr>
					<td>0x23</td>
					<td>PWM、舵机、直流电机</td>
					<td>6-3、6-4</td>
			</tr>
			<tr>
					<td>0x24</td>
					<td>输入捕获、测频、PWMI</td>
					<td>6-5、6-6</td>
			</tr>
			<tr>
					<td>0x25</td>
					<td>编码器接口、测速</td>
					<td>6-7、6-8</td>
			</tr>
	</tbody>
</table>
<h2 id="0x00-什么是定时器timertim">
<a class="header-anchor" href="#0x00-%e4%bb%80%e4%b9%88%e6%98%af%e5%ae%9a%e6%97%b6%e5%99%a8timertim"></a>
0x00 什么是定时器(Timer,TIM)
</h2><p>定时器可以对输入的时钟进行计数，并在计数值达到设定值时触发中断或者事件。</p>
<p>定时器按复杂度和应用场景分为高级定时器、通用定时器和基本定时器，如图</p>
<p><a class="article-gallery-item" href="/images/obsidian/Pasted%20image%2020261105193123.webp"><img src="/images/obsidian/Pasted%20image%2020261105193123.webp" alt="STM32 定时器分类：高级/通用/基本" loading="lazy" decoding="async" onload="this.parentNode.dataset.pswpWidth=this.naturalWidth;this.parentNode.dataset.pswpHeight=this.naturalHeight"></a>
</p>
<p>注意：STM32F103C8T6 只有 <strong>TIM1 ~ TIM4</strong>（1 个高级 + 3 个通用），没有基本定时器 TIM6/TIM7。</p>
<p>除了“数时间”，定时器还能做这些事：</p>
<ul>
<li><strong>定时中断</strong>：固定周期进中断，做软件定时、采样节拍；</li>
<li><strong>输出比较 / PWM</strong>：驱动 LED、舵机、直流电机、蜂鸣器；</li>
<li><strong>输入捕获</strong>：测频率、测周期、测占空比、测脉宽；</li>
<li><strong>编码器接口</strong>：直接读旋转编码器的正交信号，做位置/速度闭环；</li>
<li><strong>主从触发</strong>：定时器级联、硬件自动触发 ADC/DAC，不占 CPU。</li>
</ul>
<h2 id="0x10-定时器的组成">
<a class="header-anchor" href="#0x10-%e5%ae%9a%e6%97%b6%e5%99%a8%e7%9a%84%e7%bb%84%e6%88%90"></a>
0x10 定时器的组成
</h2><h3 id="0x11-基本定时器">
<a class="header-anchor" href="#0x11-%e5%9f%ba%e6%9c%ac%e5%ae%9a%e6%97%b6%e5%99%a8"></a>
0x11 基本定时器
</h3><p>基本定时器是结构最简单的定时器，由<strong>时基单元、时钟信号和触发控制器</strong>构成。如图</p>
        
        <hr><p>本文2026-10-29首发于<a href='https://wstek.win/'>吾思 梦阁</a>，最后修改于2026-11-06</p>]]></description><category>STM32</category></item></channel></rss>