当前位置:网站首页>stm32 mbed 入门教程(四)---PWM
stm32 mbed 入门教程(四)---PWM
2022-06-29 13:54:00 【螺丝工人】
目录
三.How to use in mbed(应用在mbed上)
一.Introduction(介绍)
PWM全称Pulse Width Modulation(脉冲宽度调制),顾名思义,就是通过调节脉冲宽度于外围设备进行控制交互。PWM的用途非常的广泛,例如:无源蜂鸣器放歌,调节电脑屏幕的亮度,调节风扇的转速,舵机的转动等等...... PWM的底层原理的内容非常的细致杂多,但是本文的侧重点在于如何在mbed程序中使用pwm并且实现一些功能。
二.Principle(原理)
这里我们从初中学过的知识进行引入,在一个电路中我们要调节LED的亮度(本质就是分给LED的电压的大小),相对简单的方法就是在电路中加入一个滑动变阻器,通过调节滑动变阻器进行分压,从而调节分给LED的电压。

那么PWM是怎么对LED进行调节的呢?我们来看电路图:

从电路中我们可以发现,我们取消了对滑动变阻器的使用, 但是在电路中多了一个开关。开关的作用大家都清楚,控制电路的通断,那么我们假设以四秒为一个周期,前两秒电路通,后两秒电路断,那么这个电路前两秒led是亮的,后两秒led是暗的,那么如果我们算这4s的平均分给led的电压那就是正常分给led电压的一半。(当然,这里我们设置的单位周期时间比较长,不足及说进行led的亮度调节)那么假如我们把这段电压的变化周期不断变小,假设一个周期为4us,前2us是亮的,后2us是暗的(此时人眼已经在一个周期的时间内观察不出来亮暗的变化)那么这个时候我们在4s内观察现象,会发现led是常亮的,(这4s内在LED上的电压我们可以就认为是一半的正常电压)但是亮度是正常亮度的一半。

上图中的黄色曲线就是脉冲图,高位代表高电平,低位代表低电平。在一个周期中,高电平持续的时间就是我们所说的Pulse width(脉冲宽度),高电平在周期中所占的比例成为Duty cycle(占空比)。


三.How to use in mbed(应用在mbed上)
整体来说,PWM的使用可以说是非常简单的。根据上文的原理,我们可以知道要确定一个属性的pwm我们只需要两个必要因素:①period(周期)②duty cycle(占空比)。接下来我们来看怎么用mbed库语言来实现。

首先我们需要定义一个PwmOut的引脚作为mbed上pwm的输出端口。 那么mbed上哪些是可以进行PwmOut输出的呢?

我们通过看mbed的结构图可以得知,如果引脚属性中有紫色PWM的则可以进行pwm输出。
接下来我们通过官方给的案例代码进行解读:
/*
* Copyright (c) 2014-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
// Adjust pin name to your board specification.
// You can use LED1/LED2/LED3/LED4 if any is connected to PWM capable pin,
// or use any PWM capable pin, and see generated signal on logical analyzer.
PwmOut led(LED1);
int main()
{
// specify period first
led.period(4.0f); // 4 second period
led.write(0.50f); // 50% duty cycle, relative to period
//led = 0.5f; // shorthand for led.write()
//led.pulsewidth(2); // alternative to led.write, set duty cycle time in seconds
while (1);
}
这里是指定mbed上的LED1对应的引脚进行PWM输出(并且命名为led),接下来在主函数中,首先是led.period(4.0f);这里是定义led的周期为4s(浮点数),接下来led.write(0.5f);定义led的占空比为0.50f,说明一半高电平,一半低电平。如果我们会发现LED1亮两秒暗两秒。同理:如果我们希望亮一秒,暗三秒,只需要调节占空比为0.25。那么如果我向让LED1一直亮,且亮度为原来亮度的一半,这个时候改变period即可。
四.Summary(总结)
PWM的基本使用是不难的,就是两个主要构成,周期和占空比。通过本文的初步介绍,感兴趣的话可以去自己动手利用mbed做一些有趣的东西,比如利用pwm让蜂鸣器放一首你喜欢的歌,利用pwm让舵机转动,甚至是调节小车的运动速度。
边栏推荐
- redis 分片集群搭建与使用教程
- BYD has three years left
- [high concurrency] cache idea
- 【重要通知】中国图象图形学学会2022年度系列奖励推荐工作启动
- 直觉与实现:Batch Normalization
- Game development of contract quantitative trading system (ready-made case analysis)
- Characteristics of human immaturity
- [high concurrency] 28000 words' summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little drifting (Part 1)
- 数字IC手撕代码--交通灯
- 用手机在指南针上开户靠谱吗?这样炒股有没有什么安全隐患
猜你喜欢

【置顶】博客使用须知,公告板,留言板,关于博主
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection

Stable currency risk profile: are usdt and usdc safe?

分布式唯一 ID 生成方案浅谈

Recruiting talents and seeking development | Jincang of the National People's Congress won the "best employer school recruitment case Award"

Uncover the practice of Baidu intelligent test in the field of automatic test execution

vmware虚拟机的作用

Summary of binary tree exercises

浅析 Istio——可观测性

留给比亚迪的时间还有三年
随机推荐
Redis哨兵机制原理详解
Thanos Store 组件
吐血整理:一份不可多得的架构师图谱!
tcpdump如何对特定的tcp标志位进行过滤
内网穿透(nc)
动荡的中介生意,不安的租房人
[network bandwidth] Mbps & Mbps
Wechat applet: Yunkai publishes white wall wechat applet source code download server free and domain name support traffic main revenue
Normalization layer of pytorch learning (batchnorm, layernorm, instancenorm, groupnorm) [easy to understand]
Redis master-slave replication principle
Wechat applet: Halloween avatar box generation tool
微信小程序:修复采集接口版云开发表情包
Redis' data expiration clearing strategy and memory obsolescence strategy
Go unit testing introductory practice
东莞虎门券商公司股票开户哪个更好更安全?
uniApp问题清单与经验
Interview high concurrent, cool!! (high energy in the whole process, collection recommended)
【Qt 教程】QPushButton 按键和双击效果
BYD has three years left
[high concurrency] cache idea