Learn PWM in a blink

Publisher:CelestialLightLatest update time:2024-08-21 Source: elecfansKeywords:PWM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The so-called PWM is to use the ratio of high-level time and waveform period to represent a number. If this number becomes a nearly continuous curve after filtering, it is DA conversion; if the voltage to control the motor is expressed by this ratio, then it is motor control. Everything that can be quantified can be expressed in PWM, which is why PWM is widely used.


Methods for generating PWM:

1. Generate a counter that rolls over after a full pulse period.

2. Generate a comparator, which outputs a high level when it reaches the proportional position, otherwise it outputs a low level. The whole process is shown in the figure below.

picture

FPGA is essentially a digital circuit. In order to generate the results in the above figure, we need to design two circuits according to the above two-step process. The first circuit is a counter, and the second circuit is a comparator behind the counter, which monitors the counter output. Once it is higher than the count value corresponding to the duty cycle, it becomes 0, and it is 1 at other times. In this way, PWM is basically realized. This corresponds to the following two pieces of code:

//Counter circuit, cnt_cycle is the counter output count result always@(posedge clk)

begin

cnt_cycle <= (cnt_cycle == (T-1)) ? 0 : cnt_cycle + 1;

end

/ Comparator circuit, pwm_buf is the output PWM result, the red word is the counter input, and the green word is the comparator comparison line /

always@(posedge clk)

begin

pwm_buf <= (cnt_cycle < duty_cycle_buf) ? 1'b1 : 1'b0;

end

In other words, the circuit corresponding to the above two codes is as follows:

picture

Regarding the above circuit, I would like to make a point: the implementation of the counter in FPGA is composed of an adder and a trigger. The adder outputs +1 each time, and the +1 result is given to the output terminal at the next clock. This is not the result of asynchronous 2-frequency division in the digital electronic textbook at all! In addition, I would like to emphasize again that due to the limitations of practical factors in the design of FPGA, some theoretical models in the digital electronic textbook are difficult to apply in large quantities in practice, so many structures and comprehensive results inside FPGA are different from those in the digital electronic textbook, but the basic principles of digital circuits have not changed at all!

There is one last point missing for PWM, because the input duty cycle can change continuously, that is, the comparison line in the first picture will move up and down. In order to avoid affecting the sudden value collected during the counting process, which will eventually lead to an unsmooth duty cycle, the duty cycle input will be collected only after each counting cycle is completed. This is what you see:

always@(posedge clk)

begin

if(cnt_cycle == (T-1)) duty_cycle_buf <= (duty_cycle > T-1) ? T-1 : duty_cycle;

end

Only when cnt_cycle counts a period T, the duty cycle value will be updated, that is, the comparison line will move!


Keywords:PWM Reference address:Learn PWM in a blink

Previous article:What is Pulse Width Modulation? Using buttons to control the PWM duty cycle
Next article:Explanation of three algorithms for PLC programming

Recommended ReadingLatest update time:2024-11-14 22:26

Why does a multimeter see a PWM signal when measuring a sensor using the voltage range?
There is a position sensor that measures position changes. When the output signal of the sensor is measured with the voltage range of a multimeter, the result shows an analog signal, that is, the position and the signal output are linearly related. However, when the output signal of the sensor is measured with an osci
[Test Measurement]
Why does a multimeter see a PWM signal when measuring a sensor using the voltage range?
PWM complementary output dead time setting for motor drive
1. Complementary PWM output Figure 1 below: Q1 and Q2 MOS tubes form a bridge arm, and the two tubes cannot be turned on at the same time. So, if both tubes are controlled by PWM waves, the two PWM waveforms have opposite polarities. The waveforms in the red circle in Figure 1 have opposite high and low levels.
[Embedded]
PWM complementary output dead time setting for motor drive
STM32 Learning Notes (5): General Timer PWM Output
1. Basic concepts of TIMER output PWM   Pulse Width Modulation (PWM), short for "Pulse Width Modulation", is a very effective technology that uses the digital output of a microprocessor to control analog circuits. To put it simply, it is the control of pulse width. It is generally used to control the speed of a step
[Microcontroller]
AVR microcontroller PWM speed regulation program and schematic diagram
Source program: #include iom16v.h #include macros.h #define uint unsigned int #define uchar unsigned char #define Left OCR1A #define Right OCR1B #define ADD 1.15; //macro definition, acceleration coefficient flying i; uchar flag; //Whether to decelerate flag float TCRT5000_IN; uint Max_Speed; uchar Min_Speed;
[Microcontroller]
STM32 general-purpose timer realizes output of two complementary PWMs with adjustable duty cycle and frequency
MCU:STM32F334C8T6 PWM, or pulse width modulation, can be used to drive motors, full-bridge circuits, etc. Those who have used STM32 know that its timer can easily achieve PWM output, and the advanced timer's TIMx_CHy and TIMx_CHyN can easily achieve the output of complementary PWM waveforms. Advanced timer resources a
[Microcontroller]
STM32 general-purpose timer realizes output of two complementary PWMs with adjustable duty cycle and frequency
[STC15 library function hands-on notes] 7. PCA and PWM
STC Experiment Box 4 IAP15W4K58S4 Keil uVision V5.29.0.0 PK51 Prof.Developers Kit Version:9.60.0.0 Hard Knowledge Hardware knowledge        Excerpted from "STC15 Series MCU Device Manual"        Some STC15 series MCUs integrate 3-channel programmable counter array (CCP/PCA) modules (STC15W4K32S4 series MCUs have onl
[Microcontroller]
[STC15 library function hands-on notes] 7. PCA and PWM
STM32 timer --- master-slave mode triggers PWM phase shift
In this article, the trigger mode is used instead of the gate mode. code show as below: void TIM3_PWMShiftInit(void)   {       TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;       GPIO_InitTypeDef  GPIO_InitStruct;       TIM_OCInitTypeDef TIM_OCInitStruct;         /**********************TIM3 GPIO configuration**
[Microcontroller]
STM32 timer --- master-slave mode triggers PWM phase shift
Using encoder (control) to output PWM based on MC9S12G
First, the principle of the encoder A and B correspond to two photosensitive receiving tubes. The distance S2 between AB is smaller than S0 and S1. Therefore, when rotating clockwise, the initial values ​​of A and B are 1, 1. A will first enter the S0 area on the left side of the initial position, that is, the value
[Microcontroller]
Using encoder (control) to output PWM based on MC9S12G
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号