You are on page 1of 6

Embedded Systems IE403

Lab Work

Lab #11

Objective
To perform DC motor speed (uni-directional) control in open-loop using PWM

Theory
Uni-directional speed control refers to the speed controlling in one direction only. It means that DC motor supply polarity needs to be fixed. This is simpler than the bi-directional control where we have the choice of moving the motor in both (clockwise and anti-clockwise) directions by changing its supply voltage polarity. The controller may or may not actually measure the speed of the motor. If it does, it is called a Feedback Speed Controller or Closed Loop Speed Controller, if not it is called an Open Loop Speed Controller. Feedback speed control is better, but more complicated, and may not be required in simple designs. PWM for Motor The speed of a DC motor is directly proportional to the supply voltage, so if we reduce the supply voltage from 12 Volts to 6 Volts, the motor will run at half the speed. How can this be achieved when the battery is fixed at 12 Volts? The speed controller works by varying the average voltage sent to the motor. It could do this by simply adjusting the voltage sent to the motor, but this is quite inefficient to do. A better way is to switch the motor's supply on and off very quickly. If the switching is fast enough, the motor doesn't notice it, it only notices the average effect.This on-off switching is performed by transistors that can turn very large currents on and off under the control of a low signal level. The time that it takes a motor to speed up and slow down under switching conditions is dependant on the inertia of the rotor (basically how heavy it is), and how much friction and load torque there is. The graph in the figure 11.1 shows the speed of a motor that is being turned on and off fairly slowly:

Figure 11.1: Motor speed response for PWM Voltage

You can see that the average speed is around 150, although it varies quite a bit. If the supply voltage is switched fast enough, it wont have time to change speed much, and the speed will be quite steady. This is the principle of switch mode speed control. Thus the speed is set by PWM Pulse Width Modulation.
VCC VCC D1 1N4007 12V S1 MOTOR 2 Q1

PWM Signal

R1 1k

1 BD139 0

Figure 11.2: Power transistor to drive motor

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #11

Flow Chart

START

Interrupt Routine Timer-0

Interrupt Routine Timer-1

Initialize Time-0(interrupt) for variable on-time

On-time is over, Pulse Low

Total period is over, Pulse High

Initialize Timer-1(interrupt) for fixed PWM period

Return

Return

Call Keypad routines(lab10) to get duty-cycle

Change Timer-0 On-time as per duty-cycle

END

Figure 11.3: Flow chart for PWM motor speed control program

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #11

Source Code
LOC OBJ LINE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 SOURCE ;Lab-11 ;This program reads dutycycle from keypad ;for example digit 5 = 50%,digit 6 = 60%.... ;and generates PWM for motor speed control ORG 0000H LJMP MAIN ORG 000BH LJMP ISR_T0 ORG 001BH LJMP ISR_T1 DUTY EQU 0x50 ;ram location that stores duty-cycle TLBYTE EQU 0x51 ;ram location that stores Timer-0 LowTHBYTE EQU 0x52 ;ram location that stores Timer-0

0000 0000 02001E 000B 000B 0200DD 001B 001B 0200E8 0050 0051 byte 0052 High-byte 001E 001E 758911

MAIN: MOV TMOD,#11H

;Timer-0,1 both in mode-1

0021 0024 0027 002A

755194 7552FF 85518A 85528C

;Initialize Timer-0 (Variable timer, for on-time) MOV TLBYTE,#94H MOV THBYTE,#0FFH MOV TL0,TLBYTE MOV TH0,THBYTE ;Initialize Timer-1 (Fixed timer, for PWM-wave period) MOV TL1,#66H ; Low-byte of Timer-1 MOV TH1,#0FCH ; High-byte of Timer-1, [FC66H = 1 ms] MOV IE,#8AH SETB TR1 ; Enable Timer-0,1 interrupts ; Start timer-1

002D 758B66 0030 758DFC 0033 75A88A 0036 D28E

0038 0038 003A 003D 0040 0043 0045 0045 0048 004B

119D B40108 755194 7552FF 80F3 B40208 755148 7552FF

;Scan Keypad for duty-cycle input SCAN: CALL IN_HEX ;get duty-cycle from keypad CJNE A,#01H,D20 ;if not 10% then check for 20% MOV TLBYTE,#94H MOV THBYTE,#0FFH SJMP SCAN D20: CJNE A,#02H,D30 ;if not 20% then check for 30% MOV TLBYTE,#48H MOV THBYTE,#0FFH

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403


004E 80E8 0050 0050 B40308 0053 7551EC 0056 7552FE 0059 80DD 005B 005B B40408 005E 755190 0061 7552FE 0064 80D2 0066 0066 B40508 0069 755134 006C 7552FE 006F 80C7 0071 0071 B40608 0074 7551D8 0077 7552FD 007A 80BC 007C 007C B40708 007F 75517B 0082 7552FD 0085 80B1 0087 0087 B40808 008A 75511F 008D 7552FD 0090 80A6 0092 0092 B409A3 valid duty-cycle 0095 7551C3 0098 7552FC 009B 809B 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

Lab Work
SJMP SCAN D30: CJNE A,#03H,D40 ;if not 30% then check MOV TLBYTE,#0ECH MOV THBYTE,#0FEH SJMP SCAN D40: CJNE A,#04H,D50 ;if not 40% then check MOV TLBYTE,#90H MOV THBYTE,#0FEH SJMP SCAN D50: CJNE A,#05H,D60 ;if not 50% then check MOV TLBYTE,#34H MOV THBYTE,#0FEH SJMP SCAN D60: CJNE A,#06H,D70 ;if not 60% then check MOV TLBYTE,#0D8H MOV THBYTE,#0FDH SJMP SCAN D70: CJNE A,#07H,D80 ;if not 70% then check MOV TLBYTE,#7BH MOV THBYTE,#0FDH SJMP SCAN D80: CJNE A,#08H,D90 ;if not 80% then check MOV TLBYTE,#1FH MOV THBYTE,#0FDH SJMP SCAN D90: CJNE A,#09H,SCAN ;if not 90% then scan MOV TLBYTE,#0C3H MOV THBYTE,#0FCH SJMP SCAN

Lab #11

for 40%

for 50%

for 60%

for 70%

for 80%

for 90%

again for a

;repeat scanning until interrupted

009D 009D 009F 009F 00A1 00A3 00A5 00A7 00A7 00A9 00A9

7B32 11B2 50FA DBFA C0E0 7B32 11B2

;IN_HEX - input hex code from keypad with debouncing ;for key press and key release (50 repeats for each) IN_HEX: MOV R3,#50 ;debounce count BACK: CALL GET_KEY ;key pressed? JNC IN_HEX ;no: check again DJNZ R3,BACK ;yes: repeat 50 times PUSH ACC ;save hex code BACK2: MOV R3,#50 ;wait for key release BACK3: CALL GET_KEY ;key pressed?

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403


00AB 00AD 00AF 00B1 98 99 100 101 102 103 104 105 Accumulator if pressed 00B2 106 00B2 74FE 107 00B4 7E04 108 00B6 109 00B6 F590 110 00B8 FF 111 00B9 E590 112 00BB 54F0 113 00BD B4F007 114 00C0 EF 115 00C1 23 116 00C2 DEF2 117 00C4 C3 118 00C5 8015 119 00C7 120 00C7 FF 121 00C8 7404 122 00CA C3 123 00CB 9E 124 00CC FE 125 00CD EF 126 00CE C4 127 00CF 7D04 128 00D1 129 00D1 13 130 00D2 5006 131 00D4 0E 132 00D5 0E 133 00D6 0E 134 00D7 0E 135 00D8 DDF7 136 00DA 137 00DA D3 138 00DB EE 139 00DC 140 00DC 22 141 142 143 00DD 144 00DD C28C 145 00DF C2B0 146 147 00E1 85518A 148 40FA DBFA D0E0 22

Lab Work
JC BACK2 DJNZ R3,BACK3 POP ACC RET ;yes: keep checking ;no: repeat 50 times ;recover hex code ;return

Lab #11

;GET_KEY - get keypad status ; - returns C = 0 if no key is pressed ; - returns C = 1 and hex code in GET_KEY: MOV A,#0FEH ;start with column 0 by grounding it MOV R6,#4 ;use R6 as column counter TEST: MOV P1,A ;activate (ground) current column MOV R7,A ;save ACC (accumulator) MOV A,P1 ;read back port ANL A,#0F0H ;isolate rows by masking columns CJNE A,#0F0H,KEY_HIT ;any row active? MOV A,R7 ;no: move to RL A ;ground next column DJNZ R6,TEST CLR C ;no key pressed SJMP EXIT ;return with C = 0 KEY_HIT: MOV R7,A ;save in R7 MOV A,#4 ;prepare to calculate column CLR C ;clear carry flag SUBB A,R6 ;column number = 4 - R6 MOV R6,A ;save result in R6 MOV A,R7 ;restore rows scan result SWAP A ;put this in low nibble MOV R5,#4 ;use R5 as counter AGAIN: RRC A ;rotate-right in carry JNC DONE ;done when C = 0 INC R6 ;add 4 INC R6 ;until the row is detected INC R6 ;R6 finally INC R6 ;holds the hexcode of pressed key DJNZ R5,AGAIN ;repeat until all rows checked DONE: SETB C ;C = 1 (key press confirmed!) MOV A,R6 ;save key hex code in A EXIT: RET

ISR_T0: CLR TR0 CLR P3.0 ;pulse low ;Reload timer for next interrupt MOV TL0,TLBYTE

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403


00E4 85528C 00E7 32 00E8 00E8 C28E 00EA D2B0 00EC 00EF 00F2 00F4 00F6 758B66 758DFC D28C D28E 32 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

Lab Work
MOV TH0,THBYTE RETI

Lab #11

;return from interrupt

ISR_T1: CLR TR1 SETB P3.0 ;pulse high ;Reload timer for next interrupt MOV TL1,#66H MOV TH1,#0FCH SETB TR0 ;start timer-0 again SETB TR1 ;start timer-1 again RETI ;return from interrupt

END

;END of program

Code 11.1: ASM listing for PWM motor speed control program

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

You might also like