Está en la página 1de 33

ICTU 04-2010

Chng 3 Vi iu khin 8051 v lp trnh hp ng


Ghp ni vo ra n gin
Pham Quoc Thinh, ictu.edu.vn. 1

Department of Electronics & Telecommunications

Ni dung bi ging.
Ghp ni 8051 vi cc thit b vo ra sau y

Cng tc Relay LED

Led 7 thanh
Ma trn led

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Cng tc
Mt s cng tc in hnh: SPST

SPDT
DPDT

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Kt ni cng tc vi 8051
+5V
EA Reset VCC

ORG 0000H : JNB P3.7, CASE1

8051
XTAL1 P3.7 XTAL2 P3.6 P3.5 P3.4 VSS SW1 SW2 SW3 SW4

CASE1: CASE2: CASE3: CASE4:

JNB P3.6, CASE2 JNB P3.5, CASE3 JNB P3.4, CASE4 : : : : Kim tra trng : thi SW1 : : : : :

SW m, input ti 8051 is HIGH (1) SW ng, input ti 8051 is LOW (0)


Pham Quoc Thinh, ictu.edu.vn. 4

Department of Electronics & Telecommunications

Relay in t

Relay in t bao gm hai phn: cun dy v tip xc relay contacts

NC: NORMAL CLOSE NO: NORMAL OPEN

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

iu khin relay

Transistor m s cho php dng qua cun dy

Vs +5V

Diode c s dng bo v Transistor


Relay s tt m ON/OFF bi TTL. Kt ni m nu TTL = High Kt ni ng nu TTL = Low TTL

Mch iu khin Relay

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Relay - Switches

Relay ng nu input = High


Relay m nu input = Low

Input Control

T1

SSR
T2

a.c. power supply

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Light-Emitting Diode (LED)


Light-emitting diode (LED) c th c m khi dng in i qua n
+5V

Hnh v ch ra mt mch TTL c bn iu khin 1 LED Tr 330 dng gii hn dng qua n TTL output l LOW, LED s ON TTL output l HIGH, LED s OFF
TTL

330

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

iu khin LED
+5V
EA Reset VCC P1.7 P1.6 P1.5

0 = m 1 = tt

8 LED c kt ni ti Port 1 v kt ni ti ngun 5V

8051

P1.4 P1.3

XTAL1 P1.2 XTAL2 P1.1 P1.0 VSS

LED c th tt m trc tip bi 8051 S dung kt ni nh hnh, LED s c th Tt khi bit cng mc logic 1
M khi bit cng mc logic 0

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Vi d 1: iu khin 8 LED ON/OFF

Tt c LED M

Tt c LED Tt

Pham Quoc Thinh, ictu.edu.vn.

10

Department of Electronics & Telecommunications

Chng trnh
Start Set A = 00 ORG CLR LOOP: MOV CPL ACALL SJMP DELAY: MOV DL1: MOV DL2: DJNZ DJNZ RET END 0000H A P1, A A DELAY LOOP R6, #250 R7, #200 R7, DL2 R6, DL1

Move the content of A to P1 Delay for 0.1s Invert the content of A

S dng thch anh 12MHz


11

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Vi d 2: iu khin LED sng theo th t


START: ORG MOV MOV MOV ACALL RL DJNZ ; MOV MOV MOV ACALL RR DJNZ SJMP ; . 0000H R1, #07H A, #11111110B P1, A DELAY A R1, LEFT

LEFT:

RIGHT:

R1, #07H A, #01111111B P1, A DELAY A R1, RIGHT START

DELAY:
Pham Quoc Thinh, ictu.edu.vn.

12

Department of Electronics & Telecommunications

V d 3: iu khin LED theo th t s dng Look-up


Table

Pham Quoc Thinh, ictu.edu.vn.

13

Department of Electronics & Telecommunications

Chng trnh
START: ORG MOV MOV MOV MOV MOVC MOV ACALL INC DJNZ AJMP ; MOV MOV MOV DJNZ DJNZ DJNZ RET ; 0000H R0, #OKMDATA+1 DPTR, #MDATA R1, #00H A, R1 A, @A+DPTR P1, A DELAY R1 R0, LOOP START R5, #2 R6, #250 R7, #200 R7, DL3 R6, DL2 R5, DL1
OK:
MDATA: DB DB DB DB DB DB DB DB ; DB DB DB DB DB DB DB DB ; DB DB DB DB END 01111110B 00111100B 00011000B 00000000B 00011000B 00111100B 01111110B 11111111B 01111110B 00111100B 00011000B 00000000B 00011000B 00111100B 01111110B 11111111B 00000000B 11111111B 00000000B 11111111B

LOOP:

DELAY: DL1: DL2: DL3:

Pham Quoc Thinh, ictu.edu.vn.

14

Department of Electronics & Telecommunications

Bi tp v nh: Vit mt chng trnh, s dng bng lookup table, iu khin LED sng theo th t nh hnh v

Pham Quoc Thinh, ictu.edu.vn.

15

Department of Electronics & Telecommunications

Cc ng dng vo ra n gin
+5V
EA Reset VCC P1.7 P1.6 P1.5

P1.4
P1.3

8051
XTAL1

P1.2 P1.1 P1.0 SW1 SW2 SW3 SW4

P3.7 XTAL2 P3.6 P3.5 P3.4 VSS

Pham Quoc Thinh, ictu.edu.vn.

16

Department of Electronics & Telecommunications

V d 4: Tham kho mch iu khin LED c bn, Vit mt


chng trnh 8051 iu khin LED c ch ra nh hnh v.

Khi SW1 ng

Khi SW2 ng

Khi SW3 ng

Khi SW4 ng

u tin: SW1
Pham Quoc Thinh, ictu.edu.vn.

SW2

SW3

SW4

YELLOW= Bt RED = tt
17

Department of Electronics & Telecommunications

Lu
2
Start SW1 closed? Initialization 1 2 N SW2 closed? N SW3closed? Read SW1SW4 status N SW4 closed?

SW1 Handler

SW2 Handler

Set P3 as input port

SW3 Handler

SW4 Handler

1
Pham Quoc Thinh, ictu.edu.vn. 18

Department of Electronics & Telecommunications

Chng trnh
ORG MOV MOV MOV MOV ; ORL JNB JNB JNB JNB AJMP ; MOV MOV ACALL XRL MOV AJMP ; 0000H R1, #00000000B R2, #01010101B R3, #00001111B R4, #11110000B P3, #0FFH P3.7, CASE1 P3.6, CASE2 P3.5, CASE3 P3.4, CASE4 TEST
A, R1 P1, A DELAY A, #11111111B P1, A TEST
CASE2:

P1.x = 0, ON P1.x = 1. OFF


MOV MOV ACALL XRL MOV AJMP ; MOV MOV ACALL XRL MOV AJMP ; MOV MOV ACALL XRL MOV AJMP ; .. END A, R2 P1, A DELAY A, #10101010B P1, A TEST A, R3 P1, A DELAY A, #11110000B P1, A TEST A, R4 P1, A DELAY A, #00001111B P1, A TEST

TEST:

CASE3:

CASE4:

CASE1:

DELAY:

Pham Quoc Thinh, ictu.edu.vn.

19

Department of Electronics & Telecommunications

7-Segment LED
Bao gm 7 on LED c sp xp nh mt s 8
C hai loi: anode chung v cathode chung

a f e g b c

Dp

Segment Pattern

Pham Quoc Thinh, ictu.edu.vn.

20

Department of Electronics & Telecommunications

7-Segment LED
Anode chung
e

Common a

Dp

Pull-high

Cathode chung
Common a b c d

Dp

Pull-low
Pham Quoc Thinh, ictu.edu.vn. 21

Department of Electronics & Telecommunications

7-Segment LED
Hin th cc thng qua cc bit ca cng tu thuc theo cch s dng loi ant chung hay catt chung
Bit ca cng Thanh hin th 7 Dp 6 g 5 f 4 e 3 d 2 c 1 b 0 a

Bnh thng cc LED c th c ni vi ngun thng qua tr chng chy

Pham Quoc Thinh, ictu.edu.vn.

22

Department of Electronics & Telecommunications

7-Segment LED
8051
XTAL1 XTAL2 P2.0 P2.1 P2.2 P2.3 P2.4 P2.5 P2.6 P2.7
.

Vcc

a
b c

a
f b

d e
f g Dp

e d

c
Dp .

Vi d 5: S dng R3 nh b m, vit chng trnh s dng phng php look-up table, hin th gi tr trong R3 ra LED 7 thanh
Pham Quoc Thinh, ictu.edu.vn. 23

Chng trnh
LOOP: ORG MOV MOV MOV MOVC

Display 0,1,2,,,,8,9,0,1,2,3.
TABLE: DB DB DB DB DB DB DB DB DB DB ; END
7 6 5

Department of Electronics & Telecommunications

0000H R3, #00H DPTR, #TABLE A, R3 A, @A+DPTR

; ; Hin th LED 7 thanh MOV ACALL ; ; Tng R3 v LOOP MOV ADD DA ANL MOV AJMP ; DELAY: ..

P2, A DELAY

11000000B ; 0 11111001B ; 1 10100100B ; 2 10110000B ; 3 10011001B ; 4 10010010B ; 5 10000010B ; 6 11111000B ; 7 10000000B ; 8 10010000B ; 9 0=ON, 1=OFF

A, R3 A, #1 A A, #0FH R3, A LOOP

Output port , P2

Display segment
7-segment 0

Dp

1 0 0 0 0 0 0
24

BCD addition
Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Dot-matrix LED Display


Bao gm mt s cc LED c sp t thnh dng matrn
a b c d e f g

VD 35 LED sp thnh matrn5 ct x 7 hng hay 64 LED thnh matrn 8 x8

hin th ta s dng phng 1 2 3 4 5 php qut Qut mt ct mt thi im. Nu qut d nhanh, n s hin ra cc cn hin th

Pham Quoc Thinh, ictu.edu.vn.

25

Department of Electronics & Telecommunications

Dot-matrix LED Display


Mch bn trong ca mt matrn LED 5 x 7
a b c d e f g 1 2 3 4 5

Pham Quoc Thinh, ictu.edu.vn.

26

Department of Electronics & Telecommunications

Dot-matrix LED Display


1
EA Vcc Reset P3.0

5 a b c d e f g

P3.1
P3.2 P3.3 P3.4 P3.5 P3.6

8051
XTAL1 XTAL2

P1.0 P1.1

Lp li 100 time
iu khin ct vi P1 On column 1 00001 Off 00000 On column 2 00010 Off 00000 On column 3 00100 Off 00000 On column 4 01000 Off 00000 On column 5 10000 Off 00000

P1.2
P1.3 P1.4 VSS

Pham Quoc Thinh, ictu.edu.vn.

27

Department of Electronics & Telecommunications

Dot-matrix LED Display


Vi d 6: hin th k t E trn ma trn
a c d e
b f g 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

a c d e
b

a c d e
b f g

f g

a c d e
b f g 1 2 3 4 5

Hin th k t E trn 5x7dot-matrix.

Bc 1: tn hiu trn cc chn12345 10000 Chn gfedcba 1111111

Bc 2: tn hiu trn cc chn12345 01000 chn gfedcba 1001001

Bc 3: tn hiu trn cc chn 12345 00100 Chn gfedcba 1001001

a b c d e
f g 1 2 3 4 5
Pham Quoc Thinh, ictu.edu.vn.

a c d e
b f g 1 2 3 4 5

Bc 4: tn hiu trn cc chn 12345 00010 chn gfedcba 1001001

28

00111110B 01010001B 01001001B 01000101B 00111110B 00000000B 01000010B 01111111B 01000000B 00000000B 01000110B 01100001B 01010001B 01001001B 01000110B 00100001B 01000001B 01001001B 01001101B 00110011B 00011000B 00010100B 00010010B 01111111B 00010000B

T m v hin th cc k t s 0 n 4

Pham Quoc Thinh, ictu.edu.vn. 29

Department of Electronics & Telecommunications

00100111B 01000101B 01000101B 01000101B 00111001B 00111100B 01001010B 01001001B 01001001B 00110000B 00000001B 00000001B 01111001B 00000101B 00000011B 00110110B 01001001B 01001001B 01001001B 00110110B 00000110B 01001001B 01001001B 00101001B 00011110B

T m v hin th ca k t s 5 n 9

Pham Quoc Thinh, ictu.edu.vn. 30

Department of Electronics & Telecommunications

Department of Electronics & Telecommunications

Chng trnh hin th k t trn matrn 5x7 (1/2)


START: LOOP: SCAN: ORG 0000H MOV DPTR, #TABLE ; tr ti a ch ca k t u tin MOV R3, #10 ; hin th 10 k t: 0,1,2,3,,,,,9,0,1,. MOV R2, #100 ; qut 100 ln cho mi k t ACALL SCAN1 ; 10ms x 100 = 1000ms DJNZ R2, SCAN INC DPTR ; tng con tr DPTR 5 ln INC DPTR ; tr ti a ch u tin ca k t tip theo INC DPTR ; INC DPTR INC DPTR DJNZ R3, LOOP ; lp li hin th 10 k t AJMP START ; ========================== ; == Chng trnh con qut led == ; ========================== MOV R1, #00H ; R1 tr ti byte u tin m ca k t MOV R5, #00000001B ; Bt u t ct u tin bn phi MOV R4, #05 ; c nm ct, R4 nh mt b m! MOV A, R1 ; MOVC A, @A+DPTR MOV P3, A ; xut m hin th ra ma trn (hng) MOV P1, R5 ; m transistor tng ng(ct)
31

SCAN1: LOOP1:

Pham Quoc Thinh, ictu.edu.vn.

Department of Electronics & Telecommunications

Chng trnh hin th k t trn matrn 5x7 (2/2)


DL1: DL2: MOV R6, #5 ; tre 2 ms MOV R7, #200 ; DJNZ R7, DL2 ; DJNZ R6, DL1 ; ANL P1,#00000000B ; tt hin th MOV A, R5 ; RL A ; chuyn ti ct tip theo MOV R5, A ; c 5 ct INC R1 ; chuyn d liu ti ct tip DJNZ R4, LOOP1 ; RET ; tr v chng trnh chnh ; ============================ ; == Bng k t == ; ============================ DB 00111110B ; m cho s 0 DB 01010001B DB 01001001B DB 01000101B DB 00111110B DB 00000000B ; m cho s 1 : ; m cc cho s tip : ; END
32

TABLE:

Pham Quoc Thinh, ictu.edu.vn.

ICTU 2010

END of Lecture
Ghp ni vo ra n gin
Pham Quoc Thinh, ictu.edu.vn. 1

También podría gustarte