Beruflich Dokumente
Kultur Dokumente
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
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:
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.
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
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. }
PWM10
Description
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
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
Note: X can be A or B
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
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)