Documentos de Académico
Documentos de Profesional
Documentos de Cultura
►Introduction
Pulse width Modulation or PWM is one of the powerful techniques used in control systems
today. They are not only employed in wide range of control application which includes: speed
control, power control, measurement and communication. This tutorial will take you through the
PWM basics and implementation of PWM on 8051 and AVR microcontrollers.
Pulse-width Modulation is achived with the help of a square wave whose duty cycle is changed
to get a varying voltage output as a result of average value of waveform. A mathematical
explaination of this is given below.
Ton is the time for which the output is high and Toff is time for which output is low. Let Ttotal be
time period of the wave such that,
This was all about theory behind PWM. Now lets take a look at the practical implementation of
PWM on microcontrollers.
The basic idea behind PWM implementation on 8051 is using timers and switching port pin
high/low at defined intervals. As we have discussed in the introduction of PWM that by changing
the Ton time, we can vary the width of square wave keeping same time period of the square wave.
We will be using 8051 Timer0 in Mode 0. Values for high and low level will be loaded in such a
way that total delay remains same. If for high level we load a value X in TH0 then for low level
TH0 will be loaded with 255-X so that total remains as 255.
In your main program you need to call this PWM_SETUP routine and your controller will have a
PWM output. Timer Interrupt service routine will take care of PWM in the background. The
width of PWM can be changed by changing the value of R7 register. In above example I am
using 160, you can choose any value from 0 to 255. R7 = 0 will give you o/p 0V approx and R7
= 255 will give you 5V approx.
You can also make use of Timer1 if you want. And the output pin can be changed to whatever
pin you want.
►C Code Example
void pwm_setup(){
TMOD = 0;
pwm_width = 160;
EA = 1;
ET0 = 1;
TR0 = 1;
}