banner
Home Microcontrollers Abbreviations Useful Tools Gallery hot links feedback
 

PWM or Pulse Width Modulation is an important and essential concept in embedded systems. We can control speed of a motor, brightness of a bulb or a LED etc by supplying a train of pulses. If the pulse has larger ON time i.e. duty cycle, the average voltage supplied will be more, thus if we have connected a motor to the output, its speed increases, if we reduce the pulse width, average voltage supplied is less, thus the speed of the motor decreases. Thus we can digitally control the speed of a motor.z

So now we will construct a pulse width modulator using 8051(89s52) to illustrate the concept of PWM I have constructed a simple circuit which is shown below, where a LED or small lamp is connected to the output of microcontroller (you can also connect a small motor or fan with an additional transistor). The microcontroller is programmed such that the brightness of the LED increases for some time, when it reaches maximum brightness, the LED's brightness starts to fade till its brightness is minimum and this continues.

PWM circuit

 



The program appears to be bigger but it is just replication of first main block with different values loaded in registers. The second program published in the second table uses a concept of lookup tables.

;*************************************************
;
;Program: PWM
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Varies brightness of an LED
;connected to any pin of P2 continuously
;
;*************************************************



outp equ P2
org 0h

main:mov r4,#0ffh
up0: mov r0,#100d     ;Set duty cycle
mov r1,#01d
acall out        ;light up LED
djnz r4,up0      ;loop for dome time

mov r4,#0ffh
up1: mov r0,#90d
mov r1,#10d
acall out
djnz r4,up1

mov r4,#0ffh
up2: mov r0,#80d
mov r1,#20d
acall out
djnz r4,up2

mov r4,#0ffh
up3: mov r0,#70d
mov r1,#30d
acall out
djnz r4,up3

mov r4,#0ffh
up4: mov r0,#60d
mov r1,#40d
acall out
djnz r4,up4

mov r4,#0ffh
up5: mov r0,#50d
mov r1,#50d
acall out
djnz r4,up5

mov r4,#0ffh
up6: mov r0,#40d
mov r1,#60d
acall out
djnz r4,up6

mov r4,#0ffh
up7: mov r0,#30d
mov r1,#70d
acall out
djnz r4,up7

mov r4,#0ffh
up8: mov r0,#20d
mov r1,#80d
acall out
djnz r4,up8


mov r4,#0ffh
up9: mov r0,#10d
mov r1,#90d
acall out
djnz r4,up9


mov r4,#0ffh
up11: mov r0,#10d
mov r1,#90d
acall out
djnz r4,up11

mov r4,#0ffh
up12: mov r0,#20d
mov r1,#80d
acall out
djnz r4,up12

mov r4,#0ffh
up13: mov r0,#30d
mov r1,#70d
acall out
djnz r4,up13

mov r4,#0ffh
up14: mov r0,#40d
mov r1,#60d
acall out
djnz r4,up14

mov r4,#0ffh
up15: mov r0,#50d
mov r1,#50d
acall out
djnz r4,up15

mov r4,#0ffh
up16: mov r0,#60d
mov r1,#40d
acall out
djnz r4,up16

mov r4,#0ffh
up17: mov r0,#70d
mov r1,#30d
acall out
djnz r4,up17

mov r4,#0ffh
up18: mov r0,#80d
mov r1,#20d
acall out
djnz r4,up18

mov r4,#0ffh
up19: mov r0,#90d
mov r1,#10d
acall out
djnz r4,up19

mov r4,#0ffh
up20: mov r0,#100d
mov r1,#01d
acall out
djnz r4,up20

ajmp main        ;Loop

**************************************************
;Out subroutine
out: mov outp,#0ffh
djnz r0,out
out1:mov outp,#00h
djnz r1,out1
ret

**************************************************
end

Program using lookup tables.

;*************************************************
;
;Program: PWM
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Varies brightness of an LED
;connected to any pin of P2 continuously
;
;*************************************************

outp equ P0
org 0h

main:mov dptr,#pwm_data ;load pointer for lookup table
clr a
movc a,@a+dptr
mov r2,a
inc dptr
up:  clr a
movc a,@a+dptr
mov 40h,a          ;ON period
clr a
inc dptr
movc a,@a+dptr
mov 41h,a          ;OFF period
inc dptr
acall out
djnz r2,up
sjmp main

;*************************************************
out: mov r4,#02h
lp0: mov r3,#0ffh
lp:  mov r0,40h
mov r1,41h
lp1: mov outp,#0ffh
djnz r0,lp1
lp2: mov outp,#00h
djnz r1,lp2
djnz r3,lp
djnz r4,lp0
ret

;*************************************************
;Lookup table
pwm_data: db 15h  ;no of elements in the
db 100d,001d      ;lookup table
db 090d,010d;      
db 080d,020d      ;each element consisting
db 070d,030d      ;of 2 bytes
db 060d,040d;
db 050d,050d      ;first byte is value
db 040d,060d      ;for ON period
db 030d,070d      ;second byte is value
db 020d,080d      ;for OFF period
db 010d,090d;
db 001d,100d;
db 010d,090d;
db 020d,080d;
db 030d,071d;
db 040d,060d;
db 050d,050d;
db 060d,040d;
db 070d,030d;
db 080d,020d;
db 090d,010d;
db 100d,001d;

;*************************************************

end

 

back