Sie sind auf Seite 1von 7

Pulse Width Modulation

Home (/) / Rikipedia (/wiki/) / Pulse Width Modulation

From Rikipedia Embedded Wiki

Page (/wiki/Pulse_Width_Modulation)
Discussion (/mediawiki/index.php?title=Talk:Pulse_Width_Modulation&action=edit&redlink=1)

Edit
Nav

Pulse width Modulation or PWM is one of the powerful techniques used in control systems today. It is
used in wide range of application which includes: speed control, power control, measurement and
communication. This tutorial will take you through basics of Pulse width modulation and its
implementation on microcontrollers.

Table of Content

Basic Principal of PWM


Period
Duty Cycle
PWM: Voltage Regulation
Implementing PWM on 8051
Assembly Code Example
C Code Example
AVR Timers as PWM
AVR: Assembly Code Example
AVR: PWM Setup in C
Further Reading
Help & Queries

Basic Principal of PWM


Pulse width modulation is basically a square wave with a varying high and low time. A basic PWM signal is
shown in the gure below.

Pulse width modulation wave


There are various terms associated with PWM:
1. On-Time: Duration of time signal is high
2. O -Time: Duration of time signal is low
3. Period: It is represented as the sum of on-time and o -time of PWM signal
4. Duty cycle: It is represented as percentage of time signal remains on during the period of the PWM
signal

Period
As shown in the the gure, Ton denotes the on-time and To denotes the o time of signal. Period is the
sum of both on and o times and is calculated as shown in the equation below:

Duty Cycle
Duty cycle is calculated as on-time to the period of time. Using the period calculated above, duty cycle is
calculated as:

PWM: Voltage Regulation


PWM signal when used at a di erent duty cycles gives a varying voltage at the output. This method is used
in various areas of application like:
Switching regulators
LED dimmers
Audio
Analog signal generation
and many more...
Voltage regulation is done by averaging the PWM signal. Output voltage is represented by the following
equation:

As you can see from the equation the output voltage can be directly varied by varying the Ton value.
If Ton is 0, Vout is also 0. if Ton is Ttotal then Vout is Vin or say maximum.

Implementing PWM on 8051


The basic idea behind PWM implementation on 8051 is using timers and switching port pin high/low at
de ned 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.

Assembly Code Example


Timer setup for PWM
1.

PWMPINEQUP1.0;PWMoutputpin

2.

PWM_FLAGEQU0 ;Flagtoindicatehigh/lowsignal

3.
4. PWM_SETUP:
5.

MOVTMOD,#00H;Timer0inMode0

6.

MOVR7,#160

7.

;ThevalueloadedinR7isvalueXas

8.

;discussedabove.

9.

SETBEA;EnableInterrupts

;Setpulsewidthcontrol

10.

SETBET0;EnableTimer0Interrupt

11.

SETBTR0;StartTimer

12.

RET

Interrupt Service Routine


As we are using Timer0 for generating PWM, timer interrupt service routine uses PWM_FLAG bit to selects
the high and low section of PWM signal. When timer over ows, PWM_FLAG is checked. If ag is set timer is
loaded with timer value for low time and if ag is cleared timer is loaded with high time value.

1. TIMER_0_INTERRUPT:
2.

JBPWM_FLAG,HIGH_DONE ;IfPWM_FLAGflagissetthenwejustfinished

3.

;thehighsectionofthe

3.

;thehighsectionofthe

;cyclesoJumptoHIGH_DONE

SETBPWM_FLAG

;MakePWM_FLAG=1toindicatestartofhighsecti

6.

SETBPWMPIN

;MakePWMoutputpinHigh

7.

MOVTH0,R7

;LoadhighbyteoftimerwithR7

8.

;(pulsewidthcontrolvalue)

9.

4. LOW_DONE:
5.
on

CLRTF0

;CleartheTimer0interruptflag

10.

RETI

;ReturnfromInterrupttowhere

11.

;theprogramcamefrom

CLRPWM_FLAG

;MakePWM_FLAG=0toindicatestartoflowsectio

14.

CLRPWMPIN

;MakePWMoutputpinlow

15.

MOVA,#0FFH

;MoveFFH(255)toA

16.

CLRC

;ClearC(thecarrybit)soitdoes

17.

;notaffectthesubtraction

18.

SUBBA,R7

;SubtractR7fromA.A=255R7.

19.

MOVTH0,A

;sothevalueloadedintoTH0+R7=255

20.

CLRTF0

;CleartheTimer0interruptflag

21.

RETI

;ReturnfromInterrupttowhere

22.

;theprogramcamefrom

12. HIGH_DONE:
13.
n

As everything is handled in ISR. so to stop PWM you can simply disable the timer.
1. PWM_STOP:
2.

CLRTR0

3.

RET

;StoptimertostopPWM

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
As explained in assembly example, the same code can be implemented in C.

1. /*Globalvariablesanddefinition*/
2. #definePWMPINP1_0
3.
4. unsignedcharpwm_width;

4. unsignedcharpwm_width;
5. bitpwm_flag=0;
6.
7. voidpwm_setup()
8. {
9.

TMOD=0;

10.

pwm_width=160;

11.

EA=1;

12.

ET0=1;

13.

TR0=1;

14. }
15.
16. /*Timer0Interruptserviceroutine*/
17. voidtimer0()interrupt1
18. {
19.

if(!pwm_flag){
/*StartofHighlevel*/

20.

pwm_flag=1;

/*Setflag*/

21.

PWMPIN=1;

22.

TH0=pwm_width;
/*Loadtimer*/

23.

TF0=0;

24.

}else{

25.

pwm_flag=0;

/*Clearflag*/

26.

PWMPIN=0;

27.

TH0=255pwm_width; /*Loadtimer*/

28.

TF0=0;

29.

/*SetPWMo/ppin*/

/*Clearinterruptflag*/
/*StartofLowlevel*/
/*ClearPWMo/ppin*/

/*ClearInterruptflag*/

30. }
31.
32. voidpwm_stop()
33. {
34.

TR0=0;

/*DisabletimertodisablePWM*/

35. }

AVR Timers as PWM


Most of AVR controllers have onchip PWM channel which makes PWM usage much simple and more
accurate. AVR timers or counters can be used in PWM mode without disturbing the basic timer function.
As in case of AT90S8515, Timer1 can be con gured in PWM mode by setting PWM11 and PWM10 bits in
TCCR1A register. Following modes are available for PWM:
PWM11

PWM10

Description

PWM operation of Timer/Counter1 is disabled

Timer/Counter1 in 8-bit PWM Mode

Timer/Counter1 in 9-bit PWM Mode

Timer/Counter1 in 10-bit PWM Mode

The pre-scalar source for Timer/Counter1 can be selected with the help of clock select bits in TCCR1B

The pre-scalar source for Timer/Counter1 can be selected with the help of clock select bits in TCCR1B
register (more information please check datasheet at page 37).
Width of pulse is loaded in the timer output compare registers OCR1A (OCR1AH & OCR1AL) and OCR1B
(OCR1BH & OCR1BL). Timer/Counter1 acts as an up/down counter, counting up from $0000 to TOP (see
table below), where it turns and counts down again to zero before cycle is repeated. When the counter
value matches the content of 10 least signi cant bits of OCR1A or OCR1B, the PD5 (OC1A)/OC1B pins are
set or cleared according to the settings of COM1A1/COM1A0 or COM1B1/COM1B0 bits in Timer/Counter1
Control register (TCCR1A), see table below.
PWM Resolution

Timer Top Value

Frequency

8-bit PWM

$00FF (255)

Ftck1/510

9-bit PWM

$01FF (511)

Ftck1/1022

10-bit PWM

$03FF (1023)

Ftck1/2046

COM1X1

COM1X0

E ect on OCX1

Not Connected

Not Connected

Cleared on compare match, up-counting. Set on compare match down-counting


(non-inverted PWM)

Cleared on compare match, down-counting. Set on compare match up-counting


(inverted PWM)

Note: X can be A or B

AVR: Assembly Code Example

1. ;8bitNonInvertedPWMcodeexample
2. .equpulse_width=$40
3. ;Pulsewidthcanbechangedfrom0toTOP
4. PWM_START:
5.

lditemp,pulse_width

;Loadpulsewidth

6.

outOCR1AL,temp
;OCR1A=Pulsewidth

7.

clrtemp

8.

outOCR1AH,temp

9.
10.

lditemp,$81

;8bitPWMMode

11.

outTCCR1A,temp
;NonInverted

12.
13.

intemp,DDRD

14.

oritemp,(1<<5)

15.

outDDRD,temp

;MakePortD.5aso/p

;StartPWM

;Returntomain

16.
17.

lditemp,$1

18.

outTCCR1B,temp

19.

ret

20.

;PWMwillruninbackgroundautomatically

AVR: PWM Setup in C


1. /*Globalvariablesanddefinition*/
2. #definePULSE_WIDTH0x40
3.
4. voidpwm_start()
5. {
6.

OCR1AL=PULSE_WIDTH;

7.

OCR1AH=0;

8.

DDRD|=(1<<5);

/*PortD.5aso/p*/

9.

TCCR1A=0x81;

/*8bit,NonInvertedPWM*/

TCCR1B=1;

/*StartPWM*/

10.

/*LoadPulsewidth*/

11. }
Most of AVRs have same set of registers for PWM setup. Just go through the datasheet once and con gure
registers. If you still feel any problem working on things, we are here to help. But please make use of
forum (http://www.8051projects.net/forum.html)

Further Reading
I2C/TWI Tutorial (/wiki/I2C_TWI_Tutorial)
8051 Tutorials (/wiki/Category:8051_Tutorials)
AVR Tutorials (/wiki/Category:AVR_Tutorials)

Help & Queries


If you have any queries, doubts or feedback on this tutorial please share in our discussion forum
(http://www.8051projects.net/forum.html). If you want us to write tutorial for more devices please let us
know in the forum.
Retrieved from "http://www.8051projects.net/mediawiki/index.php?
title=Pulse_Width_Modulation&oldid=205 (http://www.8051projects.net/mediawiki/index.php?
title=Pulse_Width_Modulation&oldid=205)"

Das könnte Ihnen auch gefallen