What is Pulse Width Modulation? Using buttons to control the PWM duty cycle

Publisher:温馨幸福Latest update time:2024-08-21 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

What is Pulse Width Modulation?

Let's first bring out a concept: Pulse Width Modulation (English: Pulse Width Modulation, abbreviated as PWM), abbreviated as PWM, is a technology that converts analog signals into pulses. Generally, the period of the pulse after conversion is fixed, but the duty cycle of the pulse will change according to the size of the analog signal.


Then everyone will definitely ask, what is duty cycle? In fact, duty cycle refers to the ratio of the effective level to the entire output signal. What role does PWM play in practical applications? Let's take the breathing light as an example to demonstrate the specific usage and practical role of PWM.


Detailed program explanation

First, configure the timer and set it to interrupt once every 100us. At the same time, in the main function, we change the duty cycle of PWM (the value of OUT here) from 0 to 100, and then from 100 to 0. The 50MS delay controls the frequency of the light on and off. You can also use buttons to control the duty cycle of PWM to achieve brightness control.

picture

In the timer interrupt function, we set a count bit, which increases from 0 to 100. When the count bit is greater than the set duty cycle OUT, it outputs a high level (P1=0xFF), and when it is less than the set value OUT, it outputs a low level (P1=0X00). Then the frequency of the PWM wave is 1s/(100us*100)=100Hz, (1s divided by each interrupt time*count).

picture

At this time, the output of the IO port is actually a waveform composed of high and low levels as shown in the figure. One high level plus one low level is one cycle.

picture

Effect display

In actual operation, the LED light gradually becomes brighter and then gradually dims. If you adjust the rate of change, you will have new discoveries. You can give it a try.

The above is the method of using the timer to generate PWM waves. Now many newer microcontrollers have built-in PWM outputs, such as stc12c5a60s2, stm32, etc. The configuration methods are different, but they can all achieve PWM output and control. PWM is a very common tool and has a wide range of applications in lighting and motor control. If you are not satisfied with a simple breathing light, you can also control the motor through a motor driver and use PWM to achieve motor speed regulation. I hope you can master the use of PWM through hands-on practice.

1.jpg

1.jpg


Reference address:What is Pulse Width Modulation? Using buttons to control the PWM duty cycle

Previous article:How to decarbonize the smart factory
Next article:Learn PWM in a blink

Latest Embedded Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号