3750 views|3 replies

1140

Posts

0

Resources
The OP
 

The principle and method of generating multiple PWM waveforms using one timer [Copy link]

In many engineering applications, PWM waves (pulse width modulation) are needed, such as motor speed regulation, temperature control and power adjustment. This article describes how to use a timer of a single-chip microcomputer to generate multiple PWM waveforms. Generally, the cycle t1 of PWM is a fixed value, such as 1ms, 10ms, 100ms, etc., and a cycle contains a high level t2 and a low level t3, and their relationship is t2+t3=t1. Changing the time of the high level in a cycle can achieve the purpose of speed or power adjustment. PWM is suitable for the control of high-speed switching devices, but not suitable for the control of low-speed switching elements such as relays, because low-speed elements such as relays cannot achieve such a fast switching speed. The key to PWM control is to control and change the high level time t2 of PWM. This time is modified by the control algorithm in other subroutines, such as PID control algorithm.
Figure 1
In a microprocessor, such as a single-chip microcomputer, the method of implementing a timer to generate multiple PWM is as follows. First, let's take a look at how to use a timer to achieve a PWM output: >>Define a time base scale t, which is also the timer interrupt cycle. Generally, t does not change in the program. The PWM cycle and high and low level time are composed of several base times t; >>Define a global timing variable n, n indicates how many base times t have passed during this period; >>Define a global timing variable n1, n1 indicates how many base times t are included in a PWM cycle, n1=t1/t. Generally, n1 and t1 do not change in the program. >>Define a global timing variable n2, n2 indicates how many base times t the high level contains in a PWM cycle, n2=t2/t. Since the high level time is controlled and changed by other algorithms, n2 is also changing at any time. The program flow is as follows: ①Initialize the timer and set the timer interrupt time to t, such as setting t to 1ms or other; ②Initialize variables: n=0; //Timing variable n is cleared n1=t1/t; //How many base times t are included in a PWM cycle t1. //If the PWM cycle is 100ms, then n1=100; n2=t2/t;//Calculate how many base times //t make up a high level time t2 in a PWM. t2 is changed by other control algorithms. ③ Start timing and turn on the timer interrupt. In the timer interrupt program n++; When n当n" backcolor][="" color][="" n=n2&& n当n" backcolor][="" color][="" size]="">When n1, return to step ① to perform the next cycle of PWM. The program flow chart is as follows:
Figure 2
If you need multiple PWM channels in the same timer, define multiple groups of similar variables and follow the process in Figure 2.

This post is from Microcontroller MCU

Latest reply

The accuracy of such a PWM wave won’t be too high, and the frequency will also be limited, right?  Details Published on 2019-4-13 10:34
 

2618

Posts

0

Resources
2
 
PWM is used in so many places. It is a really good post. The principle of multi-channel PWM is written very clearly.
This post is from Microcontroller MCU
 
 

173

Posts

0

Resources
3
 
Thanks for sharing!
This post is from Microcontroller MCU
 
Personal signature服务器大全
 
 

59

Posts

3

Resources
4
 
The accuracy of such a PWM wave won’t be too high, and the frequency will also be limited, right?
This post is from Microcontroller MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list