当前位置:网站首页>Stm32 mbed tutorial (IV) --pwm
Stm32 mbed tutorial (IV) --pwm
2022-06-29 14:26:00 【Screw worker】
Catalog
One .Introduction( Introduce )
3、 ... and .How to use in mbed( Apply to mbed On )
One .Introduction( Introduce )
PWM Full name Pulse Width Modulation( Pulse width modulation ), seeing the name of a thing one thinks of its function , It is to control and interact with peripheral devices by adjusting the pulse width .PWM It is widely used , for example : The passive buzzer plays a song , Adjust the brightness of the computer screen , Adjust the fan speed , Steering gear rotation, etc ...... PWM The content of the underlying principle of is very detailed and miscellaneous , But the focus of this article is how to mbed Used in program pwm And realize some functions .
Two .Principle( principle )
Here we introduce the knowledge we have learned in junior high school , In a circuit we have to adjust LED The brightness of ( The essence is to give to LED The magnitude of the voltage ), The relatively simple method is to add a Slide rheostat , Divide the voltage by adjusting the sliding rheostat , So as to adjust the distribution LED The voltage of .

that PWM How is it right LED For adjustment ? Let's look at the circuit diagram :

From the circuit we can find , We have eliminated the use of sliding rheostat , But there is a switch in the circuit . Everyone knows the function of the switch , On / off of control circuit , So let's assume a period of four seconds , In the first two seconds, the circuit is open , The circuit is disconnected in the next two seconds , So the first two seconds of this circuit led It's bright , Two seconds later led It's dark , So if we count this 4s The average score of led That's the normal distribution led Half the voltage .( Of course , The unit cycle time we set here is relatively long , Not enough to say led Brightness adjustment of ) So if we reduce the change period of this voltage , Suppose a period is 4us, front 2us It's bright , after 2us It's dark ( At this time, the human eye has been unable to observe the light and dark changes in a period of time ) So at this time we are 4s Internal observation phenomenon , Will find led It is always on ,( this 4s Inside LED The voltage on the can be considered as half of the normal voltage ) But the brightness is half the normal brightness .

The Yellow curve in the above figure is the pulse diagram , High level represents high level , Low level means low level . In a cycle , The duration of high level is what we call Pulse width( Pulse width ), The proportion of high level in the cycle becomes Duty cycle( Duty cycle ).


3、 ... and .How to use in mbed( Apply to mbed On )
As a whole ,PWM It is very simple to use . According to the above principle , We can know that to determine an attribute pwm We only need two necessary factors :①period( cycle )②duty cycle( Duty cycle ). Next, let's see how to use mbed Library language .

First we need to define a PwmOut The pin of is used as mbed On pwm Output port of . that mbed What can be done PwmOut What about the output ?

Let's look at mbed You can see from the structure diagram of , If there is purple in the pin attribute PWM Can be carried out pwm Output .
Next, we will interpret the case code given by the official :
/*
* 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);
}
Here is the designation mbed Upper LED1 The corresponding pin PWM Output ( And named it led), Next in the main function , First of all led.period(4.0f); Here is the definition led The period of is 4s( Floating point numbers ), Next led.write(0.5f); Definition led The duty cycle of is 0.50f, Indicates half of the high level , Half low level . If we find LED1 Two seconds on and two seconds off . Empathy : If we want to light up for a second , Dark three seconds , Just adjust the duty cycle to 0.25. Well, if I ask Jean LED1 Always on , And the brightness is half of the original brightness , This time changes period that will do .
Four .Summary( summary )
PWM The basic use of is not difficult , There are two main components , Cycle and duty cycle . Through the preliminary introduction of this article , If you are interested, you can use it yourself mbed Do something interesting , Such as the use of pwm Give Way Buzzer Play a song you like , utilize pwm Give Way The steering gear turn , Even adjust the speed of the car .
边栏推荐
- 灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs
- 动荡的中介生意,不安的租房人
- 微信小程序:全新獨家雲開發微群人脈
- Redis fragment cluster setup and use tutorial
- 一次mysql的.ibd文件过大处理过程记录
- Industry analysis - quick intercom, building intercom
- 布隆过滤器Bloom Filter简介
- 喜迎市科协“十大”•致敬科技工作者 | 卢毅:守护电网传输安全的探索者
- Why is redis so fast? Is redis single threaded or multi-threaded?
- 【烹饪记录】--- 酸辣白菜
猜你喜欢

BYD has three years left

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

“死掉”的诺基亚,一年躺赚1500亿

Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout

微信小程序:(更新)云开发微群人脉

"Dead" Nokia makes 150billion a year
![[dark horse morning post] the market value of China public education has evaporated by more than 200billion; New Oriental has more than 20million live fans; HM closes its first store in China; Vanke Y](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] the market value of China public education has evaporated by more than 200billion; New Oriental has more than 20million live fans; HM closes its first store in China; Vanke Y

Wechat applet: install B artifact and P diagram, modify wechat traffic main applet source code, Download funny joke diagram, make server free domain name

Istio网格中访问外部服务方法

Crazy digital collections, the next myth of making wealth?
随机推荐
Stable currency risk profile: are usdt and usdc safe?
微信小程序:云开发表白墙微信小程序源码下载免服务器和域名支持流量主收益
Dynamics 365Online Lookup查找字段多选
Nuscenes configuration information about radar
关于MongoDB报错:connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
【关联分析实战篇】为什么 BI 软件都搞不定关联分析
【blackduck】jenkins下配置指定的synopsys-detect扫描版本
Transport layer user datagram protocol (UDP)
How goby exports scan results
Redis' data expiration clearing strategy and memory obsolescence strategy
Characteristics of human immaturity
【shell】jenkins shell实现自动部署
《canvas》之第5章 文本操作
疯狂的数字藏品,下一个造富神话?
传输层 用户数据报协议(UDP)
Istio网格中访问外部服务方法
Wechat applet: Yunkai publishes white wall wechat applet source code download server free and domain name support traffic main revenue
goby如何导出扫描结果
微信小程序:全新独家云开发微群人脉
中康控股开启招股:拟募资净额3.95亿港元,预计7月12日上市