Vous êtes sur la page 1sur 338

1rucng o| Hcc 5u Phom Ky 1huot

Khco |en 1u 8c mcn Ccng Nghe V|en 1hcng


-------------oOo-------------










NCUEN NH PHU
NAM 2006

LI NOI AU

. | . |~ . ~| :|~: :-| |~ | :|-| .~ | -| | | |~ | |-: :| ~ |- :||
- | |-| :| | ||-: | | ||| :| ~ :|- : : ~| || ||| || .~ :. || |~ | | ~ .
| . ~ - ~ ||- | || -. ||- | :|| |~| ~ :-: | ~ |- :|| - | |-|
~ ||~ ||- | | | || ||| ||-| | | ~ |- :|| - | |-| .~ -| || . | . -
:|~. :|-. .~ || ||| . :|- ~ :|| ~ . || .| . | . :|~ : :-| |~ . ~| |~| |~ :||
|| . .- | ~ | -| |- | - | ~ | : : -| :|| :~: |~ :|~| | | | || . | . |-| ~ ||~
||-| || ~ :| |: |- . | . :|~|| : ~ . -| |- |
|| || -~| |~ . | :- | -~| 7 |||. :| ||| ' :|- | .- | -|
:|~ : :-| |~ . | .. ||| :|| :~ . ~ | :| | ~ . | . ` : :. ||| :|| :~ . .- ~ |
:| . | . ' : : ``/```. ||| - :|| :~ . .- . - | |-| ` :: | \'. |||
:|| :~ . .- ~ : :|| :~| |: || | . -| |-|. ||| :|| :~ . .- ~ - .~ ~
|~ . :||| | | :| ~ |- :|| . | . .~ . -| |-| ||| 7 :|| :~ . ~ ~ :-:
:||| ~ : :| :|| :- |~ . - | |- | . ~ :|- : : |~ .
|| :| || -~ | ||~ :~| : ~ -| :|| :~| .- . | . .~ . - | |- | |
-|| .-| |~|| - | -| :|. ~ :- : ~ . - | |-| | \' .~ ~ || || ~ :-: .~ - |
|-| :~|
\ | : ~ . || | |~ . .- :~ ~ :|- ~ | :||_|9.~| | |~|
:|~|| ~ |



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
MUC LUC
LOI NOI AU
CHUONG z. LICH SU PHAT TRIEN VI XU LY '
|. C|0| 1H|EU L|CH 5U PHA 1 1k|E N CU A CA C HE V| XU L
' 3 :| -| | -| :|~: : - | |~ ~ . | .
|| |~| |~ . | .
\~ . . :|| -
||. CA C KHA| N|EM C0 8A N VE CA U 1kU C CU A V| XU L
' | -| ~ :| | -|
|~ |~ | :|. |~: : ||
~ . - |~ . | .
- ~ :|~|| | |~ . | .
~ - || |~ . | .
~ - | :|. |~: : || 7
7 ~ ~ : || 7
` ~ ~ | - | ~ : - : |~ . |~ . | . '
CHUONG z. CAU TRUC VI XU LY B BIT VA TAP LENH '
|. 50 0 KH0| CPU 8 8|1 C0 8A N '-
' | |~ . | . '-
| / '-
||. 10 CHU C CA C 1HANH CH| '
' ~ :|~|| | :-| :| . | . '
|| |~| ~ :|~|| | '
|||. LE NH CU A V| XU L -
' ~ : - || |~ . | . -
| ||
~ || -|| :~|
- ~ -| :|. |~ : ~ | |~ . | .
|V. V| XU L Z80 7
' ~ | :| :-| :| |~ . | . ` 7
|| :|~|| | :- | :| . | . ` 3
|| |~| ~ |~ | |~ . | . ` '
- 3~ | :| ~| |~ . | . `
~ : | | .- | ~ | |~ : |~ . | . ` `
|~ | | | .~ :|~| - |~ . | . ` -'
V. V| XU L 8085A
' ~ | :| :-| :| |~ . | . ``/
|| :|~|| | :- | :| |~ . | . ``/ 7
|~ | |~ . | . ``/ 7
- 3~ | :| ~| |~ . | . ``/ '
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
3~ ~ | : ~ | .~ | - | | . | . ``/
~ : - || |~ . | . ``/
V|. 10M 1A 1 CA U H0| 0N 1A P 8A| 1AP 7
' :~ : 7
~ | | | :~ : :~ :~: 7-
CHUONG 4. CAU TRUC VI XU LY zo BIT BoBo 77
|. CA U 1kU C V| XU L 8086/8088 7`
' ~| :| :-| :| . | . ``/``` `
~ :|~|| | :-| :| . | . `
|| :|~ | ~| | ~ . | . `7
- ---: . | . `3
:~ : :|~ | || . | . `` `3
||. CA C PHU0NC PHA P |NH |A CH| CU A V| XU L 8086 37
|||. 1AP LE NH CU A V| XU L 8086 ''
' \~ -|| | ~ . | . ``/``` ''
| .|~| .- :~ : - || | ~ . | . `` '''
|~ -~ : :~: -|| | ~ . | . `` ''
|V. CA U H0| VA 8A| 1AP ''7
CHUONG d. HO VI IEU KHIEN Bosz ''3
|. C|0| 1H|EU V| |E U KH|EN MC55 '
||. KHA 0 5A 1 PHAN CU NC V| |E U KH|EN MC55 ''
' ~| :| :- | :| . -| | - | \' ''
|~ -~ : - |~ | `3' '
~ | - : | : - || || | ~ | | | : || | '
|||. CA U 1kU C 80 NH0 CU A V| |E U KH|E N '
' || : || '
~ :|~|| | || |~ | ~ : -: '
|V. KHA 0 5A 1 1AP LENH CUA V| |E U KH|E N '7
' 3 :|- | '7
~ -| || ~ | '`
|~ -~ : :~: -|| . -| |-| '-'
V. KHA 0 5A 1 H0A 1 0 NC 1|MEk/C0UN1Ek CU A V| |EU KH|EN '7
' 3 :|- | '7
|~|| | || -| ~ .- | :- \- - -:- '77
|~|| | -| | - | : - |: --:- '7`
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
- ~ -| |~ : | | ~ : - .~ :~| '7`
~ ||| || - '`
-| | -| ~ : - |~ : | '`'
7 | :~ .~ :|. |~ : ~ | ~ :-/||:- '`
` |~ -~ : : - | ~ | . -| | -| \ '`
V|. H0A 1 0 NC 1kUEN DU L|E U CUA V| |E U KH|E N '`
' 3 :|- | '`
|~|| | -| | - | :|.-| | -| | : -: '`7
~ -| :|.- | | - | | :- : '`7
- | :~ .~ :|. |~ : ~ :|~|| | :|.- | | -| | :- : '3'
|.-| | -| | : -: :| |- :|| || -| . -| | -| '3
| -: ~: : :|.-| | -| | : -: '3
V||. H0A 1 0 NC NCA 1 CU A V| |EU KH|E N '3
' 3 :|- | '3
|~|| | -| | - | :|.-| | -| | : -: '37
~ -| :|.- | | - | | :- : '3`
- | :~ .~ :|. |~ : ~ :|~|| | :|.- | | -| | :- : '33
|.-| | -| | : -: :| |- :| | || -| . - | | -|
| -: ~: : :|.-| | -| | : -: '
V|||. 8A| 1AP
CHUONG s. LAP TRNH CHO VI IEU KHIEN BoCsz
' 3 :|- |
~ : | | ~ :|| :- | | ~---:-
~| :| | ~ || | |: || 7
- ~ || ~ : -| :|| :| :| ~| | '
~ | ~ | | ~---:- '-
~ -| |- | | ~ :|| | |: || '3
7 ~ : | -| - :
` -| || \~
3 ~ | . -: ||| :|| |: ||
CHUONG o. KHAO SAT CAC IC NGOAI VI VA THIET KE HE
THONG VI XU LY 3
|. CA C L|NH K|E N 50
' | :
~ ~ '
-
- || \ .~ /\
||. CA C L|NH K|E N |C NC0A| V| -
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
' |~ . ~ : :|| `/ -
|~ . ~ : :|| `73
|||. 1H|E 1 KE HE 1H0NC V| XU L
' ~ :|- : - - '
~ :|- : - -
|V. 8A| 1AP
CHUONG y. GIAO TIEP UNG DUNG DUNG VI IEU KHIEN
. V| |e u Kh|e n C|oc 1|e p Vc | Led `
' 3 ~ :-: . - | 3
3 ~ :-: . - 7 ~| 7'
2. V| |EU KH|EN C|A0 1|EP V0| PHM NHA N 77
' 3 ~ :-: . ' |~ || : ||~| 7`
3 ~ :-: . ~ :~| :| `
3. V| |EU KH|EN C|A0 1|EP V0| ADC VA DAC `
' 3 ~ :-: . / `3 `
3 ~ :-: . / 7'3 3'
3 ~ :-: . / `` 3
4. V| |EU KH|EN C|A0 1|EP V0| kELA VA 0NC C0 8U0 C 3-
' 3 ~ :-: . / 3-
3 ~ :-: . | :| 3
5. V| |EU KH|EN C|A0 1|EP V0| LCD
6. V| |EU KH|EN C|A0 1|EP V0| LED MA 1kA N ''
7. V| |EU KH|EN C|A0 1|EP V0| kEAL1|ME '
1o| ||eu thom khoc.



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M








Chng 1





C|0| 1H|EU L|CH 5U PHA1 1k|EN CUA CAC HE V| XU L
' 3 :| - | | -| :|~ : : - | | ~ ~ . | .
|| |~ | | ~ . | .
\~ . . :||
CAC KHA| N|EM C0 8AN VE CAU 1kUC CUA V| XU L
' | - | ~ :| | - |
|~ |~ | :|. |~ : : ||
~ . - | ~ . | .
- ~ :|~|| | | ~ . | .
~ - || | ~ . | .
~ - | :|. |~ : : ||
7 ~ ~ : ||
` ~ ~ | - | ~ : - : |~ . | ~ . | .




Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

2 . | .
I. GII THIEU LCH S PHAT THEN CUA CAC HE VI X LY:
1. Gii thieu lch s phat trien cua cac he vi x ly:
May tnh so la mot he thong bao gom cac mach ien t x ly tn hieu dang so c ieu
khien bi chng trnh, co the lam nhng cong viec ma con ngi mong muon. Chng trnh se
ieu khien cac mach ien so cach di chuyen va x ly d lieu (data) bang cach ieu khien cac
mach logic so hoc, cac bo nh (memory), cac thiet b xuat / nhap (Input/output). Cach thc cac
mach ien logic cua may tnh so ket hp lai vi nhau tao thanh cac mach logic so hoc, cac vi mach
nh va cac thiet b xuat / nhap c goi la cau truc.
Vi x ly co cau truc giong nh may tnh so va co the xem no la may tnh so v ca hai eu tnh
toan di s ieu khien cua chng trnh.
Lch s phat trien cua vi x ly gan lien vi s phat trien cua cac vi mach ien t v vi x ly
la vi mach ien t che tao theo cong nghe LSI (large scale integrated) cho en VLSI (very large
scale integrated).
Vi s kham pha ra transistor va phat trien cua cong nghe che tao vi mach SSI, MSI, may
tnh van con la mot nhom gom nhieu IC ket hp lai vi nhau, cho en thap nien 70, vi s phat
trien cua cong nghe LSI, cau truc may tnh c rut gon bi cac nha thiet ke va c che tao
thanh mot IC duy nhat c goi la vi x ly (microprocessor).
Vi x ly ket hp vi cac thiet b khac tao ra cac may tnh co kha nang tnh toan rat ln nh
may vi tnh va co the tao ra ca c san pham khac cac may ien thoai, cac tong ai ien thoai, ca c he
thong ieu khien t ong...
Vi x ly au tien co kha nang x ly 4 bit d lieu, cac vi x ly nay co toc o x ly rat cham,
cac nha thiet ke cai tien thanh vi x ly 8bit, sau o la vi x ly 16 bit va 32 bit. S phat trien ve
dung lng cac bit cua vi x ly lam tang them so lng cac lenh ieu khien va cac lenh tnh toan
phc tap.
L|ch su phot tr|en cu o v| xu |y gon ||en vc | hong |N1EL:
Thang 4 nam 1971, Intel san xuat ra vi x ly 4 bit co ma so 4004 co the truy xuat
4096 o nh 4 bit va co 45 lenh.
Thang 4 nam 1972, Intel cai tien va cho ra vi x ly 8 bit co ma so 8008 co the truy
xuat 16K o nh 8 bit va co 48 lenh.
Thang 4 nam 1974, Intel cai tien vi x ly 8008 thanh vi x ly 8080 co the truy xuat
64Kbyte bo nh va co nhieu lenh hn, chay nhanh gap 10 lan so vi 8008.
Thang 4 nam 1976, Intel cai tien vi x ly 8080 thanh vi x ly 8085 co them mach tao
xung clock c tch hp ben trong, co nhieu ngat tren chip phuc vu cho nhieu ng
dung va tch hp mach ieu khien he thong tren chip.
Thang 6 nam 1978, Intel san xuat vi x ly 16 bit co ma so la 8086: co 20 ng a
ch cho phep truy xuat 1MB bo nh va bus d lieu ben trong va ben ngoai eu la
16bit.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

. | . 3
Thang 6 nam 1979, Intel san xuat vi x ly 16 bit co ma so la 8088 chu yeu da vao
vi x ly 8086 nhng khac vi vi x ly 8086 la bus d lieu ben ngoai ch co 8 bit
nhng bus d lieu ben trong vi x ly la 16 bit, muc ch cai tien nay nham ha gia
thanh he thong va tr thanh vi x ly trong may tnh IBM-PC/XT.
Vao cuoi nam 1981 va 1982, Intel cho ra i vi x ly 80186 va phien ban m rong
cua vi x ly 8086 co ho tr quan ly bo nh theo phan oan va bao ve bo nh, bus a
ch co 24 ng cho phep truy xuat 16Mbyte bo nh.
Thang 2 nam 1982, Intel cho ra i vi x ly 80286 cung la vi x ly 16 bit va chu yeu
cung phat trien t vi x ly 8086 co them nhieu chc nang nh mach nh thi c
tch hp, mach ieu khien DMA, mach ieu khien ngat va mach chon chip bo nh
c thiet ke rieng cho cac ng dung nhung vi gia chip thap.
Thang 10 nam 1985, Intel cho ra i vi x ly 80386 chnh la vi x ly 32bit, co quan ly
bo nh theo trang va phan oan bo nh, bus d lieu ben trong va ben ngoai eu la 32
bit, tap thanh ghi c m rong.
Thang 4 nam 1989, Intel cho ra i vi x ly 80486, co cai thien kien truc e tang
hieu suat, cung cap bo nh cache tren board, n v dau cham ong tren board. Co
them 6 lenh so vi vi x ly 80386. Lenh nh thi c cai tien e tang hieu suat.
Thang 3 nam 1993, Intel cho ra i vi x ly Pentium la vi x ly 64 bit co n v dau
cham ong hieu suat cao. Lenh nh thi c cai tien so vi 80486.
Thang 3 nam 1995, Intel cho ra i vi x ly Pentium Pro co 2 cap cache co san.
Thang 3 nam 1997, Intel cho ra i vi x ly Pentium II - Pentium Pro + MMX.
Nam 1999, Intel cho ra i vi x ly Pentium III IA64, m rong tao luong SIMD
Nam 2000, Intel cho ra i vi x ly Pentium IV.
2. Chc nang cua vi x ly
Vi x ly dung cac cong logic giong nh cac cong logic c s dung trong n v x ly
trung tam (central processing unit) cua may tnh so. Do cau truc giong nh CPU va c xay
dng t cac mach vi ien t nen co ten la vi x ly: microprocessor. Giong nh CPU,
microprocessor co cac mach ien t cho viec ieu khien d lieu (data) va tnh toan d lieu di
s ieu khien cua chng trnh. Ngoai ra microprocessor la mot n v x ly d lieu.
Ccng v|ec xu |y du ||eu |o chuc nong ch|nh cuo v| xu |y. Viec x ly d lieu bao gom
tnh toan va ieu khien d lieu. Viec tnh toan c thc hien bi cac mach ien logic c goi
la n v x logic so hoc (arithmetic logic unit: ALU ) co the thc hien cac phep toan nh Add,
Subtract, And, Or, Compare, Increment, Decrement.
ALU khong the thc hien mot phep toan ma khong co d lieu, v du ALU cong 2 d lieu
vi nhau th 2 d lieu phai at ung v tr trc khi cong. ALU khong the thc hien viec chuyen
d lieu t ni nay en ni khac. e ALU co d lieu cho viec x ly th ngoai ma ch ien ALU, vi
x ly con co cac mach ien logic khac e ieu khien d lieu. Cac mach ien logic ieu khien d
lieu se di chuyen d lieu vao ung v tr e khoi ALU x ly d lieu. Sau khi thc hien xong,
khoi ieu khien se di chuyen d lieu en bat c ni nao mong muon.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

4 . | .
e x ly d lieu, vi x ly phai ieu khien cac mach logic, e vi x ly ieu khien cac mach
logic th can phai co chng trnh. Chng trnh la tap hp cac lenh e x ly d lieu thc hien
tng lenh a c lu tr trong bo nh, cong viec thc hien lenh bao gom cac bc nh sau: on
lenh t bo nh, sau o cac mach logic ieu khien se giai ma lenh va sau cung th cac mach
logic ieu khien se thc hien lenh sau khi ma giai ma.
Do cac lenh lu tr trong bo nh nen co the thay oi cac lenh neu can. Khi thay oi cac
lenh cua vi x ly tc la thay oi cach thc x ly d lieu. Cac lenh lu tr trong bo nh se quyet
nh cong viec ma vi x ly se lam.
Tom tat: Chc nang chnh cua vi x ly la x ly d lieu. e lam c ieu nay vi x ly phai
co cac mach logic cho viec x ly va ieu khien d lieu va cac mach logic ieu khien. Cac mach
logic x ly se di chuyen d lieu t ni nay sang ni khac va thc hien cac phep toan tren d
lieu, mach logic ieu khien se quyet nh mach ien nao cho viec x ly d lieu. vi x ly thc
hien mot lenh vi trnh t nh sau: on lenh t bo nh, tiep theo mach logic ieu khien se giai
ma lenh e xem lenh o yeu cau vi x ly thc hien cong viec g, sau o vi x ly se thc hien
ung cong viec cua lenh a yeu cau, qua trnh nay c goi la chu ky on - va - thc hien lenh
(fetch / execute cycle).
Ngoai chc nang on va thc hien lenh, cac mach logic ieu khien con ieu khien cac
mach ien giao tiep ben ngoai ket noi vi vi x ly. Vi x ly can phai co s tr giup cua cac
mach ien ben ngoai. Cac mach ien dung e lu tr lenh e vi x ly x ly c goi la bo nh,
cac mach ien giao tiep e di chuyen d lieu t ben ngoai vao ben trong vi x ly va xuat d lieu
t ben trong vi x ly ra ngoai c goi la cac thiet b I/O hay cac thiet b ngoai vi.
3. May vi tnh (Microcomputer):
Vi x ly la mot IC chuyen ve x ly d lieu va ieu khien con may vi tnh la mot he thong
may tnh hoan chnh c xay dng t mot vi x ly. May vi tnh hoan chnh bao gom mot vi x
ly, bo nh va cac cong I/O.
S o khoi cua mot he thong may vi tnh nh hnh 1-1:














H|nh -. Cou tru c cuo mct moy v| t|nh.
keyboard

P

clock


U
A
R
T




Graphic


Disk


Disk Video
Power supply
Microcomputer bus
Disk
controller Video
CPU
and IO CPU
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

. | . 5
May vi tnh to chc theo card bao gom: CPU card, card bo nh RAM, card ieu khien a,
card ieu khien man hnh, ngoai ra may vi tnh con co man hnh video, ban phm...
Tat ca cac card trong may vi tnh c ket noi vi vi x ly thong qua bus, bus bao gom
nhieu ng tn hieu e phan biet va x ly cac card khac nhau.
Trong card CPU co mach tao xung Clock dung e tao ra tn hieu clock cho vi x ly. Card
CPU con co cac IC giao tiep e nang cao kha nang giao tiep cua CPU.
Bo nh ROM dung e lu tr cac lenh cua chng trnh e cho phep nap cac chng trnh
t a mem.., card bo nh RAM bao gom cac IC RAM e vi x ly lu tr chng trnh va d lieu
khi x ly. Trong card bo nh co phan xuat nhap data noi tiep UART ( Universal asynchronous
receiver - transmitter ), hai khoi nay co the tach ri. UART dung e chuyen oi d lieu song
song thanh noi tiep e may vi tnh co the giao tiep vi may in, cac modem, va cac thiet b ieu
khien khac.
e giao tiep vi man hnh video can phai co card video, ben cach cac IC giao tiep vi bus
cua vi x ly con co cac IC ieu khien man hnh Video. Man hnh Video dung e hien th noi
dung cua mot vung nh ac biet trong bo nh RAM do o Card video co cac IC RAM.
Khoi nguon cung cap ien cho tat ca cac he thong.
II. CAC KHAI NIEM C BAN VE CAU TRUC CUA VI X LY:
1. Chieu dai t d lieu:
Vi x ly au tien co chieu dai t d lieu la 4 bit, tiep theo la cac vi x ly 8 bit, 16 bit, 32
bit va 64 bit. Moi vi x ly co chieu dai t d lieu khac nhau se co mot kha nang ng dung khac
nhau, cac vi x ly co chieu dai t d lieu ln, toc o lam viec nhanh, kha nang truy xuat bo nh
ln c dung trong cac cong viec x ly d lieu, ieu khien phc tap, cac vi x ly co chieu dai
t d lieu nho hn, kha nang truy xuat bo nh nho hn, toc o lam viec thap hn c s dung
trong cac cong viec ieu khien va x ly n gian, chnh v the cac vi x ly nay van ton tai.
Cac vi x ly 16 bit, 32 bit c s dung rat nhieu trong may tnh. May vi tnh au tien cua
IBM s dung vi x ly 8088 vao nam 1981. Cau truc ben trong cua vi x ly 8088 co the x ly cac
t d lieu 16 bit, nhng bus d lieu giao tiep ben ngoai ch co 8 bit. Do cau truc ben trong 16 bit
nen cac may tnh PC s dung bo vi x ly 8088 co the tng thch vi cac may tnh mi s dung
cac vi x ly 16 bit: 286, hoac cac vi x ly 32 bit: 386, 486 va bo vi x ly Pentium.
Hau het cac ng dung c ieu khien bi may tnh tot hn nhieu so vi vi x ly va tuy
theo yeu cau ieu khien ma chon ieu khien bang may tnh hay ieu khien bang vi x ly. Cac
lnh vc ieu khien bang vi x ly nh: cong nghiep, khoa hoc, y hoc... Mot lnh vc ieu khien
phc tap la robot khi o cac bo vi x ly 16 bit va 32 bit la thch hp. Tuy theo yeu cau o phc
tap ma chon bo vi x ly thch hp.
Vi x ly 32 bit la s phat trien cua vi x ly 16 bit va ng dung au tien cua cac vi x ly 32
bit la cac may tnh 32 bit. Cac vi x ly 32 bit co kha nang lam viec nhanh hn v moi lan lay d
lieu t bo nh vi x ly co the lay mot lan 4 byte, trong khi o cac vi x ly 8 bit th phai lam 4
lan, vi vi x ly 16 bit phai thc hien 2 lan. Vay neu so vi vi x ly 8 bit th vi x ly 32 bit co toc
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

6 . | .
o tang gap 4, vi vi x ly 16 bit th toc o vi x ly 32 bit tang gap oi. e tang toc o lam viec
cua vi x ly la muc tieu hang au cua cac nha che ta o vi x ly.
Kho| n|em 8us du ||eu : la ng truyen d lieu 2 chieu e chuyen d lieu gia vi x ly
va cac thanh phan khac cua he thong nh bo nh, IC ngoai vi. Vi x ly 8 bit th bus d lieu se la
8 bit, vi x ly 16 bit th bus d lieu giao tiep cung la 16 bit ngoai tr vi x ly 8088.
2. Kha nang truy xuat bo nh:
Dung lng bo nh ma vi x ly co the truy xuat la mot phan trong cau truc cua vi x ly.
Cac vi x ly au tien b gii han ve kha nang truy xuat bo nh: vi x 4004 co 14 ng a ch
nen co the truy xuat c 2
14
= 16384 o nh, vi x ly 8 bit co 16 ng a ch nen co the truy
xuat c 2
16
= 65536 o nh, vi x ly 16 bit co 20 ng a ch nen co the truy xuat 2
20
=
1024000 o nh, vi x ly 32 bit nh 386 hay 68020 co the truy xuat 4 G o nh. Vi x ly co kha
nang truy xuat bo nh cang ln nen co the x ly cac chng trnh ln. Tuy theo ng dung cu the
ma chon mot vi x ly thch hp.
Kho| n|em 8us d|o ch| la tat ca cac ng a ch cua vi x ly dung e xac nh a ch
cua mot o nh hay mot thiet b ngoai vi trc khi thc hien viec truy xuat d lieu.
Kho| n|em 8us d|eu kh|en la tat ca cac ng ma vi x ly dung e ieu khien cac oi
tng khac trong he thong nh ieu khien oc bo nh, ieu khien ghi bo nh, ieu khien oc IO,
ieu khien ghi IO.

3. Toc o lam viec cua vi x ly:
Tan so xung clock cung cap cho vi x ly lam viec quyet nh en toc o lam viec cua vi
x ly, vi x ly co toc o lam viec cang ln th kha nang x ly lenh cang nhanh. Tan so xung clock
lam viec cua cac vi x ly c cho bi cac nha che ta o:

Vi x ly Tan so xung clock chieu dai t d lieu
8051 12MHz 8-bit
Z80A 4MHz 8-bit
Z80B 6MHz 8-bit
286 16MHz 16-bit
486DX2-66 66Mhz 32-bit
Pentium 66MHz 32-bit
4. Cac thanh ghi cua vi x ly:
Cac thanh ghi la mot phan quan trong trong cau truc cua vi x ly.
Cac thanh ghi ben trong cua vi x ly dung e x ly d lieu, co nhieu loai thanh ghi khac
nhau cho cac chc nang khac nhau trong vi x ly, so lng cac thanh ghi ong mot vai tro rat
quan trong oi vi vi x ly va ngi lap trnh.
Cac vi x ly khac nhau se co so lng va chc nang cua cac thanh cung khac nhau.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

. | . 7
Neu vi x ly co so lng thanh ghi nhieu th ngi lap trnh co the viet cac chng trnh
ieu khien vi x ly n gian hn, lam tang toc o x ly chng trnh. Neu vi x ly co so lng
thanh ghi t th chng trnh se phc tap hn, toc o x ly chng trnh cham hn.
e hieu ro cac thanh ghi ben trong cua mot vi x ly can phai khao sat mot vi x ly cu the.
Vay so lng cac thanh ghi ben trong vi x ly cung anh hng en toc o va kha nang x ly
chng trnh.
5. Cac lenh cua vi x ly:
Tap lenh cua vi x ly la mot trong nhng yeu to c ban e anh gia toc o lam viec cua vi
x ly. Neu vi x ly co nhieu mach ien logic ben trong e thc hien th so lenh ieu khien cua
vi x ly cang nhieu, khi o vi x ly cang ln va o phc tap cang ln. V du so sanh 2 tap lenh
cua 2 vi x ly 8 bit la 80C51 va Z80 th 80X51 co 111 lenh khac nhau con Z80 co 178 lenh. Tap
lenh cua mot vi x ly cang nhieu rat co ch khi lap trnh hay viet chng trnh cho vi x ly.
6. Cac kieu truy xuat bo nh:
Mot yeu to quyet nh s mem deo trong lap trnh la so lng cac kieu truy xuat bo nh
khac nhau cua vi x ly, vi x ly co nhieu kieu truy xuat bo nh se co kha nang x ly cang nhanh
va cau truc cac mach ien ben trong cang phc tap. Cac kieu truy xuat bo nh cua cac vi x ly 8
bit va 16 bit:

Kieu truy xuat bo nh ( Addressing
mode )
Vi x ly 6800 Vi x ly Z80 Vi x ly 8088
Implied - hieu ngam. x x x
8-bit x x x
16-direct x x x
8-bit immediate x x x
16-bit immediate x x x
8-bit relative x x x
8-bit index x x x
16-bit index x
Bit x x
8-bit indirect x
16-bit indirect x x
16-bit computed x
8-bit I/O x x
16-bit I/O x

Vi x ly 16 bit va 32 bit co so lng cac kieu truy xuat bo nh rat ln, tuy thuoc vao yeu
cau ieu khien ma chon vi x ly thch hp.
7. Bo nh:
0 nhc 8|t la mot n v nh nho nhat, o nh bit la o nh ch co the lu c d lieu 1 bit
la so nh phan ch co 1 trong 2 gia tr 0 hay 1. O nh bit thng c tch hp trong cac vi x
ly ieu khien cong nghiep.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

8 . | .
0 nhc 8yte la mot o nh co the cha c d lieu so nh phan 8 bit.
0 nhc Wcrd la mot o nh co the cha c d lieu so nh phan 16 bit thng la s ket
hp cua 2 o nh byte ket noi song song.
Moi mot o nh co 1 a ch duy nhat, a ch cua bo nh la so nh phan va d lieu lu trong
o nh cung la so nh phan.
Cac loai bo nh:
8c nhc kAM (kondcm Access Memcry| la loai bo nh cho phep oc ghi d lieu bat
ky o nh nao trong bo nh. ieu nay khac vi bo nh bo nh bang t la khong the oc ghi tuy y.
Bo nh ban dan c lam t cac flip flop, khi ta lu d lieu vao flip flop th d lieu se c lu
lai va d lieu thay oi khi ta thay oi d lieu va d lieu se mat i khi mat ien. Loai RAM nay
c goi la SRAM.
8c nhc DkAM (Dynom|c kAM = kAM dcng| s dung tu ien gate source (cong
nguon) cua MOSFET e lu tr thong tin nh ien tch tren tu. Do co ro r ien tch trong tu ien
nen khi s dung DRAM th phai can lam ti sau moi mot thi gian vai ms (thng khoang 2
en 10ms). Qua trnh lam ti bo nh thng c thc hien bang IC ieu khien bo nh ong
hoac che tao san nhng ng ieu khien lam ti bo nh trong CPU. Ca 2 loai bo nh SRAM
va DRAM eu la bo nh bay hi (mat ien th mat d lieu).
k0M (keod 0n|y Memcry| la loai bo nh ch oc noi dung bo nh a c ghi trc, qua
trnh ghi d lieu c ket hp vi qua trnh san xuat. D lieu do ngi dung cung cap cho nha
san xuat. D lieu trong bo nh khong b mat khi mat ien. ROM thng c dung e lu cac d
lieu co nh khong thay oi. Bo nh nay khong cho phep xoa va ghi lai d lieu mi.
Pk0M (Prcgrommob|e k0M| giong nh bo nh ROM nhng viec ghi d lieu vao bo nh
oc lap vi qua trnh san xuat, nha san xuat ra cac PROM cha co d lieu va ngi dung co the
thc hien viec ghi d lieu tuy y vao bo nh va ch c phep ghi 1 lan. D lieu ben trong mac
nhien la d lieu 1 va ngi lap trnh thay oi trang thai t 1 ve 0 bang cach ot cau ch ben
trong PROM.
EPk0M (Erosob|e Pk0M| giong nh bo nh PROM nhng cho phep xoa va ghi lai d
lieu nhieu lan, khi xoa phai dung tia cc tm chieu vao ca so the khoi phuc lai trang thai d
lieu 1. Sau khi ghi xong d lieu th phai che ca so lai e tranh tia cc tm lam mat d lieu.
EEPk0M (E|ectr|co||y EPk0M| giong nh bo nh EPROM nhng cho phep xoa bang xung
ien thay v xoa bang tia cc tm va cho phep ghi lai d lieu. Qua trnh nap xoa co the thc
hien c t 1000 en 100000 lan.
Dung |ucng bc nhc: Mot bo nh co N ng a ch va m ng d lieu th se co dung
lng la 2
N
m.
Dung lng bo nh c tnh theo kilobyte (KB), megabyte (MB) va gigabyte (GB) vi m
= 8.
Cou truc bc nhc:
Mong bc nhc (memcry orroy|: phan chnh cua bo nh la mang bo nh, moi hang cua
cac te bao nh (memory cell) c ieu khien bi ng t (word line). Khi mot hang cu the
c kch hoat qua ng t, cac gia tr d lieu cua nhng hang te bao cua hang o co the c
oc hay ghi vao qua cac ng bit (bit line).
V du: s o cua bo nh 16 bit c to chc theo mang 44 nh hnh 1-2
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

. | . 9

H|nh -2. 1c chuc mong bc nhc 44.
Cac kieu bo nh khac nhau c phan biet chu yeu da vao cach thc lu tr te bao nh.
Moch hc trc :
Ngoai mang bo nh con co mach ho tr e giao tiep vi cac thiet b ben ngoai. S o khoi
cua mot bo nh nh hnh 1-3:

H|nh -3. Moch hc trc chc bc nhc.
Mach ho tr co chc nang:
- Giai ma a ch e xac nh o nh can truy xuat.
- Cung cap cac tn hieu ieu khien e oc va ghi mang bo nh.
Coc t|n h|eu d|eu kh|en:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

10 . | .
Tn hieu CS (chip select): la tn hieu chon chip: neu 1 CS th bo nh khong c chon
va cac ng d lieu se trang thai tong tr cao, neu 0 CS th bo nh c phep truy xuat ghi
hay oc.
Tn hieu OE (Output Enable): cho phep xuat d lieu t bo nh a ra ngoai cho cac thiet
b khac v du nh vi x ly ieu khien chan OE e yeu cau bo nh xuat d lieu.
Tn hieu WR (Write Enable): cho phep ghi d lieu t ben ngoai va bo nh v du nh vi x
ly ieu khien chan WR e yeu cau bo nh nhan d lieu t bus d lieu e cat vao o nh.
Gian o thi gian oc va ghi d lieu cua bo nh nh hnh 1-4:


H|nh -4. C|on dc thc| g|on dcc gh| bc nhc.

8. Cac mach ien giao tiep ngoai vi cua vi x ly:
Ngoai giao tiep vi bo nh, vi x ly co cac mach ien giao tiep vi cac mach ien ben
ngoai e ieu khien hay m rong kha nang ieu khien. Cac mach ien ben ngoai la cac IC va
c goi la IC ngoai vi. Moi IC ngoai vi co mot chc nang rieng, tuy thuoc vao yeu cau ieu
khien ma chon cac IC ngoai vi.
Bang danh sach sau ay trnh bay cac IC ngoai vi co the giao tiep vi Z80:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

. | . 11

Ma so IC Chc nang dang vo
8410 Direct memory access controller 40 pin -DIP
8420 Parallel input/output controller 40 pin -DIP
8430 Counter timer circuit 28 pin -DIP
8440 Serial input/output controller 40 pin -DIP
8470 Dual channel asynchronous receiver transmitter 40 pin -DIP
8530 Serial communications controller 40 pin -DIP

Bang danh sach sau ay trnh bay cac IC ngoai vi co the giao tiep vi 8088/80286:

Ma so IC Chc nang dang vo
8087/80287 Arithmetic coprocessor 40 pin -DIP
8116 Dual baud rate clock generator (programmable) 18 pin -DIP
8202 Dynamic RAM controller 40 pin -DIP
8224 Clock generator/driver 16 pin -DIP
8250 Asynchronous communications element 40 pin -DIP
8253 Programmable interval timer 24 pin -DIP
8272 Floppy disk controller 40 pin -DIP
























Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ' 3 :| - | | -| :|~ : : - | | ~ . | .

12 . | .












Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M





Chaang 2





|. 50 0 KH0| CPU 8 8|1 C0 8AN
' | | ~ . | .
| /
||. 10 CHUC CAC 1HANH CH|
' ~ :|~|| | :- | :| . | .
|| |~ | ~ :|~|| |
|||. LENH CUA V| XU L
' ~ : - || | ~ . | .
| ||
~ || - || :~ |
- ~ - | :|. |~ : ~ | | ~ . | .
|V. V| XU L Z80
' ~ | :| :- | :| | ~ . | . `
|| :|~|| | :- | :| . | . `
|| |~ | ~ |~ | | ~ . | . `
- 3~ | :| ~| | ~ . | . `
~ : | | .- | ~ | |~ : | ~ . | . `
|~ | | | .~ :|~ | - | ~ . | . `
V. V| XU L 8085A
' ~ | :| :- | :| | ~ . | . ``/
|| :|~|| | :- | :| | ~ . | . ``/
|~ | | ~ . | . ``/
- 3~ | :| ~| | ~ . | . ``/
3~ ~ | : ~ | .~ | - | | . | . ``/
~ : - || | ~ . | . ``/
V|. 10M 1A1 CAU H0| 0N 1AP 8A| 1AP
' :~ :
~ | | | :~ : :~ :~ :



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
'- Vi x ly
|. 50 0 KH0| CPU 8 8|1 C0 8AN:
1. .o !o no i rv u .i av |u .
Cau truc cua tat ca cac vi x ly eu co cac khoi c ban giong nhau nh ALU, cac thanh
ghi, khoi ieu khien la cac mach logic. e nam ro nguyen ly lam viec cua vi x ly can phai
khao sat nguyen ly ket hp cac khoi vi nhau e x ly mot chng trnh.
S o khoi cua vi x ly se trnh bay cau truc cua mot vi x ly. Moi mot vi x ly khac
nhau se co cau truc khac nhau. V du vi x ly 8 bit se co cau truc khac vi vi x ly 16 bit...
Vi moi vi x ly eu co mot s o cau truc ben trong va c cho trong cac so tay cua
nha che tao. S o cau truc dang khoi rat tien li va de trnh bay nguyen ly hoat ong cua
vi x ly. Hnh 2-1 trnh bay s o khoi cua vi x ly 8 bit:

H|nh 2-. 5c dc cou truc ben trcng cuo v| xu |y.
Trong s o khoi cua vi x ly bao gom cac khoi chnh nh sau: khoi ALU, cac thanh ghi
va khoi control logic. Ngoai ra s o khoi con trnh bay cac ng truyen tai tn hieu t ni
nay en ni khac ben trong va ben ngoai he thong.
!. Ino i ZL.
ALU la khoi quan trong nhat cua vi x ly, khoi ALU cha cac mach ien logic chuyen
ve x ly d lieu. Khoi ALU co 2 ngo vao co ten la IN chnh la cac ngo vao d lieu cho
ALU x ly va 1 ngo ra co ten la OUT chnh la ngo ra ket qua d lieu sau khi ALU x ly
xong.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 15
D lieu trc khi vao ALU c cha thanh ghi tam thi (Temporarily Register) co
ten la TEMP 1 va TEMP 2. Bus d lieu ben trong vi x ly c ket noi vi 2 ngo vao IN
cua ALU thong qua 2 thanh ghi tam thi. Viec ket noi nay cho phep ALU co the lay bat ky
d lieu nao tren bus d lieu ben trong vi x ly.
Thng th ALU luon lay d lieu t mot thanh ghi ac biet co ten la accumulator (A).
Ngo ra OUT cua ALU cho phep ALU co the gi ket d lieu sau khi x ly xong len bus d lieu
ben trong vi x ly, do o thiet b nao ket noi vi bus ben trong eu co the nhan d lieu nay.
Thng th ALU gi d lieu sau khi x ly xong ti thanh ghi Accumulator.
V du khi ALU cong 2 d lieu th mot trong 2 d lieu c cha trong thanh ghi
Accumulator, sau khi phep cong c thc hien bi ALU th ket qua se gi tr lai thanh ghi
Accumulator va lu tr thanh ghi nay.
ALU x ly mot d lieu hay 2 d lieu tuy thuoc vao lenh hay yeu cau ieu khien, v du
khi cong 2 d lieu th ALU se x ly 2 d lieu va dung 2 ngo vao IN e nhap d lieu, khi
tang mot d lieu nao o len 1 n v hay lay bu mot d lieu, khi o ALU ch x ly 1 d lieu
va ch can mot ngo vao IN.
Khoi ALU co the thc hien cac phep toan x ly nh sau:
Add Complement OR Exclusive OR
Subtract Shift right Increment
AND Shift left Decrement
Tom Tat: Chc nang chnh cua khoi ALU la lam thay oi d lieu hay chuyen ve x ly
d lieu nhng khong lu tr d lieu. e hieu ro them chc nang ac biet cua ALU can phai
khao sat mot vi x ly cu the.
||. 10 CHUC CAC 1HANH CH|:
1. .u r nunn qni ve n onq rv u .i av |u .
Cac thanh ghi ben trong co chc nang lu tr tam thi cac d lieu khi x ly. Trong so
cac thanh ghi co mot vai thanh ghi ac biet khi thc hien cac lenh ac biet, cac thanh ghi con
lai goi la cac thanh ghi thong dung. Vi s o khoi minh hoa tren, cac thanh ghi thong dung
co ten Reg B, Reg C, Reg D, Reg E.
Cac thanh ghi thong dung rat hu dung cho ngi lap trnh dung e lu tr d lieu phuc
vu cho cong viec x ly d lieu va ieu khien, khi viet chng trnh chung ta luon s dung cac
thanh ghi nay. So lng cac thanh ghi thong dung thay oi tuy thuoc vao tng vi x ly.
So lng va cach s dung cac thanh ghi thong dung tuy thuoc vao cau truc cua tng vi
x ly, nhng chung co mot vai iem c ban giong nhau. Cang nhieu thanh ghi thong dung th
van e lap trnh n gian hn.
Cac thanh ghi c ban luon co trong mot vi x ly la thanh ghi A (Accumulator register),
thanh ghi bo em chng trnh PC (Program Counter register), thanh ghi con tro ngan xep SP
( Stack pointer register), thanh ghi trang thai F (Status register Flag register), cac thanh ghi
thong dung, thanh ghi lenh IR (Instruction register), thanh ghi a ch AR (address register).
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
' Vi x ly

H|nh 2-2. 5c dc m|nh hco coc thonh gh| ben trcng cuo M|crcprccesscr ducc tc dom.
!. .nv r nu nq rv u ru r nunn qni.
1honh gh| Accumu|otcr:
Thanh ghi A la mot thanh ghi quan trong cua vi x ly co chc nang lu tr d lieu khi
tnh toan. Hau het cac phep toan so hoc va cac phep toan logic eu xay ra gia ALU va
Accumulator.
V du khi thc hien mot lenh cong 1 d lieu A vi mot d lieu B, th mot d lieu phai
cha trong thanh ghi Accumulator gia s la d lieu A, sau o se thc hien lenh cong d lieu
A (cha trong Accumulator) vi d lieu B (co the cha trong o nh hoac trong mot thanh ghi
thong dung), ket qua cua lenh cong la d lieu C se c at trong thanh ghi A thay the cho d
lieu A trc o.
Chu y: Ket qua sau khi thc hien ALU thng gi vao thanh ghi Accumulator lam cho
d lieu trc o cha trong Accumulator se mat.
Mot chc nang quan trong khac cua thanh ghi Accumulator la e truyen d lieu t bo
nh hoac t cac thanh ghi ben trong cua vi x ly ra cac thiet b ieu khien ben ngoai th d
lieu o phai cha trong thanh ghi Accumulator.
Thanh ghi Accumulator con nhieu chc nang quan trong khac se c thay ro qua tap
lenh cua mot vi x ly cu the, so bit cua thanh ghi Accumulator chnh la n v o cua vi x ly,
vi x ly 8 bit th thanh ghi Accumulator co o dai 8 bit.
1honh gh| bc dem chucng tr|nh PC (Prcgrom ccunter|:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 17
Thanh ghi PC la mot thanh ghi co vai tro quan trong nhat cua vi x ly. Chng trnh la
mot cuoi cac lenh noi tiep nhau trong bo nh cua vi x ly, cac lenh nay se yeu cau vi x ly
thc hien chnh xac cac cong viec e giai quyet mot van e.
Tng lenh phai n gian va chnh xac va cac lenh phai theo ung mot trnh t e
chng trnh thc hien ung. Chc nang cua thanh ghi PC la quan ly lenh ang thc hien va
lenh se c thc hien tiep theo.
Thanh ghi PC trong vi x ly co chieu dai t d lieu ln hn chieu dai t d lieu cua vi
x ly. V du oi vi cac vi x ly 8 bit co the giao tiep vi 65536 o nh th thanh ghi PC phai
co chieu dai la 16 bit e co the truy xuat tng o nh bat au t o nh th 0 en o nh th
65535.
Chu y noi dung cha trong thanh ghi PC cung chnh la noi dung cha trong thanh ghi a
ch.
Trc khi vi x ly thc hien mot chng trnh th thanh ghi PC phai c nap mot con so
: Do ch/nh /a d/a ch/ caa o nha chaa /enh daa t/en caa chaang trnh.
a ch cua lenh au tien c gi en IC nh thong qua bus a ch 16 bit. Sau o bo
nh se at noi dung cua o nh len bus d lieu, noi dung nay chnh la ma lenh, qua trnh nay
goi la on lenh t bo nh.
Tiep theo vi x ly t ong tang noi dung cua thanh ghi PC e chuan b on lenh ke. PC
ch c tang khi vi x ly bat au thc hien lenh c on trc o. Lenh ang thc hien co
chieu dai bao nhieu byte th thanh ghi PC tang len ung bay nhieu byte.
Mot vai lenh trong chng trnh co the nap vao thanh ghi PC mot gia tr mi, khi lenh
lam thay oi thanh ghi PC sang gia tr mi c thc hien th lenh ke co the xay ra mot a
ch mi oi vi ca c lenh nhay hoac lenh goi chng trnh con.
1honh gh| trong tho| (5totus keg|ster|:

H|nh 2-3. Cou truc cuo mct thonh gh| trong tho|.
Thanh ghi trang thai con c goi la thanh ghi c (Flag register) dung e lu tr ket qua
cua mot so lenh kiem tra. Viec lu tr cac ket qua kiem tra cho phep ngi lap trnh thc
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
'` Vi x ly
hien viec re nhanh trong chng trnh. Khi re nhanh, chng trnh se bat au tai mot v tr
mi. Trong trng hp re nhanh co ieu kien th chng trnh re nhanh ch c thc hien khi
ket qua kiem tra ung ieu kien. Thanh ghi trang thai se lu tr cac ket qua kiem tra nay.
Cac bit thng co trong mot thanh ghi trang thai c trnh bay hnh 2-3.
Cac lenh xay ra trong khoi ALU thng anh hng en thanh ghi trang thai, v du khi
thc hien mot lenh cong 2 d lieu 8 bit, neu ket qua ln hn 11111111
2
th bit carry se mang
gia tr la 1. Ngc lai neu ket qua cua phep cong nho hn 11111111
2
th bit carry bang 0. V
du lenh tang hay giam gia tr cua mot thanh ghi, neu ket qua trong thanh ghi khac 0 th bit Z
luon bang 0, ngc lai neu ket qua bang 0 th bit Z bang 1.
V du ve re nhanh khi kiem tra bit trong thanh ghi trang thai: hay viet mot chng trnh
giam gia tr cua mot thanh ghi co gia tr la 10.
1. Nap vao thanh ghi mot so nh phan co gia tr la 10.
2. Giam noi dung cua thanh ghi i 1.
3. Kiem tra bit Zero cua thanh ghi trang thai co bang 1 hay khong ?
4. Neu khong nhay en thc hien tiep lenh bc 2
5. Neu ung ket thuc chng trnh.
Y ngha cua cac bit trong thanh ghi tra ng thai:
[o]. Corry/bcrrcw (cc tron/mucn|: la bit carry khi thc hien mot phep cong co gia tr tuy
thuoc vao ket qua cua phep cong. Ket qua tran th bit carry =1, ngc lai bit carry = 0.
La bit borrow khi thc hien mot phep tr: neu so b tr ln hn so tr th bit borrow = 0,
ngc lai bit borrow =1. Bit carry hay bit borrow la 1 bit ch c phan biet khi thc
hien lenh cu the.
[b]. Zerc: bit Z bang mot khi ket qua cua phep toan bang 0, ngc lai bit Z=1.
[c]. Negot|ve (cc sc om|: bit N = 1 khi bit MSB cua thanh ghi co gia tr la 1, ngc lai
N=0.
[d]. |ntermed|ote corry (cc tron phu|: giong nh bit Carry nhng ch co tac dung oi vi
phep cong hay tr 4 bit thap.
[e]. |nterrupt F|og (cc boc ngot|: Bit IF co gia tr la 1 khi ngi lap trnh muon cho phep
ngat, ngc lai th khong cho phep ngat.
[f]. 0verf|cw (cc tron sc cc dou|: bit nay bang 1 khi bit tran cua phep toan cong vi bit
dau cua d lieu.
[g]. Por|ty (cc chon |e|: bit nay co gia tr la 1 khi ket qua cua phep toan la so chan,
ngc lai la so le th bit P = 0.
So lng cac bit co trong thanh ghi trang thai tuy thuoc vao tng vi x ly. Trong mot so
vi x ly co the xoa hoac at cac bit cua thanh ghi tra ng thai.
1honh gh| ccn trc ngon xep (5tock Pc|nter keg|ster|:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 19
Thanh ghi con tro ngan xep la mot thanh ghi quan trong cua vi x ly, o dai t d lieu
cua thanh ghi SP bang thanh ghi PC, chc nang cua thanh ghi SP gan giong nh thanh ghi PC
nhng no dung e quan ly bo nh ngan xep khi muon lu tr tam thi d lieu vao ngan xep.
Giong nh thanh ghi PC, thanh ghi SP cung t ong ch en o nh ke. Trong hau het cac
vi x ly, thanh ghi SP giam (e ch en o nh tiep theo trong ngan xep) sau khi thc hien lenh
cat d lieu vao ngan xep. Do o khi thiet lap gia tr cho thanh ghi SP la a ch cuoi cung cua
bo nh.
Thanh ghi SP phai ch en mot o nh do ngi lap trnh thiet lap, qua trnh nay goi la
khi tao con tro ngan xep. Neu khong khi tao, con tro ngan xep se ch en mot o nh ngau
nhien. Khi o d lieu cat vao ngan xep co the ghi e len d lieu quan trong khac lam chng
trnh x ly sai hoac thanh ghi SP ch en vung nh khong phai la bo nh RAM lam chng
trnh thc hien khong ung v khong lu tr c d lieu can cat tam vao bo nh ngan xep.
To chc cua ngan xep la vao sau ra trc (LAST IN FIRST OUT : LIFO).
1honh gh| d|o ch| bc nhc (oddress keg|ster|:
Moi khi vi x ly truy xuat bo nh th thanh ghi a ch phai tao ra ung a ch ma vi x
ly muon. Ngo ra cua thanh ghi a ch c at len bus a ch 16 bit. Bus a ch dung e la
chon mot o nh hay la chon 1 port Input/Output.
Noi dung cua thanh ghi a ch o nh va noi dung cua thanh ghi PC la giong nhau khi vi
x ly truy xuat bo nh e on lenh, khi lenh ang c giai ma th thanh ghi PC tang len e
chuan b on lenh tiep theo, trong khi o noi dung cua thanh ghi a ch bo nh khong tang,
trong suot chu ky thc hien lenh, noi dung cua thanh ghi a ch phu thuoc vao lenh ang c
thc hien, neu lenh yeu cau vi x ly truy xuat bo nh th thanh ghi a ch bo nh c dung
lan th 2 trong khi thc hien mot lenh.
Trong tat ca cac vi x ly, thanh ghi a ch bo nh co chieu dai bang vi thanh ghi PC.
1honh gh| |enh (|nstruct|cn keg|ster|:
Thanh ghi lenh dung e cha ma lenh vi x ly ang thc hien.
Mot chu ky lenh bao gom on lenh t bo nh va thc hien lenh.
au tien la lenh c on t bo nh, sau o PC ch en lenh ke trong bo nh. Khi mot
lenh c on co ngha la d lieu trong o nh o c copy vao vi x ly thong qua bus d lieu
en thanh ghi lenh. Tiep theo lenh se c thc hien, trong khi thc hien lenh bo giai ma
lenh oc noi dung cua thanh ghi lenh. Bo giai ma se giai ma lenh e bao cho vi x ly thc
hien chnh xac cong viec ma lenh yeu cau.
Chieu dai t d lieu cua thanh ghi lenh tuy thuoc vao tng vi x ly.
Thanh ghi lenh do vi x ly s dung ngi lap trnh khong c s dung thanh ghi nay.
1honh gh| chuo du ||eu tom thc| (1empcrory doto keg|ster|:
Thanh ghi lu tr d lieu tam thi dung e ALU thc hien cac phep toan x ly d lieu.
Do ALU ch x ly d lieu khong co chc nang lu tr d lieu, bat ky d lieu nao a en ngo
vao cua ALU, lap tc se xuat hien ngo ra.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
D lieu xuat hien tai ngo ra cua ALU c quyet nh bi lenh trong chng trnh yeu
cau ALU thc hien. ALU lay d lieu t bus d lieu ben trong vi x ly, x ly d lieu, sau o
at d lieu va x ly xong tr lai thanh ghi Accumulator, do o can phai co thanh ghi lu tr
d lieu tam thi e ALU thc hien. Ngi lap trnh khong c phep x dung cac thanh ghi
tam thi. So lng cac thanh ghi nay tuy thuoc vao tng vi x ly cu the.
Khc| d|eu kh|en |cg|c (ccntrc| |cg|c| vo khc| g|o| mo |enh (|nstruct|cn deccder|:
Chc nang cua khoi giai ma lenh la nhan lenh t thanh ghi lenh sau o giai ma e gi
tn hieu ieu khien en cho khoi ieu khien logic.
Chc nang cua khoi ieu khien logic (control logic) la nhan lenh hay tn hieu ieu khien
t bo giai ma lenh, sau o se thc hien ung cac yeu cau cua lenh. Khoi ieu khien logic
c xem la mot vi x ly nho nam trong mot vi x ly.
Cac tn hieu ieu khien cua khoi ieu khien logic la cac tn hieu ieu khien bo nh,
ieu khien cac thiet b ngoai vi, cac ng tn hieu oc-ghi...va cac tn hieu ieu khien vi x
ly t cac thiet b ben ngoai. Cac ng tn hieu nay se c trnh bay cu the trong s o cua
tng vi x ly cu the.
Ngo tn hieu vao quan trong nhat cua khoi ieu khien logic la tn hieu clock can thiet
cho khoi ieu khien logic hoat ong. Neu khong co tn hieu clock th vi x ly khong lam viec.
Mach tao xung clock la cac mach dao ong, tn hieu c a en ngo vao clock cua vi x ly.
Co nhieu vi x ly co tch hp mach tao dao ong ben trong, khi o ch can them tu thach
anh ben ngoai.
8us du ||eu ben trcng v| xu |y:
Bus d lieu dung e ket noi cac thanh ghi ben trong va ALU vi nhau, tat ca cac d lieu
di chuyen trong vi x ly eu thong qua bus d lieu nay. Cac thanh ghi ben trong co the nhan
d lieu t bus hay co the at d lieu len bus nen bus d lieu nay la bus d lieu 2 chieu. Bus
d lieu ben trong co the ket noi ra bus ben ngoai khi vi x ly can truy xuat d lieu t bo nh
ben ngoai hay cac thiet b IO. Bus d lieu ben ngoai cung la bus d lieu 2 chieu v vi x ly co
the nhan d lieu t ben ngoai hay gi d lieu ra.
e biet trnh t lam viec cua bus d lieu ben trong vi x ly hoat ong, hay cho vi x ly
thc hien mot lenh cong 2 so nh phan cha trong thanh ghi 2 thanh ghi: thanh ghi
Accumulator (goi tat la A) =1101 1110
2
va thanh ghi D=1101 1010
2
.
Trnh t cong nh sau:
Trc khi thc hien lenh cong, noi dung cua 2 thanh ghi phai cha 2 d lieu va 2 thanh
ghi nay co the ang ket noi vi cac thiet b khac. e thc hien lenh cong noi dung 2
thanh ghi A va D th thanh ghi lenh phai mang ung ma lenh cua phep cong nay va gia
s ma lenh o la ADD. c trnh bay hnh 2-4.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 21

H|nh 2-4. 1rucc kh| ccng du ||eu.
D lieu cua thanh ghi A c at len bus d lieu ben trong vi x ly, mot trong 2 thanh
ghi lu tr d lieu tam thi c ket noi vi Bus d lieu. Thanh ghi tam thi se copy d
lieu cha trong thanh ghi A. Ch co thanh ghi A va thanh ghi tam thi c ket noi vi
bus tai thi iem nay. Xem hnh 2-5
D lieu cua thanh ghi D c ket noi vi bus d lieu va thanh ghi tam thi con lai cung
c phep ket noi vi bus d lieu. Thanh ghi tam thi se copy noi dung cua thanh ghi D.
Ch co thanh ghi D va thanh ghi tam thi c ket noi vi bus tai thi iem nay. Xem
hnh 2-6.
ALU se cong trc tiep 2 d lieu tai 2 ngo vao. Ngo ra cua ALU c ket noi vi thanh
ghi A, ket qua cua phep cong c nap vao thanh ghi A. Xem hnh 2-7.
Sau khi at ket qua vao trong thanh ghi A va cap nhat s thay oi cac bit trong thanh ghi
trang thai th s ket noi gia thanh A va khoi ALU cham dt, cac thanh ghi tam thi tr
lai trang thai san sang cho lenh tiep theo. Xem hnh 2-8

Phep cong 2 so nh phan: 1101 1110
+ 1101 1010
1 1011 1000
Carry negative


Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly


H|nh 2-5. Du ||eu thonh gh| A ducc duo den thonh gh| 1emp.

H|nh 2-6. Du ||eu thonh gh| D ducc duo den thonh gh| 1emp2.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 23

H|nh 2-7. Ket quo |uu trc |o| thonh gh| A.

H|nh 2-8. Coc thonh gh| tom trc |o| trong tho| bon dou.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly
|||. LENH CUA V| XU L:
1. Iu v |e nn rv u .i av |u .
Lenh cua vi x ly la mot d lieu so nh phan, khi vi x ly oc mot lenh th t d lieu nh
phan nay se yeu cau vi x ly lam mot cong viec n gian. Moi mot t d lieu tng ng
vi mot cong viec ma vi x ly phai lam. Hau het cac lenh cua vi x ly la cac lenh chuyen d
lieu va x ly d lieu.
Khi noi en tap lenh cua vi x ly tc noi en tat ca cac lenh ma vi x ly co the hieu va
thc hien c.
Neu tap lenh cua mot vi x ly giong vi tap lenh cua mot vi x ly khac th cau truc cua
2 vi x ly giong nhau.
o dai cua mot lenh bang vi o dai t d lieu cua vi x ly, oi vi vi x ly 8 bit th o
dai cua mot lenh la 8 bit, oi vi vi x ly 16 bit th o dai cua mot lenh la 16 bit...
Trong chu ky on lenh, ma lenh se c gi en thanh ghi lenh, bo giai ma lenh, va bo
ieu khien logic cua vi x ly. Chc nang cua cac khoi se xac nh lenh nay lam g va se gi
cac tn hieu ieu khien en cac mach ien logic khac trong vi x ly, cac tn hieu logic nay se
thc hien ung chc nang ma lenh yeu cau.
Hnh 2-9 minh hoa chu ky thc hien lenh:












H|nh 2-. Chu ky thuc h|en |enh cuo v| xu |y.
Mot lenh c thc hien can phai hoi u 2 yeu to:
eu tc thu nhot la lenh se yeu cau vi x ly thc hien cong viec g. V du yeu cau vi
x ly thc hien mot lenh cong: ADD, mot lenh dch chuyen d lieu MOV... la nhng lenh ma
vi x ly co thc hien c.
eu tc thu ho| la lenh phai cho vi x ly biet cac thong tin a ch tc la v tr cua cac
d lieu ma vi x ly phai thc hien. V du khi thc hien mot lenh cong noi dung 2 thanh ghi A
va B, hoac noi dung thanh ghi A va d lieu cha trong mot o nh. Yeu to th 2 trong trng
hp nay la cac thanh ghi A va B, hoac thanh ghi A va a ch cua o nh.
Yeu to th nhat goi la ma lenh : op code (operation code) va yeu to th 2 goi la a ch.
Ma lenh se bao cho vi x ly lam g va a ch se cho vi x ly biet v tr cua d lieu.
cn |enh tu bc nhc
C|o| mo |enh
1hu c h|en |enh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 25
S o hnh 2-10 minh hoa cho cau truc 1 lenh.


hoac




H|nh 2-0. Cou truc cuo mct |enh boc gcm mo |enh vo d|o ch|.
T d lieu au tien luon la ma lenh, cac t d lieu tiep theo la a ch. oi vi cac lenh
ch co mot t d lieu th a ch a c hieu ngam.
Do co nhieu cach ch cho vi x ly biet a ch cua d lieu c goi la cac kieu truy xuat
bo nh. Khi s dung mot vi x ly can phai biet cac kieu truy xuat nay.
!. Iv qo i nno (^newonir.).
Mot lenh cua vi x ly la cac con so nh phan. oi vi lenh ch co mot byte th rat kho
nh, neu lenh dai 2, 3, 4 hoac nhieu hn na th khong the nao nh het. e giam bt s phc
tap cua so nh phan co the dung so Hex e thay the, khi o cac lenh de viet va de oc hn
nhieu nhng cung khong the nao giup ngi s dung nh het c va quan trong nhat la khi
viet chng trnh cung nh luc g roi chng trnh.
e giai quyet van e nay lenh c viet thanh cac t gi nh rat gan vi chc nang va
y ngha cua cac lenh. Trong hau het cac t gi nh cua lenh, ma lenh c rut gon ch con
khoang 3 ky t. V du lenh di chuyen d lieu co t gi nh la MOV, lenh tr la SUB... Khi
lenh co cac a ch i sau th cac a ch nay van la cac con so. V du lenh nhay en mot o
nh viet nh sau:
JMP FA90
H
.
Khi s dung cac t gi nh nay giup ngi lap trnh rat de nh tat ca cac lenh cua vi x
ly, khi viet chng trnh ngi lap trnh dung cac t gi nh e viet chng trnh, cac t gi
nh nay tao thanh mot ngon ng goi la Assembly khi viet chng trnh bang ngon ng
Assembly th vi x ly se khong hieu muon vi x ly hieu va thc hien chng trnh th phai
s dung chng trnh bien dch Assembler e chuyen cac lenh viet di dang ngon ng
Assembly thanh cac lenh dang so nh phan va cac a ch dang nh phan tng ng roi nap
vao bo nh th vi x ly mi co the thc hien c.
. .u r nno w |e nn ro vu n rv u .i av |u .
oi vi hau het cac vi x ly tap lenh c chia ra lam 9 nhom lenh c ban:
Nhom lenh truyen d lieu: Data transfers.
Nhom lenh trao oi, truyen khoi d lieu, lenh tm kiem: Exchanges, Block transfers,
Searches.
Nhom lenh so hoc va logic: arithmetic and logic.
Nhom lenh xoay va dch: Rotates and shifts.
0p ccde
Address
0p ccde
Address
Address
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
Nhom lenh ieu khien CPU.
Nhom lenh ve bit: Bit set, bit reset, and bit test.
Nhom lenh nhay: Jumps.
Nhom lenh goi, tr ve va nhom lenh bat au: Calls, Return, and Restarts.
Nhom lenh xuat nhap: Input and Output.
Cac ma gi nh va cac ma nh phan cua tat ca cac lenh se c cho trong cac so tay
cua nha che tao oi vi tng vi x ly cu the.
!. .u r ie v vu avu !iu rn rv u wo .i av |u .
Nh a trnh bay cac phan tren, vi x ly co the truy xuat bo nh bang nhieu cach e
lay d lieu. Vi x ly co nhieu cach truy xuat th chng trnh khi viet se cang ngan goi rat co
li cho ngi lap trnh va lam giam thi gian thc hien chng trnh.
Cha Y: Danh ta tray xaat bo nha co ngha /a tao ra d/a ch/ de tray xaat da //ea, r/ xa
/y tray xaat da //ea co the /a /ay da //ea ta o nha nay hoac /aa tra da //ea rao o nha nay. Co
the go/ /a cac k/ea d/a ch/ hoa bo nha hay cac k/ea tao d/a ch/ de tray xaat bo nha.
e biet vi x ly co bao nhieu cac truy xuat bo nh can phai khao sat tng vi x ly cu
the. Cac kieu truy xuat nay c cho trong cac so tay che tao.
Cac kieu truy xuat a ch c ban cua mot vi x ly (c goi tat la kieu nh a ch):
Kieu nh a ch ngam nh (Implied Addressing Mode).
Kieu nh a ch tc thi (Immediate Addressing Mode).
Kieu nh a ch trc tiep (Direct Addressing Mode).
Kieu nh a ch gian tiep dung thanh ghi (Register Indirect Addressing Mode).
Kieu nh a ch ch so (Indexed Addressing Mode).
Kieu nh a ch tng oi (Relative Addressing Mode).
~ - | || ~ | |~ ||
e hieu cac kieu truy xuat phai dung tap lenh cua mot vi x ly 8 bit.
V du lenh cong: ADD reg
Lenh nay c hieu la noi dung cua thanh ghi A c cong vi noi dung cua thanh ghi
Reg ket qua lu tr vao thanh ghi A.
: - | || ~ | :| :|
Mot lenh c chia ra lam 2 phan th nhat la ma lenh hay con goi la ma cong tac, phan
th 2 la a ch. oi vi kieu a ch tc thi th phan th 2 la d lieu khong phai la a ch.
V du lenh nap mot d lieu tc thi vao thanh ghi A c viet nh sau: MVI A, FE
H
.
Trong o MVI la ma gi nh, FE la d lieu dang so Hex. V thanh ghi A ch co 8 bit nen d
lieu tc thi co o dai la 8 bit.
- | || ~ | :| :- :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 27
V du lenh di chuyen noi dung cua mot o nh co a ch 8000
H
vao thanh ghi A: LDA
8000
H
. LDA la ma gi nh, a ch 8000
H
c viet trc tiep trong cau lenh, vi vi x ly 8 bit
co 16 ng a ch nen phai dung 4 so Hex e ch nh mot o nh.
oi vi nhng lenh dung kieu a ch trc tiep th lenh co o dai la 3 byte: mot byte la
ma lenh, 2 byte con lai la a ch cua o nh (oi vi vi x ly 8 bit).
- | || ~ | ~ | :- : | | :|~|| |
e minh hoa kieu a ch gian tiep dung thanh ghi ta dung lenh sau:
V du: MOV A,M. Lenh nay se di chuyen noi dung cua o nh M co a ch cha trong
mot cap thanh ghi. oi vi vi x ly 8085 th a ch nay thng cha trong cap thanh ghi HL,
v a ch 16 bit nen phai dung cap thanh ghi mi cha het 16 bit a ch.
Cha y kh/ dang /enh k/ea nay ngaa/ /ap trnh pha/ qaan /y g/a tr/ trong cap thanh gh/.
- - | || ~ | | -
oi vi mot vai vi x ly co cac thanh ghi ch so (Index register) c dung cho kieu a
ch ch so.
Kieu a ch nay c thc hien bang cach cong byte th 2 cua lenh vi noi dung cua
thanh ghi ch so ID. V du: lenh cong noi dung thanh ghi A vi noi dung cua o nh co a ch
cha trong thanh ghi ch so ID vi byte d lieu th 2: ADD A, (ID +n). n la mot so co chieu
dai 8 bit.
- | || ~ | :||
Kieu a ch nay gan giong nh kieu a ch ch so nhng thanh ghi ID c thay the
bang thanh ghi PC. a ch cua o nh can truy xuat c tnh bang cach cong noi dung hien
tai cha trong thanh ghi PC cong vi byte d lieu th 2.
V du lenh JP 05
H
: nhay en ti thc hien lenh co a ch cach bo em chng trnh PC
la 5 byte.
|V. KHA0 5A1 V| XU L 8 8|1 Z80:
Sau khi khao sat mot vi x ly tong quat bay gi chung ta can khao sat mot vai vi x ly 8
bit cu the c s dung pho bien trong cac he thong ieu khien.
Mac du hien tai cac vi x ly ngay cang manh ve so bit, cao toc o lam viec, kha nang
truy xuat bo nh ln nhng cac vi x ly 8 bit van ton tai v chung c s dung trong cac he
thong ieu khien nho vi lng d lieu x ly khong nhieu v du nh may giat t ong th vi x ly
ch thc hien cac cong viec nh ieu khien ong c quay thuan nghch theo thi gian, hien th
thi gian tren led 7 oan, kiem tra cac valve e ong m nc, kiem tra mc nc,... lng d
lieu x ly khong nhieu neu so vi lng d lieu ma ca c vi x ly cua ma y tnh x ly.
Trong phan nay chung ta khao sat 2 vi x ly 8 bit: vi x ly 8085 cua hang Intel va vi x ly
Z80 cua hang Zilog.
Vi x ly Z80 cua hang Zilog c phat trien t vi x ly goc 8080 cua hang Intel.
1. .o !o ru v v r ve n onq rv u .i av |u Z8c.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
` Vi x ly
Cau truc cua vi x ly Z80 nh hnh 2-11 :

H|nh 2-. 5c dc cou truc ben trcng cuo v| xu |y Z80.
Trong s o cau truc cua vi x ly Z80 co:
Khoi ALU cung vi 2 thanh ghi co ten la Latch co chc nang giong nh a khao sa t.
Khoi cac thanh ghi gom co: thanh ghi A, B, C, D, H, L, F, A, B, C, D, H, L, F, IP, PC,
SP, IX va IY.
Bus d lieu 2 chieu D0 D7 dung giao tiep d lieu vi cac thiet b nen ngoai khoi em
bus d lieu co chc nang em e cho phep ket noi nhieu thiet b hay tang fan_out.
Bus d lieu ben trong cho phep lien lac d lieu gia cac khoi.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 29
Bus a ch mot chieu gom co 16 ng co chc nang tai a ch e truy xuat bo nh va
thiet b ngoai vi, khoi em bus a ch dung e tang fan-out.
Khoi giai ma lenh dung e giai ma lenh cho khoi ieu khien thc hien lenh.
Khoi ieu khien ngat.
Cac khoi con bao gom: khoi ieu khien bus, khoi ieu khien oc ghi bo nh, oc ghi IO,
khoi ieu khien vi x ly, khoi reset va lam ti.
Nguon s dung cho vi x ly la +5V, xung clock c cung cap t ben ngoai.
!. Io rnv r nunn qni ve n onq rv u .i av |u Z8c.
Vi x ly Z80 co 16 thanh ghi 8 bit va 4 thanh ghi 16 bit c trnh bay nh hnh 2-12. Tat
ca cac thanh ghi cua vi x ly Z80 c s dung giong nh cac o nh RAM tnh. Cac thanh ghi
bao gom 2 nhom thanh ghi thong dung co the s dung mot cach oc lap nh nhng thanh ghi 8
bit hoac co the ket hp lai tao thanh nhng cap thanh ghi 16 bit. Moi nhom thanh ghi gom 6
thanh ghi thong dung, mot thanh ghi tch luy A va mot thanh ghi tra ng thai.

H|nh 2-2. 5c dc chon cuo v| xu |y Z80.
1honh gh| PC: thanh ghi nay giong nh thanh ghi PC a trnh bay. Thanh ghi nay cha
a ch cua lenh hien hanh ang c on t bo nh. Noi dung cua thanh ghi PC c tang len
e quan ly lenh tiep theo sau khi noi dung cua no c chuyen sang bus a ch. Khi thc hien
lenh Jump th a ch cua lenh ni nhay en se c at vao thanh ghi PC va se ghi e len gia tr
a tang.
1honh gh| 5P: thanh ghi nay giong nh thanh ghi SP a trnh bay. Thanh ghi SP cha a
ch hien hanh cua bo nh ngan xep nam trong vung bo nh RAM. Vung nh RAM ben ngoai
c to chc theo kieu vao sau ra trc (LIFO). D lieu cua cac thanh ghi c cat vao ngan
xep hoac c lay ra khoi ngan xep bang cach thc hien cac lenh PUSH va POP. Bo nh ngan
xep con c dung e lu tr a ch cua thanh ghi PC khi thc hien lenh goi chng trnh con
hay khi CPU b ngat.
1honh gh| ch| sc |X vo |: 2 thanh ghi ch so lu a ch 16 bit c dung cho kieu nh
a ch dung ch so. Trong kieu nh a ch nay th mot thanh ghi se quan ly a ch cua vung d
lieu c lu tr hoac quan ly a ch cua vung d lieu can di chuyen.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
Ngoai ra 2 thanh ghi nay con c s dung trong kieu nh a ch dung ch so co o di se
tien ch hn khi bang d lieu c s dung.
1honh gh| |uu d|o ch| ngot (||: thng th cac ngat cua vi x ly co 1 a ch ngat co nh
v du nh ngat khong ngan c NMI th khi vi x ly ap ng ngat nay no se thc hien chng
trnh phuc vu ngat tai a ch 0066H do nha thiet ke qui nh. Vi kieu nh vay th ch co mot
chng trnh con phuc vu ngat va phai bat au tai a ch qui nh. e ap ng c nhieu
chng trnh con phuc vu ngat khac nhau th nha thiet ke vi x ly Z80 s dung ngat INT va viec
x ly a ch nh sau: a ch cua chng trnh con phuc vu ngat la 16 bit, trong o 8 bit cao c
lu trong thanh ghi I, con 8 bit a ch thap th do thiet b yeu cau ngat cung cap.
Cau truc nay cho phep cac chng trnh con phuc vu ngat co the nh v tai bat ky v tr
nao trong vung nh vi thi gian nhay en thc hien chng trnh con phuc vu ngat la toi thieu.
1honh gh| |om tuc| bc nhc k (refresh reg|ster|: vi x ly Z80 co mot bo em lam ti
bo nh e cho phep s dung bo nh ong DRAM giong nh SRAM. 7 bit cua thanh ghi 8 bit nay
chnh la bo em se t ong tang gia tr sau moi chu ky on lenh t bo nh. Bit th 8 con lai co
the lap trnh c t lenh LD R, A. D lieu em trong bo em 7 bit c gi en bus a ch
thap cung vi tn hieu lam ti bo nh khi CPU ang giai ma lenh va thc hien lenh a on ve.
Vi kieu lam ti bo nh nh the se khong lam cham toc o lam viec cua CPU.
Trong khoang thi gian lam ti bo nh th noi dung cua thanh ghi I c at len 8 bit cao
cua bus a ch.
1honh gh| Accumu|otcr vo thonh gh| F|og: vi x ly Z80 co 2 thanh ghi A va 2 thanh
ghi c 8 bit. Hoat ong cua cac thanh ghi nay giong nh a trnh bay phan trc.
Coc thonh gh| thcng dung: vi x ly Z80 co 2 nhom thanh ghi thong dung moi nhom
co 8 thanh ghi 8 bit co the s dung nh nhng thanh ghi 8 bit oc lap hay cung co the s dung
nh nhng cap thanh ghi 16 bit. Mot nhom c goi la BC, DE va HL trong khi nhom con lai la
BC, DE va HL.
Tai moi mot thi iem, ngi lap trnh co the la chon 1 trong 2 nhom thanh ghi e lam
viec thong qua lenh. Trong nhng he thong oi hoi ap ng ngat nhanh th nhom cac thanh ghi
thong dung cung vi thanh ghi A, thanh ghi c co the c e danh s dung cho chng trnh con
phuc vu ngat yeu cau ap ng nhanh ch can thc hien 1 lenh trao oi e chuyen sang chng
trnh con. Chc nang nay lam thi gian ap ng yeu cau ngat nhanh hn v neu khong co chc
nang nay th phai tien hanh cat d lieu cua cac thanh ghi vao bo nh ngan xep lam ton nhieu
thi gian trc khi thc hien chng trnh con phuc vu ngat.
Khc| ALU: khoi ALU ben trong vi x ly Z80 co chc nang thc hien cac lenh so hoc va
lenh logic. Khoi ALU ket noi vi cac thanh ghi ben trong va bus d lieu ben ngoai thong qua bus
d lieu ben trong. Cac lenh c thc hien bi khoi ALU bao gom:
Lenh cong, lenh tr, lenh AND, lenh OR, lenh EX-OR, lenh so sanh , lenh dch trai, dch
phai, lenh xoay, lenh tang, lenh giam, lenh set bit, lenh reset bit lenh test bit.
1honh gh| |enh |k vo khc| d|eu kh|en CPU: moi ma lenh c on ve t bo nh se lu
trong thanh ghi lenh IR va se c giai ma. Cac thanh phan cua khoi ieu khien se thc hien
chc nang nay, sau o khoi ieu khien tao ra va cung cap cac tn hieu ieu khien can thiet e
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 31
oc d lieu t cac thanh ghi hoac ghi d lieu len cac thanh ghi, ieu khien khoi ALU thc hien
phep toan va ta o ra cac tn hieu ieu khien cac oi tng ben ngoai.
. .nv r nu nq ru r rnu n rv u .i av |u Z8c.
Vi x ly Z80 co s o chan nh hnh 2-13:

H|nh 2-3. 5c dc chon cuo v| xu |y Z80.
Vi x ly Z80 co 40 chan, trong o:
Bus a ch co 16 chan t A0 A15.
Bus d lieu co 8 chan t D0 D7.
Bus ieu khien he thong co 6 chan: 1 M , MREQ, IORQ, RD, WR, RFSH
ieu khien CPU co 5 chan: HALT , WAIT , INT , NMI , RESET
ieu khien bus co 2 chan: BUSRQ, BUSACK
Nguon cung cap 2 chan: Vcc va GND.
Nguon nhan xung clock 1 chan CLK.
Giong nh vi x ly tong quat th chc nang cua bus a ch dung e tai a ch khi vi x ly
truy xuat bo nh hoac ngoai vi IO. Chc nang cua bus d lieu la tai d lieu gia vi x ly vi bo
nh hoac thiet b ngoai vi. Hai bus a ch va d lieu dung e ket noi vi bo nh va ngoai vi IO.
Tn hieu RD, WR, MREQ, IORQ: la cac chan xuat tn hieu, khi vi x ly ieu khien cac
chan RD, WR cung vi tn hieu MREQ e thc hien qua trnh oc hoac ghi d lieu gia vi x
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
ly vi bo nh. Khi vi x ly ieu khien cac chan RD, WR cung vi tn hieu IORQ e thc hien
qua trnh oc hoac ghi d lieu gia vi x ly vi thiet b ngoai vi IO. Trong he thong ta phai s
dung cac chan nay e giao tiep vi bo nh va ngoai vi IO.
Tn hieu RFSH dung e ieu khien lam ti bo nh RAM. Khi khong can lam ti th
chan nay e trong.
Tn hieu 1 M la tn hieu bao hieu mot chu ky may. Tn hieu 1 M ket hp vi tn hieu
MREQ e xac nh chu ky may hien tai la chu ky on lenh trong qua trnh thc hien lenh. Tn
hieu 1 M ket hp vi tn hieu IORQ e xac nh chu ky tra li ngat.
Tn hieu INT la tn hieu yeu cau ngat t ben ngoai hay t thiet b ngoai vi tch cc mc
thap. CPU se thc hien yeu cau ngat sau khi thc hien xong lenh ang thc hien va tr khi ngat
nay c cho phep bi phan mem. Khi s dung th ngo vao nay thng noi vi ien tr keo len
e khi tac ong th xuong mc thap.
Tn hieu NMI la tn hieu yeu cau ngat t ben ngoai hay t thiet b ngoai vi tch cc canh
xuong. Ngat NMI cao hn so vi ngat INT va la tn hieu ngat khong the ngan c . CPU se
thc hien yeu cau ngat sau khi thc hien xong lenh ang thc hien, a ch cua chng trnh con
phuc vu ngat tai a ch 0066H.
Tn hieu RESET la tn hieu ngo vao tch cc mc thap, khi ngo vao reset b tac ong th
CPU se reset tat ca cac bit cho phep ngat, xoa thanh ghi PC, cac thanh ghi I va R thiet lap trang
thai ngat mode 0. Trong khoang thi gian ngat th cac bus a ch va bus d lieu trang thai
tong tr cao, tat ca cac ng tn hieu ieu khien eu trang thai khong tch cc. Chu y tn hieu
reset phai trang thai tch cc trong khoang thi gian t nhat la 3 chu ky xung clock.
Tn hieu WAIT la tn hieu ngo vao tch cc mc thap, tn hieu nay (khi mc cao) dung
e bao cho CPU biet o nh ang truy xuat hay thiet b ngoai vi ang truy xuat cha san sang cho
viec nhan hay gi d lieu. CPU se vao trang thai ch cho en khi tn hieu WAIT tr lai trang
thai tch cc mc thap.
Tn hieu HALT la tn hieu ngo ra tch cc mc thap, tn hieu nay (khi mc cao) dung e
bao cho thiet b ben ngoai biet: CPU ang thc hien lenh HALT va ang i mot trong 2 yeu
cau ngat xay ra trc khi khoi phuc lai hoat ong bnh thng. Trong khoang thi gian HALT th
CPU thc hien lenh NOP e tiep tuc gi hoat ong lam ti bo nh.
Tn hieu BUSRQ la tn hieu ngo vao tch cc mc thap, tn hieu nay co mc u tien cao
hn ngat khong che c NMI va no se c thc hien ngay sau moi chu ky may hay chu ky
xung clock. Tn hieu nay se yeu cau vi x ly ieu khien bus a ch, bus d lieu va cac ng
tn hieu ieu khien MREQ, IORQ, RD, WR trang thai tong tr cao va thiet b khac co the s
dung cac bus trong he thong.
Tn hieu BUSACK la tn hieu ngo ra tch cc mc thap, tn hieu nay xac nh CPU ieu
khien bus a ch, bus d lieu va cac ng tn hieu ieu khien MREQ, IORQ, RD, WR
trang thai tong tr cao va thiet b khac co the s dung cac bus trong he thong. Ta co the xem tn
hieu BUSACK la tn hieu tra li cua CPU khi CPU nhan tn hieu yeu cau nhng bus BUSRQ.
!. Giu n !o no i qiun rv u .i av |u Z8c.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 33
Vi x ly Z80 thc hien cac lenh theo tng bc thong qua cac hoat ong c ban bao gom:
oc hoac ghi bo nh.
oc hoac ghi IO.
Tra li ngat.
Tat ca cac lenh la mot chuoi hoat ong c ban noi tiep nhau. Moi mot hoat ong co the
thc hien t 3 chu ky xung clock en 6 chu ky xung clock e hoan tat hoac co the b keo dai hn
e ong bo toc o lam viec cua CPU vi toc o lam viec cua cac thiet b ben ngoai.
Cac chu ky xung clock c xem la cac chu ky T (time) va cac hoat ong c xem la cac
chu ky may M (machine). Hnh 2-14 minh hoa cac chu ky xung clock T va chu ky may M khi
CPU thc hien lien tuc cac lenh:

H|nh 2-4. 5c dc chon cuo v| xu |y Z80.
Chu y lenh nay bao gom 3 chu ky may M1, M2 va M3.
chu ky may th nhat cua bat ky lenh nao chnh la chu ky on lenh t bo nh chu ky
nay can t 4, 5 hoac 6 chu ky xung clock T. Chu ky on lenh c s dung e on ma lenh cua
lenh ke e thc hien.
Tiep theo la cac chu ky di chuyen d lieu (oc hoac ghi) gia CPU va bo nh hoac thiet b
ngoai vi IO xay ra khoang t 3 en 5 chu ky xung clock T.
Trong khoang thi gian T2 va moi chu ky i Tw theo sau th CPU se lay mau tn hieu ngo
vao WAIT khi co canh xuong cua xung clock. Neu tn hieu WAIT trang thai tch cc th mot
chu ky i tiep theo se c thc hien. S dung ky thuat nay e CPU co the keo dai thi gian
oc hoac ghi e tng thch vi bat ky loai bo nh nao.
Chu ky cn |enh: hnh 2-15 la gian o thi gian cua chu ky on lenh t bo nh hay chu
ky M1. Trong chu ky nay CPU at a ch len bus a ch tai thi iem bat au cua chu ky M1.
Sau na chu ky cua xung clock: th tn hieu MREQ chuyen sang trang thai tch cc. Tai thi
iem nay a ch tren bus a co u thi gian e on nh, do o canh xuong cua tn hieu MREQ
c dung nh xung clock cho phep chip oi vi bo nh ong.
Tn hieu RD cung chuyen sang trang thai tch cc e xac nh rang d lieu oc t bo nh
se c phep vao bus d lieu cua CPU.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly
CPU se lay mau d lieu t bo nh tren bus d lieu khi co canh len cua xung clock chu ky
T3 va ong thi CPU se chuyen cac tn hieu MREQ, RD trang thai khong tch cc. Vay CPU a
lay d lieu trc khi chuyen tn hieu RD sang trang thai khong tch cc. Cac chu ky T3 va T4
dung e lam ti cac bo nh RAM ong. CPU se dung cac khoang thi gian nay e giai ma lenh
va thc hien lenh a c on nen se khong co hoat ong nao c thc hien tong khoang thi
gian cua T3 va T4.
Chu y d lieu oc chu ky nay la ma lenh.

H|nh 2-5. Chu ky dcn mo |enh.
Chu ky dcc hcoc gh| du ||eu : hnh 2-16 la gian o thi gian cua chu ky oc hoac ghi d
lieu gia CPU vi bo nh khac vi chu ky on lenh. Tn hieu MREQ va tn hieu RD c ieu
khien giong nhau trong chu ky oc d lieu.
Trong chu ky ghi d lieu len bo nh, tn hieu MREQ cung trang thai tch cc khi a ch
tren bus a on nh nen no c dung nh la tn hieu cho phep oi vi bo nh ong.
Tn hieu WR se chuyen sang trang thai tch cc khi d lieu tren bus a on nh nen no
c dung e ieu khien chan WR cua bo nh e yeu cau bo nh cat d lieu tren bus d lieu.
Tn hieu WR se chuyen sang trang thai khong tch cc trc khi a ch va d lieu tren bus
thay oi hay trc khi chuyen sang chu ky may ke tiep.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 35

H|nh 2-6. Chu ky dcc hcoc gh| bc nhc.
Chu ky dcc hcoc gh| ngco| v| : hnh 2-17 la gian o thi gian cua chu ky oc hoac ghi
d lieu gia CPU va thiet b ngoai vi. Trong khoang thi gian oc /ghi IO th CPU thng t
ong xen vao 1 chu ky i. Ly do chen them 1 chu ky i la do khoang thi gian CPU ieu khien
chan IORQ sang trang thai tch cc cho en khi lay mau rat ngan. Neu khong chen them chu ky
i th cac thiet b IO loai MOS se khong bat kp toc o lam viec cua CPU.
Trong khoang thi gian i th tn hieu i WAIT se c lay mau.
Trong hoat ong oc d lieu t IO th tn hieu RD c dung e yeu cau thiet b IO a ch
nh xuat d lieu, trong hoat ong ghi d lieu len IO th tn hieu WR c dung e yeu cau thiet
b IO a ch nh nhan d lieu.

H|nh 2-7. Chu ky dcc hcoc gh| th|et b| ngco| v|.
Chu ky yeu cou bus/tro |c| yeu cou bus : hnh 2-18 la gian o thi gian cua chu ky
yeu cau bus /tra li bus. Tn hieu BUSRQ c CPU lay mau khi co canh len cua xung clock
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
cuoi cung cua moi chu ky may. Neu tn hieu BUSRQ trang thai tch cc th CPU thiet lap bus
a ch, bus d lieu va cac tn hieu ieu khien 3 trang thai sang trang thai tong tr cao ngay khi
co canh len cua xung clock ke. Tai thi iem nay bat ky thiet b nao ben ngoai co the ieu
khien cac bus e chuyen d lieu gia bo nh va cac thiet b ngoai vi IO (hoat ong nay thng
c goi la truy xuat bo nh trc tiep DMA (direct memory access).
Thi gian toi a e CPU ap ng yeu cau nhng bus dai ung bang 1 chu ky may va thiet
b yeu cau nhng bus phai gi tn hieu ieu khien BUSRQ trang thai tch cc trong khoang
ung bang 1 chu ky may. Neu thi gian yeu cau nhng bus qua dai va neu he thong s dung bo
nh ong th thiet b yeu cau nhng bus phai thc hien cong viec lam ti bo nh. Trng hp
nay ch xay ra khi khoi lng d lieu qua ln can phai di chuyen bi DMA. Trong khoang thi
gian thc hien yeu cau nhng bus th CPU khong the thc hien bat ky yeu cau ngat nao cho du
la ngat khong che c.

H|nh 2-8. Chu ky dcc hcoc gh| th|et b| ngco| v|.
Chu ky yeu cou ngot/tro |c| ngot : hnh 2-19 la gian o thi gian thc hien yeu cau
ngat cua CPU. CPU lay mau tn hieu INT khi co canh len cua xung clock cuoi cung cua moi
chu ky on ma lenh. Tn hieu ngat INT se khong c ap ng neu nh b cam bi phan mem
hoac tn hieu yeu cau nhng bus ang trang thai tch cc. Trong khoang thi gian cua chu ky
M1 ke tiep, tn hieu IORQ tr nen tch cc e xac nh rang thiet b yeu cau ngat co the at
vector a ch ngat len bus d lieu. Hai chu ky trang thai i c t ong them vao trong chu ky
ap ng yeu cau ngat. Hai trang thai nay c them vao e u thi gian cho cac tn hieu e on
nh va xac nh thiet b yeu cau ngat nao can phai a vector a ch ngat.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 37

H|nh 2-. Chu ky ngot vo dop ung yeu cou ngot.
op ung ngot khcng ngon ducc : hnh 2-20 la gian o thi gian thc hien yeu cau
ngat khong ngan c NMI cua CPU. CPU lay mau tn hieu NMI giong nh ngat INT nhng
yeu cau ngat NMI co mc o u tien cao nhat va khong the cam bang phan mem. Ngat nay
thng c s dung e CPU ap ng tc thi cho nhng yeu cau quan trong nh s co hong cua
nguon ien.
CPU ap ng yeu cau ngat khong ngan c NMI tng t nh hoat ong oc bo nh bnh
thng. S khac biet la noi dung cua d lieu tren bus b bo qua trong khi o vi x ly t ong lu
tr noi dung cua thanh ghi PC ( la a ch lenh ke) vao bo nh ngan xep ben ngoai va thc hien
chng trnh tai a ch 0066H. Chng trnh con phuc vu ngat phai c viet tai a ch nay.

H|nh 2-20. C|on dc thc| g|on cuo ngot khcng ngon ducc NMI .
Lenh Ho|t: khi thc hien lenh Halt th CPU thc hien lenh NOP cho en khi CPU nhan
c tn hieu yeu cau ngat ( INT hoac NMI ). Hai tn hieu ngat nay c lay mau khi co canh
len cua xung clock moi chu ky T4 nh hnh 2-21.
Khi co yeu cau ngat khong ngan c NMI hoac ngat INT c cho phep bi phan mem
th sau o CPU se thoat khoi trang thai Halt khi co canh len cua xung clock ke.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
` Vi x ly
Chu ky tiep theo la ap ng yeu cau ngat tuy thuoc vao ngat nhan c. Neu ca 2 ngat
cung xay ra th ngat co mc u tien cao hn se c ap ng chnh la ngat NMI .
Chc nang thc hien lenh NOP khi CPU trang thai Halt nham e lam ti bo nh RAM
ong. Moi chu ky trong trang thai Halt c xem la chu ky M1 (on lenh) ch ngoai tr d lieu
nhan t bo nh b bo qua va lenh NOP c thc hien ben trong vi x ly. Tn hieu tra li HALT
se trang thai tch cc e bao cho biet vi x ly ang trang thai HALT.

H|nh 2-2. C|on dc thc| g|on cuo ngot khcng ngon ducc NMI .
. u v v nq ue v ru v nqu rv u .i av |u Z8c.
C|c| th|eu:
Mot yeu cau ngat cho phep cac thiet b ngoai vi ngng hoat ong cua CPU va yeu cau
CPU phai thc hien chng trnh con phuc vu ngat cua thiet b yeu cau ngat o. Chng trnh
con phuc vu ngat bao gom cac cong viec trao oi d lieu, trang thai hoac cac thong tin ieu
khien gia CPU va thiet b ngoai vi. Khi chng trnh phuc vu ngat ket thuc th CPU tr lai thc
hien tiep chng trnh ang gian oan.
Chc phep/com ngot:
Vi x ly Z80 co 2 ngo vao ngat: mot la ngat co the che c (cho phep/cam bang phan
mem ) INT va mot ngat khong the che c NMI . Ngat khong the che c co ngha la khong
the cam bang phan mem, ngat nay thng c s dung cho cac chc nang quan trong c la
chon bi ngi lap trnh.
oi vi ngat co the che c INT th Z80 co mot flip flop cho phep ngat (interrupt flip
flop = IFF) va ngi lap trnh co the set hay reset bang cach dung cac lenh cho phep ngat EI
(enable interrupt) va lenh cam ngat DI (disable interrupt). Khi bit IFF b reset th CPU se khong
thc hien yeu cau ngat INT .
Co 2 bit flip flop nh hnh 2-22:

H|nh 2-22. Ho| b|t f||p f|cp |FF vo |FF2 cuo ngot INT .
Khi CPU b reset th CPU se reset ca 2 bit IFF1 va IFF2 e cam ngat. Cac yeu cau co the
c phep tai bat ky thi iem nao phu thuoc vao lenh EI cua ngi lap trnh. Khi lenh EI c
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 39
thc hien th bat ky yeu cau ngat nao ang i (v cha c phep) th se c ap ng ngay sau
khi CPU thc hien xong lenh EI.
Mot lenh tr hoan la rat can thiet c thc hien trc khi thc hien lenh ket thuc chng
trnh con phuc vu ngat tr lai chng trnh chnh. Cac yeu cau ngat khong c phep cho en khi
thc hien xong lenh tr ve. Khi CPU thc hien yeu cau ngat co the che c th ca 2 bit IFF1 va
IFF2 b reset e cam cac yeu cau ngat khac cho en khi thc hien lai lenh EI.
Vai tro cua bit IFF2 la cat trang thai cua IFF1 khi xay ra yeu cau ngat khong ngan c
NMI . Khi yeu cau ngat NMI c chap nhan th bit IFF1 b reset e cam cac yeu cau ngat
khac xay ra cho en khi c cho phep tr lai bi ngi lap trnh. Vay sau khi ngat NMI c
chap nhan, cac yeu cau ngat co the ngan c b cam nhng trang thai trc o cua IFF1 c
cat e tiep tuc cho phep ngat sau khi CPU thc hien xong yeu cau ngat NMI . Khi lenh LD
A,I hoac lenh LD A,R c thc hien th trang thai cua IFF2 c copy vao c chan le trong
thanh ghi tra ng thai e kiem tra hay e lu tr.
Mot phng phap khac e khoi phuc lai trang thai cua IFF1 bang cach thc hien lenh tr
ve t lenh ngat khong ngan c RETI (return from non mask able interrupt). Lenh RETI la
lenh ket thuc chng trnh con phuc vu ngat e tr ve chng trnh chnh va noi dung cua IFF2
c copy tr lai IFF1 e cho trang thai cua IFF1 trc khi chap nhan yeu cau ngat khong ngan
c t ong khoi phuc.
Bang liet ke trang thai cac flip flop cho phep /cam ngat:
Hoa t ong IFF1 IFF2 Dien giai
Khi CPU b reset 0 0 Nga t ngan c b cam
Thc hien lenh DI 0 0 Nga t ngan c b cam
Thc hien lenh EI 1 1 Nga t ngan c c phep
Thc hien lenh LD A,I * * IFF2 c chan le
Thc hien lenh LD A,R * * IFF2 c chan le
Thc hien ngat NMI
0 * Nga t ngan c b cam
Thc hien lenh RETI IFF2 * IFF2 xac nh ket thuc chng trnh
con phuc vu ngat khong the ngan c.
8ong 2-. 8ong ||et ke coc trong tho| b|t |FF vo |FF2.
Ngot khcng ngon ducc:
CPU thng chap nhan yeu cau ngat khong ngan c. Khi ngat nay xay ra th CPU khong
thc hien lenh ke ma tien hanh thc hien lenh bat au tai a ch 0066H. Hoat ong nay cua
CPU giong nh thc hien lenh khi ong lai nhng khac a ch. Lenh khi ong n thuan ch
la lenh goi chng trnh tai mot a ch ac biet trong trang th 0 cua bo nh.
CPU co the c lap trnh e ap ng ngat khong ngan c bang mot trong 3 cach.
!oce :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly
Trong mode nay thiet b yeu cau ngat co the at bat ky lenh nao len bus d lieu va CPU
se thc hien. Do o thiet b yeu cau ngat cung cap lenh ke e c thc hien. Thng la lenh
khi ong lai bi v thiet b yeu cau ngat ch can cung cap mot byte ma lenh duy nhat.
Tng t oi vi lenh khac chang han nh lenh call (co 3 byte ma lenh) th chng trnh co
the thc hien tai bat ky v tr na o trong bo nh.
So lng chu ky xung clock can thiet e thc hien ap ng ngat nhieu hn thc hien lenh
bnh thng 2 chu ky xung clock. Ly do la CPU t ong them vao 2 chu ky i e chu ky ap
ng yeu cau ngat co u thi gian thc hien. Sau khi reset th CPU se t ong hoat ong mode
0.
!oce :
Khi mode 1 c la chon bi ngi lap trnh th CPU ap ng ngat bang cach thc hien
chng trnh bat au tai a ch 0038H giong nh ngat khong ngan c th thc hien a ch
0066H.
So lng chu ky yeu cau e thc hien nhieu hn 2 chu ky do CPU cong them 2 chu ky i.
!oce 2:
ay la mode manh nhat trong cac mode ap ng yeu cau ngat. Vi byte 8 bit duy nhat t
ngi dung th mot lenh goi gian tiep co the c thc hien bat ky o nh nao.
Trong mode nay ngi lap trnh thiet lap mot bang cac a ch bat au 16 bit cho moi
chng trnh con phuc vu ngat nh hnh 2-23. Bang nay co the c nh v bat ky v tr nao
trong bo nh. Khi ngat c chap nhan th con tro 16 bit phai c thiet lap e co c a ch
bat au chng trnh con phuc vu ngat yeu cau trong bang a ch.
8 bit a ch cao cua con tro c thiet lap t noi dung cua thanh ghi I, thanh ghi I phai
c nap gia tr a ch cua chng trnh con bi ngi lap trnh bang lenh LD I,A. Khi CPU b
reset th gia tr trong thanh ghi I bang 00H.
8 bit a ch thap cua con tro phai c cung cap bi thiet b ngat. Ch co 7 bit c yeu
cau t thiet b yeu cau ngat con bit LSB phai bang 0. ieu nay la bat buoc bi v con tro c s
dung e lay 2 byte lien tiep e tao a ch bat au cua chng trnh con phuc vu ngat 16 bit va
cac a ch phai luon bat au tai o nh co a ch chan.

H|nh 2-23. op ung ngot c mcde 2.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 41
Byte th nhat trong bang la byte a ch thap. Ngi lap trnh phai thiet lap ay u a ch
chnh xac trc khi yeu cau c chap nhan.
Ngi lap trnh co the thay oi bang a ch e mot yeu cau ngat co the thc hien nhieu
chng trnh con phuc vu ngat khac nhau.
Khi thiet b yeu cau ngat cung cap a ch thap en con tro th CPU t ong cat a ch cua
thanh ghi PC vao bo nh ngan xep, nhan a ch bat au cua chng trnh con phuc vu ngat t
bang va bat au thc hien chng trnh tai a ch o.
Mode nay can 19 chu ky xung clock e thc hien (7 chu ky e on 8 bit a ch thap t
thiet b yeu cau ngat, 6 chu ky e cat noi dung thanh ghi PC vao bo nh ngan xep va 6 chu ky e
nhay en a ch cua chng trnh con phuc vu ngat).
. 1nu n rv nq .u vnu n we w rv u .i av |u Z8c.
Phon cung :
e :|o r :o . :|.e . :
Trong phan nay se gii thieu cac he thong ng dung dung vi x ly Z80. Hnh 2-24 trnh bay
mot he thong n gian dung Z80.

H|nh 2-24. Ho| b|t f||p f|cp |FF vo |FF2 cuo ngot INT .
Bat ky he thong dung vi x ly Z80 eu phai co cac khoi sau:
Khoi nguon cung cap 5V.
Khoi dao ong.
Cac IC nh.
Cac mach ien ket noi vi thiet b ngoai vi.
CPU Z80.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly
Bo nh ngoai cua Z80 co the la tong hp 2 bo nh chuan RAM, ROM hoac PROM. Trong
hnh 2-24 co bo nh ROM 8Kbit (1kbyte), cac thanh ghi ben trong u e lam cac o nh co the
oc ghi nen khong can them bo nh Ram ben ngoai.
Cac mach IO cho phep he thong giao tiep vi cac thiet b ben ngoai. Trong hnh 2-24 co 8
ngo ra ieu khien va co 8 ngo vao nhan tn hieu t ben ngoai. Cac d lieu t ben ngoai co the
a vao ben trong vi x ly thong khoi ieu khien 3 trang thai, khoi ngo ra c chot bi mach
chot dung TTL chuan. IC IO Z80 PIO se thc hien chc nang nay.
!o ro r ro r|o <! :
Hau het cac he thong may tnh eu dung bo nh m rong Read/Write e lu tr d lieu va
lam bo nh ngan xep. Hnh 2-25 trnh bay bo nh co 256 byte bo nh RAM tnh c them vao
he thong hnh 2-24. Khong gian bo nh c them vao co a ch nh sau:

a ch cua bo nh c mo ta dang so HEX. Khi phan tch ra thanh so nh phan th bit
a ch A10 se la ng phan chia bo nh ROM va bo nh RAM nen se dung ng a ch nay
e ieu khien chon chip. Khi lng bo nh m rong kha ln co nhieu RAM, nhieu ROM th phai
dung mach giai ma TTL e thc hien chon chip.

H|nh 2-25. Mc rcng them bc nhc.
r.e . ||.e r :o c co ro r|o :
Bo nh co toc o thap th gia thanh cung thap dan en gia thanh cua he thong cung thap.
Tn hieu WAIT cua vi x ly cho phep vi x ly co the lam viec vi cac bo nh co toc o bat ky
nhanh hay cham. Nh a trnh bay phan gian o thi gian truy xuat bo nh, tat ca cac chu ky
truy xuat bo nh se hoan tat trong khoang thi gian na chu ky, tuy nhien trong mot so trng
hp can phai them mot chu ky i trong chu ky M1 e co the s dung cac bo nh co toc o
cham.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 43
Hnh 2-26 la mot mach ien n gian e cong them mot chu ky i. Mach ien hnh 2-26
co the thay oi nh hnh 2-27 e co the cong them mot chu ky i e truy xuat bat ky bo nh
nao.

H|nh 2-26. 1hem mct chu ky dc| voc chu ky M.

H|nh 2-27. 1hem mct chu ky dc| de truy xuot bc nhc bot ky.
c.:o :.e r .o . ro r|o co r:
Moi bo nh RAM ong co cac thong so lam viec. Hnh 2-28 minh hoa mach ien dung cac
cong logic e giao tiep bo nh RAM ong 8 kbyte s dung 2 bo nh 4Kbyte.
Gia s ng a ch A12 c dung lam ng ieu khien chon chip select.
Trong khoang thi gian lam ti bo nh, tat ca cac bo nh trong he thong phai c oc.
CPU cung cap a ch lam ti bo nh tren cac ng a ch t A0 en A6. Khi m rong bo nh
cho he thong nay th ch can thay the cac cong logic hoat ong vi ng a ch A12 bang mach
giai ma hoat ong vi cac ng a ch con lai tuy dung lng bo nh m rong them.
Can phai co cac bo em cho cac ng a ch khi he thong giao tiep nhieu bo nh.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
-- Vi x ly


H|nh 2-28. 1hem mct chu ky dc| de truy xuot bc nhc bot ky.
Phon mem vo coc chucng tr|nh v| du :
o r c.:r .e c: . :r. c r|: r re r :
Tap lenh cua vi x ly Z80 cung cap cho ngi s dung tat ca cac hoat ong e ieu khien
cpu Z80. Cac thanh ghi chnh, thanh ghi luan phien va cac thanh ghi ch so co the lu tr d lieu
cho cac lenh logic va lenh so hoc, lu tr a ch cua bo nh hoac lu tr d lieu.
D lieu co the chuyen t thanh ghi sang thanh ghi, t bo nh sang bo nh, t bo nh sang
thanh ghi hoac t thanh ghi sang bo nh. Ben canh o con co cac lenh trao oi d lieu gi cac
thanh ghi, gia thanh ghi va bo nh.
ac biet la noi dung cua cac thanh ghi chnh va thanh ghi luan phien co the trao oi bang
hai lenh duy nhat la EX va EXX. Lenh trao oi d lieu cua cac thanh ghi c dung e chia tap
cac thanh ghi ang lam viec khac vi tap thanh ghi trong chng trnh con hay trong thu tuc.
Viec cat d lieu cua cac thanh ghi tam thi vao bo nh ngan xep va lay d lieu ra khoi bo
nh ngan xep co the c thc hien bang lenh Push va lenh Pop. a ch cua bo nh ngan xep
c quan ly bi thanh ghi SP.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 45
Khi thc hien chng trnh con th a ch cua lenh ke trong thanh ghi PC c CPU t
ong cat vao bo nh ngan xep va khi ket thuc chng trnh con bang lenh tr ve th a ch c
tra lai cho thanh ghi PC va chng trnh chnh c thc hien tiep tuc.
Ket qua thc hien cua cac lenh logic, lenh so hoc, lenh dch, lenh so sanh se ieu khien 6
c trang thai trong thanh ghi trang thai.
Sau khi thc hien mot lenh th cac thanh ghi c se b tac ong tuy thuoc vao ket qua sau
khi x ly, cac c c dung e lam ieu kien cho lenh nhay hoac lenh tr ve.
Mot nhom lenh ay u cac lenh logic bao gom lenh AND, OR, XOR, CPL, NEG co the
thc hien vi d lieu 8 bit gia thanh ghi A va tat ca cac thanh ghi con lai, gia thanh ghi A va
bo nh va gia thanh ghi A va d lieu tc thi.
Ngoai ra con co cac lenh so hoc va cac lenh dch theo ca hai chieu, lenh nay thc hien
c oi vi d lieu 8 bit cha trong cac thanh ghi 8 bit hoac trong mot o nh co a ch bat ky.
C carry co the c s dung trong cac lenh xoay, lech dch e tao d lieu au vao hay nhan d
lieu dch au ra e kiem tra ket qua cua lenh dch.
.: c c|.or :rr| . c. :
/ c. :
Chng trnh thc hien viec copy 737 byte d lieu trong vung nh bat au tai a ch
DATA sang vung nh co a ch bat au BUFFER nh sau:
LD HL, DATA ;HL quan ly a ch bat au vung d lieu DATA
LD DE, BUFFER ;DE quan ly a ch bat au vung d lieu BUFFER
LD BC, 737 ;BC quan ly chieu dai vung d lieu.
LDIR ;copy chuoi c ch nh bi HL sang vung nh c
;ch nh bi DE, giam HL, giam DE va giam BC tiep
;tuc thc hien cho en khi BC = 0.

Khi bien dch th oan chng trnh tren chiem 11 byte va moi mot byte khi di chuyen mat
21 chu ky xung clock.
/ c. 2:
Chng trnh thc hien viec copy mot chuoi (co chieu dai toi a la 132) bat au tai a ch
bat au DATA sang mot vung nh co a ch bat au BUFFER cho en khi k t $ c tm thay:
LD HL, DATA ;HL quan ly a ch bat au vung d lieu DATA
LD DE, BUFFER ;DE quan ly a ch bat au vung d lieu BUFFER
LD BC, 132 ;BC quan ly chieu dai vung d lieu.
LD A, $ ;Nap k t $ vao A e so sanh.
LOOP: CP (HL) ;So sanh noi dung o nh vi A.
JR Z, END ;nhay ket thuc neu ung la k t $.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly
LDI ;khong ung th tien hanh copy d lieu va tang .
JP PE, LOOP ;nhay lai lam tiep cho en khi het 132 k t.
END:

Khi bien dch th oan chng trnh tren chiem 19 byte.
Mc to |enh cuo v| xu |y Z80 :
o r c.:r .e :: r |e r|:
Vi x ly Z80 co 158 lenh khac nhau bao gom luon ca 78 lenh cua vi x ly 8080A. Cac lenh
nay c chia ra lam cac nhom nh sau:
Lenh nap va trao oi d lieu.
Lenh chuyen khoi d lieu va lenh tm kiem.
Lenh so hoc va lenh logic.
Lenh xoay va lenh dch.
Lenh ieu khien bit: set, reset va test.
Lenh nhay, lenh goi chng trnh con va lenh ket thuc chng trnh con.
Lenh xuat nhap IO
Cac lenh ieu khien CPU.
.: c |.e . c.r| c.: c|. :
Hau het cac lenh cua vi x ly Z80 eu x ly d lieu c lu tr cac thanh ghi ben trong
CPU, lu tr bo nh ngoai hoac lu tr IO. Kieu nh a ch e ch ro a ch cua d lieu ma
lenh se x ly.
Cac kieu nh a ch cua vi x ly Z80 giong nh nhng kieu a trnh bay phan trc,
cac kieu bao gom:
.e . c.r| c.: c|. :. c :|o . ('rrec.::e):
.e . c.r| c.: c|. :. c :|o . ro ro r ('rrec.::e x:ercec): giong nh kieu nh a
ch tc thi nh d lieu la 16 bit c nap cho cap thanh ghi 8 bit.
.e . c.r| c.: c|. |.e . c|.r| :r:r ro r|o zero : Z80 co 1 lenh goi ac biet ch co 1
byte e xac nh 1 trong 8 v tr trong trang bo nh zero. Ma lenh cua lenh nay ch la 1 byte
nhng cho phep thanh lap a ch 16 bit va lenh nay c dung e goi cac chng trnh thng
s dung nam trong trang bo nh e tiet kiem dung lng bo nh (v lenh 1 byte thay v lenh 3
byte).

.e . c.r| c.: c|. :.or co . (re|::..e :ccreee.r):
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 47
.e . c.r| c.: c|. ro ro r(x:ercec ):
Kieu nay cung cap luon 2 byte (16 bit) cua a ch trong lenh. 2 byte a ch co the la a
ch cua lenh nhay hay la a ch cua d lieu.
.e . c.r| c.: c|. c|. eo ('rcexec :ccreee.r):
.e . c.r| c.: c|. :|:r| |. (re.e:er :ccreee.r):
.e . c.r| c.: c|. |.e . r: r ('rr|.ec :ccreee.r):
.e . c.r| c.: c|. .: r :.e r c. r :|:r| |. (re.e:er .rc.rec: :ccreee.r):
.e . c.r| c.: c|. r.: (.: :ccreee.r):
!o :: .: . |e r| c. : .. x. |y Z:
e r| ! .. : c: c :|:r| |. :
Hnh 2-29 tom tat lenh chuyen d lieu t thanh ghi nguon 8 bit reg8S sang thanh ghi ch
8 bit reg8D.
Ma lenh 1 byte 8 bit bao gom 2 bit 01[reg8D][reg8S]: trong o [reg8D] la 3 bit cua thanh
ghi reg8D va [reg8S] la 3 bit cua thanh ghi reg8S.
Moi thanh ghi co 3 bit c trnh bay trong hnh, thi gian thc hien lenh mat 4 chu ky T
va mat 7 chu ky T oi vi lenh chuyen d lieu gia o nh (co a ch lu trong HL) va thanh ghi.
Neu ghi chi tiet cho tng thanh ghi th lenh tong quat nay co tong cong 64 lenh: 8 lenh cho thanh
ghi A va cac thanh ghi, 8 lenh cho thanh ghi B va cac thanh ghi,
Chu y: (HL) la o nh co a ch lu trong HL c xem nh la 1 thanh ghi co ma la [110].

H|nh 2-2. Lenh tcng quot LD reg8D,reg85.
Hnh 2-30 tom tat lenh nap d lieu tc thi 8 bit vao thanh ghi 8 bit reg8.
Ma lenh 1 byte 8 bit bao gom 2 bit 01[reg8]110]: trong o [reg8D] la 3 bit cua thanh ghi
reg8 .
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
-` Vi x ly
Thi gian thc hien lenh mat 7 chu ky T va mat 10 chu ky T oi vi lenh nap d lieu vao
o nh (co a ch lu trong HL).

H|nh 2-30. Lenh tcng quot LD reg8,|mm8.
Hnh 2-31 tom tat lenh xoay trai thanh ghi 8 bit reg8. Ma lenh 2 byte 8 bit: 11001011 :
00010 [reg8]. Thi gian thc hien lenh mat 8 chu ky T va ma t 15 chu ky T oi vi lenh xoay d
lieu trong o nh (co a ch lu trong HL).

H|nh 2-3. Lenh tcng quot kL reg8.
Do co rat nhieu lenh nen phan nay ch trnh bay mot so lenh, co the xem them phan
phu luc.
o r :: : :: r |e r| c. : .. x. |y Z:
Tap lenh c tom tat cac trang tiep theo vi nguon thong tin c lay t ma ng internet
vi a ch http://www.ftp83plus.net/Tutorials/z80inset1A.htm
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 49

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 51

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 53

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 55
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly

V. KHA0 5A1 V| XU L 8 8|1 8085A:
Chung ta mi va khao sat xong vi x ly Z80 th phan nay se tiep tuc khao sat vi x ly
8085A cua Intel. Tac gia muon trnh bay them vi x ly 8085A e chung ta co the lam quen vi
mot so cau hnh khac ma vi x ly Z80 khong co.
1. .o !o ru v v r ve n onq rv u .i av |u 8c8Z.
Hnh 2-32 la s o cau truc cua vi x ly 8085A:

H|nh 2-32. 5c dc cou truc ben trcng cuo v| xu |y 8085A.
Trong s o cau truc cua vi x ly 8085A co:
Khoi ALU cung vi 2 thanh ghi co ten la thanh ghi A va thanh ghi TEMP co chc nang
giong nh a khao sat nham lu tr d lieu cho khoi ALU thc hien cac phep toan. Ket qua sau
khi thc hien se lu tr lai thanh ghi A va tuy thuoc vao ket qua ma khoi ALU se ieu khien cac
bit c trong thanh ghi trang thai.
Khoi cac thanh ghi gom co: thanh ghi A, B, C, D, H, L, PC, SP, IDAL, IP.
Bus d lieu ben trong D0 D7 cho phep trao oi d lieu gia cac khoi vi nhau va c
a ti khoi em va a hp vi 8 ng a ch thap A0 A7 e tao thanh bus a ch / d lieu
AD0 AD7.
u iem cua viec a hp 8 ng a ch va 8 ng d lieu la thuan tien cho viec ong
goi IC 40 chan, neu khong tch hp th IC se nhieu hn 40 chan. Co 2 ly do cho viec a hp: th
nhat la tai thi iem o cong nghe mi bat au nen cha co the tang so chan cho IC va phai
thay oi cong nghe neu so chan thay oi, th 2 la viec a hp van khong lam thay oi chc nang
hay toc o cua vi x ly.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 57
Khuyet iem: mach ien phc tap v phai a hp va giai a hp khi s dung e tach a
ch va d lieu oc lap vi nhau, phai co them tn hieu ieu khien giai a hp.
Thanh ghi lenh IR se lu tr ma lenh va khoi giai ma lenh dung e giai ma lenh cho khoi
ieu khien thc hien lenh.
Khoi ieu khien ngat, khoi truyen d lieu noi tiep.
Cac khoi con bao gom: khoi ieu khien bus, khoi ieu khien oc ghi bo nh, oc ghi IO,
khoi ieu khien vi x ly, khoi reset .
Nguon s dung cho vi x ly la +5V, mach tao xung clock c tch hp ben trong va ch
can gan them tu thach anh ben ngoai.
2. Io rnv r nunn qni rv u .i av |u 8c8Z.
Vi x ly 8085A co 8 thanh ghi 8 bit va 3 thanh ghi 16 bit c trnh bay nh hnh 2-33.
Vai tro cua cac thanh ghi giong nh a trnh bay vi x ly Z80 ch co khac la so thanh ghi
trong vi x ly 8085A t hn so vi Z80. Trong nhom thanh ghi 16 bit th co thanh ghi IDAL co
chc nang nh sau: khi CPU truy xuat ma lenh th noi dung cua thanh ghi PC c trao cho thanh
ghi IDAL va no se ieu khien bus a ch tao ra ung a ch can truy xuat ma lenh.
Sau khi nhan ma lenh ve e thc hien th noi dung cua PC a tang len e chuan bit thc
hien lenh tiep theo, neu lenh ang thc hien la truy xuat byte d lieu bo nh ngoai th CPU se
ieu khien thanh ghi IDAL tang len 1 hoac 2 gia tr e truy xuat ung o nh can lay d lieu.
Neu a ch cua o nh cha trong cap thanh ghi HL th noi dung trong HL se trao cho IDAL e
truy xuat bo nh.

A reg (8| F reg (8|
8 reg (8| C reg (8|
D reg (8| E reg (8|
H reg (8| L reg (8|
51ACK P0|N1Ek (6|
Pk0CkAM C0UN1Ek (6|
|NCkEMEN1Ek/DECkEMEN1Ek
ADDkE55 LA1CH (6|
H|nh 2-33. Coc thonh gh| ben trcng V| xu |y 8085A.
3. .o !o rnu n rv u .i av |u 8c8Z.
Hnh 2-34 la s o chan cua vi x ly 8085A:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
` Vi x ly

H|nh 2-34. 5c dc chon cuo v| xu |y 8085A.
Vi x ly 8085A co 40 chan, trong o:
A8 A15 (address) la bus a ch cao, tn hieu ra dang 3 trang thai, se trang thai tong tr
cao khi mode Hold va Halt va khi b reset.
AD0 AD7 (address/data) la bus a hp a ch va d lieu, tn hieu vao/ra 3 trang thai,
bus se tai a ch trong khoang thi gian xung clock th nhat cua chu ky may va se tai d lieu
cac chu ky xung clock con lai.
ALE (address latch enable) la tn hieu ra dung e tao xung chot a ch khi bus AD0
AD7 ong vai tro la tai a ch va ch chot vao thi iem canh xuong.
S0, S1 va M IO/ la cac tn hieu ngo ra cho biet cac trang thai lam viec cua vi x ly:

M IO/
S1 S0 Trang thai
0 0 1 Ghi d lieu len bo nh
0 1 0 oc d lieu t bo nh
1 0 1 Ghi d lieu len IO
1 1 0 oc d lieu t IO
0 1 1 on ma lenh t bo nh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 59
1 1 1 on ma lenh t bo nh
1 1 1 Tra li yeu cau ngat
HZ 0 0 Halt
HZ x x Hold
HZ x x Reset
S1 co the c dung nh la trang thai oc ghi W R/ . Cac tn hieu M IO/ , S1, S0 se bat
au co gia tr (hieu lc) khi bat au mot chu ky may va van on nh trang thai nay cac chu
ky clock con lai. Canh xuong cua xung ALE co the chot cac tn hieu nay.
RD la tn hieu ngo ra, khi RD mc thap se ieu khien oc d lieu t bo nh hoac IO va
se trang thai tong tr cao khi vi x ly trang thai HOLD, HALT va RESET.
WR la tn hieu ngo ra, khi WR mc thap se ieu khien ghi d lieu len bo nh hoac IO va
se trang thai tong tr cao khi vi x ly trang thai HOLD, HALT va RESET.
READY la tn hieu ngo vao, neu READY mc cao trong khoang thi gian oc hoac ghi
d lieu gia vi x ly va bo nh e cho biet bo nh hoac thiet b ngoai vi IO a san sang cho viec
oc hoac ghi d lieu. Neu tn hieu READY mc thap th CPU phai thc hien them cac chu ky
i ch cho en khi READY tr lai mc cao th hoan tat chu ky oc hoac ghi bo nh.

HOLD la tn hieu ngo vao tch cc mc cao, thiet b ngoai vi ieu khien chan nay len mc
cao e yeu cau vi x ly nhng quyen s dung bus a ch va bus d lieu. Khi cpu nhan c tn
hieu Hold th se ngng s dung bus ngay lap tc va chuyen quyen s dung bus. Qua trnh x ly
lenh ben trong se tiep tuc c thc hien khi yeu cau dung bus cua thiet b ngoai vi khong con.
HLDA la tn hieu ngo ra dung e bao cho thiet b yeu cau HOLD biet vi x ly se ngng s
dung bus chu ky xung clock ke. HLDA se tr lai mc thap khi thiet b yeu cau ngat khong con
yeu cau nhng bus.
INTR la tn hieu ngo vao yeu cau ngat tong quat. Tn hieu nay c lay mau trong khoang
thi gian cua chu ky xung clock ke cuoi va trong khoang thi gian HOLD va HALT.
Neu INTR trang thai tch cc th vi x ly se ngng thc hien lenh tiep theo va ieu khien
tra li ngat chan INTA. Trong khoang chu ky nay th lenh RESTART hoac lenh CALL co the
c chen vao e nhay en chng trnh con phuc vu ngat.
INTR la tn hieu ngat co the cho phep hoac cam bang phan mem.
INTA la tn hieu ngo ra tra li ap ng yeu cau ngat, tn hieu nay c dung thay the cho
chan RD trong khoang thi gian chu ky lenh sau khi tn hieu yeu cau ngat INTR c chap
nhan. Tn hieu nay c dung e tac ong en chip ieu khien ngat 8259A hoac cac thiet b
ngat khac.
RST 5.5, RST 6.5, RST 7.5 la tn hieu ngo vao yeu cau ngat. Ba tn hieu ngat nay giong
nh tn hieu ngat INTR va ch khac la chung c bat au tai a ch hoan toan t ong.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
TRAP la tn hieu ngo vao yeu cau ngat khong ngan c, tn hieu ngat nay c lay mau
giong nh cac tn hieu yeu cau ngat INTR va cac ngat RST. Yeu cau ngat nay khong the ngan
c bang phan mem nen no co mc o u tien cao nhat.
IN RESET _ la tn hieu ngo vao reset tch cc mc thap se xoa thanh ghi PC ve 0000H va
se reset cac bit cho phep ngat va flip flop HLDA. Bus d lieu, bus a ch va bus ieu khien eu
trang thai tong tr cao khi b reset. Cac thanh ghi ben trong va thanh ghi c co the b thay oi
vi nhng gia tr mi khong xac nh khi CPU b reset. Ngo vao reset nay c noi vi R va C
e tao mach auto reset khi m may. Khi PC = 0000H th chng trnh c bat au thc hien tai
a ch 0000H tng ng.
RESET OUT la tn hieu ngo ra e bao hieu CPU ang b reset. Tn hieu nay co the dung
e reset cac thiet b khac trong he thong v du nh IC ngoai vi 8255A, 8279.
X1, X2 la tn hieu ngo vao, hai ngo vao nay c noi vi tu thach anh hoac mach LC hoac
mach RC e tao dao ong cho vi x ly hoat ong.
Mach dao ong a tch hp ben trong va ch can noi them tu thach anh nhng neu co
nguon tao xung ben ngoai th ta co the cung cap xung o en 1 ngo vao X1.
Tan so xung clock ngo vao hay tan so cua tu thach anh c chia cho 2 roi mi cung cap
cho cac hoat ong ben trong cua vi x ly.
CLK la tn hieu ngo ra xung clock dung e cung cap co cac thiet b khac can xung clock e
lam viec v du nh IC 8279. Tan so cua xung clock nay bang tan so cua tu thach anh ang s
dung chia cho 2.
SID la tn hieu ngo vao d lieu noi tiep, d lieu ngo vao nay c nap vao bit th 7 cua
thanh ghi A khi thc hien RIM.
SOD la tn hieu ngo ra d lieu noi tiep, d lieu ngo ra nay mc logic 0 hay mc logic 1
c thc hien bi lenh SIM.
V
CC
la chan nguon cung cap +5V cho vi x ly.
V
SS
la chan nguon cung cap GND cho vi x ly.
Bang th t u tien ngat va cac a ch ngat:
Ten Th t u tien a ch cua ca c chng
trnh con phuc vu ngat
Mc tac ong
TRAP 1 0024H Ca nh len and vi mc cao ch cho
en khi c lay mau
RST 7.5 2 003CH Ca nh len (c chot lai)
RST 7.5 3 0034H Mc cao cho en khi c lay
mau
RST 7.5 4 002CH Mc cao cho en khi c lay
mau
INTR 5 Xem chu y Mc cao cho en khi c lay
mau
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 61
Chu y: a ch cua chng trnh con phuc vu ngat tuy thuoc vao lenh cung cap cho CPU khi
ngat c chap nhan.
4. Giu n !o no i qiun rv u .i av |u 8c8Z.
Vi x ly 8085A co a hp bus d lieu va bus a ch, ALE c dung e chot 8 bit a ch
thap tren bus AD0 AD7, hnh 2-35 trnh bay gian o thi gian on lenh, chu ky oc bo nh va
chu ky ghi d lieu len bo nh.
Co 7 loai chu ky may khac nhau tuy thuoc vao lenh ang thc hien va 7 trang thai khac
nhau nay c the hien thong qua trang thai cua cac tn hieu M IO/ , S1, S0 va cac chan ieu
khien RD, WR, INTA.
Mot chu ky may thong thng gom 3 trang thai T ngoai tr chu ky on lenh t bo nh th
thc hien can 4 hoac 6 chu ky trang thai T (cha tnh en cac tnh huong i WAIT hoac
HOLD).

H|nh 2-35. C|on dc thc| g|on hcot dcng cuo v| xu |y 8085A.
chu ky on lenh: trong khoang thi gian cua trang thai T1: th CPU se tao ra a ch 8 bit
cao bus A8 A15, 8 bit thap bus AD0 AD7, cac trang thai ieu khien S1, S0 va ng ieu
khien M IO/ xuong mc 0. ong thi tao xung chot ALE e chot a ch va cac trang thai ieu
khien. Khi sang chu ky trang thai T2 th CPU ieu khien chan RD xuong mc thap e tac ong
en bo nh thc hien viec oc d lieu. Bo nh ngay lap tc xuat d lieu tng ng vi o nh a
nhan a ch va d lieu o c a len bus AD0 AD7 bay gi bus nay ong vai tro tai d
lieu. CPU ieu khien chan RD tr lai trang thai khong tch cc trang thai T3 va tien hanh
nhan d lieu t bus o chnh la ma lenh va cat vao thanh ghi lenh IR cham dt qua trnh
on lenh t bo nh trang thai T4.
Tng t cho cac chu ky oc d lieu va ghi d lieu th ch co 3 trang thai T.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
5. Giu i !u no v !iu rn .u vv |ie v rno .i av |u 8c8Z.
Khi s dung vi x ly 8085A co a hp bus d lieu va bus a ch th phai s dung IC chot
e tach a ch va d lieu nh hnh 2-36:


H|nh 2-36. C|o| do hcp d|o ch| vo du ||eu cuo v| xu |y 8085A.
Chan ALE se ieu khien IC chot 74573 e chot 8 bit a ch, ngo ra cua IC chot la 8 bit
a ch thap A0 A7 ket hp vi 8 bit a ch cao A8 A15 tao thanh bus a ch 16 bit oc lap
vi 8 ng d lieu D0 D7 giong nh vi x ly Z80.
Trong he thong khong s dung trang thai HOLD va ngat INTR nen chung c noi vi
GND, cac ngat TRAP, RST c s dung, tn hieu ngo vao reset c ket noi vi tu C1, ien tr
R1 va nut nhan reset, tu thach anh s dung la 6Mhz c noi vi X1 va X2.
Tn hieu READY cung c noi vi mc cao e vi x ly khong chen them cac chu ky i
nen phai s dung cac loai bo nh co thi gian truy xuat nhanh hn vi x ly.
Cac ng ieu khien con lai dung e ket noi vi bo nh va thiet b ngoai vi IO.
6. Iu v |e nn rv u .i av |u 8c8Z.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 63
Tap lenh cua vi x ly 8085 giong nh tap lenh cua vi x ly 8080 ngoai tr 2 lenh mi them
vao la lenh SIM va RIM.
Phan nay se gii thieu thanh phan ma lenh, hoat ong cua cac bit trong thanh ghi c trang
thai, cac kieu nh a ch va cach thc s dung cap thanh ghi H, L cho kieu nh a ch gian
tiep.
Cac k hieu dung trong tap lenh c dung e dien ta chc nang va moi quan he gia cac
thanh phan cua lenh. Cac k hieu ch thng nh r dung e chnh nh mot thanh ghi, rp e
xac nh cap thanh ghi, addr dung e xac nh a ch, data dung e xac nh byte d lieu 8
bit, data 16 dung e xac nh, rh va rl dung e xac nh cac thanh ghi byte cao va thanh
ghi byte thap cua mot cap thanh ghi theo th t.
1c chuc |enh:
Cac lenh cua vi x ly 8085 thng nh dang theo byte bao gom cac lenh mot byte, lenh 2
byte va lenh 3 byte. Byte au tien la byte cha cac thong tin c ban cua lenh chnh la ma lenh,
cac byte th 2 va th 3 la cac byte thong tin cho lenh nh d lieu hay a ch cua d lieu.
Trong byte ma lenh x ly d lieu tren cac thanh ghi th bit th 2,1,0 thng la ma cua
cac thanh ghi nguon va cac bit th 5,4,3 la ma cua thanh ghi ch, ma cua cac thanh ghi nh
bang sau :
DDD or SSS Register Name
111 A
000 B
001 C
010 D
011 E
100 H
101 L
Tng t cho cac lenh co lien quan en cap thanh ghi th ma cua cac cap thanh ghi nh
bang sau:
RP Bits Register Pair
00 B-C
01 D-E
10 H-L
11 SP
Coc k|eu d|nh d|o ch| cuo v| xu |y 8085:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
- Vi x ly
Vi x ly 8085 co 4 kieu nh a ch khac nhau e truy xuat d lieu nam trong cac thanh
ghi hay d lieu nam trong bo nh.
.e . c.r| c.: c|. :r. c :.e r: kieu nay co ma lenh dai 3 byte. Byte th nhat la byte ma
lenh, byte th 2 va byte th 3 la a ch cua o nh can lay d lieu. Byte th 2 la byte a ch thap
va byte th 3 la byte a ch cao.
.e . c.r| c.: c|. c. r :|:r| |. : kieu nay co ma lenh dai 1 byte. Trong byte ma lenh
co cac bit ch nh thanh ghi cha d lieu hay cap thanh ghi cha d lieu.
.e . c.r| c.: c|. .: r :.e r c. r :|:r| |. : kieu nay co ma lenh dai 1 byte. Trong byte
ma lenh co cac bit ch nh cap thanh ghi cha d lieu.
.e . c.r| c.: c|. :. c :|o . : kieu nay co ma lenh dai 2 byte hoac 3 byte. Byte th nhat la
ma lenh, byte th 2 va byte th 3 la d lieu tc thi 8 bit hay 16 bit. Neu d lieu 16 bit th byte
th 2 la byte thap va byte th 3 la byte cao.
Coc k|eu |enh re nhonh:
Viec thc hien cac lenh bnh thng Theo th t t tren xuong. Nhng lenh re nhanh co
tac dung thay the tien trnh ang thc hien. Cac lenh nay ch nh a ch cua lenh ke ma vi x
ly phai thc hien, co 2 dang lenh nhay:
e r| r|: y :r. c :.e r: trong kieu lenh nhay nay cha a ch cua ni ma vi x ly se nhay
en e thc hien, a ch cua lenh nhay nay la 2 byte, byte th 2 trong ma lenh la byte a ch
thap va byte th 3 la byte a ch cao.
e r| r|: y .: r :.e r c. r :|:r| |. : trong kieu lenh nhay nay a ch ma vi x ly nhay
en c cha trong cap thanh ghi. a ch phai lu trong cap thanh ghi trc khi thc hien lenh
nhay.
Coc nhcm |enh:
Hang Intel chia cac lenh cua vi x ly 8085 thanh 5 nhom bao gom: nhom lenh truyen d
lieu, nhom lenh so hoc, nhom lenh logic, nhom lenh nhay va nhom lenh ieu khien IO.
!|o r |e r| :r.ye r c. |.e . : trong nhom lenh nay co chc nang copy d lieu gia cac
thanh ghi ben trong vi x ly, gia bo nh va thanh ghi.
Nhom Ienh MOV: bao gom 3 lenh (MOV r1,r2; MOV r,M va MOV M,r) co chc nang
copy d lieu gia thanh ghi va thanh ghi gia thanh ghi va o nh. Lenh ch thc hien copy 1 byte
d lieu. Trong 3 lenh tren th lenh th 2 va lenh th 3 s dung kieu nh a ch gian tiep dung
thanh ghi, a ch cua o nh lu trong cap thanh ghi HL.
Nhom Ienh MVI: bao gom 2 lenh (MVI r,data va MVI M,data) co chc nang nap mot
byte d lieu tc thi vao 1 thanh ghi hay mot o nh.
Nhom Ienh LXI: bao gom 1 lenh (LXI rp,data 16) co chc nang nap 2 byte d lieu tc
thi vao 1 cap thanh ghi, byte th 2 trong ma lenh la byte d lieu thap va byte th 3 trong ma
lenh la byte cao cua d lieu.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 65
Nhom Ienh LDA va STA: la lenh Load Accumulator Direct (LDA) va Store
Accumulator Direct (STA) co chc nang copy d lieu gia thanh ghi A va o nh trong RAM, a
ch cua o nh c lu trong thanh ghi HL.
Nhom Ienh LHLD va SHLD: la lenh Load H&L Direct (LHLD) va Store H&L Direct
(SHLD) co chc nang copy d lieu gia thanh ghi H&L vi d lieu trong bo nh RAM. Lenh
LHLD se copy d lieu cua 2 o nh co a ch lu trong HL va HL+1 nap vao thanh ghi L va
thanh ghi H theo th t. Lenh SHLD th lu noi dung cua cap thanh ghi HL vao 2 o nh co a
ch lu trong HL va HL +1.
Nhom Ienh LDAX va STAX: la lenh Load Accumulator Indirect (LDAX) va Store
Accumulator Indirect (STAX) co chc nang copy d lieu gia thanh ghi A va o nh RAM co a
ch gian tiep, a ch nay co the lu trong cap thanh ghi BC hoac DE.
Nhom Ienh XCHG: la lenh Exchange (XCHG) cho phep trao oi noi dung cap thanh ghi
HL vi DE.
!|o r |e r| eo |o c : trong nhom lenh nay co chc nang thc hien cac phep toan so hoc
vi d lieu 8 bit.
Nhom Ienh ADD: la lenh ADD Register (ADD r) va ADD memory (ADD M) co chc
nang cong noi dung thanh ghi A vi thanh ghi hoac noi dung thanh ghi A vi noi dung cua o nh
co a ch lu trong cap thanh ghi HL. Neu ket qua phep toan khong tran th c carry bang 0 con
neu co tran th c carry bang 1. Ngoai ra lenh con anh hng en cac bit c khac.
Nhom Ienh ADD voI co carry: la lenh ADD Register with carry (ADC r) va ADD
memory with carry (ADC M) co chc nang cong noi dung thanh ghi A vi thanh ghi va c carry
hoac noi dung thanh ghi A vi noi dung cua o nh co a ch lu trong cap thanh ghi HL va c
carry. Neu ket qua phep toan khong tran th c carry bang 0 con neu co tran th c carry bang 1.
Ngoai ra lenh con anh hng en cac bit c khac.
Nhom Ienh ADD tuc thoI: la lenh ADD Immediate (ADI data) va ADD Immediate
memory with carry (ACI data) co chc nang cong noi dung thanh ghi A vi d lieu tc thi 8 bit
va cong noi dung thanh ghi A vi d lieu tc thi 8 bit va c carry. Neu ket qua phep toan khong
tran th c carry bang 0 con neu co tran th c carry bang 1. Ngoai ra lenh con anh hng en
cac bit c khac.
Nhom Ienh SUBTRACT: la lenh Subtract Register (SUB r) va Subtract memory (SUB
M) co chc nang tr noi dung thanh ghi A vi thanh ghi hoac noi dung thanh ghi A vi noi dung
cua o nh co a ch lu trong cap thanh ghi HL. Neu ket qua phep toan khong mn th c carry
bang 0 con neu co mn th c carry bang 1. Ngoai ra lenh con anh hng en cac bit c khac.
Nhom Ienh Subtract voI co carry: la lenh Subtract Register with borrow (SBB r) va
Subtract memory with borrow (SBB M) co chc nang tr noi dung thanh ghi A vi thanh ghi va
c carry hoac noi dung thanh ghi A vi noi dung cua o nh co a ch lu trong cap thanh ghi HL
va c carry. Neu ket qua phep toan khong mn th c carry bang 0 con neu co mn th c
carry bang 1. Ngoai ra lenh con anh hng en cac bit c khac.
Nhom Ienh Subtract tuc thoI: la lenh Subtract immediate (SUI data) va Subtract
immediate with borrow (SBI data) co chc nang tr noi dung thanh ghi A vi d lieu tc thi 8
bit hoac noi dung thanh ghi A vi d lieu tc thi 8 bit va c carry. Neu ket qua phep toan khong
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly
mn th c carry bang 0 con neu co mn th c carry bang 1. Ngoai ra lenh con anh hng en
cac bit c khac.
Nhom Ienh tang Increment : la lenh increment register (INR r) va Increment
memory (INR M) co chc nang tang noi dung thanh ghi len 1 hoac tang noi dung o nh co a
ch lu trong cap thanh ghi HL len 1. Lenh khong anh hng en thanh ghi tra ng thai.
Nhom Ienh gIam Decrement : la lenh Decrement register (DCR r) va Decrement
memory (DCR M) co chc nang giam noi dung thanh ghi xuong 1 hoac giam noi dung o nh co
a ch lu trong cap thanh ghi HL xuong 1. Lenh khong anh hng en thanh ghi trang thai.
Nhom Ienh xu Iy cap thanh ghI : la lenh Increment Register Pair (INR rp) va
Decrement Register Pair (DCR rp) co chc nang tang noi dung cap thanh ghi len 1 hoac giam
noi dung cap thanh ghi xuong 1.
Nhom Ienh cong cap thanh ghI : la lenh Add Register Pair (DAD rp) co chc nang
cong noi dung cap thanh ghi BC hoac DE vi cap thanh ghi HL, ket qua lu vao cap thanh ghi
HL. Lenh se anh hng en thanh ghi trang thai.
Lenh hIeu chnh thap phan : la lenh Decimal Adjust Accumulator (DAA) co chc
nang hieu chnh gia tr 8 bit trong thanh ghi A thanh so BCD sau khi thc hien phep toan cong 2
so BCD.
!|o r |e r| |o.c: trong nhom lenh nay co chc nang thc hien cac phep toan logic vi d
lieu 8 bit.
Nhom Ienh AND: la lenh AND Register (ANA r) va AND memory (ANA M) co chc
nang AND noi dung thanh ghi A vi thanh ghi hoac noi dung thanh ghi A vi noi dung cua o nh
co a ch lu trong cap thanh ghi HL. Lenh And se thc hien phep toan and tng cap bit vi
nhau theo th t. Ket qua sau khi thc hien c lu vao thanh ghi A va thanh ghi trang thai b
anh hng va c carry th luon bang 0.
Nhom Ienh AND ImmedIate : la lenh AND Immediate (ANI data) co chc nang
AND noi dung thanh ghi A vi d lieu tc thi 8 bit. Ket qua sau khi thc hien c lu vao
thanh ghi A va thanh ghi tra ng thai b anh hng va c carry th luon bang 0.
Nhom Ienh OR: la lenh Or Register (ORA r) va Or memory (ORA M) co chc nang OR
noi dung thanh ghi A vi thanh ghi hoac noi dung thanh ghi A vi noi dung cua o nh co a ch
lu trong cap thanh ghi HL. Ket qua sau khi thc hien c lu vao thanh ghi A va thanh ghi
trang thai b anh hng va c carry th luon bang 0.
Nhom Ienh OR ImmedIate : la lenh OR Immediate (ORI data) co chc nang OR noi
dung thanh ghi A vi d lieu tc thi 8 bit. Ket qua sau khi thc hien c lu vao thanh ghi A
va thanh ghi tra ng thai b anh hng va c carry th luon bang 0.
Nhom Ienh ExcIusIve Or : la lenh Exclusive Or Register (XRA r) va Exclusive Or
memory (XRA M) co chc nang Exclusive noi dung thanh ghi A vi thanh ghi hoac noi dung
thanh ghi A vi noi dung cua o nh co a ch lu trong cap thanh ghi HL. Ket qua sau khi thc
hien c lu vao thanh ghi A va thanh ghi tra ng thai b anh hng va c carry th luon bang 0.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 67
Nhom Ienh ExcIusIve ImmedIate : la lenh Exclusive Or Immediate (XRI data) co
chc nang Exclusive Or noi dung thanh ghi A vi d lieu tc thi 8 bit. Ket qua sau khi thc hien
c lu vao thanh ghi A va thanh ghi tra ng thai b anh hng va c carry th luon bang 0.
Nhom Ienh so sanh : la lenh Compare Register (CMP r) va Compare memory (CMP
M) co chc nang so sanh noi dung thanh ghi A vi thanh ghi hoac noi dung thanh ghi A vi noi
dung cua o nh co a ch lu trong cap thanh ghi HL. Ket qua sau khi thc hien anh hng en
thanh ghi tra ng thai.
Nhom Ienh so sanh tuc thoI : la lenh Compare Immediate (CPI data) co chc nang
so sanh noi dung thanh ghi A vi d lieu tc thi 8 bit. Ket qua sau khi thc hien anh hng en
thanh ghi tra ng thai.
Nhom Ienh xoay : la lenh Rotate Left (RLC ) va Rotate Right (RRC) co chc nang
xoay noi dung thanh ghi A sang trai hoac sang phai 1 bit. Ch co c carry b anh hng.
Nhom Ienh xoay thong qua co carry : la lenh Rotate Left through carry (RAL ) va
Rotate Right through carry (RAR) co chc nang xoay noi dung thanh ghi A va c carry sang trai
hoac sang phai 1 bit. Ch co c carry b anh hng.
Nhom Ienh bu noI dung thanh ghI A : la lenh Complement Accumulator (CMA)
co chc nang thc hien phep toan bu 1 noi dung thanh ghi A.
Nhom Ienh bu va cIear co carry : la lenh Complement carry (CMC) va Set Carry
(STC) co chc nang nghch ao c Carry va lam c carry bang 1.
!|o r |e r| re r|: r|: trong nhom lenh nay gom co lenh nhay khong ieu kien va lenh
nhay co ieu kien, lenh goi chng trnh con khong ieu kien va lenh goi chng trnh con co
ieu kien.
Nhom Ienh nhay : la lenh Jump (JMP addr) va Jump Conditional (Jxx Addr) co chc
nang thay oi noi dung cua thanh ghi PC e nhay en a ch Addr e tiep tuc thc hien chng
trnh tai o. oi vi lenh nhay co ieu khien th se nhay khi thoa ieu kien va se khong nhay
khi khong thoa ieu kien. Cac lenh nhay co ieu kien la can c vao cac bit trang thai c liet
ke bang sau:

Mnemonic Condition CCC Bits
NZ Not Zero (Z=0) 000
Z Zero (Z=1) 001
NC Not Carry (C=0) 010
C Carry (C=1) 011
PO Parity Odd (P=0) 100
PE
Parity Even
(P=1)
101
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
` Vi x ly
P Plus (S=0) 110
M Minus (S=1) 111
Nhom Ienh goI chuong trnh con : la lenh Call (Call addr) va Call Conditional (Cxx
Addr) co chc nang thc hien chng trnh con tai a ch Addr va lenh goi co ieu kien th
chng trnh con ch c thc hien khi thoa ieu kien, neu khong thoa th khong thc hien. Cac
lenh goi chng trnh con co ieu kien se can c vao cac bit c giong nh lenh nhay co ieu
kien. Khi thc hien cac lenh goi chng trnh con th noi dung cua thanh ghi PC c cat vao
ngan xep roi mi nap a ch cua chng trnh con vao thanh ghi PC, sau khi thc hien xong
chng trnh con th noi dung cat trong ngan xep c tra lai cho PC e tiep tuc thc hien lenh
tiep theo trong chng trnh chnh, viec cat a ch trong PC va lay lai tng ng vi 2 lenh goi
Call va lenh tr ve RET.
Nhom Ienh Ret thuc chuong trnh con : la lenh Return (RET) va Return
Conditional (Rxx ) co chc nang ket thuc chng trnh con e tr ve chng trnh chnh khong
ieu kien va co ieu khien. Trong chng trnh con phai luon co lenh ket thuc RET. Khong c
nhay t chng trnh con ve chng trnh chnh va ngc lai.
Nhom Ienh bat dau IaI : la lenh Restart (RST n) co chc nang goi mot trong 8 chng
trnh con tai a ch do nha che tao vi x ly qui nh giong nh cac chng trnh con phuc vu ngat
c liet ke bang sau:
NAME: ADDRESS:
RST 0 00H
RST 1 08H
RST 2 10H
RST 3 18H
RST 4 20H
TRAP 24H
RST 5 28H
REST 5.5 2CH
RST 6 30H
RST 6.5 34H
RST 7 38H
RST 7.5 3CH
Cach thc thc hien cung giong nh lenh goi Call.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 69
Nhom Ienh nhay gIan tIep : la lenh Jump H&L Indirect (PCHL) co chc nang thay
oi noi dung cua thanh ghi PC bang gia tr trong cap thanh ghi HL va bat au thc hien chng
trnh tai a ch mi va thay oi.
!|o r |e r| c.e . ||.e r r: y :
Nhom Ienh cat va Iay du IIeu : la lenh Push Register Pair (PUSH rp) va Pop
Register Pair (POP rp) co chc nang cat tam noi dung cap thanh ghi vao bo nh ngan xep va lay
lai noi dung a cat tam trong ngan xep.
Nhom Ienh cat va Iay noI dung thanh ghI trang thaI : la lenh Push Processor
Status Word (PUSH PSW) va Pop Processor Status Word (POP PSW) co chc nang cat tam noi
dung cap thanh ghi A va thanh ghi trang thai vao bo nh ngan xep va lay lai t ngan xep giong
nh cac cap thanh ghi khac.
Nhom Ienh trao doI noI dung thanh ghI voI bo nho ngan xep : la lenh
Exchange Stack Top (XTHL) se thc hien trao oi noi dung cua cap thanh ghi HL vi noi dung
cua 2 o nh ngan xep.
Nhom Ienh copy noI dung cap thanh ghI : la lenh Move HL Register to SP
(SPHL) se thc hien trao noi dung cua cap thanh ghi HL cho thi SP.
!|o r |e r| ' :
Nhom Ienh nhap va xuat du IIeu ra port : la lenh Input (IN port) va Output (OUT
port) co chc nang nhap va xuat d lieu gia thanh ghi A trong vi x ly vi cac thiet b ngoai vi
IO. Noi dung can gi ra port phai lu trong thanh ghi A va d lieu oc t port ve cung lu trong
thanh ghi A.
Nhom Ienh cho phe p ngat va cam ngat : la lenh Enable Interrupt (EI) va
Disenable Interrupt (DI) co chc nang cho phep va cam ngat oi vi cac ngat co the ngan c.
Nhom Ienh ngung va Ienh Nop : la lenh Halt (HLT) va No - operation (NOP) co
chc nang ngng thc hien chng trnh va lenh Nop th khong lam g ca e danh vung nh s
dung ve sau hoac delay mot vai s.
1cm tot top |enh v| xu |y 8085 :
----------------------------------------------------------------
|Mnemonic |Op|SZAPC|~s|Description |Notes |
|---------+--+-----+--+--------------------------+-------------|
|ACI n |CE|*****| 7|Add with Carry Immediate |A=A+n+CY |
|ADC r |8F|*****| 4|Add with Carry |A=A+r+CY(21X)|
|ADC M |8E|*****| 7|Add with Carry to Memory |A=A+[HL]+CY |
|ADD r |87|*****| 4|Add |A=A+r (20X)|
|ADD M |86|*****| 7|Add to Memory |A=A+[HL] |
|ADI n |C6|*****| 7|Add Immediate |A=A+n |
|ANA r |A7|****0| 4|AND Accumulator |A=A&r (24X)|
|ANA M |A6|****0| 7|AND Accumulator and Memory|A=A&[HL] |
|ANI n |E6|**0*0| 7|AND Immediate |A=A&n |
|CALL a |CD|-----|18|Call unconditional |-[SP]=PC,PC=a|
|CC a |DC|-----| 9|Call on Carry |If CY=1(18~s)|
|CM a |FC|-----| 9|Call on Minus |If S=1 (18~s)|
|CMA |2F|-----| 4|Complement Accumulator |A=~A |
|CMC |3F|----*| 4|Complement Carry |CY=~CY |
|CMP r |BF|*****| 4|Compare |A-r (27X)|
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
7 Vi x ly
|CMP M |BF|*****| 7|Compare with Memory |A-[HL] |
|CNC a |D4|-----| 9|Call on No Carry |If CY=0(18~s)|
|CNZ a |C4|-----| 9|Call on No Zero |If Z=0 (18~s)|
|CP a |F4|-----| 9|Call on Plus |If S=0 (18~s)|
|CPE a |EC|-----| 9|Call on Parity Even |If P=1 (18~s)|
|CPI n |FE|*****| 7|Compare Immediate |A-n |
|CPO a |E4|-----| 9|Call on Parity Odd |If P=0 (18~s)|
|CZ a |CC|-----| 9|Call on Zero |If Z=1 (18~s)|
|DAA |27|*****| 4|Decimal Adjust Accumulator|A=BCD format |
|DAD B |09|----*|10|Double Add BC to HL |HL=HL+BC |
|DAD D |19|----*|10|Double Add DE to HL |HL=HL+DE |
|DAD H |29|----*|10|Double Add HL to HL |HL=HL+HL |
|DAD SP |39|----*|10|Double Add SP to HL |HL=HL+SP |
|DCR r |3D|****-| 4|Decrement |r=r-1 (0X5)|
|DCR M |35|****-|10|Decrement Memory |[HL]=[HL]-1 |
|DCX B |0B|-----| 6|Decrement BC |BC=BC-1 |
|DCX D |1B|-----| 6|Decrement DE |DE=DE-1 |
|DCX H |2B|-----| 6|Decrement HL |HL=HL-1 |
|DCX SP |3B|-----| 6|Decrement Stack Pointer |SP=SP-1 |
|DI |F3|-----| 4|Disable Interrupts | |
|EI |FB|-----| 4|Enable Interrupts | |
|HLT |76|-----| 5|Halt | |
|IN p |DB|-----|10|Input |A=[p] |
|INR r |3C|****-| 4|Increment |r=r+1 (0X4)|
|INR M |3C|****-|10|Increment Memory |[HL]=[HL]+1 |
|INX B |03|-----| 6|Increment BC |BC=BC+1 |
|INX D |13|-----| 6|Increment DE |DE=DE+1 |
|INX H |23|-----| 6|Increment HL |HL=HL+1 |
|INX SP |33|-----| 6|Increment Stack Pointer |SP=SP+1 |
|JMP a |C3|-----| 7|Jump unconditional |PC=a |
|JC a |DA|-----| 7|Jump on Carry |If CY=1(10~s)|
|JM a |FA|-----| 7|Jump on Minus |If S=1 (10~s)|
|JNC a |D2|-----| 7|Jump on No Carry |If CY=0(10~s)|
|JNZ a |C2|-----| 7|Jump on No Zero |If Z=0 (10~s)|
|JP a |F2|-----| 7|Jump on Plus |If S=0 (10~s)|
|JPE a |EA|-----| 7|Jump on Parity Even |If P=1 (10~s)|
|JPO a |E2|-----| 7|Jump on Parity Odd |If P=0 (10~s)|
|JZ a |CA|-----| 7|Jump on Zero |If Z=1 (10~s)|
|LDA a |3A|-----|13|Load Accumulator direct |A=[a] |
|LDAX B |0A|-----| 7|Load Accumulator indirect |A=[BC] |
|LDAX D |1A|-----| 7|Load Accumulator indirect |A=[DE] |
|LHLD a |2A|-----|16|Load HL Direct |HL=[a] |
|LXI B,nn |01|-----|10|Load Immediate BC |BC=nn |
|LXI D,nn |11|-----|10|Load Immediate DE |DE=nn |
|LXI H,nn |21|-----|10|Load Immediate HL |HL=nn |
|LXI SP,nn|31|-----|10|Load Immediate Stack Ptr |SP=nn |
|MOV r1,r2|7F|-----| 4|Move register to register |r1=r2 (1XX)|
|MOV M,r |77|-----| 7|Move register to Memory |[HL]=r (16X)|
|MOV r,M |7E|-----| 7|Move Memory to register |r=[HL] (1X6)|
|MVI r,n |3E|-----| 7|Move Immediate |r=n (0X6)|
|MVI M,n |36|-----|10|Move Immediate to Memory |[HL]=n |
|NOP |00|-----| 4|No Operation | |
|ORA r |B7|**0*0| 4|Inclusive OR Accumulator |A=Avr (26X)|
|ORA M |B6|**0*0| 7|Inclusive OR Accumulator |A=Av[HL] |
|ORI n |F6|**0*0| 7|Inclusive OR Immediate |A=Avn |
|OUT p |D3|-----|10|Output |[p]=A |
|PCHL |E9|-----| 6|Jump HL indirect |PC=[HL] |
|POP B |C1|-----|10|Pop BC |BC=[SP]+ |
|POP D |D1|-----|10|Pop DE |DE=[SP]+ |
|POP H |E1|-----|10|Pop HL |HL=[SP]+ |
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 71
|POP PSW |F1|-----|10|Pop Processor Status Word |{PSW,A}=[SP]+|
----------------------------------------------------------------
----------------------------------------------------------------
|Mnemonic |Op|SZAPC|~s|Description |Notes |
|---------+--+-----+--+--------------------------+-------------|
|PUSH B |C5|-----|12|Push BC |-[SP]=BC |
|PUSH D |D5|-----|12|Push DE |-[SP]=DE |
|PUSH H |E5|-----|12|Push HL |-[SP]=HL |
|PUSH PSW |F5|-----|12|Push Processor Status Word|-[SP]={PSW,A}|
|RAL |17|----*| 4|Rotate Accumulator Left |A={CY,A}<- |
|RAR |1F|----*| 4|Rotate Accumulator Righ |A=->{CY,A} |
|RET |C9|-----|10|Return |PC=[SP]+ |
|RC |D8|-----| 6|Return on Carry |If CY=1(12~s)|
|RIM |20|-----| 4|Read Interrupt Mask |A=mask |
|RM |F8|-----| 6|Return on Minus |If S=1 (12~s)|
|RNC |D0|-----| 6|Return on No Carry |If CY=0(12~s)|
|RNZ |C0|-----| 6|Return on No Zero |If Z=0 (12~s)|
|RP |F0|-----| 6|Return on Plus |If S=0 (12~s)|
|RPE |E8|-----| 6|Return on Parity Even |If P=1 (12~s)|
|RPO |E0|-----| 6|Return on Parity Odd |If P=0 (12~s)|
|RZ |C8|-----| 6|Return on Zero |If Z=1 (12~s)|
|RLC |07|----*| 4|Rotate Left Circular |A=A<- |
|RRC |0F|----*| 4|Rotate Right Circular |A=->A |
|RST z |C7|-----|12|Restart (3X7)|-[SP]=PC,PC=z|
|SBB r |9F|*****| 4|Subtract with Borrow |A=A-r-CY |
|SBB M |9E|*****| 7|Subtract with Borrow |A=A-[HL]-CY |
|SBI n |DE|*****| 7|Subtract with Borrow Immed|A=A-n-CY |
|SHLD a |22|-----|16|Store HL Direct |[a]=HL |
|SIM |30|-----| 4|Set Interrupt Mask |mask=A |
|SPHL |F9|-----| 6|Move HL to SP |SP=HL |
|STA a |32|-----|13|Store Accumulator |[a]=A |
|STAX B |02|-----| 7|Store Accumulator indirect|[BC]=A |
|STAX D |12|-----| 7|Store Accumulator indirect|[DE]=A |
|STC |37|----1| 4|Set Carry |CY=1 |
|SUB r |97|*****| 4|Subtract |A=A-r (22X)|
|SUB M |96|*****| 7|Subtract Memory |A=A-[HL] |
|SUI n |D6|*****| 7|Subtract Immediate |A=A-n |
|XCHG |EB|-----| 4|Exchange HL with DE |HL<->DE |
|XRA r |AF|**0*0| 4|Exclusive OR Accumulator |A=Axr (25X)|
|XRA M |AE|**0*0| 7|Exclusive OR Accumulator |A=Ax[HL] |
|XRI n |EE|**0*0| 7|Exclusive OR Immediate |A=Axn |
|XTHL |E3|-----|16|Exchange stack Top with HL|[SP]<->HL |
|------------+-----+--+----------------------------------------|
| PSW |-*01 | |Flag unaffected/affected/reset/set |
| S |S | |Sign (Bit 7) |
| Z | Z | |Zero (Bit 6) |
| AC | A | |Auxilary Carry (Bit 4) |
| P | P | |Parity (Bit 2) |
| CY | C| |Carry (Bit 0) |
|---------------------+----------------------------------------|
| a p |Direct addressing |
| M z |Register indirect addressing |
| n nn |Immediate addressing |
| r |Register addressing |
|---------------------+----------------------------------------|
|DB n(,n) |Define Byte(s) |
|DB 'string' |Define Byte ASCII character string |
|DS nn |Define Storage Block |
|DW nn(,nn) |Define Word(s) |
|---------------------+----------------------------------------|
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
7 Vi x ly
| A B C D E H L |Registers (8-bit) |
| BC DE HL |Register pairs (16-bit) |
| PC |Program Counter register (16-bit) |
| PSW |Processor Status Word (8-bit) |
| SP |Stack Pointer register (16-bit) |
|---------------------+----------------------------------------|
| a nn |16-bit address/data (0 to 65535) |
| n p |8-bit data/port (0 to 255) |
| r |Register (X=B,C,D,E,H,L,M,A) |
| z |Vector (X=0H,8H,10H,18H,20H,28H,30H,38H)|
|---------------------+----------------------------------------|
| + - |Arithmetic addition/subtraction |
| & ~ |Logical AND/NOT |
| v x |Logical inclusive/exclusive OR |
| <- -> |Rotate left/right |
| <-> |Exchange |
| [ ] |Indirect addressing |
| [ ]+ -[ ] |Indirect address auto-inc/decrement |
| { } |Combination operands |
| ( X ) |Octal op code where X is a 3-bit code |
| If ( ~s) |Number of cycles if condition true |
----------------------------------------------------------------
V|. 10M 1A1 CAU H0| 0N 1AP 8A| 1AP:
Chung ta mi va khao sat xong vi x ly Z80 th phan nay se tiep tuc khao sat vi x ly
1. Tom tat:
1. Mot vi x ly co 3 phan chnh: khoi ALU, cac thanh ghi, va khoi control logic.
2. Khoi ALU co 2 ngo vao va mot ngo ra. Mot trong 2 port nhan d lieu t data bus,
ngo vao con lai se nhan d lieu t thanh ghi A. khoi ALU co the x ly 1 hoac 2 d
lieu, chc nang chnh cua khoi ALU la thc hien cac phep toan so hoc, cac phep
toan logic va kiem tra d lieu.
3. Thanh ghi trong vi x ly co the lu tr tam thi cac d lieu, cac thanh ghi thong
dung, cac thanh ghi co chc nang ac biet.
4. Tat ca cac vi x ly eu co cac thanh ghi c ban
Accumulator.
Program counter.
Stack pointer.
General purpose registers.
Memory address register and logic.
Instruction register.
Temporary register.
5. Cac thanh ghi rat can thiet cho vi x ly lam viec. Tuy nhien ngi lap trnh khong
the s dung het tat ca cac thanh ghi nay.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 73
6. Thanh ghi Accumulator luon lam viec vi khoi ALU, Accumulator la mot thanh ghi
quan trong cua vi x ly trong x ly d lieu. Chieu dai t d lieu cua thanh ghi
Accumulator bang vi chieu dai t d lieu cua vi x ly.
7. Thanh ghi Program counter co chc nang tao a ch e on lenh khi vi x ly thc
hien chng trnh, khi vi x ly on lenh xong va thc hien lenh th noi dung cua PC
tang len e chuan b on lenh tiep theo. Thanh ghi PC phai co chieu dai t d lieu
hay so bit co kha nang truy xuat het bo nh.
8. Mot chng trnh co the bat au tai bat ky v tr nao trong bo nh va co the ket thuc
tai bat ky v tr nao trong bo nh. Tuy nhien cac lenh trong chng trnh phai theo
ung mot trnh t hp ly.
9. Khi vi x ly bat au thc hien chng trnh:
Tng lenh trong chng trnh se c thc hien theo mot trnh t noi tiep tr khi
co lenh ac biet lam thay oi trnh t nay.
Khi PC ch en mot o nh th khoi control logic se on lenh t o nh nay.
Moi khi lenh c on, vi x ly se tang noi dung cua PC e chuan b cho lenh ke
va bat au thc hien lenh va on.
Thanh ghi a ch bo nh ch en moi o nh e truy xuat d lieu khi vi x ly yeu
cau.
D lieu trong thanh ghi a ch bo nh se c gi ra bus a ch ben ngoai e ket
noi vi bo nh. Thanh ghi a ch bo nh phai co u so bit e co the truy xuat het
tat ca cac o nh ma vi x ly co the.
10. Thanh ghi trang thai se lu tr lai cac ket qua cua mot so lenh, mot thanh ghi trang
thai thng co cac bit sau: bit zero, bit negative, bit carry, bit haft carry, bit parity,
bit overflow, bit interrupt...cac bit trang thai dung e xac nh trang thai cua mot so
la chon trong chng trnh.
11. Thanh ghi SP ch en mot o nh dung e lu tr d lieu tam thi. Moi khi ngan xep
dung e lu tr d lieu th gia tr trong SP se giam e chuan b cho viec lu tr d
lieu tiep theo.
12. Thanh ghi lenh lu tr lenh dang so nh phan e ra lenh cho khoi control logic thc
hien cai ma lenh yeu cau.
13. Khi lenh c on t bo nh co ngha la thc hien mot qua trnh copy d lieu trong
o nh cua chng trnh vao thanh ghi lenh.
14. Trong qua trnh thc hien lenh khoi control logic va khoi giai ma lenh se oc lenh
trong thanh ghi lenh.
15. Thanh ghi tam thi dung e lu tr d lieu cho ALU x ly.
16. Khoi giai ma lenh thc hien cong viec giai ma lenh e xem lenh yeu cau thc hien
cong viec g, sau o khoi control logic se thc hien ung cong viec o.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
7- Vi x ly
17. Cac khoi trong vi x ly c ket noi vi nhau thong qua bus d lieu ben trong. Qua
trnh ket noi e trao oi d lieu c khoi control logic quyet nh, s quyet nh
nay tuy thuoc vao lenh. Bus d lieu luon la bus 2 chieu.
18. Mot lenh cua vi x ly la mot t dang so nh phan, dung e khoi giai ma va khoi
control logic thc hien mot cong viec nhat nh.
19. Tap lenh cua vi x ly la tat ca cac lenh ma vi x ly co the hieu va thc hien c.
20. Chieu dai cua 1 lenh (so lng cac bit cua 1 lenh) bang vi chieu dai t d lieu
21. Lenh cua vi x ly c giai ma va thc hien khi lenh c nap thanh ghi lenh ben
trong vi x ly chu ky on lenh. chu ky thc hien lenh khoi giai ma va khoi
control logic se thc hien cac yeu cau cua lenh.
22. Mot lenh cua vi x ly bao gom 2 phan hay 2 thong tin. Thong tin th nhat e bao
cho vi x ly biet lam cong viec g. Thong tin th 2 bao cho vi x ly a ch cua d
lieu. Thong tin th nhat thng goi la ma lenh hay ma cong tac. Thong tin th 2 goi
la a ch hay a ch cong tac (co ngha la lenh xay ra oi vi d lieu tai a ch
o).
23. Co nhom lenh c ban va co rat nhieu ma lenh cho 1 nhom lenh.
24. Lenh cua vi x ly la mot so nh phan gom ca thong tin va a ch c goi la ma
may. e de nh lenh ma may a c chuyen sang ma gi nh, t gi nh co y
ngha gan vi chc nang cua lenh. Tap hp cac t gi nh goi la ngon ng
Assembly. Khi viet chng trnh bang Assembly, e may thc hien chng trnh nay
th phai co chng trnh dch cac lenh viet bang Assembly sang ma may e vi x ly
x ly, chng trnh dch c goi la Assembler.
2. Cau hoi on tap va bai tap trac nghiem:
1. S o khoi cua vi x ly dung e
A. Dien ta chi tiet cac cong logic va cac Flip Flop c dung e thiet ke nen vi x ly.
B. Dien ta cac mach logic cua vi x ly ket noi vi cac thiet b IO va bo nh ben ngoai.
C. Dung e trnh bay cac khoi logic co chc nang x ly d lieu e giai quyet mot van
e.
D. Ca 3 cau tren eu ung.
2. Trong cac cau sau cau nao khong phai la chc nang cua ALU:
A. Add
B. Shift
C. Complement
D. Lu tr d lieu.
3. ALU co 2 ngo vao, 2 ngo vao nay c ket noi vi:
A. Program Counter.
B. Bus d lieu ben trong.
C. Control logic.
D. Thanh ghi a ch bo nh.
4. Chc nang chnh cua khoi ALU:
A. Thc hien phep cong.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
Vi x ly 75
B. ong vai tro xuat d lieu giong nh thanh ghi Accumulator.
C. Thc hien cac phep toan logic va so hoc e x ly d lieu.
D. Tat ca 3 cau tren eu ung.
5. Hau het cac phep toan logic va so hoc trong vi x ly thc hien gia noi dung cua mot o
nh hoac noi dung cua mot thanh ghi vi:
A. Noi dung cua thanh ghi Accumulator.
B. Noi dung thanh ghi Program Counter.
C. Noi dung thanh ghi a ch.
D. Thanh ghi lenh.
6. Mot vi x ly 16 bit co the truy xuat 2
20
= 1.048.567 o nh co the cho biet thanh ghi PC
cua Microprocessor nay co chieu dai t d lieu bao nhieu bit:
A. 4 B. 8 C. 16
D. 20 E. 22 E. 32
7. Thanh ghi Program counter cua vi x ly la mot trong nhng thanh ghi:
A. ac biet.
B. Thong dung
C. Memory.
D. Tat ca 3 cau tren.
8. Khi vi x ly ang thc hien lenh, thanh ghi PC ang ch en:
A. Lenh va thc hien.
B. Lenh ang thc hien.
C. Lenh tiep theo.
D. Ca 3 cau tren eu sai.

9. Hay thc hien cac phep cong cac so nh phan 8 bit. Sau khi cong xong cac con so nay,
hay xac nh anh hng cua phep cong en cac bit Zero (Z), bit Negative (N), bit Carry (C).

0000 1111 0011 1011
+ 1111 0000 + 1100 0101

1110 1111 0000 0001
+ 1111 1111 + 1111 0001

0111 0100 0000 0001
+ 1100 1100 + 0111 1111

10. Khi tang noi dung cua thanh ghi len 3 lan (moi lan tang 1) lam cho bit Zero cua thanh
trang thai c Set mc logic 1 lan tang th 3. Vay gia tr ban au cha trong thanh ghi la
bao nhieu ?
11. Thanh ghi a ch bo nh dung e ch en:
A. Noi dung cua bo nh.
B. V tr cua o nh.
C. V tr cua CPU.
D. V tr cua cac thanh ghi.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ | :| . | . .~ :~ : - ||
7 Vi x ly
12. Thanh ghi a ch bo nh ket noi vi bus d lieu ben trong vi x ly e no co the nap gia
tr t:
A. Thanh ghi Program counter.
B. Cac thanh ghi thong dung.
C. Memory.
D. Ca 3 cau tren.
13. Cac ngo ra thanh ghi a ch dung e ket noi vi
A. Thanh ghi Accumulator cua vi x ly.
B. Bus d lieu ben trong cua Microprocessor.
C. Bus a ch bo nh ben ngoai cua vi x ly.
D. Vi ngo vao cua bo giai ma lenh.








Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M





Chucng |||




CA U 1kUC V| XU L 8086/8088
' ~| :| :-| :| . | . ``/```
~ :|~|| | :-| :| . | .
|| :|~ | ~| |~ . | .
- ---: . | .
:~: :|~| || . | . ``
CA C PHU0NC PHAP |NH |A CH| CUA V| XU L 8086
1AP LENH CUA V| XU L 8086
' \~ -|| |~ . | . ``/```
| .|~| .- :~: -|| |~ . | . ``
|~ -~: :~: -|| |~ . | . ``
CA U H0| VA 8A| 1A P
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
7` . | .

|. CAU 1kUC CUA V| XU L 8086/8088:
' ~| :| :-| :| . | . ``/```
Nh a trnh bay phan gii thieu th chng nay se khao sat vi x ly 16 bit va vi x ly
tieu bieu la 8086/8088.
Cau truc ben trong cua vi x ly gom hai khoi chnh nh hnh 3-1, no c to chc thanh 2
bo x ly rieng la BIU (Bus Interface Unit) va EU (Execution Unit).


H|nh 3-. Cou truc be n trcng cuo v| xu |y 8086/8088.
Chc nang cua kho i BIU: nhan ma lenh t bo nh va at chung vao hang ch le nh. Khoi
EU giai ma va thc thi nhng ma lenh trong hang ch lenh.
V cac n v lam viec oc lap vi nhau nen khoi BIU co the ang nhan 1 lenh mi trong
khi EU ang thc thi lenh trc o. Khi khoi EU san sang thc thi lenh mi th no se tm thay
ngay ma lenh mi ang i trong hang ch le nh.
Chc nang cua khoi EU: nhan nhng ma lenh cua chng trnh va d lieu t BIU, thc thi
lenh va gi tr ket qua tr lai cho khoi BIU e lu vao thanh ghi hay bo nh hoac co the xuat ra
ngoai thiet b giao tiep. Chu y: khoi EU khong co ket noi vi bus he thong nen ch nhan va
xuat d lieu eu thong qua khoi BIU.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . 73
Vi x ly 8088 cung la mot vi x ly 16 bit, s khac nhau gia vi x ly 8086 va 8088 la khoi
BIU: vi x ly 8086 th bus d lieu giao tiep ben trong va ben ngoai eu la 16 bit trong khi o
vi x ly 8088 th bus ben trong la 16 bit con bus ben ngoai ch co 8 bit. S khac nhau nay dan
en to chc bo nh giao tiep vi vi x ly 8086 va 8088 cung khac nhau. Vi x ly 8088 th bo
nh to chc theo byte, con vi x ly 8086 th bo nh to chc theo word (2 byte).
Mot iem khac biet th 2 o la hang i lenh cua vi x ly 8086 co 6 byte con vi x ly 8088
ch co 4 byte.
Con tat ca cac khoi con lai eu giong nhau ne n ieu nay cho phep mot chng trnh cho
8086 th co the chay tren vi x ly 8088.
Qua trnh nha n lenh va thc thi:
8ucc : Khoi BIU xuat noi dung cua thanh ghi con tro lenh IP (Instruction Pointer) ra bus
a ch e truy xuat bo nh tien hanh oc ma lenh roi lu vao khoi BIU.
8ucc 2: Tang thanh ghi con tro lenh len e tro e n lenh ke.
8ucc 3: Khi ma lenh a trong BIU th no c chuyen vao hang ch lenh (queue). Hang
ch lenh la cac thanh ghi to chc theo kieu vao trc ra trc (FIFO = First In First Out).
8ucc 4: Gia s ban au hang i lenh trong th khoi EU se lap tc lay ma lenh va thc
hien ngay ma lenh o .
8ucc 5: Trong khi khoi EU ang thc thi lenh th khoi BIU tien hanh nhan ma lenh mi.
Tuy thuoc vao thi gian thc thi lenh ma khoi BIU co the lay nhieu ma lenh trc khi khoi EU
lay ma lenh ke.
Khoi BIU c lap trnh e nhan mot lenh mi bat ky khi nao hang i con cho tro ng. S
ket hp nay co u iem la khoi EU thc hien lenh lien tuc khong phai ch nhan ma lenh so vi
vi x ly trc nh khoi BIU dan e n lam tang toc o x ly chng trnh.
Toan bo qua trnh thc hien c minh hoa nh hnh 3-2:

H|nh 3-2. Quo tr|nh nhon |e nh vo thuc th| |e nh.
H|nh (o| |o dong v| xu |y thuc h|e n thec coch thcng thucng.
H|nh (b| |o dong v| xu |y thuc h|e n thec cou truc ducng cng.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
` . | .
Ghi chu: (1) Cac ma lenh nay b bo qua.
(2) Lenh nay can d lieu kho ng nam trong hang i.
(3) Khi thc hie n lenh Jump.
(fetch: on lenh t bo nh, execute: thc hie n lenh, Read data: oc d lieu, wait: i).
Trong qua trnh khoi BIU nhan ma lenh a vao hang i va khoi EU thc thi lenh th van
xay ra 3 trng hp khoi EU phai ri vao trang thai ch va khoi BIU cu ng ri vao trang thai
ch.
1rucng hcp : xay ra khi lenh can truy xuat en mot o nh e lay d lieu khong co trong
hang i. Khoi BIU phai tam ngng viec nhan ma lenh ma tien hanh xuat a ch e nhan d
lieu t bo nh gi cho khoi EU. Sau khi thc thi xong khoi EU tiep tuc nhan ma lenh t hang va
BIU tiep tuc on ma lenh gi vao hang.
1rucng hcp 2: xay ra khi lenh ang thc thi la lenh nhay jump. Trong trng hp nay
khoi EU se nhay ti thc hien lenh tai a ch mi, trong khi o khoi BIU a nhan ma cua lenh
ke, chnh v the EU phai i ch khoi BIU nhan a ch cua ni nhay en, sau khi nhan a ch
t khoi BIU th khoi EU se nhay va khoi BIU tiep tuc nhan ma lenh t bo nh a vao hang
i.
1rucng hcp 3: trng hp lam BIU ngng nhan ma lenh. Trng hp nay xay ra khi khoi
EU thc thi lenh mat nhieu thi gian. V du nh lenh AAM (lenh hieu chnh thap phan cho le nh
nhan) can 83 chu ky xung nhp mi thc hien xong, trong khi o khoi BIU can 4 xung th nhan
xong mot ma lenh ne n BIU nhan ma lenh lam ay hang i nhng EU van cha thc hien xong
lenh nen BIU phai ch.
Qua trnh nhan lenh trong khi EU thc thi lenh co mot tien ch la co the s du ng cac bo
nh co toc o truy xuat cham ma van khong lam anh hng en nang suat he thong nh co kien
truc ng o ng.
Chuon PC: Vi bus d lieu 16 bit th can 2 day bo nh nhng tai thi iem o bo nh rat
at tien. Do o Intel cho ra i vi x ly 8088 hoan toan giong 8086 ngoai tr bus d lieu ben
ngoai la 8 bit.
Sau o IBM cho ra i may tnh IBM-PC s du ng vi x ly 8088 va bo nh 16Kbyte (co the
m rong len en 64Kbyte) va cho ra may tnh PC chuan vi toc bo xung clock la 4.77 MHz.
~ :|~|| | :| . | . ``/```
Vi x ly 8086/8088 co 14 thanh ghi va c phan loai nh sau:
Cac thanh ghi d lieu.
Cac thanh ghi ch so va con tro.
Cac thanh ghi oan hay con goi la cac thanh ghi phan oan.
Cac thanh ghi trang thai va ieu khien.
Hnh 3-3 trnh bay cac thanh ghi ben trong vi x ly 8086/8088:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . `'

H|nh 3-3. 4 thonh gh| ben trcng v| xu |y.
a. Khao sat cac thanh ghi d lieu:
Cac thanh ghi d lieu bao gom 4 thanh ghi co ten la thanh ghi AX, BX, CX va DX, chung
eu la thanh ghi 16 bit nen cho phep lu tr c d lieu 16 bit. Moi thanh ghi co the chia ra
lam 2 thanh ghi: thanh ghi lu tr byte thap (Low) va thanh ghi lu tr byte cao (High) co the
truy xuat oc lap va cu the la:
AH, AL c chia t thanh ghi AX.
BH, BL c chia t thanh ghi BX.
CH, CL c chia t thanh ghi CX.
DH, DL c chia t thanh ghi DX.
Chc nang cua cac thanh ghi: dung e thc hien cac phep toan so hoc, phep toan logic va
chuyen d lieu.
Co mot so thanh ghi khi s dung c hieu ngam trong mot so lenh nh cho bang 3-1.
1honh gh| ucc h|e u ngom trcng mct sc |enh:
AX MUL, |MUL
D|V, |D|V
|N, 0U1, CWD vo coc phep tcon chuc|.
AL MUL, |MUL
D|V, |D|V
|N, 0U1
AAA, AAD, AAM, AAX
C8W
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
` . | .
DAA, DA5 vo coc phep tcon chuc|.
AH MUL, |MUL
D|V, |D|V
C8W
DX XLA1
CX L00P, L00PE, L00PNE
Coc phep tcon str|ng vc| t|e p dou ngu kEP
CL kCk, kCL, k0k, k0L (quoy vc| sc |on |uu trcng CL|
5Hk, 5Ak, 5AL (d|ch vc| sc |on |uu trcng CL|
DX MUL, |MUL
D|V, |D|V
8ong 3-. Coc thonh gh| kh| su dung ducc h|eu ngo m.
Thonh ght tch luu AX (Accunulotor):
La thanh ghi c s du ng nhie u nhat trong cac lenh so hoc, lenh logic va truyen d lieu
vi ly do la khi s dung thanh ghi nay tao ra ma may ngan nhat.
Trong cac phep toan nhan hoa c chia th mot trong cac so han tham gia phai cha trong AH
hoac AL, cac thao tac vao ra cung s dung thanh ghi AH hoac AL.
Thonh ght co so RX (Rose):
Thanh ghi BX c dung cho tnh toan a ch trong phng phap nh a ch gian tiep.
Thonh ght den CX (Count):
Viec thc hien cac vong lap c thc hien de dang nh thanh ghi CX, trong o CX ong
vai tro la bo em vong lap. Mot lenh thng s dung thanh ghi CX o la lenh REP (Repeat). CL
cung c s dung la mot bien em trong cac lenh dch hay xoay cac bit.
Thonh ght du lteu DX (Doto):
DX dung e nh a ch gian tiep trong cac thao tac xuat nhap (In/Out), no cung con c
s dung cha toan han, ket qua trong phep nhan va chia.
Thonh ght con tro uo ch so:
Cac thanh ghi SP, BP, SI, DI thng tro ti cac o nh (tc la chc cac a ch offset cua
cac o nh o). Khac vi thanh ghi oan, cac thanh ghi con tro va ngan xep c s du ng trong
cac thao tac so hoc va mot so thao tac khac nhau.
b. Khao sat cac thanh ghi ch so va thanh ghi con tro:
Cac thanh ghi SP, BP, SI va DI la cac thanh ghi 16 bit dung e cha cac gia tr offset (con
goi la o lech) cho cac phan t nh a ch trong mot oan (segment). Cac thanh ghi nay con
c s dung trong cac phep toan so hoc va logic.
Hai thanh ghi con tro SP va BP dung e quan ly bo nh ngan xep hien hanh.
Hai thanh ghi ch so SI va DI dung e truy xuat hay quan ly vung nh d lieu va vung nh
d lieu m rong (extra segment).
Thonh ght con tro ngon ep SP (Stock Potnter):
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . `
Dung e ket hp vi thanh ghi oan SS (Stack Segment) e quan ly a ch cua bo nh
ngan xep.
Thonh ght con tro co so RP (Rose Potnter):
Thanh ghi nay c dung e truy cap d lieu trong ngan xep ma kho ng lam thay oi SP.
Tuy nhien, khac vi SP thanh ghi BP cung con c s dung e truy cap d lieu cac vu ng nh
khac.
Thonh ght ch so nguon SI (Source Inde):
Thanh ghi SI c s du ng e tro ti cac o nh trong oan d lieu c ch nh bi thanh
ghi oan d lieu DS (Data Segment), co the truy cap de dang cac o nh lien tiep bang cach tang
gia tr cua thanh ghi SI.
Thonh ght ch so dch DI (Desttnotton Inde):
Thanh ghi DI co chc nang tng t nh thanh ghi SI va c dung ket hp vi thanh ghi
oan them ES (Extra Segment). Ca hai DI va SI thch h p trong cac thao tac sao chep, di
chuyen hoac so sanh cac khoi d lieu co dung l ng e n 64kB.
c. Khao sat cac thanh ghi oan:
Co 4 thanh ghi oan CS, DS, SS va ES e u la cac thanh ghi 16 bit.
Trc khi trnh bay chc nang cua cac thanh ghi nay chung ta can phan tch bo nh giao
tiep vi vi x ly 8086/8088.
Bo nh la tap hp cac byte o nh trong o moi byte co mot a ch xac nh. Vi x ly
8086/8088 co 20 ng a ch e gian tiep vi bo nh co dung lng 1 Mbyte, moi o nh co
mot a ch 20 bit. Cac byte au tien cua bo nh co a ch nh sau:
A
19
A
18
A
17
A
16
A
15
A
14
A
13
A
12
A
11
A
10
A
9
A
8
A
7
A
6
A
5
A
4
A
3
A
2
A
1
A
0
HEX
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 00001
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 00002


1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 FFFFF
Do cac a ch cua cac o nh la 20 bit khong the cha trong mot thanh ghi 16 bit cua vi x
ly 8086/8088 (16 bit) nen 8086/8088 chia bo nh 1Mbyte thanh 16 oan bo nh (Memory
Segment). 16 oan bo nh do cac thanh ghi oan quan ly goi la thanh ghi segment c trnh
bay nh hnh 3-4:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
`- . | .

H|nh 3-4. Coc thonh gh| dco n quon |y coc vung nhc.
Moi oan bo nh eu co dung l ng la 64Kbyte va co a ch t 0000H cho en FFFFH.
e quan ly a ch vung nh cua mot oan phai s dung thanh ghi 16 bit goi la thanh ghi offset
a ch trong 1 oan con goi la a ch logic hay a ch offset.
Khi o a ch that (con goi la a ch logic) cua mot o nh c xac nh bang cach ket
hp thanh ghi quan ly a ch oan va thanh ghi quan ly a ch lenh segment:offset.
Trong hnh 3-4 co cho v du a ch segment:offset = 0FE6:012Bh th a ch segment la
0FE6H va a ch offset la 012BH. e tm a ch vat ly (Physical Address = PA) cua o nh th
vi x ly phai dch a ch segment ve ben trai 4 bit (bit them vao la bit 0) hay co the xem nh
them vao mot con so 0H ben trai (a ch 0FE6H sau khi dch th tr thanh 0FE60H )va sau o
cong vi a ch offset (012BH), ket qua a ch vat ly cua o nh co a ch 0FE6:012Bh se la
0FF8Bh.
Bon thanh ghi oan co chc nang quan ly 4 vung nh oan tng ng:
Thanh ghi oan CS cung vi thanh ghi offset co chc nang quan ly vung nh cha ma
lenh (Code Segment).
Thanh ghi oan ES cung vi thanh ghi offset co chc nang quan ly vung nh m rong
(Extra Segment).
Thanh ghi oan SS cung vi thanh ghi offset co chc nang quan ly vung nh ngan xep
cha cac d lieu tam thi (Stack Segment).
Thanh ghi oan DS cung vi thanh ghi offset co chc nang quan ly vung nh cha d lieu
(Data Segment).
Vung nh 1Mbyte c chia ra lam 16 oan oc lap nh bang 3-2:
Vu ng nh oan Segment: offset (ba t au) Segment: offset (ket thuc)
0 0000:0000 0000:FFFF
1 1000:0000 1000:FFFF
2 2000:0000 2000:FFFF
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . `
3 3000:0000 3000:FFFF
4 4000:0000 4000:FFFF
5 5000:0000 5000:FFFF
6 6000:0000 6000:FFFF
7 7000:0000 7000:FFFF
8 8000:0000 8000:FFFF
9 9000:0000 9000:FFFF
10 A000:0000 A000:FFFF
11 B000:0000 B000:FFFF
12 C000:0000 C000:FFFF
13 D000:0000 D000:FFFF
14 E000:0000 E000:FFFF
15 F000:0000 F000:FFFF
8ong 3-2. Coc dcon bc nhc dcc |op.
Vi viec phan chia nh bang tren th cac oan la oc lap khong b chong len nhau that ra
con co the cach xa nhau, nhng trong thc te do phan cng thiet ke khong ay u dung l ng
1Mbyte v du nh he thong ch co 128Kbyte th bat buoc cac oan phai chong len nhau. Ngi
lap trnh phai phan chia cac vung nh theo he thong phan cng a thiet ke.
Mot chng trnh khong phai luc nao cung chiem het mot oan 64KByte, do ac iem
chong nhau gia cac oan nen cho phep cac oan cua mot chng trnh nho hn 64KB co the
at gan nhau. Tai mot thi iem, ch co cac o nh c nh a ch bi 4 thanh ghi oan mi co
the truy cap, ngha la ch co 4 oan bo nh la tac ong. Tuy nhien noi dung cua cac thanh ghi
oan co the thay oi bi chng trnh e truy cap en cac oan khac nhau.
d. Khao sat thanh ghi con tro lenh va thanh ghi trang thai :
Thanh ghi con tro lenh IP (Instruction Pointer) co chc nang quan ly lenh giong nh thanh
ghi PC (Program Counter) cua cac vi x ly 8 bit.
Khoi BIU quan ly thanh ghi IP e lu tr a ch offset va ket hp vi thanh ghi CS e tao
ra a ch vat ly e nhan ma lenh t bo nh cha ma lenh Code Segment. Sau khi on ma lenh
th noi dung cua thanh ghi IP tang len e on ma cua lenh ke.
Ngi lap trnh khong the s du ng trc tiep thanh ghi nay.
Thanh ghi c (Flag Register) cua 8086 co o dai 16bit, trong o co 3 bit ieu khien va 6
bit trang thai con c goi la c. Cac bit con lai cha c thiet lap nen ngi dung khong the
truy xuat. Hnh 3-5 la cau tao cua thanh ghi c trong 8086/8088:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
` . | .

H|nh 3-5. Coc b|t trcng thonh gh| cc.
Khi vi x ly thc hien cac phep toan th can c vao ket qua sau khi x ly se tac ong en
mot so c trong thanh ghi trang thai nh a trnh bay phan trc. Chc nang cua cac thanh ghi
c c e cap lai nh sau:
+ Cc tron CF:
C nh CF bang 1 khi thc hien phep toan cong co tran hoac phep toan tr co mn,
ngc lai neu kho ng tran, khong mn th c CF bang 0. C CF cung b anh h ng bi cac lenh
xoay va lenh dch.
+ Cc chon |e PF:
Sau khi thc hien cac lenh so hoc hoac lenh logic oi vi d lieu dang byte: neu byte ket
qua la so chan th c PF bang 1 ngc lai th c PF bang 0 ne u la so le, oi vi d lieu x ly
la word ch xet cac byte thap.
+ Cc nhc phu AF:
Khi thc hien lenh cong hoac lenh tr so BCD: neu d lieu dang byte th c AF bang 1 khi
cong hoac tr 4 bit thap b tran hoac co m n, neu d lieu dang word th c AF bang 1 khi co ng
hoac tr byte thap b tran hoac co mn. C AF c s dung trong cac lenh x ly vi d lieu
dang so BCD.
+ Cc zerc ZF:
C zero bang 1 khi ket qua x ly bang 0.
+ Cc dou 5F:
C dau bang 1 khi bit MSB cua ket qua bang 1 tc la so am, oi vi lenh byte MSB la
bit 7 va trong lenh word la bit 15. Ngc lai c SF bang 0.
+ Cc boy 1F:
Co chc nang thc hien chng trnh theo t ng bc, khi TF bang 1 th vi x ly
8086/8088 phat sinh ngat cng loai 1. Chng trnh DEBUG s dung khi thi hanh lenh T (trace)
e chay tng lenh mot. au tien DEBUG thiet lap c TF roi mi chuyen ieu khien cho lenh
o. Sau khi lenh c thi hanh vi x ly se phat sinh ngat do TF c lap chng trnh DEBUG
s dung chnh phuc vu ngat nay e lay quyen ie u khien t vi x ly.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . `7
+ Cc ngot |F:
C ngat c s dung e ieu khien cac ngat phan cng be n ngoai, neu c IF bang 1 th
cac ngat c phep. Khi c IF bang 0 th cam cac ngat ngoai hay con goi la cac ngat a b che.
Thc ra van con mot ngat cng c phep ngat o la ngat NMI (Non Maskable Interrupt).
Trc khi vi x ly thc hien chng trnh phuc vu ngat no se t ong xoa ca c IF va c
TF, viec nay co chc nang cam cac ngat khac lam gian oan vi x ly ang thc hie n chng
trnh hien tai. Tuy nhien neu chung ta cho phep ngat trong chng trnh phuc vu ngat va neu
ngat xay ra th chng trnh nay se b ngat e phuc vu chng trnh ngat mi.
+ Cc tron 0F:
C tran OF bang 1 khi ket qua la so nh phan co dau vt qua gii han qui nh va ngc
lai th c OF bang 0.
Nh a trnh bay tren th c CF la c tran oi vi d lieu x ly va ket qua sau khi x ly
eu la so nh phan khong dau. Co n c OF la c tran oi vi so nh phan co dau.
Vi so nh phan kho ng dau 8 bit th vung d lieu co gia tr bat au t 0 en 255, so nh
phan 16 bit th t 0 en 65535.
Vi so nh phan co dau 8 bit th vung d lieu co gia tr t -128 e n +127, so nh phan 16
bit co gia tr t -32768 en 32767.
+ Cc d|eu kh|en DF:
La mot trong ba c ieu khie n du ng ieu khien cac lenh x ly cua vi x ly, cong dung cua
DF la dch hng cho cac thao tac chuoi, cac thao tac nay c thc hien bi hai thanh ghi ch
so SI & DI, noi dung cua hai thanh ghi nay se t ong tang len khi DF = 0 va giam xuong khi
DF =1.
|| :|~ | ~|
Vi x ly 8086 la vi x ly 16 bit nhng bo nh van to chc theo byte (8bit). Co 2 nguyen
nhan cua viec s dung to chc bo nh nh the la:
Nguyen nhan th nhat: Cho phep vi x ly truy xuat byte va truy xuat word eu c. To
chc rat quan trong vi cac thiet b IO nh may in, thiet b au cuoi, modem eu x ly d lieu
c ma hoa dang ASCII 7 bit hay 8 bit.
Nguyen nha n th hai: nhieu ma lenh cua vi x ly 8086/8088 ch co o dai 1 byte, cac
lenh khac co o dai t 2 en 8 byte. Vi cach to chc theo byte se cho phep truy xuat cac byte
mot cach oc lap e x ly cac lenh co so byte ma lenh le.
Vi x ly 8086 khi truy xuat d lieu dai 16 bit th no se truy xuat ong thi byte co a ch
le va byte co a ch chan nen to chc bo nh cua vi x ly 8086 c chia lam 2 day bo nh,
mot day co a ch le va mot day co a ch chan nh hnh 3-6.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
`` . | .

H|nh 3-6. 1c chuc bc nhc cuo v| xu |y 8086.
Vi cach to chc bo nh thanh 2 day ta co the xem bo nh 1024Kbyte moi o nh 1 byte
ch con 512Kword moi o nh cha 2 byte. Khi o d lieu cua moi o eu co a ch chan.
Khi truy xuat d lieu 16 bit nam trong mot o nh th ch can 1 lan truy xuat la co the lay
c 16 bit d lieu, nhng khi truy xuat d lieu 16 bit co a ch le: trong o 8 bit thap nam o
nh co a ch le va 8 bit cao nam o nh co a ch chan (tnh theo byte) th vi x ly cung thc
hien c nhng phai thc hien 2 lan truy xuat va se lam cham qua trnh x ly chng trnh.
Vi vi x ly 8088 th do bus d lieu ben ngoai ch co 8 bit nen ch co 1 day bo nh va ch
truy xuat byte ieu nay cho phep vi x ly ch thc hien 2 lan oc (moi lan 1 byte) cho du a
ch cua d lieu 16bit co a ch chan hay le.
Coc uu d|em cuo bc nhc phon dcon:
Vi kieu to chc bo nh theo oan bao gom oan bo nh cha ma lenh, oan bo nh cha
d lieu, oan bo nh cha d lieu m rong, oan bo nh lam ngan xep e cha cac d lieu tam
thi, cac bo nh oan nay hoan toan co oc lap vi nhau.
Vi kieu tach oc lap nay cho phep chng trnh co the x ly nhieu oan d lieu khac
nhau bang cach ch can thay oi gia tr cua thanh ghi DS e tro en vung d lieu mi.
Mot u iem ln nhat vi kieu bo nh phan oan la cac chng trnh s dung kie u nh
a ch tham chieu co the nap va chay bat ky v tr nao trong bo nh. ieu nay thc hien c
la do cac a ch logic luon trai t a ch 00000H en FFFFFH khong phu thuoc a ch cua
oan.
Mot chng trnh cha thc thi c cat tam tren a t va khi muon thc thi th c nap
vao bo nh ma kho ng can phai quan tam en a ch vat ly cua chng trnh trong bo nh he
thong. Cac chng trnh nh the c xem la cac chng trnh tai nh v c co ngha la
chung co the chay bat ky v tr nao trong bo nh. Cac yeu cau e co mot chng trnh tai nh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . `3
v c la khong co s dung cac tham chieu a ch vat ly va khong lam thay oi cac thanh ghi
oan cho phe p.
Coc khuyet d|em cuo bc nhc phon dcon:
Phc tap phan cng: viec nh a ch can 2 thanh ghi: mot thanh ghi segment va 1 thanh
offset.
Phc tap phan mem: cac chng trnh b gii han kch thc ch nam trong khoang
64Kbyte t vi x ly 80386 tr i cac oan co dung lng 4Gbyte bang toan bo dung lng bo
nh kho ng cho phep phan oan bo nh.
- ---: |- . | .
Khi m may hoac khi reset v vi x ly 8086/8088 bat au thc thi lenh tai a ch
FFFF:0000H. Cac thanh ghi c thiet lap cac gia tr nh bang 3-3:
Cac thanh ghi Gia tr
1honh gh| cc 1o t co deu b| xco
|P 0000H
C5 FFFFH
D5 0000H
DD 0000H
E5 0000H
8ong 3-3. 1rong tho | cuo coc thonh gh| kh| v| xu |y b| reset.
Trong cac may vi tnh, a ch logic FFFF:0000H cha ma cua lenh nhay JUMP e nhay
en lenh th nhat trong chng trnh BIOS co n goi la POST Power On Self Test t kiem tra
khi bat au m may e kiem tra va khi ong phan cng.
:~: :|~| || . | . ``/```
a. Khao sat ac tnh ien :
S o chan cua vi x ly 8086 va 8088 nh hnh 3-7.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
3 . | .

H|nh 3-7. 5c dc chon v| xu |y 8086 vo v| xu |y 8088.
Do vi x ly 8086 co bus d lieu ben ngoai 16 bit con vi x ly 8088 th bus d lieu ben
ngoai ch co 8 bit nen s o chan cua chung khac nhau nen khong the thay the chung trong
mach ien.
Coc yeu cou ve ngucn d|en:
Vi x ly 8086/8088 s dung nguon +5V cho phep sai so 10%.
Dong ien tieu thu vi phien ban NMOS th vi x ly 8086 dong lam viec khoang 360mA,
vi x ly 8088 dong lam viec khoang 340mA.
Vi phien ban CMOS th do ng tieu thu khoang 10mA.
Tam nhiet o lam viec vi phien ban thng mai t 0 en 70 C.
oc t|nh cuo coc chon:
ac tnh mc logic ngo vao DC:
Muc |cg|c |en op Dcng d|e n
0 -0.5V (mox| -0.8V (mox| 0A (mox|
2V(m|n| +0.5V (mox| 0A (mox|
ac tnh mc logic ngo ra DC:
Muc |cg|c |en op Dcng d|e n
0 0.45V (mox| |
0L
= 2.5mA (c 0.45V|
2.4V(mox| |
0H
= -400A (c 2.4V|
H|gh Z 0.45V Vcut Vcc |
L0
= 0A (mox|
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . 3'
Nhung khoc b|et vc| 11L:
Mc logic 0 cua vi x ly 8086/8088 la 0.45V
Mc logic 0 cua TTL la 0.4V.
Mien nhieu cua TTL la 0.8V 0.4V = 0.4V
Mien nhieu cua vi x ly 8086/8088 la 0.8V 0.45V = 0.35V nen mien nhieu b giam.
1ucng th|ch vc| coc hc |cg|c:
Vi x ly 8086/8088 tng thch hau het vi cac ho logic.
b. Mo ta chan cua vi x ly 8086 :
Vi x ly 8086 co mot bus a ch 20 bit, bus d lieu 16 bit, 3 chan nguon va 17 chan con
lai dung cho cac chc nang ieu khien va nh thi. Tat ca cac chan nay c chia ra lam 3
nhom bus: bus a ch, bus d lieu va bus ieu khie n.
Bus d lieu 16 ng c a hp vi 16 ng a ch thap cua bus a ch e lam giam
bt so chan cho IC. Hnh 3-7 trnh bay s o chan cua vi x ly 8086 vi 2 che o hoat ong toi
thieu (minimum mode) va che o hoat ong toi a (maximum mode). che o maximum vi x
ly se ket noi vi bo ieu khien bus 8288 e tao cac tn hieu ieu khien va co the lam viec trong
he thong co nhieu vi x ly.
Cac nha thiet ke vi x ly thng s dung ky thuat a hp hay don kenh theo thi gian e
cho phe p 1 chan co the thc hien nhieu chc nang nh 16 ng AD0 AD15. Khi truy xuat bo
nh th 16 ng AD0 AD15 c phan chia theo thi gian nh sau: trong khoang thi gian
T1 chung ong vai tro la 16 ng a ch hay co chc nang tai a ch, trong khoang thi gian
con lai t T2 en T4, chung ong vai tro la 16 ng d lieu hay co chc nang tai d lieu. Khi
thc hien chc nang tai d lieu th a ch xuat ra thi gian T1 khong co n na ne n phai du ng
mach phan kenh e tach va lu lai a ch.
Hnh 3-8 trnh bay gian o thi gian lam truy xuat bo nh cua vi x ly va giai thch hoat
ong cua tng chu ky.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
3 . | .

H|nh 3-8. 5c dc chon v| xu |y 8086 vo v| xu |y 8088.
+ 0 chu ky 1:
Trong chu ky nay a ch cua bo nh hay thiet b ngoai vi a ra tren cac ng a ch.
Cac tn hieu ieu khien ALE, DT/R, M/IO cu ng c a ra e ho tr cac vi mach hoan tat viec
gi thong tin a ch nay.
+ 0 chu ky 12:
Trong chu ky nay vi x ly a ra cac tn hieu ieu khien RD hoac WR, DEN va tn hieu
d lieu tren D0 D15 neu la lenh ghi. DEN thng dung e m cac bo em cua bus d lieu
neu chu ng c dung trong he thong. Tai cuoi chu ky T2 th vi x ly lay mau tn hieu Ready e
x ly trong chu ky tiep theo khi no phai i khi lam viec vi bo nh hay thiet b ngoai vi co toc
o cham hay khong.
+ 0 chu ky 13:
Trong chu ky nay vi x ly danh thi gian cho bo nh hay thiet b ngoai vi truy cap d lieu.
Neu la chu ky oc d lieu th tai cuoi T
3
vi x ly se lay mau tn hieu cua bus d lieu. Neu tai
cuoi chu ky T
2
ma vi x ly phat hien ra tn hie u Ready = 0 th vi x ly t xen vao T
3
mot chu ky
i Twait e tao chu ky i T
w
= n * T nham keo dai thi gian e thc hien lenh, tao ieu kien
cho bo nh va thiet b ngoai vi co toc o cham co u thi gian hoan tat cong viec oc-ghi d
lieu.
+ 0 chu ky 14:
Trong chu ky nay cac tn hieu tren bus c a ve trang thai khong tch cc e chuan b
cho chu ky oc/ghi mi. Tn hieu WR trong khi chuyen trang thai t 0 len 1 se kch hoat
trang thai ghi vao bo nh hay thiet b ngoai vi.

Chuc nong coc chon cuo v| xu |y 8086:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . 3
8us du ||eu AD0 AD5: 16 chan nay la bus d lieu 2 chieu trong khoang thi gian
cac chu ky t T2 en T4. Trong khoang thi gian T1, 16 chan nay co chc nang tai a ch 16 bit
cua bo nh hoac cua IO.
8us d|o ch| AD0 AD5 vo A6/53 A/56: 20 chan nay tng ng vi bus a ch
20 bit cho phep truy xuat 1048576 o nh. Cac ng a ch la cac ngo ra ch xuat hien trong
khoang thi gian T1.
Chon chc phep chct d|o ch| ALE (Address Lotch Enob|e|: tn hieu ra chan ALE
c dung e tach a ch va d lieu cac ng AD0 AD15, tach a ch va trang thai cac
ng A16/S3 A19/S6 va tach 7 / S BHE . Hnh 3-8 co trnh bay dang song cua tn hieu xung
ALE cho cac chu ky oc va ghi bo nh. Moi chu ky oc hay ghi luo n bat au bang 1 xung nhp
ALE xuat hien trong khoang thi cua chu ky T1. a ch 20 bit phai on nh khi co xung ALE
chuyen trang thai t mc 1 xuong mc 0 gan cuoi chu ky T1 (xem nh xung ALE tch cc canh
xuong). Xung ALE c dung e ieu khien mach chot e chot lai a ch.
1|n h|eu d|eu kh|en bc nhc/ ngco| v| IO M / (memcry/|0|: tn hieu ra nay co chc
nang bao cho biet vi x ly ang truy xuat bo nh th chan nay 1 / IO M va khi vi x ly truy
xuat IO th 0 / IO M .
1|n h|eu d|eu kh|en dcc bc nhc/ ngco| v| RD(keod|: bnh thng chan nay mc
logic 1, khi vi x ly oc d lieu t bo nh hoac t thiet b ngoai vi IO th vi x ly se ieu khien
chan RD xuong mc logic 0 e ieu khien bo nh hoac IO xuat d lieu, sau o vi x ly cho no
tr lai mc 1 e ket thuc qua trnh oc d lieu. Trong gian o thi gian ta thay chan RD xuong
mc 0 thi iem cuoi chu ky T2 va tr lai mc 1 chu ky T4 .
1|n h|eu d|eu kh|en gh| bc nhc/ ngco| v| WR(Wr|te|: bnh thng chan nay mc
logic 1, khi vi x ly muon ghi d lieu vao bo nh hoac vao thiet b ngoai vi IO th vi x ly se
ieu khie n chan WR xuong mc logic 0 e ieu khien bo nh hoac IO nhan d lieu, sau o vi
x ly cho no tr lai mc 1 e ket thuc qua trnh ghi d lieu. Trong gian o thi gian ta thay chan
WR xuong mc 0 thi iem cuoi chu ky T2 va tr lai mc 1 chu ky T4 .
Cac chan ieu khien RD va WR thng ket hp vi chan IO M / qua cac cong logic e
tao ra cac ng ieu khien ghi oc bo nh va ghi oc IO mot cach oc lap nh hnh 3-9.

H|nh 3-. 1oc coc t|n h|eu gh| dcc bc nhc vo |0.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
3- . | .
1|n h|eu xung nh|p c|cck: tat ca cac hoat ong trong vi x ly c ong bo vi xung
clock c a en chan CLK. Bang sau cho biet tan so hoat o ng toi a la 10MHz oi vi vi
x ly 8086-1.
V| xu |y 1o n sc (MHz| Dcng d|e n
|
CC
(mox|(mA|
Ccng suot t|eu to n (W|
8086 5 340 ,7
8086 - 2 8 350 ,75
8086 - 2 0 360 ,8
8088 5 340 ,7
8088 2 8 350 ,75
P8088 5 250 ,25
8ong 3-4. ot t|nh tcc dc vo ccng suot t|e u ton.
1|n h|eu BHE (bus high enable): tn hieu nay c vi x ly xuat ra trong khoang thi
gian cua chu ky T1. Khi BHE mc thap se xac nh bus AD0 AD15 lien quan e n chuyen
d lieu co the xay ra oi vi viec truy xuat bo nh hoac IO hoac truy xuat mot byte d lieu
a ch le. Tn hieu BHE va ng a ch A0 thng c dung e chon bank bo nh chan hay
le hoac cac IO chan hay le c liet ke bang sau:
BHE
A0 1ro ng tho|
0 0 1ruy xuot tu du ||eu 6 b|t
0 1ruy xuot byte du ||eu |e (D8 D5|
0 1ruy xuot byte du ||eu chon (D0 D7|
Khcng to c dcng
8ong 3-5. Mo hco truy xuot bc nhc cuo v| xu |y 8086.
1|n h|eu truyen/nho n du ||eu R DT / (data transmit/receive): tn hieu nay dung e
ieu khie n hng cua vao / ra cua d lieu qua cac bo em vao bus d lieu he thong cua vi x
ly. Khi tn hieu nay mc thap th vi x ly ang tien hanh oc d lieu khi o tn hieu nay se
ieu khie n cac mach em m co ng em d lieu t bo nh hay IO qua bo em roi a en vi x
ly, khi tn hieu nay mc cao th vi x ly ang tien hanh ghi d lieu khi o tn hieu nay se
ieu khien cac mach em m cong em d lieu t vi x ly qua bo em roi a en bo nh hay
IO.
1|n h|eu chc phep gh| du ||eu DEN (data enable): tn hieu nay cung vi tn hieu
R DT / e cho phep cac bo em 2 chieu c noi en bus d lieu cua he thong e ngan chan
viec tranh chap bus (hai mach cung ieu khie n mot ng bus) bang cach cam cac bo em d
lieu cho en chu ky T2 khi o bus a ch/d lieu khong con tai a ch ma bat au tai d lieu.
1|n h|eu chcn che dc tc| th|eu/tc| do MX MN / (minimum/maximum mode): Chc
nang cua cac chan t 24 en 32 thay oi phu thuoc vao mc logic a en chan nay. Neu
0 / MX MN th vi x ly 8086 hoat ong mode toi a, ngc lai th hoat ong mode toi
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . 3
thieu. Hnh 3-9 minh hoa vi x ly hoat ong minimum mode va hnh 3-10 minh hoa vi x ly
hoat ong maximum mode:

H|nh 3-0. V| xu |y hcot dcng c m|n|mum mcde.

H|nh 3-. V| xu |y hcot dcng c mox|mum mcde.
1|n h|eu reset : khi ngo vao nay co xung mc cao se reset vi x ly dan en ket thuc
hoat ong hien tai va bat au thc hien chng trnh tai a ch bat au FFFF:0000H nh a
trnh bay. Trang thai reset c tac ong khi mi bat au cap ien cho he thong hoac khi co s
co hoat ong cua he thong.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
3 . | .
1|n h|eu test : ngo vao nay c dung cu ng vi lenh wait: neu ngo vao test mc cao
khi gap lenh wait trong chng trnh ang x ly th vi x ly se ngng chng trnh ang thc
hien va chuyen sang che o ngh (idle mode) va khi ngo vao test tr lai m c thap th vi x ly se
tiep tuc thc hien lenh nam ngay sau lenh wait. Thng th ngo vao nay c ieu khien bi IC
toan hoc 8087.
1|n h|eu keody: ngo vao nay c vi x ly lay mau canh len cua xung nhp T2: neu
tn hieu nay c phat hien ang mc cao th vi x ly se ket thuc qua trnh oc hoac ghi ung
4 chu ky xung nhp t T1 T4, neu tn hieu nay c phat hien ang mc thap th vi x ly se
them vao cac chu ky i (wait) ngay sau chu ky T3 (hay xem gian o tn hieu) cho en khi vi x
ly phat hien tn hieu ready tr lai mc cao th thc hien tiep chu ky con lai T4 va cham dt qua
trnh x ly.
Coc t|n h|eu ngot |nterrupt (|N1k, NM| vo INTA|: cac tn hieu ngo vao ngat INTR
va NMI la cac yeu cau ngat c tac ong bang phan c ng (tn hieu ie n). Tn hieu ngat NMI
tch cc khi co canh len cua tn hieu, INTR tch cc mc cao. Ngat INTR co the ieu khien cho
phep hoac khong cho phep bang phan mem, ngat NMI la ngat khong the che c nen luon
luon c phuc vu khi tac ong. Ngat NMI c dung x ly cac s kien nh ho ng nguon hoac
cac loi bo nh.
Khi NMI tac ong th vi x ly t ong chuyen ieu khien en a ch c lu san cac o
nh co a ch 00008 0000BH.
Khi INTR tac ong th chu ky ghi nhan ngat c thc hien. Ne u c phe p th vi x ly se
tra li bang cach tac ong en ngo ra INTA bao cho thiet b bao ngat biet vi x ly a san sang
ap ng, thiet b yeu cau ngat se gi a ch 8 bit vao bus d lieu thap roi chuyen en a ch
c cha cac v tr kieu 4 en kieu 4 + 3.
Coc t|n h|eu H0LD vo HLDA: hold la tn hie u ngo vao tac ong mc cao lam cho vi
x ly treo tat ca cac ng bus trang thai tong tr cao hay co the xem nh tat ca h mach e
cho phep vi x ly khac truy cap e n bo nh hay cac thiet b ngoai vi IO. Qua trnh nay c
xem nh la truy xuat bo nh trc tiep DMA (Direct Memory Access), tn hieu HLDA bao cho
thiet b yeu cau vi x ly ngng biet vi x ly a vao trang thai ngng e thiet b bat au c
phep truy xuat bo nh hoac IO. Sau khi truy xuat xong thiet b o phai lam chan Hold xuo ng
mc 0 e vi x ly thc hien tiep.
Ngucn Vcc vo moss CND: vi x ly 8086 s dung nguon +5V va co 2 chan GND.
c. Tao xung clock va mach reset vi x ly 8086:
Nh ta a biet vi x ly cung la 1 IC so nen can phai co xung clock e ieu khien tat ca
cac he thong lam viec.
Vi x ly 8086 can xung clock co thi gian len nhanh va thi gian xuong nhanh nho hn
10ns, cac mc logic 0 va 1 nam trong khoang t -0.5V 0.6V va t 3.9V 5.0V va co he so
cong tac la 33%.
Tn hieu reset cua vi x ly phai c ong bo vi xung nhp he thong va ton tai t nhat
trong 4 chu ky trang thai T.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . 37
Do cac yeu cau nghiem ngat cua xung clock nen Intel a che tao luon mach tao xung nhp
dung chip 8284A. Ngoai chc nang thc hien cac yeu cau cua xung clock th vi mach nay con
co chc nang ong bo cac yeu cau i t cac bo nh co toc o cham.
Tat ca cac hoat ong cua vi x ly 8086 eu tuan t va ong bo vi tn hieu xung nhp cua
he thong. Trong khoang thi gian T1 cua clock th vi x ly xuat a ch cua bo nh hoac cua IO,
trong khoang thi gian T2 th vi x ly xuat cac tn hieu ieu khie n e tac ong en bo nh hoac
IO, trong khoang thi gian T3 th vi x ly ch bo nh hoac IO ap ng xuat hoac nhan d lieu
va khoang thi gian T4 th vi x ly ket thuc hoat ong.
Neu khong co tn hieu xung clock e ong bo cac hoat ong th he thong se chay sai. Cac
thiet b bo nh va IO can co thi gian thiet lap va thi gian gi c cung cap bi chu ky bus c
ban gom 4 xung clock cua vi x ly.
(Thi gian thiet lap va thi gian gi cua bo nh chnh la khoang thi gian bo nh t khi
nhan a ch cho en khi giai ma tm ung o nh va thc hien qua trnh xuat hoac ghi d lieu).
Xung clock ket hp vi cac cong logic e lam ti cac bo nh RAM khoi b mat d lieu.
Mach tao tn hieu reset nh hnh 3-12:

H|nh 3-2. Moch toc t|n h|eu reset.
Mach tao tn hieu reset cho phep reset bang 2 cach: reset khi bat au cap ien cho he
thong va reset khi nhan nut reset.
Khi cap ien th tu ien c nap ien t 0v en 5v. Vi thi hang thch hp ngo vao
RES c duy tr mc thap u thi gian can thiet e am bao reset vi x ly.
Khi ta nhan nut reset lam cho ngo vao RES c duy tr mc thap sau mot khoang thi
gian lam tu xa het ien va khi ta buong tay ra th tu c nap ien tr lai va mach reset hoat
ong giong nh khi t ong reset khi cap ien.
Thi hang RC nen cho n e c thi gian tch cc reset toi thieu la 50s. Diode co trong
mach co chc nang xa ien cho tu ien tr lai nguon cung cap khi tat nguon cung cap cho he
thong.
||. CAC PHU0NC PHAP |NH |A CH|:
Cac lenh cua vi x ly co chc nang x ly d lieu va d lieu th c cat trong bo nh
hoac thiet b IO hoac trong cac thanh ghi cua vi x ly. Cach nh a ch la cung cap a ch
cua d lieu cho vi x ly biet e tien hanh lay d lieu e thc thi lenh.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
3` . | .
Vi x ly 8086 co 9 cach nh a ch gom:
nh a ch tc thi.
nh a ch thanh ghi.
nh a ch trc tiep.
nh a ch gian tiep.
nh a ch ch so.
nh a ch co nen.
nh a ch co nen va co ch so.
nh a ch co nen va co ch so ket hp vi o di.
nh a ch kieu chuoi.
Trnh bien dch assembler se xac nh a ch nao ang c s dung trong le nh thong qua
cu phap cua vung toan hang.
a. Cach nh a ch dung thanh ghi:
Vi cach nh a ch tc thi th tac to can truy xuat thng la d lieu lu trong thanh ghi
cua vi x ly.
V du lenh M0V AX,8X


H|nh 3-3o. 1rucc kh| thuc h|e n |e nh M0V AX, 8X.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . 33

H|nh 3-3b. 5ou kh| thuc h|e n |e nh M0V AX, 8X.
Lenh nay se thc hien viec copy d lieu t lu trong thanh ghi BX (tac to nguon source
operand) sang thanh ghi DX (tac to nhan destination operand).
Hnh 3-13a va 3-13b se minh hoa qua trnh vi x ly trc khi thc hien le nh va sau khi
thc hien lenh.
Noi dung cua thanh ghi AX trc khi thc hie n la XXXX, noi dung thanh ghi BX bang
ABCD va sau khi thc hien th noi dung thanh ghi AX bang ABCD.
Lenh 2 byte nen sau khi thc hien xong con tro lenh IP tang len 2 e tro en le nh ke .
b. Cach nh a ch tc thi:
Cac lenh dung a ch tc thi xem d lieu la mot phan cua lenh. Trong cach nh a ch
tc thi d lieu c at trong lenh.
V du: M0V AL,5H
Lenh co chc nang nap d lieu 15H vao thanh ghi AL va qua trnh c thc hien nh
hnh 3-14a va 3-14b.
Cach nh a ch tc thi dung e nap 1 d lieu vao 1 thanh ghi hoac nap d lieu vao o
nh. Cac lenh nay khong the ap dung cho cac thanh ghi oan.
Cac hang so toan hang co chieu dai la 1 byte hoac 2 byte.
Trc khi thc hien lenh th noi dung thanh ghi AX la xxxx, sau khi thc hien lenh th noi
dung thanh ghi AX la xx15.
Lenh 2 byte nen sau khi thc hien th noi dung thanh ghi IP tang len 2 e chuan b thc
hien lenh ke .

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
' . | .

H|nh 3-4o. 1rucc kh| thuc h|e n |e nh M0V AL,5H.

H|nh 3-4b. 5ou kh| thuc h|e n |e nh M0V AL,5H.
c. Cach nh a ch trc tiep:
Cach nh a ch gian tiep khac vi cach nh a ch trc tiep la cac o nh i sau ma
lenh la a ch tng oi effective memory address (EA) thay v d lieu. EA la a ch offset 16
bit se ket hp vi thanh ghi DS e tao ra a ch that cua o nh can truy xuat d lieu.
V du lenh M0V CX, 8E1A
Lenh co chc nang copy d lieu trong o nh co a ch offset c k hieu la BETA trong
data segment hien tai vao thanh ghi CX va qua trnh c thc hien nh hnh 3-15a va 3-15b.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . ''
Trong lenh nay th a ch offset bang 1234H nam sau 2 byte co ma lenh B0 va 15H. a
ch offset nay c cong them a ch cua thanh ghi DS hien hanh sau khi dch sang phai 4 bit
nh sau:
PA = 02000H + 1234H = 03234H
Chnh la a ch cua o nh can truy xuat d lieu.

H|nh 3-5o. 1rucc kh| thuc h|e n |e nh M0V CX,8E1A.

H|nh 3-5b. 5ou kh| thuc h|e n |e nh M0V CX,8E1A.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
' . | .
Sau khi thc hien lenh th thanh ghi IP tang len 4 n v v ma cua lenh nay bang 4 byte,
d lieu trong 2 o nh lien tiep co a ch 03234H va 03235H c copy vao thanh ghi CX theo
th t byte thap trc byte cao sau.
Ket qua thanh ghi CX = BEEDH.
d. Cac cach nh a ch gia n tiep:
Cach nh a ch trc tiep th s dung rat tien li cho cac trng hp truy cap bo nh
khong thng xuyen vung d lieu truy cap ch co 1 hoac 2 byte hoac word. Trong trng hp
vung d lieu nhieu va lien tuc th cach dung a ch trc tiep la khong hieu qua v du copy d
lieu t vung nh nay sang vung nh khac vi dung l ng d lieu la vai tram byte.
Cach thc hien tot nhat la kieu nh a ch gian tiep dung thanh ghi, trong o a ch cua
nguon d lieu do 1 thanh ghi quan ly va a ch ch cua nguon d lieu do 1 thanh ghi khac quan
ly va d nhien la phai ket hp vi thanh ghi oan. Ngoai ra o di bu 2 co the c cong vao
thanh ghi pointer va thanh ghi index e di i so vi v tr c ch en. Bang sau trnh bay
nhieu ket h p co the s dung:
Coch d|nh d|o ch| |o ch| h|eu dung
c dc| 1honh gh| nen 1honh gh| ch| sc
C|o n t|ep thonh gh| Khcng cc +
Khcng cc +
8X hcoc 8P +
Khcng cc +
Khcng cc 5| hcoc D|
Cc ch| sc -28 de n 27 + Khcng cc + 5| hcoc D|
Cc nen -28 de n 27 + 8X hcoc 8P + Khcng cc
Cc nen vo cc ch| sc Khcng cc + 8X hcoc 8P + 5| hcoc D|
Cc nen, cc ch| sc vc| dc dc| -28 de n 27 + 8X hcoc 8P + 5| hcoc D|
8ong 3-6. 1|nh tcon d|o ch| h|e u dung chc coc coch d|nh d|o ch| g|o n t|ep.
Tong quat th mot o di co the c cong vao thanh ghi nen va cong vi thanh ghi index
e tao ra a ch that hay a ch hieu dung EA (effective address).
Trong lap trnh ta co the s dung cach nh a ch gian tiep di dang nh sau:
M0V AX,1A8LE[5|]
Trong o SI la nhan cho trc cua vung d lieu lu trong bo nh.
|nh d|o ch| g|on t|ep thonh gh|
Cach nh a ch dung thanh ghi giong nh cach nh a ch trc tiep tuy nhien a ch
offset c lu trong thanh ghi.
a ch offset co the lu tr cac thanh ghi BP, BX, Di hoac SI. Cac dau ngoac vuong [ ]
dung e ch cach nh a ch gian tiep.
V du lenh M0V AX, [5|]
Lenh co chc nang copy d lieu trong o nh co a ch offset lu trong thanh ghi SI
trong data segment hien tai vao thanh ghi AX va qua trnh c thc hien nh hnh 3-16a va 3-
16b.
Trong lenh nay th a ch offset lu trong SI bang 1234H nam trong vi x ly . a ch
offset nay c cong them a ch cua thanh ghi DS hien hanh sau khi dch sang phai 4 bit nh
sau:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . '
PA = 02000H + 1234H = 03234H
Chnh la a ch cua o nh can truy xuat d lieu.

H|nh 3-6o. 1rucc kh| thuc h|e n |e nh M0V AX,[5|].

H|nh 3-6b. 5ou kh| thuc h|e n |e nh M0V AX,[5|].
Sau khi thc hien lenh th thanh ghi IP tang len 2 n v v ma cua lenh nay bang 2 byte,
d lieu trong 2 o nh lien tiep co a ch 03234H va 03235H c copy vao thanh ghi AX theo
th t byte thap trc byte cao sau.
Thanh ghi DS la thanh ghi a ch oan mac nhien. Trong mot so trng hp khong ro
rang th trnh bien dch can them ch dan BYTE PTR hoac WORD PTR e ch kch thc cua
a ch d lieu bang con tro bo nh.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
'- . | .
V du: lenh MOV [DI],10H la lenh khong ro rang va co the hieu la cat d lieu 10H vao o
nh byte hay word. e tranh hieu nham ta phai them ch dan BYTE PTR hay WORD PTR nh
sau:
MOV BYTE PTR [DI], 10H
MOV WORD PTR [DI], 10H
Cach nh a ch gian tiep thanh ghi thng c dung e truy xuat bang d lieu trong bo
nh.
|nh d|o ch| nen (bose oddress|ng|:
Trong cach nh a ch nay th a ch that cua o nh cha toan hang c tnh toan bang
cach cong o di gian tiep hoac trc tiep vi noi dung cua thanh ghi BX hoac thanh ghi BP va
cong vi noi dung thanh ghi oan DS hoac SS theo th t.
V du lenh M0V [8X] + 8E1A, AL
Lenh co chc nang copy d lieu trong thanh ghi AL sang o nh co a ch offset bang gia
tr lu trong thanh ghi BX vi hang so BETA trong data segment hien tai va qua trnh c
thc hien nh hnh 3-17a va 3-17b.
Trong lenh nay th gia tr lu trong thanh ghi BX bang 1000H nam trong vi x ly, gia tr
BETA bang 1234H nen a ch offset bang 2234H. a ch offset nay c cong them a ch
cua thanh ghi DS hie n hanh sau khi dch sang phai 4 bit nh sau:
PA = 02000H + 2234H = 04234H
Chnh la a ch cua o nh can truy xuat d lieu.

H|nh 3-7o. 1rucc kh| thuc h|e n |e nh M0V [8X] +8E1A,AL.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . '

H|nh 3-7b. 5ou kh| thuc h|en |enh M0V [8X] +8E1A,AL.
Sau khi thc hien lenh th thanh ghi IP tang len 4 n v v ma cua lenh nay bang 4 byte,
d lieu trong o nh co a ch 04234H mang gia tr bang EDH.
o di 8 bit hay 16 bit phai xuat hien trong vung toan hang va c xem la so nh phan
co dau. Vi so nh phan 8 bit th o di phai nam trong tam t -128 +127, vi so nh phan 16
bit th o di phai nam trong tam t -32768 +32767.
|nh d|o ch| ch| sc (|ndex oddress|ng|:
Giong nh kie u nh a ch nen nhng thanh ghi s dung la thanh ghi SI hoac DI. Toan
hang co a ch la tong cua o di cua so nh phan co dau 8 bit hay 16 bit vi thanh ghi SI hoac
DI va ket h p vi thanh ghi oan (mac nhien la DS).
V du lenh M0V AL, [5|] + AkkA
Lenh co chc nang copy d lieu trong o nh co a ch offset bang gia tr lu trong thanh
ghi SI vi hang so ARRAY trong data segment hien tai sang thanh ghi AL va qua trnh c
thc hien nh hnh 3-18a va 3-18b.
Trong lenh nay th gia tr lu trong thanh ghi SI bang 2000H nam trong vi x ly, gia tr
ARRAY bang 1234H ne n a ch offset bang 3234H. a ch offset nay c cong them a ch
cua thanh ghi DS hie n hanh sau khi dch sang phai 4 bit nh sau:
PA = 02000H + 3234H = 05234H
Chnh la a ch cua o nh can truy xuat d lieu.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
' . | .

H|nh 3-8o. 1rucc kh| thuc h|e n |e nh M0V AL,[5|] +AkkA.

H|nh 3-8b. 5ou kh| thuc h|e n |e nh M0V AL,[5|] +AkkA.
Sau khi thc hien lenh th thanh ghi IP tang len 4 n v v ma cua lenh nay bang 4 byte,
d lieu trong o nh co a ch 04234H mang gia tr bang EDH c copy sang thanh ghi AL.
Cach nh a ch nen va cach nh a ch ch so con c goi la cach nh a ch tng
oi dung thanh ghi.
|nh d|o ch| nen ch| sc vc| dc dc| :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . '7
Cac thanh ghi nen va thanh ghi ch so c cong vao e tao a ch offset. Thanh ghi nen
BX hay BP c cong vi thanh ghi ch so SI hoac DI.
V du lenh M0V AH, [8X][5|] + 8E1A
Lenh co chc nang copy d lieu trong o nh co a ch offset bang gia tr lu trong thanh
ghi BX cong vi gia tr lu trong thanh ghi BX vi hang so ARRAY trong data segment hien tai
sang thanh ghi AH va qua trnh c thc hien nh hnh 3-19a va 3-19b.
Trong lenh nay th gia tr lu trong thanh ghi BX bang 12000H, cong vi gia tr lu trong
thanh ghi SI bang 2000H nam trong vi x ly, gia tr ARRAY bang 1234H nen a ch offset bang
4234H. a ch offset nay c cong them a ch cua thanh ghi DS hie n hanh sau khi dch sang
phai 4 bit nh sau:
PA = 02000H + 4234H = 065234H
Chnh la a ch cua o nh can truy xuat d lieu.

H|nh 3-o. 1rucc kh| thuc h|e n |e nh M0V AX,[8X] [5|] +8E1A.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
'` . | .

H|nh 3-b. 5ou kh| thuc h|e n |e nh M0V AX,[8X] [5|] +8E1A.
Sau khi thc hien lenh th thanh ghi IP tang len 4 n v v ma cua lenh nay bang 4 byte,
d lieu trong o nh co a ch 04234H mang gia tr bang BEH c copy sang thanh ghi AH.
e. Cach nh a ch chuoi (string):
Chuoi la 1 day lien tuc cac byte hay cac word c cat trong bo nh va c lu tr
dang ASCII. Vi x ly 8086/8088 co nhieu lenh c thiet ke rie ng e x ly cac chuoi ky t.
Cac lenh nay co cach nh a ch ac biet va s dung DS:SI e quan ly a ch cua chuoi
k t nguon va ES:DI e quan ly a ch cua vung nh ma chuoi chuyen e n. Lenh MOVSB la
lenh dung cach nh a ch chuoi co chc nang chuye n d lieu nguo n en v tr ch. Gia tr cua
SI va DI se t ong tang len hoac giam phu thuoc vao gia tr cua DF.
Cac lenh vi chuoi khong c s dung bat ky cach nh a ch nao khac, cac chuoi co
the dai en 64kbyte.
V du: cho DS = 1000H ES = 2000H, SI = 10H, DI = 20H
Lenh: MOVSB ;copy noi dung o nh [10010H] en [20020H]
f. Cach nh a ch co ng (port addressing mode):
Vi x ly 8086/8088 co vung nh va vung IO oc lap vi nhau. Vung IO co the giao tiep
len en 65536 IO. Cac cong IO co the c truy xuat vi a ch trc tiep dang byte th ch
truy xuat c 256 cong IO:
V du:
Lenh: IN AL,40H ;copy noi dung co ng IO co a ch 40H vao AL
OUT 40H,AL ;copy noi dung cua AL sang cong IO co a ch 80H
Cong IO co the truy cap bang thanh ghi khi o co the truy cap en 65536 cong IO:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . '3
V du:
Lenh: IN AL,DX ;copy noi dung co ng IO co a ch lu trong DX vao AL
OUT DX,AL ;copy noi dung cua AL sang cong IO co a ch trong DX
g. Cach nh a ch ngan xep:
Cach nay s dung cho cac lenh goi chng trnh con va lenh ket thuc chng trnh con
CALL/RET va lenh PUSH /POP. Ngan xep cua vi x ly 8086/8088 c to chc theo cau truc
vao sau ra trc LIFO.
V du:
Lenh: PUSH AX ;cat noi dung AX vao ngan xep tai [SS:SP-1] va [SS:SP-2]
Gia s cho AX = 1234H th no cat vao ngan xep c minh hoa nh hnh 3-20:

1rucc kh| PU5H 5ou kh| PU5H
H|nh 3-20. Hcot dcng cuo ngon xep vc| |e nh PU5H.
oi vi lenh goi chng trnh con CALL th a ch cua lenh quay ve trong thanh ghi IP
c t ong cat vao ngan xep. Khi ket thuc chng trnh con bang lenh RET th a ch a cat
trong ngan xep c tra lai cho thanh ghi IP. Neu la lenh goi xa (far call) th 2 word c cat
vao ngan xep (offset c cat trc ) va khi lay lai cung lay 2 word va ma ket thuc lenh bang
lenh RET khac.
Chu y: lenh PUSH va POP luon lam viec vi toan hang la word.
h. Cac qui tac ket hp cac thanh ghi oan va offset:
Vi x ly co cac qui tac ket hp thanh ghi oan va thanh ghi offset c s dung cho 1 so
cach nh a ch nhat nh. Tuy nhien ta co the viet chong len bang cach s dung tiep au ng
viet chong oan, v du MOV CL,DS:[BP]
1honh gh| 0ffset 1honh gh| dcon moc nh|e n 1|ep dou ngu v|et chcng
|P C5 Khcng cc
5P 55 Khcng cc
8P 55 D5, E5 hcoc C5
5|, D| D5 E5, 55 hcoc C5
D| chc coc |e nh chuc| k| tu E5 E5, 55 hcoc C5
8ong 3-7. Qu| tot ket hcp coc thonh gh| dcon vo thonh gh| cffset.
V du:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
'' . | .
Lenh: MOV AX,[DI] ;AX DS: [DI]
MOV AX,ES:[DI] ;AX ES: [DI]

|||. 1AP LENH:
' \~ -|| . | . ``/```
Vi x ly 8086/8088 co tap lenh khoang 300 lenh, moi lenh co the dai 1 byte en 6 byte.
Hnh 3-21 la dang ma lenh cua vi x ly 8086/8088:

H|nh 3-2. Dong mo |e nh.
Giai thch ma lenh:
Vung thonh gh| kEC (reg|ster| :
kEC W= W=0
000 AX AL
00 8X CL
00 CX DL
0 DX 8L
00 5P AH
0 8P 8H
0 5| DH
D| 8H
8ong 3-8. 8ong ||e t ke g|o tr| cuo kEC.
Vung M0D :
M0D Chuc nong
00 1con hong |o bc nhc truc t|ep neu k/M =0, ngucc |o| tcon hong truc t|ep.
0 1con hong g|on t|e p, dc dc| 8 b|t
0 1con hong g|on t|e p, dc dc| 6 b|t
Ho| thonh gh| ducc su dung, vung k/M |o vung kEC
8ong 3-. 8ong ||e t ke g|o tr| cuo M0D.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . '''

Vung k/M (reg|ster/memcry| :
Neu vung MOD = 11 th vung R/M la thanh ghi nguon du ng cac ma cua vung REG, ngc
lai th:
k/M K|eu
000 D5:[8X+5|+d]
00 D5:[8X+D|+d]
00 55:[8P+5|+d]
0 55:[8P+D|+d]
00 D5:[5|+d]
0 D5:[D|+d]
0 D5:[8P+d]
D5:[8X+d]
8ong 3-0. 8ong ||et ke g|o tr| cuo k/M.
| .|~| .- :~: -|| . | . ``/```
Trong phan nay chung ta se khao sat cac lenh cua vi x ly va s dung cac toan hang hay
ch dan assembler da tren trnh bien dch hp ng cua Microsoft (MASM 6.0).
Vi x ly 8086 co 90 lenh c ban (khong ke cac lenh bien oi ve cach nh a ch), c
chia lam 6 nhom lenh nh sau: nhom lenh chuye n d lieu, nhom lenh so hoc, nhom lenh x ly
chuoi, nhom lenh chuyen quye n ieu khien va nhom lenh ieu khien vi x ly .
Nhcm |enh chuyen du ||eu: gom co 14 lenh nh sau:
MOV, PUSH, POP, XCHG, IN, OUT, XLAT, LEA,
LDS, LES, LAHF, SAHF, PUSHF, POPF.
Nhcm |enh sc hcc: gom co 20 lenh nh sau:
ADD, ADC, INC AAA, BAA, SUB, SSB, DEC, NEG, CMP,
AAS, DAS, MUL, IMUL, AAM, DIV, IDIV, AAD, CBW, CWD.
Nhcm |enh L0C|C: gom co 12 lenh nh sau:
NOT, SHL, SAL, SHR, SAR, ROL, ROR, RCL, RCR, AND, TEST, OR, XOR.
Nhcm |enh xu |y chuc|: gom co 6 lenh nh sau:
REP, MOVS, XMPS, SCAS, LODS, STOS.
Nhcm |enh chuyen d|eu kh|en: gom co 26 lenh nh sau:
CALL, JMP, RET, JE/JZ, JL/JNGE, JLE/JNG, JB/JNAE,
JBE/JNA, JP/JPE, JO, JS, JNE/JNZ, JNL/JGE, JNLE/JG,JNB/JAE,
JNBE/JA, JNP/JPO, JNO, JNS, LOOP, LOOPZ/LOOPE, CJXZ, INT, INTO, IRETR.
Nhcm |enh d|eu kh|en V| XU L : gom co 12 lenh nh sau:
CLR, CMC, STC, CLD, STD, CLI, STI, HLT, WAIT, ESC, LOCK, NOP.
Ma lenh bao gom nhng thong tin nh ma lenh, a ch, d lieu.
So lng chu ky xung clock can e thc hien lenh phu thuoc vao cach nh a ch. Vi he
thong 8086/8088 th qua trnh tnh toan a ch that (20bit) can nhieu xung clock vi nh ng vi
x ly t 80286 tr ve sau th khong con tnh toan a ch nen khong phai mat nhieu chu ky xung
clock e tnh a ch.
V| du: Mct sc |e nh vo sc chu ky thuc h|en:
Coch d|nh d|o ch| Lenh m|nh hco 5c chu ky
C|o n t|ep thonh gh| M0V CL,[D|] 5
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
'' . | .
1ruc t|ep M0V CL,DA1A 3
Ch| sc nen M0V 8L,[8P+D|] 7
Ch| sc nen (cc dc dc|| M0V CX,[8P+D|+DA1A]
1|ep dou ngu v|et chcng dcon M0V AL,E5:DA1A EA+2
8ong 3-. 8ong ||et ke chu ky thuc h|en cuo coc |enh.
Tom tat tap lenh cua vi x ly 8086/8088:



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . ''


Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
''- . | .



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . ''



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
'' . | .




|~ -~: :~: -|| . | . ``/```
Sau khi a khao sat lenh tom tat a trnh bay tren th viec tnh toan ra ma lenh do trnh
bien dch assembler thc hien, ngi lap trnh ch can biet co bao nhieu le nh va chc nang x ly
d lieu cua tng lenh e viet trnh.
Do co rat nhieu lenh va moi lenh eu co v du minh hoa nen phan nay c trnh bay
phan phu luc.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
. | . ''7
|V. CAU H0| VA 8A| 1AP:
~| ' Ngi lap trnh vi x ly 8086/8088 bang ngo n ng Assembly can biet nhng thanh
ghi nao ben trong cua vi x ly.
~| Co bao nhieu thanh ghi ben trong cua vi x ly 8086/8088.
~| a ch cao nhat cua vi x ly 8086 co gia tr bang bao nhieu? Thap nhat bang bao
nhieu?
~| - a ch cao nhat cua vi x ly 8088 co gia tr bang bao nhieu? Thap nhat bang bao
nhieu?
~| To chc bo nh cua vi x ly 8086 theo bit, byte, word hay double word.
~| To chc bo nh cua vi x ly 8088 theo bit, byte, word hay double word.
~| 7 Cac noi dung cua cac o nh co a ch B0000H la FFH va B0001H la 00H vay th
noi dung cua word co a ch chan la B0000H la bao nhieu?
~| ` Hay trnh bay cach lu tr d lieu double word 12345678H trong o nh co a ch
bat au A001H nh the nao?
~| 3 Cac thanh ghi nao ben trong vi x ly lam thanh ghi oan bo nh.
~| ' Thanh phan nao trong khong gian a ch bo nh cua vi x ly 8086 c du ng e
lu tr lenh cua chng trnh.
~| '' Cai g se c lu tai a ch FFFF0H ?
~| ' Chc nang cua thanh ghi con tro lenh IP ?
~| ' Sau khi thc hien lenh th thanh ghi IP thay oi nh the nao ?
~| '- Hay liet ke cac thanh ghi a nang cua vi x ly 8086/8088 ?
~| ' Hay cho biet vai tro chc nang lu tr cua cac thanh ghi d lieu a nang ?
~| ' Hay cho biet loai d lieu nao c lu tr trong thanh ghi con tro (pointer) va
thanh ghi ch so (index) ?
~| '7 Hay cho biet ten cua 2 thanh ghi con tro?
~| '` Hay cho biet thanh ghi con tro ket hp vi thanh ghi oan nao ?
~| '3 Hay cho biet thanh ghi ch so ket hp vi thanh ghi oan nao ?
~| Hay cho s khac nhau cua thanh ghi con tro va thanh ghi ch so ?
~| ' Hay liet ke cac bit trong thanh ghi tra ng thai va cho biet chc nang cua chung ?
~| Hay cho biet bit nao trong thanh ghi tra ng thai ch hng a ch tang hoac giam?
~| Hay cho biet hai thanh phan nao ket hp lai tao thanh a ch that (PA) ?
~| - Neu gia tr hien tai cua thanh ghi CS va thanh ghi IP bang 0200H va 01ACH theo
th t th a ch that cua lenh ke bang bao nhie u ?
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~| :| . | . ``/```
''` . | .
~| Neu oan d lieu c nh v tai a ch A0000H en AFFFFH th gia tr phai nap
vao thanh ghi DS bang bao nhieu ?
~| T ket qua tren neu muon o nh co a ch A1234H lu d lieu th gia tr can
phai nap vao thanh ghi DI la bao nhieu ?
~| 7 Hay cho biet chc nang cua ngan xep ?
~| ` Neu gia tr hien tai cua SS va SP la 0C00H va FF00H theo th t th a ch nh
cua ngan xep bang bao nhieu ?
~| 3 Hay trnh bay qua trnh thc hien lu gia tr trong thanh ghi AX = EE11H vao ngan
xep neu gia tr hien tai cua SS va SP la 0C00H va FF00H ?
~| oi vi vi x ly 8086 th vung nh va vung IO la chung hay oc lap ?
~| ' Hay cho biet vung IO ln nhat la bao nhieu ?
~| Hay phan biet cac lenh tren thuoc kieu nh a ch nao:
(a) MOV AL,BL
(b) MOV AX,OFFH
(c) MOV [DI],AX
(d) MOV DI,[SI]
(e) MOV [BX] +XYZ,CX
(f) MOV [DI] + XZY,AH
(g) MOV [BX][DI]+XYZ,AL
~| Neu cho cac gia tr cua cac thanh ghi: CS = 0A00H, DS = 0B00H, SI = 0100H, DI
= 0200H, BX = 0300H va XYZ = 0400H. Hay tnh:
(a) a ch cua d lieu en trong lenh (c) cua bai 32.
(b) a ch cua d lieu nguon trong lenh (d) cua bai 32.
(c) a ch cua d lieu en trong lenh (e) cua bai 32.
(d) a ch cua d lieu en trong lenh (f) cua bai 32.
(e) a ch cua d lieu en trong lenh (g) cua bai 32.











Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M

Chng 4





C|0| 1H|EU V| |EU KH|E N MC55
KHA0 5A1 PHAN CUNC V| |EU KH|EN MC55
' ~| :| :-| :| . -| |-| \'
|~ -~: - |~| `3'
~| -: | : - || || | ~ | | | : || |
CAU 1kUC 80 NH0 CUA V| |EU KH|EN
' || : ||
~ :|~|| | || |~| ~ :-:
KHA0 5A1 1AP LENH CU A V| |E U KH|EN
' 3 :|-|
~ - | || ~ |
|~ -~: :~: -|| . - | |-|
KHA0 5A1 H0A1 0NC 1|MEk/C0UN1Ek CUA V| |EU KH|EN
' 3 :|-|
|~|| | || -| ~ .- | :- \- --:-
|~|| | -| |- | :- |: --:-
- ~ -| |~: | |~ :- .~ :~|
~ ||| || -
-| |-| ~ :- |~: |
7 | :~ .~ :|. |~: ~ |~ :-/||:-
` |~ -~: :- |~ | . -| |-| \
H0A1 0 NC 1kUE N DU L|E U CU A V| |E U KH|EN
' 3 :|-|
|~|| | -| |-| :|.-| | -| | :-:
~ - | :|.-| | -| | :-:
- | :~ .~ :|. |~: ~ :|~|| | :|.-| | - | | :-:
|.-| | -| | :-: :| |- :|| ||- | . - | |-|
|-: ~: : :|.-| | -| | :-:
H0A1 0 NC NCA1 CUA V| |EU KH|EN
' 3 :|-|
|~|| | -| |-| :|.-| | -| | :-:
~ - | :|.-| | -| | :-:
- | :~ .~ :|. |~: ~ :|~|| | :|.-| | - | | :-:
|.-| | -| | :-: :| |- :|| ||- | . - | |-|
|-: ~: : :|.-| | -| | :-:
8A| 1A P

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .

|. C|0| 1H|EU V| |EU KH|EN 805:
cac chng au a gii thieu ve cau truc ben trong va chc nang cua cac khoi ben trong
cung nh trnh t hoat ong x ly d lieu cua vi x ly.
Vi x ly co rat nhieu loai bat au t 4 bit cho en 32 bit, vi x ly 4 bit hien nay khong con
nhng vi x ly 8 bit van con mac du a co vi x ly 32 bit.
Ly do s ton tai cua vi x ly 8 bit la phu hp vi 1 so yeu cau ieu khien cua cac thiet b
ieu khie n trong cong nghiep. Cac vi x ly 32 bit thng s du ng cho cac may tnh v khoi lng
d lieu cua may tnh rat ln nen can cac vi x ly cang manh cang tot.
Cac he thong ieu khien trong co ng nghiep s dung cac vi x ly 8 bit e ieu khien nh he
thong ien cua xe hi, he tho ng ieu hoa, he tho ng ieu khien cac day chuyen san xuat,
Khi s dung vi x ly can phai thiet ke mot he thong gom co:
- Vi x ly.
- Co bo nh.
- Cac IC ngoai vi.
Bo nh dung e cha chng trnh cho vi x ly thc hien va cha d lieu x ly, cac IC ngoai
vi dung e xuat nhap d lieu t be n ngoai vao x ly va ie u khien tr lai. Cac khoi nay lien ket
vi nhau tao thanh mot he thong vi x ly.
Yeu cau ieu khien cang cao th he thong cang phc tap va neu yeu cau ieu khien co n
gian v du ch can o ng m 1 en led theo mot thi gian yeu cau nao o th he thong vi x ly cung
phai co ay u cac khoi tren.
e ket noi cac khoi tren tao thanh mot he thong vi x ly oi hoi ngi thiet ke phai rat hieu
biet ve tat ca cac thanh phan vi x ly, bo nh, cac thiet b ngoai vi. He thong tao ra kha phc tap,
chiem nhieu khong gian, mach in, va van e chnh la oi hoi ngi thiet ke, ngi s dung hieu
that ro ve he thong. Mot ly do chnh na la vi x ly thng x ly d lieu theo byte hoac word trong
khi o cac oi tng ieu khien trong cong nghiep th ng ieu khien theo bit.
Chnh v s phc tap nen cac nha che tao a tch hp mot t bo nh va mot so cac thiet b
ngoai vi cung vi vi x ly tao thanh mot IC goi la vi ieu khien Microcontroller.
Khi vi ieu khien ra i a mang lai s tien li la de dang s dung trong ieu khien cong
nghiep, viec s du ng vi ieu khien khong oi hoi ngi s dung phai hieu biet mot lng kien thc
qua nhieu nh ngi s dung vi x ly d nhien ngi s dung hieu biet cang nhieu th cang tot
nhng oi vi ngi bat au th viec s du ng vi x ly la ie u rat phc tap trong khi o mong muon
la s dung c ngay.
Cac phan tiep theo chung ta se khao sat vi ieu khien e thay ro s tien li trong van e
ieu khien trong cong nghiep.
Co rat nhieu hang che tao c vi ie u khien, hang san xuat noi tieng la ATMEL. Hang
Intel la nha thiet ke. Co the truy xuat e lay tai lieu cua hang bang a ch
http://www.atmel.com/
Co nhieu ho vi ieu khie n mang cac ma so khac nhau, mot trong ho noi tieng la ho MCS-51.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . ''
Trong ho MCS-51 th vi ieu khien au tien la 80C31 khong co bo nh ben trong la do
cha tch hp c.
Vi ieu khien 80C51 tch hp c 4 kbyte bo nh Prom. Ch lap trnh 1 lan kho ng the
xoa e lap trnh lai c.
Vi ieu khien 87C51 tch hp c 4 kbyte bo nh EPROM. Cho phep lap trnh nhieu lan
va xoa bang tia cc tm.
Vi ieu khien 89C51 tch hp c 4 kbyte bo nh flash rom nap va xoa bang ien mot
cach tien li va nhanh chong. Co the cho phep nap xoa hang ngan lan.
Song song vi ho MCS-51 la ho MCS-52 co 3 timer nhie u hn ho MCS-51 mot timer va dung
lng bo nh noi ln gap oi tc la 8kbyte.
Hien nay co rat nhieu vi ieu khien the he mi co nhieu ac tnh hay hn, nhieu thanh ghi
hn, dung lng bo nh ln hn.
ng dung cua vi ie u khien rat nhieu trong cac he thong ieu khien cong nghie p, cac day
chuyen san xuat, cac bo ieu khien lap trnh, may giat, may ieu hoa nhiet o, may bm xang t
ong co the noi vi x ly va vi ieu khien c ng dung trong hau het moi lnh vc t ong.
||. KHA0 5A1 PHAN CUNC V| |EU KH|EN H0 MC5-5:
en thi iem hien nay co rat nhieu loai Vi ieu khien thuoc ho MCS-51, trong tai lieu se
gii thieu ve vi ieu khien 89C51 hoac 89C52. Cac vi ieu khien the he sau se c e cap
phan sau.
Cac vi ieu khien ho MCS-51 co cac ac iem chung nh sau:
+ Co 4 Kbyte bo nh FLASH ROM be n trong dung e lu chng trnh ie u khien.
+ Co 128 Byte RAM noi.
+ 4 Port xuat / nhap (Input/Output) 8 bit.
+ Co kha nang giao tiep truyen d lieu noi tiep.
+ Co the giao tiep vi 64 Kbyte bo nh ben ngoai dung e lu chng trnh ieu khien.
+ Co the giao tiep vi 64 Kbyte bo nh ben ngoai dung e lu d lieu.
+ Co 210 bit co the truy xuat tng bit. Co cac lenh x ly bit.
Tat ca cac vi ieu khien cung ho MCS-51 hoac MCS-52 eu co cac ac tnh c ban giong
nhau nh phan mem, co n phan cng th khac nhau, cac vi ieu khien sau nay se co nhie u tnh nang
hay hn cac vi ieu khien the he trc. V du vi ieu khien 89C51 se tien cho viec s dung hn vi
ieu khien 80C51 hay 87C51. Vi ieu khien 89S51 se hay hn 89C51 v co nhieu thanh ghi hn, co
them che o nap noi tiep rat tien li. Nhng the he i sau se ke tha tat ca nhng g cua the he i
trc. Trong phan nay ch e cap en vi ieu khie n 89C51/89C52.
' ~| :| :-| :| |~ . -| |- |
S o cau truc cua vi ieu khie n c trnh bay hnh 4-1. Cac thanh ghi co trong vi ieu
khien bao gom:
- Khoi ALU i kem vi cac thanh ghi temp1, temp2 va thanh ghi trang thai PSW.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
- Bo ieu khie n logic (timing and control).
- Vung nh RAM noi va vung nh Flash Rom lu tr chng trnh.
- Mach tao dao ong noi ket hp vi tu thach anh ben ngoai e tao dao ong.
- Khoi x ly ngat, truyen d lieu, khoi timer/counter.
- Thanh ghi A, B, dptr va 4 port0, port1, port2, port3 co chot va em.
- Thanh ghi bo em chng trnh PC (program counter).
- Con tro d lieu dptr (data pointer).
- Thanh ghi con tro ngan xep SP (stack pointer).
- Thanh ghi lenh IR (instruction register).
- Ngoai ra con co 1 so cac thanh ghi ho tr e quan ly a ch bo nh ram noi ben trong
cung nh cac thanh ghi quan ly a ch truy xuat bo nh ben ngoai.

H|nh 4-. Cou truc ben trcng cuo v| d|eu kh|en.
Cac khoi ben trong cua vi ieu khie n co cac thanh phan giong nh a trnh bay phan
chng 2 nh khoi ALU, thanh ghi temp1, thanh ghi temp2, thanh ghi bo em chng trnh PC,
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
thanh con tro ngan xep, thanh ghi trang thai PSW, thanh ghi lenh IR, khoi giai ma lenh, khoi ieu
khien logic.
Tap lenh cho ngi lap trnh la ket qua cua s lien ket cac khoi ben trong cua vi ieu khie n
nhng g tap lenh cung cap la eu do phan c ng xay d ng ne n.
|~ -~: - |~ | `3'
S o chan cua vi ieu khien 89C51 c trnh bay hnh 4-2.
Vi ieu khie n 89C51 co tat ca 40 chan. Trong o co 24 chan co tac dung kep (co ngha la 1
chan co 2 chc nang), moi ng co the hoat ong nh ng xuat nhap ieu khien IO [input
output] hoac la thanh phan cua cac bus d lieu va bus a ch e tai a ch va d lieu khi giao tiep
vi bo nh ngoai.

H|nh 4-2. 5c dc chon cuo 8C5
Chc nang cac chan cua 89C51:
a. Cac Port:
Pcrt 0:
Port 0 la port co 2 chc nang vi so th t chan 32 39.
Trong cac he thong ie u khien n gian s du ng bo nh ben trong khong dung bo nh m
rong be n ngoai th port 0 c dung lam cac ng ieu khie n IO (Input- Output).
Trong cac he thong ieu khie n ln s du ng bo nh m ro ng ben ngoai th port 0 co chc
nang la bus a ch va bus d lieu AD7 - AD0. (Address: a ch, data: d lieu)
Pcrt :
Port 1 vi so th t chan 1- 8. Port1 ch co 1 chc nang dung lam cac ng ieu khien
xuat nhap IO, port 1 khong co chc nang khac.
Pcrt 2:
Port 2 la port co 2 chc nang vi so th t chan 21 28.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
Trong cac he thong ie u khien n gian s du ng bo nh ben trong khong dung bo nh m
rong be n ngoai th port 2 c dung lam cac ng ieu khie n IO (Input- Output).
Trong cac he thong ieu khie n ln s du ng bo nh m ro ng ben ngoai th port 2 co chc
nang la bus a ch cao A8 - A15.
Pcrt 3:
Port 3 la port co 2 chc nang vi so th t chan 10 -17.
Cac chan cua port nay co nhieu chc nang, cac cong dung chuyen oi co lien he vi cac
ac tnh ac biet cua 89C51 nh bang 4-1:

Bit Ten Chc nang chuyen oi
P3.0 RxD Ngo vao nhan d lieu noi tiep.
P3.1 TxD Ngo xuat d lieu noi tiep.
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
0 INT
1 INT
T0
T1
WR
RD
Ngo vao ngat cng th 0.
Ngo vao ngat cng th 1.
Ngo vao cua timer/counter th 0.
Ngo vao cua timer/counter th 1.
Tn hieu ieu khie n ghi d lieu len bo nh ngoai.
Tn hieu ieu khie n oc d lieu t bo nh ngoai.
8ong 4-. Chuc nong coc chon cuo pcrt 3.
b. Cac ngo tn hieu ieu khie n:
Ngo tn hieu PSEN (Program store enable):
PSEN la tn hie u ngo ra chan 29 co tac du ng cho phep oc bo nh chng trnh m rong
thng noi en chan OE ( output enable hoac RD) cua EPROM cho phep oc cac byte ma lenh.
Khi co giao tiep vi bo nh chng trnh ben ngoai th mi dung en PSEN , neu khong
co giao tiep th chan PSEN bo trong. PSEN
( PSEN mc thap trong thi gian vi ieu khien 89C51 lay lenh. Cac ma lenh cua chng
trnh oc t EPROM qua bus d lieu va c chot vao thanh ghi lenh be n trong 89C51 e giai ma
lenh. Khi 89C51 thi hanh chng trnh trong EPROM noi th PSEN mc logic 1).
Ngo tn hieu ieu khie n ALE (Address Latch Enable ) :
Khi vi ieu khie n 89C51 truy xuat bo nh ben ngoai, port 0 co chc nang la bus tai a ch
va bus d lieu [AD7 AD0] do o phai tach cac ng d lieu va a ch. Tn hieu ra ALE chan
th 30 dung lam tn hieu ieu khien e giai a hp cac ng a ch va d lieu khi ket noi chung
vi IC chot. Xem hnh 4-3.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '

H|nh 4-3. Ket nc| v| d|eu kh|en vc| |C chct, bc nhc EPk0M ngco|, moch reset, tu thoch onh.
Tn hieu ra chan ALE la mot xung trong khoang thi gian port 0 ong vai tro la a ch thap
nen viec chot a ch c thc hien 1 cach hoan toan t o ng.
Cac xung tn hieu ALE co toc o bang 1/6 lan tan so dao ong cua tu thach anh gan vao vi
ieu khien va co the dung tn hieu xung ngo ra ALE lam xung clock cung cap cho cac phan khac
cua he thong.
Trong che o lap trnh cho bo nh noi cua vi ie u khien th chan ALE c dung lam ngo
vao nhan xung lap trnh t ben ngoai e la p trnh cho bo nh flash rom trong 89C51.
hnh 4-3 ch la minh hoa ket noi vi ieu khien (89C52) vi bo nh EPROM ngoai e thay
vai tro cua tn hieu ALE, cac ng con lai cua vi ieu khien co the dung e ket noi ieu khien
cac oi tng khac.
Ngo tn hieu EA (External Access):
Tn hieu vao EA chan 31 thng noi len mc 1 hoac mc 0.
Neu noi EA len mc logic 1 (+5v) th vi ieu khien se thi hanh chng trnh t bo nh noi.
Neu noi EA vi mc logic 0 (0V) th vi ie u khien se thi hanh chng trnh t bo nh ngoai.
Ngo tn hieu RST (Reset):
Ngo vao RST chan 9 la ngo vao Reset cua 89C51. S o ket noi mach reset nh hnh 3-
3. Khi cap ien cho he thong hoac khi nhan nut reset th mach se reset vi ieu khien. Khi reset th
tn hieu reset phai mc cao t nhat la 2 chu ky may, khi o cac thanh ghi be n trong c nap
nhng gia tr thch hp e khi o ng he thong.
Trang thai cua tat ca cac thanh ghi trong 89C51 sau khi reset he thong c tom tat nh bang
4-2:

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
Thanh ghi
Noi dung
Bo em chng trnh PC
Thanh ghi tch luy A
Thanh ghi B
Thanh ghi trang thai PSW
Thanh ghi con tro SP
DPTR
Port 0 e n port 3
IP
IE
Cac thanh ghi nh thi
SCON SBUF
PCON (HMOS)
PCON (CMOS)

0000H
00H
00H
00H
07H
0000H
FFH (1111 1111)
XXX0 0000 B
0X0X 0000 B
00H
00H
00H
0XXX XXXXH
0XXX 0000 B
8ong 4-2. Coc thonh gh| sou kh| v| d|eu kh|e n b| reset.
Thanh ghi quan trong nhat la thanh ghi bo em chng trnh PC = 0000H. Sau khi reset vi
ieu khien luon bat au thc hien chng trnh tai a ch 0000H cua bo nh chng trnh nen cac
chng trnh viet cho vi ieu khie n luon bat au viet tai a ch 0000H.
Noi dung cua RAM tren chip kho ng b thay oi bi tac ong cua ngo vao reset [co ngha
la vi ieu khien ang s dung cac thanh ghi e lu tr d lieu nhng neu vi ieu khien b reset th
d lieu trong cac thanh ghi van khong oi].
Cac ngo vao bo dao ong Xtal1, Xtal2:
Bo dao ong c tch hp ben trong 89C51, khi s dung 89C51 ngi thiet ke ch can ket
noi them tu thach anh va cac tu nh trong hnh 3-3. Tan so tu thach anh thng s dung cho
89C51 la 12Mhz 24Mhz.
Chan 40 (Vcc) c noi len nguon 5V, cha n 20 GND noi mass.
~| -: | : - || || | ~ | | | : || |
a. Mach ong ho so hien th gi phut giay tre n led 7 oan:
Trong s o hnh 4-4 s dung 6 led 7 oan loai anode chung e hien th gi, phut va giay s
dung phng phap quet, 6 transistor s dung la loai pnp thng la A564 va ien tr cc B co gia tr
khoang 10kO, ien tr han dong cho cac oan co gia tr 220O. e hieu hoat ong ieu khien
quet led 7 oan hay oc chng 8.
Hai ien tr mang 9 chan co gia tr la 4,7kO hoac 10kO, tu reset co gia tr 10F, ien tr
reset co gia tr 10kO, thach anh co gia tr thng la 12MHz.
3 nut nhan S1, S2 va S3 dung e chnh ca c thong so gi phut giay.
Mach nguon s dung IC on ap 5V.
e he tho ng hoat ong th phai co chng trnh.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7

H|nh 4-4. Moch dc ng hc sc dung |ed 7 dcon.
b. Mach nh thi hien th thi gian tre n 2 led 7 oan co 1 relay ieu khien:

H|nh 4-5. Moch d|nh thc| d|eu kh|en re|oy vo h|en th| thc| g|on tren 2 |ed.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .
c. Mach ong ho so hien th gi phut giay tre n LCD:

H|nh 4-6. Moch dc ng hc sc h|e n th| dung LCD.
d. Mach ong ho co the ba o chuo ng tiet hoc s dung real-time:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3

H|nh 4-7. Mo ch dcng hc sc h|en th| dung LCD cc them boc chucng g|c hcc.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
|||. CAU 1kUC 80 NH0 CUA V| |EU KH|EN:
' || : ||
Vi ieu khien 89C51 co bo nh noi ben trong va co them kha nang giao tiep vi bo nh ben
ngoai neu bo nh be n trong khong u kha nang lu tr chng trnh.
Bo nh noi ben trong gom co 2 loai bo nh: bo nh d lieu va bo chng trnh. Bo nh d
lieu co 256 byte, bo nh chng trnh co dung lng 4kbyte. [89C52 co 8 kbyte, 89W55 co
16kbyte].
Bo nh m rong ben ngoai cung c chia ra lam 2 loai bo nh: bo nh d lieu va bo nh
chng trnh. Kha nang giao tiep la 64kbyte cho moi loai. Hnh 4-8 minh hoa kha nang giao tiep
bo nh cua vi ieu khien 89C51.
Bo nh m rong be n ngoai va bo nh chng trnh ben trong khong co g ac biet ch co
chc nang lu tr d lieu va ma chng trnh ne n kho ng can phai khao sat.
Bo nh chng trnh ben trong cua vi ieu khien thuoc loai bo nh Flash rom cho phep xoa
bang xung ien va lap trnh lai.
Bo nh ram noi be n trong la mot bo nh ac biet ngi s dung vi ie u khien can phai nam
ro cac to chc va ca c chc nang ac biet cua bo nh nay.
S o cau truc ben trong cua bo nh nay c trnh bay nh hnh 4-9.


H|nh 4-8. 8ong tcm to t coc vung nhc 8C5.
RAM ben trong 89C51 c phan chia nh sau:
Cac bank thanh ghi co a ch t 00H e n 1FH.
RAM a ch hoa tng bit co a ch t 20H en 2FH.
RAM a dung t 30H e n 7FH.
Cac thanh ghi chc nang ac biet t 80H e n FFH.


Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . ''


7F FF
F0 F7 F6 F5 F4 F3 F2 F1 F0 B
RAM a dung
E0 E7 E6 E5 E4 E3 E2 E1 E0 ACC

D0 D7 D6 D5 D4 D3 D2 D1 D0 PSW

30 B8 - - - BC BB BA B9 B8 IP
2F 7F 7E 7D 7C 7B 7A 79 78
2E 77 76 75 74 73 72 71 70 B0 B7 B6 B5 B4 B3 B2 B1 B0 P3
2D 6F 6E 6D 6C 6B 6A 69 68
2C 67 66 65 64 63 62 61 60 A8 AF AC AB AA A9 A8 IE
2B 5F 5E 5D 5C 5B 5A 59 58
2A 57 56 55 54 53 52 51 50 A0 A7 A6 A5 A4 A3 A2 A1 A0 P2
29 4F 4E 4D 4C 4B 4A 49 48
28 47 46 45 44 43 42 41 40 99 SBUF
27 3F 3E 3D 3C 3B 3A 39 38 98 9F 9E 9D 9C 9B 9A 99 98 SCON
26 37 36 35 34 33 32 31 30
25 2F 2E 2D 2C 2B 2A 29 28 90 97 96 95 94 93 92 91 90 P1
24 27 26 25 24 23 22 21 20
23 1F 1E 1D 1C 1B 1A 19 18 8D TH1
22 17 16 15 14 13 12 11 10 8C TH0
21 0F 0E 0D 0C 0B 0A 09 08 8B TL1
20 07 06 05 04 03 02 01 00 8A TL0
1F Bank 3 89 TMOD
18 88 8F 8E 8D 8C 8B 8A 89 88 TCON
17 Bank 2 87 PCON
10
0F Bank 1 83 DPH
08 82 DPL
07 Bank thanh ghi 0 81 SP
00 (mac nh cho gan cho R0 -R7) 80 87 86 85 84 83 82 81 80 P0
RAM Cac Thanh Ghi co Chc Nang ac Biet
H|nh 4-: Cou truc bc nhc kAM ben trcng v| d|eu kh|e n.
Cac bank thanh ghi :
32 byte thap cua bo nh noi c danh cho 4 bank thanh ghi.
Bo lenh 89C51 ho tr them 8 thanh ghi co ten la R0 en R7 va theo mac nh sau khi reset
he thong th cac thanh ghi R0 e n R7 c gan cho 8 o nh co a ch t 00H e n 07H c minh
hoa bi hnh 3-10, khi o bank 0 co 2 cach truy xuat bang a ch trc tiep va bang thanh ghi R.
a ch
byte

a ch bit a ch bit
a ch
byte

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
Cac lenh dung cac thanh ghi R0 en R7 se co so lng byte ma lenh t hn va thi gian thc
hien lenh nhanh hn so vi cac lenh co chc nang tng ng neu dung kie u a ch trc tiep.
Cac d lieu c dung thng xuyen nen lu tr mot trong cac thanh ghi nay.
Do co 4 bank thanh ghi nen tai mot thi iem ch co mot bank thanh ghi c truy xuat bi
cac thanh ghi R0 en R7, e chuyen oi viec truy xuat cac bank thanh ghi ta phai thay oi cac bit
chon bank trong thanh ghi tra ng thai.


H|nh 4-0. M|nh hco coch gon bonk thonh gh| chc nhcm thonh gh| k.
Ngi lap trnh dung vung nh 4 bank thanh ghi e lu tr d lieu phuc vu cho viec x ly d
lieu khi viet chng trnh.
Chc nang chnh cua 4 bank thanh ghi nay la neu trong he thong co s du ng nhie u chng
trnh th chng trnh th nhat ban co the s dung het cac thanh ghi R0 en R7 cua bank0, khi
chuyen sang chng trnh th 2 e x ly mot cong viec g o va van s dung cac thanh ghi R0 en
R7 e lu tr cho viec s ly d lieu ma kho ng lam anh hng en cac d lieu R0 en R7 trc ay
va khong can phai thc hien cong viec cat d lieu th cach nhanh nhat la gan nhom thanh ghi R0
en R7 cho bank1 la xong. Tng t co the m them hai chng trnh na va gan cho cac bank 3
va 4.
RAM co the truy xuat t ng bit:
Vi ieu khien 89C51 co 210 o nh bit co the truy xuat tng bit, trong o co 128 bit nam cac
o nh byte co a ch t 20H e n 2FH va cac bit con lai cha trong nhom thanh ghi co chc nang
ac biet.
Cac o nh cho phep truy xuat tng bit va cac lenh x ly bit la mot the manh cua vi ie u
khien. Cac bit co the c at, xoa, AND, OR bang 1 le nh duy nhat trong khi o e x ly cac bit
th vi x ly van co the x ly c nhng phai s du ng rat nhie u lenh e at c cung mot ket qua
v vi x ly thng x ly byte.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
Cac port cung co the truy xuat c tng bit.
128 o nh bit cho phep truy xuat tng bit va cung co the truy xuat byte phu thuoc vao lenh
c dung la lenh x bit hay lenh x ly byte. Chu y a ch cua o nh byte va bit trung nhau.
Ngi lap trnh dung vung nh nay e lu tr d lieu phuc vu cho viec x ly d lieu byte
hoac bit. Cac d lieu x ly bit nen lu vao vung nh nay.
Chu y: cac o nh nao ma chia ra lam 8 va co cac con so ben trong la cac o nh va cho truy
xuat byte va ca truy xuat bit. Nhng o nh co n lai th khong the truy xuat bit.
RAM a dung :
Vung nh ram a dung gom co 80 byte co a ch t 30H en 7FH vu ng nh nay khong co
g ac biet so vi 2 vung nh tren. Vung nh bank thanh ghi 32 byte t 00H en 1FH cung co the
dung lam vung nh ram a dung mac du ca c o nh nay a co chc nang nh a trnh bay.
Moi a ch trong vung RAM a dung eu co the truy xuat t do dung kieu a ch trc tiep
hoac gian tiep.
Bo nh ngan xep cua vi ieu khie n dung bo nh Ram noi nen dung lng cua bo nh ngan
xep nho trong khi o cac bo vi x ly dung bo nh ben ngoai lam bo nh ngan xep nen dung lng
tuy y m rong.
~ :|~|| | || |~ | ~ :-:
Cac thanh ghi noi cua 89C51 c truy xuat ngam nh bi bo lenh.
Cac thanh ghi trong 89C51 c nh dang nh mot phan cua RAM tren chip v vay moi
thanh ghi se co mot a ch (ngoai tr thanh ghi bo em chng trnh va thanh ghi lu tr ma lenh
v cac thanh ghi nay a co chc nang co nh). Cung nh cac thanh ghi R0 e n R7, vi ieu khien
89C51 co 21 thanh ghi co chc nang ac biet nam vung tren cua RAM noi co a ch t 80H en
FFH.
Chu y: 128 o nh co a ch t 80H en FFH th ch co 21 thanh ghi co chc nang ac biet
c xac nh cac a ch con cac o nh con lai th cha thiet lap va trong tng lai se c cac
nha thiet ke vi ieu khien thiet lap them khi o se co cac vi ieu khien the he mi hn.
Trong phan nay ch mang tnh gii thieu cac phan tiep theo se trnh bay chi tiet hn ve cac
thanh ghi nay.
- Cac o nh co a ch 80H, 90H, A0h, B0h:
La cac Port cua 89C51 bao gom Port0 co a ch 80H, Port1 co a ch 90H, Port2 co a ch
A0H va Port3 co a ch B0H. Tat ca cac Port nay eu co the truy xuat tng bit ne n rat thuan tien
trong ieu khien IO. a ch cua cac bit c at ten vi o bat au chnh la a ch cua port tng
ng v du nh bit au tien cua port 0 la 80h cung chnh la a ch bat au cua port 0. Ngi lap
trnh kho ng can nh a ch cac bit trong cac port v phan mem lap trnh cho phep truy xuat bang
ten tng bit de nh nh sau: P0.0 chnh la bit co a ch 80h cua port0.
Ngoai tr thanh ghi A co the c truy xuat ngam, a so cac thanh ghi co chc nang ac biet
SFR co the a ch hoa tng bit hoac byte.
- O nh co a ch 81h:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
La thanh ghi con tro ngan xep SP (stack pointer) - co chc nang quan ly a ch cua bo nh
ngan xep. Bo nh ngan xep dung e lu tr tam thi cac d lieu trong qua trnh thc hien chng
trnh cua vi ieu khien.
Cac lenh lien quan en ngan xep bao gom cac lenh cat d lieu vao ngan xep (lenh push) va
lay d lieu ra khoi ngan xe p (lenh pop).
Lenh cat d lieu vao ngan xep se lam tang SP trc khi ghi d lieu vao.
Sau lenh lay ra khoi ngan xep se lam giam SP.
Bo nh ngan xep cua 89C51 nam trong RAM noi va b gii han ve cach truy xuat a ch -
ch cho phep truy xuat a ch gian tiep. Dung lng bo nh ngan xep l n nhat la 128 byte ram noi
cua 89C51.
Khi Reset 89C51 th thanh ghi SP se mang gia tr mac nh la 07H va d lieu au tien se
c cat vao o nh ngan xep co a ch 08H.
Neu phan mem ng du ng kho ng khi tao SP mot gia tr mi th bank 1 co the ca 2 va 3 se
khong dung c v vung nh nay a c dung lam ngan xep.
Ngan xep c truy xuat trc tiep bang cac lenh PUSH va POP e lu tr tam thi va lay lai
d lieu, hoac truy xuat ngam bang le nh goi chng trnh con ( ACALL, LCALL) va cac lenh tr
ve (RET, RETI) e lu tr a ch cua bo em chng trnh khi bat au thc hie n chng trnh con
va lay lai a ch khi ket thuc chng trnh con.
- O nh co a ch 82h va 83h :
La 2 thanh ghi dpl (byte thap) co a ch la 82H va dph (byte cao) co a ch 83H. Hai thanh
ghi nay co the s dung oc lap e lu tr d lieu va co the ket hp lai tao thanh 1 thanh ghi 16 bit
co ten la dptr va goi la con tro d lieu - c dung e lu a ch 16 bit khi truy xuat d lieu cua bo
nh d lieu ben ngoai. Cac v ieu khien sau nay co them mot thanh ghi dptr.
- O nh co a ch 87h: :
La thanh ghi pcon (power control) co chc nang ieu khien cong xuat khi vi ieu khien lam
viec hay che o ch. Khi vi ie u khien khong con s ly g na th ngi lap trnh co the lap trnh
cho vi ieu khien chuyen sang che o ch e giam bt co ng suat tieu thu nhat la khi nguon cung
cap cho vi ieu khien la pin.
- Cac o nh co a ch t 88h en 8dh :
La cac thanh ghi phuc vu cho 2 timer/ counter T1, T0.
Thanh ghi tcon (timer control): thanh ghi ieu khien timer / counter.
Thanh ghi tmod (timer mode): thanh ghi la chon mode hoat ong cho timer / counter.
Thanh ghi TH0 va TL0 ket h p lai tao thanh 1 thanh ghi 16 bit co chc nang lu tr xung
em cho timer/counter T0. Tng t cho 2 thanh ghi TH1 va TL1 ket h p lai e lu tr xung em
cho timer/counter T1. Kha nang lu tr so l ng xung em c la 65536 xung.
Chc nang cua cac thanh ghi nay se c trnh bay ro chng timer counter.
- Cac o nh co a ch t 98h en 99h :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
La 2 thanh ghi scon va sbuf: scon (series control): thanh ghi ieu khien truyen d lieu noi
tiep. Sbuf (series buffer ): thanh ghi em d lieu truye n noi tiep. D lieu muon truyen i th phai
lu vao thanh ghi SBUF va d lieu nhan ve noi tiep cung lu thanh ghi nay. Khi co s dung
truyen d lieu th phai s dung 2 thanh ghi nay.
Chc nang cua cac thanh ghi nay se c trnh bay ro chng truyen d lieu.
- Cac o nh co a ch t a8h en b9h :
La 2 thanh ghi IE va IP thanh ghi IE (interrupt enable): thanh ghi ieu khie n cho phep /
khong cho phep ngat. IP (interrupt priority): thanh ghi ie u khie n u tien ngat. Khi co s dung
en ngat th phai dung en 2 thanh ghi nay. Mac nhie n cac thanh ghi nay c khi tao che o
cam ngat.
Chc nang cua cac thanh ghi nay se c trnh bay ro chng ngat.
- Thanh ghi tra ng thai chng trnh (PSW: Program Status Word):
Thanh ghi trang thai chng trnh a ch D0H c tom tat nh bang 4-3:

8|1 5M80L ADDkE55 DE5Ck|P1|0N
P5W.7 C hcoc Cy D7H Cory F|og
P5W.6 AC D6H Aux|||ory Cory F|og
P5W.5 F0 D5H F|og 0 cc n gc| |o cc Zerc k| h|e u |o Z
P5W4 k5 D4H keg|ster 8onk 5e|ect : b|t |uo chcn bonk thonh gh|.
P5W.3 k50 D3H keg|ster 8onk 5e|ect 0: b|t |uo chc n bonk thonh gh|.
00 = 8onk 0, c nhc cc oddress 00H07H gon chc k0-k7
0 = 8onk , c nhc cc oddress 08H0FH gon chc k0-k7
0 = 8onk 2, c nhc cc oddress 0H7H gon chc k0-k7
= 8onk 3, c nhc cc oddress 8HFH go n chc k0-k7
P5W.2 0V D2H 0verf|cw F|og: cc tron sc nh| phon cc dou.
P5W. - DH keserved: chuo th|et ke ne n chuo su du ng ducc.
P5W.0 P D0H Even Por|ty F|og: cc chon |e.
8ong 4-3. Coc b|t trcng thonh gh| trong tho|.
Chc nang tng bit trang thai:
- C Carry CY (Carry Flag):
C nh co tac dung kep. C C c s du ng cho cac lenh toan hoc:
C = 1 neu phep toan cong co tran hoac phep tr co m n.
C = 0 neu phep toan cong khong tran va phep tr khong co mn.
- C Carry phu AC (Auxiliary Carry Flag):
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
Khi cong nh ng gia tr BCD (Binary Code Decimal), c nh phu AC c set [AC=1] ne u
ket qua 4 bit ln hn 09H, ngc lai AC= 0. C AC c dung e chnh so BCD khi thc hien lenh
cong 2 so BCD.
- C 0 (Flag 0):
C 0 (F0) con goi la c zero, c zero =1 khi ket qua x ly bang 0 va c zero = 0 khi ket qua
x ly khac 0.
- Cac bit chon bank thanh ghi truy xuat:
Hai bit RS1 va RS0 dung e thay oi cach gan 8 thanh ghi R7 R0 cho 1 trong 4 bank thanh
ghi. Hai bit nay se b xoa sau khi reset vi ieu khien va c thay oi bi chng trnh cua ngi
lap trnh.
Hai bit RS1, RS0 = 00, 01, 10, 11 se c chon Bank thanh ghi tch cc tng ng la Bank0,
Bank1, Bank2, Bank3.
k5 k50 8onk thonh gh|
ducc |uo chc n
0 0 8onk 0
0 8onk
0 8onk 2
8onk 3
8ong 4-4. Coc b|t |uo chc n bonk thonh gh|.
- C tran OV (Over Flag) :
Khi cac so co dau c cong hoac tr vi nhau, phan mem co the kiem tra bit nay e xac
nh xem ket qua co nam trong vung gia tr xac nh hay khong. Vi so nh phan 8 bit co dau th
so dng t 0 en +127, so am t -128 e n 1. Neu ket qua cong 2 so dng ln hn +127 hoac
cong 2 so am ket qua nho hn 128 th ket qua a vt ra ngoai vung gia tr cho phep th khoi
ALU trong vi ieu khie n se lam bit OV = 1.
Khi cong cac so nh phan khong dau th khong can quan tam en bit OV.
- Bit Parity (P) :
Bit P t ong c Set hay Clear moi chu ky may e lap Parity chan vi thanh ghi A. em
cac bit 1 trong thanh ghi A cong vi bit Parity luon luon la so chan. V du thanh ghi A cha nh
phan 10101101B th bit P set len mot e cho biet tong so bit 1 trong thanh ghi A va ca bit P tao
thanh so chan.
Bit Parity thng c dung ket hp vi nhng thu tuc truyen d lieu noi tiep e tao ra bit
Parity cho d lieu trc khi truyen i hoac kiem tra bit Parity sau khi nhan d lieu.
- Thanh ghi B :
Thanh ghi B a ch F0H c dung cung vi thanh ghi A e thc hien cac phep toan nhan
chia. Lenh MUL AB: se nhan nhng gia tr khong dau 8 bit vi 8 bit trong hai thanh ghi A va B, roi
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7
tra ve ket qua 16 bit trong A (byte cao) va B(byte thap). Lenh DIV AB: lay gia tr trong thanh ghi
A chia cho gia tr trong thanh ghi B, ket qua nguyen lu trong A, so d lu trong B.
Thanh ghi B co the c dung nh mot thanh ghi em trung gian nhieu chc nang.
Tom tat
Vi ieu khien co tat ca cac thanh phan cau truc ben trong giong nh vi x ly nh
khoi ALU, cac thanh ghi: thanh ghi A, thanh ghi bo nh chng trnh PC, thanh ghi
con tro ngan xe p SP,
Vi ieu khien co tch h p bo nh noi ben trong bao gom bo nh chng trnh va bo
nh ram.
Bo nh chng trnh dung e cha chng trnh ieu khien.
Bo nh Ram du ng e lu tr d lieu phuc vu cho viec x ly chng trnh.
Kch thc cua bo nh chng trnh ben trong tuy thuoc vao tng loai vi ie u khien
cu the.
Ngoai cac bo nh ben trong vi ieu khien co n co the giao tiep vi bo nh m rong
ben ngoai. Khi giao tiep vi bo nh ben ngoai th port 0 va port 2 co chc nang giao
tiep a ch va d lieu va cac ng ieu khien cua port 3, so lng ng ieu
khien IO con lai rat t. Muon co nhieu ng ieu khien IO th phai giao tiep them
IC ngoai vi.
Vi ieu khien co 32 ng IO, co timer/counter, co truyen d lieu noi tiep, co ngat
cho phep giao tiep de dang vi oi tng ieu khien.
Mot trong nhng the manh cua vi ieu khien la kha nang x ly d lieu bit rat phu
hp trong lanh vc ieu khie n.

|V. 1AP LENH V| |EU KH|EN MC55
' 3 :|-|
Vi ieu khien hay vi x ly la cac IC lap trnh, khi ban a thiet ke he thong ieu khien co s
dung vi x ly hay vi ieu khien v du nh he thong ieu khien en giao thong cho mot nga t gom
co cac en Xanh, Vang, o va cac led 7 oan e hien th thi gian th o mi ch la phan cng,
muon he thong van hanh th ban phai viet mot chng trnh ieu khien nap vao bo nh noi ben
trong vi ieu khie n hoac bo nh ben ngoai va gan vao trong he thong e he thong van hanh va d
nhien ban phai viet ung th he thong mi van hanh ung. Chng trnh goi la phan mem.
Phan mem va phan cng co quan he vi nhau, ngi lap trnh phai hie u ro hoat ong cua
phan cng e viet chng trnh. chng nay se trnh bay chi tiet ve tap lenh cua vi ieu khien
giup ban hieu ro tng lenh e ban co the lap trnh c.
Cac khai niem ve chng trnh, lenh, tap lenh va ngon ng gi nh a trnh bay chng 1
va 2, ay ch tom tat lai.
Chng trnh la mot tap hp cac lenh c to chc theo mot trnh t hp l e giai quyet
ung cac yeu cau cua ngi lap trnh.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .
Ngi lap trnh la ngi biet giai thuat e viet chng trnh va sap xep ung cac lenh theo
giai thuat. Ngi lap trnh phai biet chc nang cua tat ca cac lenh cua vi ieu khien e viet
chng trnh.
Tat ca cac lenh co the co cua mot ngon ng lap trnh con goi la tap lenh.
Ho vi ieu khien MCS-51 e u co chung 1 tap lenh, cac vi ie u khien the he sau ch phat
trien nhieu ve phan cng con le nh th t m rong.
Tap lenh ho MCS-51 co ma lenh 8 bit ne n co kha nang cung cap 2
8
= 256 lenh.
Co lenh co 1 hoac 2 byte bi d lieu hoac a ch them vao Opcode.
Trong toan bo tap lenh cua vi ie u khien co139 lenh 1 byte, 92 lenh 2 byte va 24 le nh 3 byte.
Lenh cua vi ieu khien la mot so nh phan 8 bit [co n goi la ma may]. 256 byte t 0000 0000b
en 1111 1111b tng ng vi 256 lenh khac nhau. Do ma lenh dang so nh phan qua dai va kho
nh nen cac nha lap trnh a xay dng mot ngon ng lap trnh Assembly cho de nh, ieu nay giup
cho viec lap trnh c thc hien mot cach de dang va nhanh chong cung nh oc hieu va g roi
chng trnh.
Khi viet chng trnh bang ngon ng lap trnh Assembly th vi ieu khien se khong thc hie n
c ma phai dung chng trnh bien dch Assembler e chuyen oi cac lenh viet bang Assembly
ra ma lenh nh phan tng ng roi nap vao bo nh khi o vi ieu khien mi thc hie n c
chng trnh.
Ngon ng lap trnh Assembly do con ngi tao ra, khi s du ng ngon ng Assembly e viet th
ngi lap trnh vi ieu khie n phai hoc het tat ca cac lenh va viet ung theo qui c ve cu phap,
trnh t sap xep d lieu e chng trnh bien dch co the bien dch ung.
~ -| || ~ | |~ . -| |-| \'
Phan nay a trnh bay mot cach tong quat chng 2, ay se trnh bay mot cach chi tiet
hn. Cac kieu nh a ch la mot qui c thong nhat cua tap lenh.
Cac kieu nh a ch cho phep nh ro ni lay d lieu hoac ni nhan d lieu tuy thuoc vao
cach thc s dung lenh cua ngi lap trnh.
Vi ieu khien ho MCS-51 co 8 kie u nh a ch nh sau:
\ Kieu nh a ch dung thanh ghi.
\ Kieu nh a ch trc tiep.
\ Kieu nh a ch gian tiep.
\ Kieu nh a ch tc thi.
\ Kieu nh a ch tng oi.
\ Kieu nh a ch tuyet oi.
\ Kieu nh a ch dai.
\ Kieu nh a ch nh v.
a. Kieu nh a ch dung thanh ghi (Register Addressing) :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3
Kieu nay thng c dung cho cac lenh x ly d lieu ma d lieu luo n lu trong cac thanh
ghi. oi vi vi ieu khie n th ma lenh thuoc kieu nay ch co 1 byte.
V du: Mov A,R1 ; copy noi dung thanh ghi R1 vao thanh ghi A
b. Kieu nh a ch trc tiep (Direct Addressing) :
Kieu nay thng c dung e truy xuat d lieu cua bat ky o nh nao trong 256 byte bo nh
RAM noi cua vi ieu khien 89C51.
Cac lenh thuoc kieu nay thng co ma lenh 2 byte: byte th nhat la ma lenh, byte th 2 la
a ch cua o nh:




V du: Mov A,05H ; copy noi dung o nh co a ch 05H vao thanh ghi A
c. nh a ch gian tiep (Indirect Addressing) :
Kieu nh a ch gian tiep c tng trng bi ky hieu @ va c at trc cac thanh ghi
R0, R1 hay DPTR. R0 va R1 co the hoat ong nh mot thanh ghi con tro, noi dung cua no cho biet
a ch cua mot o nh trong RAM noi ma d lieu se ghi hoac se oc. Con dptr dung e truy xuat o
nh ngoai. Cac lenh thuoc dang nay ch co 1 byte.


V du: Mov A,@R1 ; copy noi dung o nh co a ch trong thanh ghi R1 vao
;thanh ghi A
d. nh a ch tc thi (Immediate Addressing) :
Kieu nh a ch tc thi c tng trng bi ky hieu # va c at trc mot hang so.
Lenh nay thng dung e nap 1 gia tr la 1 hang so byte th 2 (hoac byte th 3) vao thanh ghi
hoac o nh.





V du: Mov a,#30H ; nap d lieu la con so 30H vao thanh ghi A
e. nh a ch tng oi :

Opcode

Opcode

Direct Addressing


Opcode

Immediate Data
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
Kieu nh a ch tng oi ch s du ng vi nh ng lenh nhay. Ni nhay en co a ch bang
a ch ang lu trong thanh ghi PC cong vi 1 gia tr 8 bit [con goi la gia tr lech tng oi:
relative offset] co gia tr t 128 en +127 nen vi ieu khien co the nhay lui [neu so cong vi so
am] va nhay ti [ neu so cong vi so dng]. Lenh nay co ma lenh 2 byte, byte th 2 chnh la gia
tr lech tng oi:




Ni nhay en th ng c xac nh bi nhan (label) va trnh bien dch se tnh toan gia tr
lech.
nh v tng oi co u iem la ma lenh co nh, nhng khuyet iem la ch nhay ngan trong
pham vi -128127 byte [256byte], neu ni nhay en xa hn th lenh nay khong ap ng c se
co loi.
V du: Sjmp X1 ;nhay en nhan co ten la X1 nam trong tam vc 256 byte
f. nh a ch tuyet oi (Absolute Addressing) :
Kieu nh a ch tuyet oi c dung vi cac lenh ACALL va AJMP. Cac lenh nay co ma
lenh 2 byte cho phep phan chia bo nh theo trang - moi trang co kch thc ung bang 2Kbyte so
vi gia tr cha trong thanh ghi PC hien hanh. 11 bit a ch A10A0 c thay the cho 11 a ch
thap trong thanh ghi PC nam trong cau truc ma lenh nh sau:




nh a ch tuyet oi co u iem la ma lenh ngan (2 byte), nhng khuyet iem la ma lenh
thay oi va gii han pham vi ni nhay e n, goi en kho ng qua 2 kbyte.
V du: Ajmp X1 ;nhay en nhan co ten la X1 nam trong tam vc 2 kbyte
g. nh a ch dai (Long Addressing) :
Kieu nh a ch dai c dung vi lenh LCALL va LJMP. Cac lenh nay co ma lenh 3 byte
trong o co 2 byte (16bit) la a ch cua ni en. Cau truc ma lenh la 3 byte nh sau:





Opcode

Relative Offset

Addr 7 Addr 0

Opcode
Addr 10 Addr 8

Opcode

Addr 15 Addr 8

Addr 7 Addr 0
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '-'

u iem cua nh a ch dai la co the goi 1 chng trnh con hoac co the nhay en bat ky
vung nh nao vung nh 64K, nhc iem la cac lenh kieu nay dai 3 byte va phu thuoc vao v tr
en ieu nay se bat tien bi khong the di toan bo ma le nh cua chng trnh t vung nh nay
sang cac vung nh khac co ngha la khi chng trnh a viet ni en tai a ch 1000h th sau khi
dch ra ma lenh dang so nh phan th sau o nap vao bo nh th a ch bat au phai ung vi a
ch a viet la 1000h; neu nap vung a ch khac a ch 1000h th chng trnh se thc hien sai.
V du: Ljmp X1 ;nhay en nhan co ten la X1 nam trong tam vc 64kbyte
h. nh a ch ch so (Index Addressing) :
Kieu nh a ch ch so dung mot thanh ghi c ban: la bo em chng trnh PC hoac bo em
d lieu DPTR ket h p vi mot gia tr lech (offset) con goi la gia tr tng oi [thng lu trong
thanh ghi] e tao ra 1 a ch cua o nh can truy xuat hoac la a ch cua ni nhay en. Viec ket
hp c minh hoa nh sau:
Base Register Offset Effective Address
+ =

V du: MOVX A, @A + DPTR ;lay d lieu trong o nh co a ch bang DPTR + A
Khi khao sat tap lenh mot cach chi tiet th chc nang cua cac thanh ghi va cac kieu truy xuat
nay se c trnh bay ro rang hn.
|~ -~: :~: -|| . -| |-| \'
e khao sat tap lenh th phai thong nhat mot so qui nh ve cac t ng k hie u trong tap lenh
thng c s dung:
- Direct tng trng cho o nh noi co a ch Direct.
- Rn tng trng cho cac thanh ghi t thanh ghi R0 en thanh ghi R7.
- @Ri tng trng cho o nh co a ch lu trong thanh ghi Ri va Ri ch co 2 thanh ghi la R0
va R1.
- Cac lenh thng xay ra gia cac oi tng sau:
+ Thanh ghi A.
+ Thanh ghi Rn.
+ O nh co a ch direct.
+ O nh co a ch lu trong thanh ghi @Ri.
+ D lieu 8 bit #data.
+ addr11 la a ch 11 bit t A11 A0: a ch nay phuc vu cho lenh nhay hoac lenh goi
chng trnh con trong pham vi 2 kbyte.
+ Addr16 la a ch 16 bit t A15 A0: a ch nay phuc vu cho le nh nhay va lenh goi
chng trnh con xa trong pham vi 64 kbyte o chnh la a ch nhay e n, hoac a
ch cua chng trnh con.
PC (cr PD1k|

A
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
Khi viet chng trnh ngi lap trnh co the thay the a ch bang nhan (label) e khoi phai
tnh toan cac a ch cu the . Nhan (label) se c at tai v tr addr thay cho addr va phai co mot
nhan at tai ni muon nhay en - goi la 1 cap nhan cung ten.
Co the nhieu ni nhay e n cung mot nhan. Khong c at cac nhan cung ten.
Cac lenh co anh hng en thanh ghi trang thai th co trnh bay trong lenh, con cac lenh
khong e cap en thanh ghi trang thai th co ngha la no khong anh hng.

A.
1. Lenh chuye n d lieu t mot thanh ghi vao thanh ghi A:
Cu phap : Mov A,Rn
Ma lenh :
1 1 1 0 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Chuye n noi dung cua thanh ghi Rn vao thanh ghi A, noi dung thanh ghi
Rn van gi nguyen.
V du: Gia s thanh ghi R0 co noi dung la 32h , lenh:
Mov A,R0 ;ket qua nh sau: (A) = 32h, (R0) = 32h.
Gia tr ban au cha trong A th kho ng can quan tam.
2. Lenh chuye n d lieu t o nh trc tiep vao thanh ghi A :
Cu phap : Mov A, direct
Ma lenh :
1 1 1 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0

Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Chuyen noi dung cua o nh trong Ram noi co a ch direct byte th
hai vao thanh ghi A. Trc tiep co ngha la a ch cua o nh c ghi trong lenh.
V du : Gia s o nh co a ch 30h lu noi dung 32h. Lenh:
Mov A,30h ;chuyen noi dung cua o nh co a ch la 30h sang thanh ghi A.
;Ket qua nh sau: (A)= 32h. chu y a ch 30h ghi trong le nh.
;a7..a0 = 00110000b la a ch cua o nh 30h
3. Lenh chuye n d lieu t o nh gian tiep vao thanh ghi A :
Cu phap : MOV A,@Ri
Ma lenh :
1 1 1 0 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Chuyen noi dung o nh trong Ram noi, co a ch cha trong thanh ghi
Ri, vao thanh ghi A.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '-
V du1: thay v thc hien mov A,30h cua v du tre n th ta co the thay bang
lenh mov A,@R0 se co cung 1 ket qua neu a ch 30h lu vao R0. a ch
30h khong con ghi trong lenh ma c thay bang @R0 nen kieu lenh nay
goi la lenh gian tiep v a ch 30h khong con xuat hien trong lenh. Chu y
trc khi s dung lenh nay ta phai lam cho R0 mang gia tr la 30h.
V du : Gia s R0 co noi dung la 70h, o nh co a ch 70h cha noi dung la 0B8h. Lenh:
Mov A,@R0 ;ket qua nh sau: (A) = 0B8h.
4. Lenh nap d lieu 8 bit vao thanh ghi A :
Cu phap : MOV A, #data
Ma lenh :
0 1 1 1 0 1 0 0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Nap d lieu 8 bit data (d0 en d7) vao thanh ghi A.
V du : Gia s A co noi dung 47h, d lieu trc tiep la 32h, lenh:
Mov A,#32h ;ket qua nh sau: (A) = 32h.
;d7..d0 = 00110010b
5. Lenh chuye n d lieu t thanh ghi A vao thanh ghi :
Cu phap : Mov Rn, A
Ma lenh :
1 1 1 1 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Chuyen noi dung cua thanh ghi A vao thanh ghi Rn.
V du : Gia s A co noi dung 47h , lenh:
Mov R0, A ;ket qua nh sau: (R0) = 47h, (A) = 47h.
6. Lenh chuye n d lieu t o nh trc tiep vao thanh ghi Rn :
Cu phap : MOV Rn, direct
Ma lenh :
1 0 1 0 1 n2 n1 n0
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Chuyen noi dung cua o nh trong Ram noi co a ch direct va o thanh
ghi Rn.
V du : Gia s R1 co noi dung 47h, o nh co a ch 30h cha noi dung 0afh. Lenh:
Mov R1,30h
Lenh chuye n noi dung o nh co a ch 30h sang thanh ghi R1.
Ket qua nh sau: (R1) = 0afh, d lieu trong o nh co a ch 30h khong oi.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'-- . | .
7. Lenh chuye n tc thi d lieu 8 bit vao thanh ghi Rn :
Cu phap : MOV Rn, #data
Ma lenh :
0 1 1 1 1 n2 n1 n0
d7 D6 d5 d4 D3 d2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Nap d lieu 8 bit data (d0 en d7) vao thanh ghi Rn.
V du: Gia s muon chuyen d lieu 47h vao thanh ghi R1:
Mov R1,#47h ;ket qua nh sau: (R1)= 47h.
8. Lenh chuye n d lieu t thanh ghi A vao o nh trc tiep :
Cu phap : MOV direct, A
Ma lenh :
1 1 1 1 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Chuyen noi dung cua thanh ghi A vao o nh trong Ram noi co a ch
direct.
V du : Cho noi dung thanh ghi (A ) = 35H, noi dung o nh co a ch 10H bang 50H.
Mov 10h,A
Sau khi thc hien xong th noi dung o nh co a ch 10h bang 35H.
9. Lenh chuye n d lieu t thanh ghi Rn vao o nh trc tiep :
Cu phap : MOV direct, Rn
Ma lenh :
1 0 0 0 1 n2 n1 n0
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Chuyen noi dung cua thanh ghi Rn vao o nh trong Ram noi co a ch
direct.
V du : Cho noi dung thanh ghi (R0 ) = 35H, noi dung o nh 10H bang 50H.
Mov 10h,R0
Sau khi thc hien xong th noi dung o nh co a ch 10h bang 35H.
10. Lenh chuye n d lieu t o nh trc tiep vao o nh trc tiep :
Cu phap : MOV direct, direct
Ma lenh :
1 0 0 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '-
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Chuyen noi dung cua o nh trong Ram noi co a ch direct va o o nh co
a ch trc direct.
V du : Cho noi dung o nh co a ch 20H bang 35H va noi dung o nh co a ch 10H
bang 50H.
Mov 10h,20h
Sau khi thc hien xong th noi dung o nh co a ch 10h bang 35H.
11. Lenh chuye n d lieu t o nh gian tiep vao o nh trc tiep :
Cu phap : MOV direct, @Ri
Ma lenh :
1 0 0 0 0 1 1 i
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Chuyen noi dung o nh co a ch cha trong thanh ghi Ri vao o nh co
a ch direct.
V du : Cho noi dung thanh ghi (R0 ) = 05H, noi dung o nh co a ch 05h bang FFH va
noi dung o nh co a ch 10H bang 50H.
Mov 10h,@r0
Sau khi thc hien xong th noi dung o nh co a ch 10h bang FFH.
12. Lenh chuye n d lieu vao o nh trc tiep :
Cu phap : MOV direct, #data
Ma lenh :
0 1 1 1 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
d7 d6 d5 d4 d3 d2 d1 d0

Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Nap d lieu data 8 bit (d0 en d7 ) vao o nh co a ch direct.
V du : Cho noi dung o nh co a ch 05h bang FFH.
Mov 05h,#25H
Sau khi thc hien xong th noi dung o nh co a ch 05h bang 25H.
13. Lenh chuye n d lieu t thanh ghi A vao o nh gian tiep :
Cu phap : MOV @Ri, A
Ma lenh :
1 1 1 1 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
Chc nang : Chuyen noi dung cua thanh ghi A vao o nh trong Ram noi co a ch
cha trong thanh ghi Ri.
14. Lenh chuye n d lieu t o nh trc tiep vao o nh gian tiep :
Cu phap : MOV @Ri, direct
Ma lenh :
1 0 1 0 0 1 1 i
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Chuyen noi dung o nh co a ch direct vao o nh co a ch cha trong
thanh ghi Ri.
15. Lenh chuye n d lieu tc thi vao o nh gian tiep :
Cu phap : MOV @Ri, #data
Ma lenh :
0 1 1 1 0 1 1 I
d7 d6 d5 d4 d3 d2 d1 D0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Nap d lieu data 8 bit (d0 en d7) vao o nh co a ch cha trong thanh
ghi Ri.
16. Lenh chuye n d lieu tc thi 16 bit vao thanh ghi con tro d lieu :
Cu phap : MOV dptr, #data16
Ma lenh :
1 0 0 1 0 0 0 0
d1
5
d1
4
d1
3
d1
2
d1
1
d1
0
d9 d8
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Nap d lieu data 16 bit vao thanh ghi con tro d lieu dptr.
17. Lenh chuye n d lieu t o nh co a ch la Dptr + A vao thanh ghi A :
Cu phap : MOVC A,@A+DPTR
Ma lenh :
1 0 0 1 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang : chuyen noi dung cua o nh ngoai, co a ch cha bang dptr cong vi
gia tr cha trong A, chuye n vao thanh ghi A.
18. Lenh chuye n d lieu t o nh co a ch la PC + A vao thanh ghi A :
Cu phap : MOVC A,@A+PC
Ma lenh :
1 0 0 0 0 0 1 1
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '-7
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang : chuyen noi dung cua o nh ngoai co a ch cha bang PC cong vi gia
tr cha trong A c chuyen vao thanh ghi A.
19. Lenh chuye n d lieu t o nh ngoai gian tiep (8 bit a ch) vao thanh ghi A :
Cu phap : MOVX A, @Ri
Ma lenh :
1 1 1 0 0 0 1 I
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang : chuyen noi dung o nh ngoai co a ch cha trong thanh ghi Ri vao
thanh ghi A.
20. Lenh chuye n d lieu t o nh ngoai gian tiep (16 bit a ch) vao thanh ghi A :
Cu phap : MOVX A,@DPTR
Ma lenh :
1 1 1 0 0 0 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang : chuyen noi dung cua o nh ngoai co a ch cha trong thanh ghi dptr
vao thanh ghi A.
21. Lenh chuye n d lieu t thanh ghi A vao o nh ngoai gian tiep (8 bit a ch) :
Cu phap : MOVX @ Ri, A
Ma lenh :
1 1 1 1 0 0 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang : chuyen noi dung cua thanh ghi A ra o nh ngoai co a ch cha trong
thanh ghi Ri.
22. Lenh chuye n d lieu t thanh ghi A vao o nh ngoai gian tiep (16 bit a ch) :
Cu phap : MOVX @DPTR, A
Ma lenh :
1 1 1 1 0 0 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang : Chuyen noi dung cua thanh ghi A ra o nh ngoai co a ch cha trong
thanh ghi dptr.
23. Lenh cat noi dung o nh trc tiep vao ngan xep :
Cu phap : PUSH direct
Ma lenh :
1 1 0 0 0 0 0 0
a7 a6 A5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'-` . | .
Chc nang: cat noi dung cua o nh co a ch direct vao o nh ngan xep. Con tro
ngan xep SP tang len 1 trc khi lu noi dung.
24. Lenh lay d lieu t ngan xep tra ve o nh trc tiep :
Cu phap : POP direct
Ma lenh :
1 1 0 1 0 0 0 0
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: lay noi dung cua o nh ngan xep tra cho o nh co a ch direct. Con tro
ngan xep SP giam 1 sau khi lay d lieu ra.
25. Lenh trao oi d lieu gia thanh ghi vi thanh ghi A :
Cu phap : XCH A,Rn
Ma lenh :
1 1 0 0 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : Trao oi noi dung cua thanh ghi Rn vi thanh ghi A.
V du : cho noi dung cua thanh ghi (A) = 35H va (R0) = 70H
XCH A,R0
Ket qua sau khi thc hien (A) = 70H va (R0) = 35H
26. Lenh trao oi d lieu gia o nh trc tiep vi thanh ghi A :
Cu phap : XCH A,Direct
Ma lenh :
1 1 0 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : Trao oi noi dung cua thanh ghi A vi noi dung o nh co a ch direct.
27. Lenh trao oi d lieu gia o nh gian tiep vi thanh ghi A :
Cu phap : XCH A,@Ri
Ma lenh :
1 1 0 0 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : Trao oi noi dung cua o nh co a ch cha trong thanh ghi Ri vi
thanh ghi A.
28. Lenh trao oi 4 bit d lieu gia o nh gian tiep vi thanh ghi A :
Cu phap : XCHD A,@Ri
Ma lenh :
1 1 0 1 0 1 1 i
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '-3
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : Trao oi d lieu 4 bit thap cua o nh co a ch cha trong thanh ghi Ri
vi d lieu 4 bit thap trong thanh ghi A.
B.
1. Lenh cong thanh ghi A vi thanh ghi :
Cu phap : ADD A,Rn
Ma lenh :
0 0 1 0 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : cong noi dung thanh ghi A vi noi dung thanh ghi Rn, ket qua lu trong
thanh ghi A. Lenh co anh h ng e n thanh ghi trang thai.
V du 1: Gia s A co noi dung 47h va R0 co noi dung la 32h, lenh:
ADD A,R0 ;ket qua nh sau: (A) = 79h, (C) = 0.
V du 2: Gia s A co noi dung 0D9h va R0 co noi dung la 0B8h, le nh:
ADD A,R0 ;ket qua nh sau: (A) = 91h, (C) =1.
2. Lenh cong noi dung o nh trc tiep vao thanh ghi A :
Cu phap : ADD A, direct
Ma lenh :
0 0 1 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Cong noi dung cua o nh co a ch direct vi noi dung thanh ghi A, ket
qua cha thanh ghi A. Le nh co anh hng en thanh ghi trang thai.
V du 1: Gia s A co noi dung 0D9h va o nh co a ch 30h lu noi dung 0B8h, lenh:
ADD A,30h ;ket qua nh sau: (A) = 81h, (C) =1.
V du 2: Gia s A co noi dung 47h va o nh co a ch 30h lu noi dung 32h, lenh:
ADD A,30h ;ket qua nh sau: (A) = 79h, (C) =0.
3. Lenh cong noi dung o nh gian tiep vao thanh ghi A :
Cu phap: ADD A,@Ri
Ma lenh:
0 0 1 0 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: cong noi dung cua o nh co a ch cha trong thanh ghi Ri vi thanh ghi
A, ket qua lu tr trong thanh ghi A. Lenh co anh hng e n thanh ghi trang thai.
V du: Gia s A co noi dung 0D9h, o nh co a ch 30h co noi dung la 0B8h, R0 co noi
dung la 30h, le nh:
ADD A,@R0 ;ket qua nh sau: (A) = 91h, (C) =1.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
4. Lenh cong d lieu tc thi 8 bit vao thanh ghi A :
Cu phap : ADD A, #data
Ma lenh :
0 0 1 0 0 1 0 0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Cong d lieu data 8 bit (d0 en d7) vi noi dung thanh ghi A, ket qua
lu tr trong A. Lenh co anh hng en thanh ghi tra ng thai.
V du1: Gia s A co noi dung 47h, d lieu trc tiep la 32h, lenh:
ADD A,#32h ;ket qua nh sau: (A) = 79h, (C) = 0.
V du2: Gia s A co noi dung D9h, d lieu trc tiep la B8h, lenh:
ADD A,#0B8h ;ket qua nh sau: (A) = 91h, (C) = 1.
5. Lenh cong thanh ghi A vi thanh ghi co bit carry :
Cu phap : ADDC A,Rn
Ma lenh :
0 0 1 1 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : cong noi dung thanh ghi A vi noi dung thanh ghi Rn vi bit C, ket qua
lu trong thanh ghi A. Lenh co anh h ng e n thanh ghi trang thai.
V du1: Gia s A co noi dung 47h, R1 co noi dung 32h va c (C) = 1, lenh:
ADDC A,R1 ;ket qua nh sau: (A) = 7ah, (C) = 0.
V du2: Gia s A co noi dung 0D9h, R0 co noi dung la 0B8h, (C) =1, lenh:
ADDC A,R0 ;ket qua nh sau: (A) = 92h, (C)=1.
6. Lenh cong noi dung o nh trc tiep vao thanh ghi A co bit carry :
Cu phap : ADDC A, direct
Ma lenh :
0 0 1 1 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Cong noi dung cua o nh co a direct noi dung thanh ghi A va bit C,
ket qua cha thanh ghi A. Lenh co anh hng en thanh ghi trang thai.
V du 1: Gia s A co noi dung 47h, o nh 30h co noi dung 32h va c (C) = 0, lenh:
ADDC A,30h ;ket qua nh sau: (A) = 79h, (C) = 0.
V du 2: Gia s A co noi dung 0D9h, o nh 30h co noi dung la 0B8h, C:=1, lenh:
ADDC A,30h ;ket qua nh sau: (A) = 92h, (C) = 1.
7. Lenh cong noi dung o nh gian tiep vao thanh ghi A co bit carry :
Cu phap : ADDC A,@Ri
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . ''
Ma lenh :
0 0 1 1 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : cong noi dung cua o nh co a ch cha trong thanh ghi Ri vi thanh
ghi A vi bit C, ket qua lu tr trong thanh ghi A. Lenh co anh hng en thanh ghi
trang thai.
V du 1: Gia s A co noi dung 47h, o nh 30h co noi dung 32h, R0 co noi dung la 30h va
c (C) = 0, lenh:
ADDC A,@R0 ;ket qua nh sau: (A) = 79h, (C) = 0.
V du 2: Gia s A co noi dung 0D9h, o nh 30h co noi dung la 0B8h, R0 co noi dung 30h
va (C) =1, lenh:
ADDC A,@R0 ;ket qua nh sau: (A) = 92h, (C)=1.
8. Lenh cong d lieu 8 bit vao thanh ghi A co bit carry :
Cu phap : ADDC A, #data
Ma lenh :
0 0 1 1 0 1 0 0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may.
Chc nang: Cong d lieu data 8 bit (d0 en d7) vi noi dung thanh ghi A va bit C,
ket qua lu tr trong A. Le nh co anh hng e n thanh ghi trang thai.
9. Lenh tr thanh ghi A vi thanh ghi :
Cu phap : SUBB A,Rn
Ma lenh :
1 0 0 1 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang : Tr noi dung thanh ghi A vi noi dung thanh ghi Rn va tr cho c Carry,
ket qua lu trong thanh ghi A. Lenh co anh hng en thanh ghi trang thai.
V du 1: Gia s A co noi dung 47h, noi dung thanh ghi R0 la 32h va c (C)=0, lenh:
SUBB A,R0 ;ket qua nh sau: (A) = 15h (C)=0.
V du 2: Gia s A co noi dung 0B9h, thanh ghi R0 co noi dung la 5Ah va (C)=1, lenh:
SUBB A,R0 ;ket qua nh sau: (A) = 5Eh, (C) =0.
10. Lenh tr noi dung thanh ghi A cho noi dung o nh trc tiep :
Cu phap : SUBB A, direct
Ma lenh :
1 0 0 1 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
Chc nang: Tr noi dung thanh ghi A cho noi dung cua o nh co a ch direct va tr
cho c Carry, ket qua cha thanh ghi A. Lenh co anh hng en thanh ghi trang
thai.
11. Lenh tr noi dung thanh ghi A cho noi dung o nh gian tiep :
Cu phap : SUBB A,@Ri
Ma lenh :
1 0 0 1 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Tr noi dung cua thanh ghi A cho d lieu cua o nh co a ch cha
trong thanh ghi Ri va tr cho c carry, ket qua lu tr trong thanh ghi A. Lenh co anh
hng en thanh ghi trang thai.
12. Lenh tr noi dung thanh ghi A cho d lieu tc thi 8 bit :
Cu phap : SUBB A, #data (subtract: tr)
Ma lenh :
1 0 0 1 0 1 0 0
d7 d6 d5 d4 d3 d2 D1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Tr noi dung thanh ghi A cho d lieu 8 bit d0 en d7 va tr cho c carry,
ket qua lu tr trong A. Le nh co anh hng e n thanh ghi trang thai.
13. Lenh tang noi dung thanh ghi A :
Cu phap : INC A (increment: tang le n 1 n v)
Ma lenh :
0 0 0 0 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Tang noi dung thanh ghi A len 1.
V du 1: Gia s A co noi dung 35h, lenh:
INC A ;ket qua nh sau: (A) = 36h.
V du 2: Gia s A co noi dung FFh, lenh:
INC A ;ket qua nh sau: (A) = 00h.
14. Lenh tang noi dung cua thanh ghi :
Cu phap : INC Rn
Ma lenh :
0 0 0 0 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Tang noi dung thanh ghi Rn len 1.
15. Lenh tang noi dung o nh trc tiep :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
Cu phap : INC direct
Ma lenh :
0 0 0 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Tang noi dung cua o nh co a ch trc tiep byte th 2 len 1.
16. Lenh tang noi dung o nh gian tiep :
Cu phap : INC @Ri
Ma lenh :
0 0 0 0 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Tang noi dung cua o nh co a ch cha trong thanh ghi Ri len 1.
V du : Gia s noi dung o nh 30h la 35h, thanh ghi R0 co noi dung la 30h, lenh:
INC R0 ;ket qua nh sau: o nh (30h) = 36h
17. Lenh tang noi dung con tro d lieu Dptr :
Cu phap : INC dptr
Ma lenh :
1 0 1 0 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Tang noi dung cua thanh ghi con tro d lieu dptr le n 1.
18. Lenh giam noi dung thanh ghi A :
Cu phap : DEC A
Ma lenh :
0 0 0 1 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Giam noi dung thanh ghi A xuong 1.
V du 1: Gia s A co noi dung 35h, lenh:
DEC A ;ket qua nh sau: (A) = 34h.
V du 2: Gia s A co noi dung 00h, lenh:
DEC A ;ket qua nh sau: (A) = FFh.
19. Lenh giam noi dung cua thanh ghi :
Cu phap : DEC Rn
Ma lenh :
0 0 0 1 1 n2 n1 n0

Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
Chc nang: Giam noi dung thanh ghi Rn xuong 1.
V du : Gia s R0 la 35h , lenh:
DEC R0 ;ket qua nh sau: (R0) =34h.
20. Lenh giam noi dung o nh trc tiep :
Cu phap : DEC direct
Ma lenh :
0 0 0 1 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Giam noi dung cua o nh co a ch direct byte th 2 xuo ng 1.
V du: Gia s o nh 30h co noi dung la 35h , lenh:
DEC 30h ;ket qua nh sau: o nh co a ch la 30h lu 34h.
21. Lenh giam noi dung o nh gian tiep :
Cu phap : DEC @Ri
Ma lenh :
0 0 0 1 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Giam noi dung cua o nh co a ch cha trong thanh ghi Ri xuo ng 1.
22. Lenh nhan thanh ghi A vi thanh ghi B :
Cu phap : MUL AB
Ma lenh :
1 0 1 0 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 4 chu ky may
Chc nang: Noi dung cua thanh ghi A nhan vi noi dung cua thanh ghi B, ket qua la
mot d lieu 16 bit, 8 bit thap lu tr trong thanh ghi A, 8 bit cao lu tr trong thanh
ghi B.
V du : Gia s thanh ghi A co noi dung la 50h,thanh ghi B co noi dung 0A0h , lenh:
MUL AB ;Ket qua nh sau: 50h*A0h:= 3200h th (A) = 00 va (B) = 32h.
23. Lenh chia thanh ghi A cho thanh ghi B :
Cu phap : DIV AB
Ma lenh :
1 0 0 0 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 4 chu ky may
Chc nang: Noi dung cua thanh ghi A chia cho noi dung cua thanh ghi B, ket qua cua
phep chia lu tr trong thanh ghi A, so d lu tr trong thanh ghi B. Lenh anh hng
en thanh ghi trang thai: Bit C va bit OV b xoa ve 0, neu phep chia nay ma d lieu
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
trong thanh ghi B = 00h th noi dung thanh ghi A khong thay oi, noi dung cha trong
thanh ghi B kho ng xac nh va bit OV = 1, bit Cy = 0.
24. Lenh ieu chnh thap phan noi dung thanh ghi A :
Cu phap : DA A
Ma lenh :
1 1 0 1 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 4 chu ky may
Chc nang: Neu 4 bit thap A3A2A1A0>9 hoac bit AC = 1th A3A2A1A0 + 6, ket
qua lu tr lai trong A. Ne u 4 bit cao A7A6A5A4>9 hoac bit Cy = 1 th A7A6A5A4
+ 6, ket qua lu tr lai thanh ghi A. Ket qua sau cung trong thanh ghi A la so BCD.
C.
1. Lenh and thanh ghi A vi thanh ghi :
Cu phap : ANL A,Rn (and logic)
Ma lenh :
0 1 0 1 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A and vi noi dung thanh ghi Rn, ket qua lu tr
trong thanh ghi A.
V du :
Mov A ,#10110011b
Mov R0,#11001011b
Anl a,r0 ;ket qua (A) = 10000011b
2. Lenh and thanh ghi A vi noi dung o nh trc tiep :
Cu phap : ANL A, direct
Ma lenh :
0 1 0 1 0 1 0 1
a7 A6 a5 a4 a3 A2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A and vi noi dung cua o nh co a ch direct, ket
qua cha thanh ghi A.
3. Lenh and thanh ghi A vi noi dung o nh gian tiep :
Cu phap : ANL A, @Ri
Ma lenh :
0 1 0 1 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A and vi o nh co a ch cha trong thanh ghi Ri,
ket qua lu tr trong thanh ghi A.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
4. Lenh and thanh ghi A vi d lieu tc thi 8 bit :
Cu phap : ANL A, #data
Ma lenh :
0 1 0 1 0 1 0 0
d7 d6 d5 d4 d3 D2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung cua thanh ghi A and vi d lieu d0 en d7 , ket qua lu tr
trong thanh ghi A.
V du :
Mov A ,#10110011b
Anl a,#00001111b ;ket qua (A) = 00000011b
5. Lenh and noi dung o nh trc tiep vi noi dung thanh ghi A :
Cu phap : ANL direct, A
Ma lenh :
0 1 0 1 0 0 1 0
a7 a6 A5 a4 a3 A2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung o nh co a ch direct and vi noi dung cua thanh ghi A, ket
qua lu tr vao o nh.
V du :
Mov A ,#10110011b
Mov 10h,#11110000b
Anl 10h,a ;ket qua o nh co a ch 10h lu 10110000b.
6. Lenh and noi dung o nh trc tiep vi d lieu tc thi 8 bit :
Cu phap : ANL direct, #data
Ma lenh :
0 1 0 1 0 0 1 1
a7 a6 a5 a4 a3 A2 a1 a0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Noi dung cua o nh co a ch direct and vi 8 bit d lieu 8 bit, ket qua
lu tr vao o nh.
7. Lenh or thanh ghi A vi thanh ghi :
Cu phap : ORL A, Rn
Ma lenh :
0 1 0 0 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7
Chc nang: Noi dung thanh ghi A or vi noi dung thanh ghi Rn, ket qua lu tr trong
thanh ghi A.
V du :
Mov A ,#10110011b
Mov R0,#11001011b
Orl a,r0 ;ket qua (A) = 11111011b.
8. Lenh or thanh ghi A vi noi dung o nh trc tiep :
Cu phap : ORL A, direct
Ma lenh :
0 1 0 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A or vi noi dung cua o nh co a ch direct, ket qua
cha thanh ghi A.
9. Lenh or thanh ghi A vi noi dung o nh gian tiep :
Cu phap : ORL A, @Ri
Ma lenh :
0 1 0 0 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A or vi o nh co a ch cha trong thanh ghi Ri, ket
qua lu tr trong thanh ghi A.
10. Lenh or thanh ghi A vi d lieu tc thi 8 bit :
Cu phap : ORL A, #data
Ma lenh :
0 1 0 0 0 1 0 0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung cua thanh ghi A or vi d lieu 8 bit data (t d0 en d7), ket
qua lu tr trong thanh ghi A.
11. Lenh or noi dung o nh trc tiep vi noi dung thanh ghi A :
Cu phap : ORL direct, A
Ma lenh :
0 1 0 0 0 0 1 0
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung o nh co a ch direct or vi noi dung cua thanh ghi A, ket qua
lu tr trong o nh co a ch direct.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .
12. Lenh or noi dung o nh trc tiep vi d lieu tc thi 8 bit :
Cu phap : ORL direct, #data
Ma lenh :
0 1 0 0 0 0 1 1
a7 a6 a5 a4 a3 a2 a1 a0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Noi dung cua o nh co a ch direct or vi d lieu 8 bit (t d0 en d7 )
byte th 3, ket qua lu tr trong o nh.
13. Lenh ex-or thanh ghi A vi thanh ghi :
Cu phap : XRL A, Rn
Ma lenh :
0 1 1 0 1 n2 n1 n0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A ex-or vi noi dung thanh ghi Rn, ket qua lu tr
trong thanh ghi A.
14. Lenh ex-or thanh ghi A vi noi dung o nh trc tiep :
Cu phap : XRL A, direct
Ma lenh :
0 1 1 0 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A ex-or vi noi dung cua o nh co a ch direct, ket
qua cha thanh ghi A.
15. Lenh ex-or thanh ghi A vi noi dung o nh gian tiep :
Cu phap : XRL A, @Ri
Ma lenh :
0 1 1 0 0 1 1 i
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A ex-or vi o nh co a ch cha trong thanh ghi Ri,
ket qua lu tr trong thanh ghi A.
16. Lenh ex-or thanh ghi A vi d lieu tc thi 8 bit :
Cu phap : XRL A, #data
Ma lenh :
0 1 1 0 0 1 0 0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3
Chc nang: Noi dung cua thanh ghi A ex-or vi d lieu data, ket qua lu tr trong
thanh ghi A.
17. Lenh ex-or noi dung o nh trc tiep vi noi dung thanh ghi A :
Cu phap : XRL direct, A
Ma lenh :
0 1 1 0 0 0 1 0
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung o nh co a ch direct ex-or vi noi dung cua thanh ghi A, ket
qua lu tr vao o nh.
18. Lenh ex-or noi dung o nh trc tiep vi d lieu tc thi 8 bit :
Cu phap : XRL direct, #data
Ma lenh :
0 1 1 0 0 0 1 1
a7 a6 a5 a4 a3 a2 a1 a0
d7 d6 d5 d4 d3 d2 d1 d0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Noi dung cua o nh co a ch direct ex-or vi 8 bit d lieu data 8 bit, ket
qua lu tr vao o nh.
19. Lenh xoa noi dung thanh ghi A :
Cu phap : CLR A (clear a)
Ma lenh :
1 1 1 0 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A bang zero.
20. Lenh bu noi dung thanh ghi A :
Cu phap : CPL A (complement A)
Ma lenh :
1 1 1 1 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A c lay bu 1, ket qua cha trong A.
V du :
Mov A ,#10110011b
Cpl a ;ket qua (A) = 01001100b.
21. Lenh xoay trai noi dung thanh ghi A :
Cu phap : RL A (rotate left)
Ma lenh :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
0 0 1 0 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A c xoay trai 1 bit minh hoa nh hnh ve.







V du :
Mov A,#1011 0011b ;
RL A ;lenh th nhat
Ga tr ban au cua C ta khong can quan tam en ket qua sau khi xoay th (A) = 0110 0111b
va c (C) = 1 la do bit A7 bang 1 chuyen sang.
RL A ;lenh th 2
Ket qua sau khi xoay th (A) = 11001110b va c (C) = 0 la do bit A7 bang 0 chuyen sang.
22. Lenh xoay trai noi dung thanh ghi A va bit carry :
Cu phap : RLC A
Ma lenh :
0 0 1 1 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A va bit C c xoay trai 1 bit.








V du 1: Gia s cho c C = 0 trc khi thc hien lenh
Mov A ,#10110011b
RLC A ;ket qua (A) = 01100110b va c (C) = 1

V du 2:
Setb c ;lam c C bang 1
Mov A,#00000000b

A7

A6 A5 A4 A3 A2 A7 A0 A7 A1

C

A7

A6 A5 A4 A3 A2
A7
A0 A7 A1

C
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . ''
RLC A ;ket qua (A) = 0000 0001b va c (C) = 0
Setb c ;lam c C bang 1
RLC A ;ket qua (A) = 0000 0011b va c (C) = 0
.
Setb c ;lam c C bang 1
RLC A ;ket qua (A) = 0111 1111b va c (C) = 0
Setb c ;lam c C bang 1 (lan th 8)
RLC A ;ket qua (A) = 1111 1111b va c (C) = 0

Setb c ;lam c C bang 1 (lan th 9)
RLC A ;ket qua (A) = 1111 1111b va c (C) = 1
;xxxx
clr c ;lam c C bang 0
RLC A ;ket qua (A) = 1111 1110b va c (C) = 1
23. Lenh xoay phai noi dung thanh ghi A :
Cu phap : RR A (rotate right)
Ma lenh :
0 0 0 0 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A c xoay phai 1 bit ngc vi lenh RL A.
24. Lenh xoay phai noi dung thanh ghi A va bit carry :
Cu phap : RRC A
Ma lenh :
1 1 1 0 0 1 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung thanh ghi A va bit C c xoay phai 1 bit ngc vi lenh RLC
A.
25. Lenh xoay thanh ghi A 4 bit :
Cu phap : SWAP A
Ma lenh :
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: hoan chuyen 4 bit thap va 4 bit cao trong thanh ghi A.
V du :
Mov A,#3EH
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
Swap A ;ket qua (A) = E3H
D.
Nhom lenh nay la nhom lenh chuyen quyen ieu khien co ngha la vi ieu khien ang
thc hien lenh tai a ch nay th co the nhay en hoac chuyen e n thc hien lenh tai mot a ch
khac.
Trong nhom nay gom co lenh goi chng trnh con, lenh ket thuc chng trnh con tr ve
chng trnh chnh, lenh nhay khong ieu kie n va lenh nhay co ieu kien.
Cac lenh nhay bao gom lenh nhay tng oi, lenh nhay tuyet oi, lenh nhay dai.
Cac lenh nhay co ieu kie n th khi thoa ieu kien th lenh se nhay co n ne u kho ng thoa
ieu kien th se thc hie n lenh ke ngay sau lenh nhay. ay ch trnh bay ieu kie n thoa con ieu
kien khong thoa th ta hieu ngam.
1. Lenh goi chng trnh con dung a ch tuyet oi :
Cu phap : ACALL addr11
Ma lenh :
a1
0
a9 a8 1 0 0 0 1
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may.
Chc nang: Khi lenh nay c thc hien th vi ieu khien se thc hie n chng trnh
con tai a ch addr11. Chng trnh con khong c cach lenh goi qua 2 kbyte.
Addr11 cua chng trnh con co the thay bang nhan (ten cua chng trnh con).
Chu y: Trc khi nap a ch mi va o thanh ghi PC th a ch cua lenh ke trong
chng trnh chnh c cat vao bo nh ngan xep.
2. Lenh goi chng trnh con dung a ch dai 16 bit :
Cu phap : LCALL addr16
Ma lenh :
0 0 0 1 0 0 1 0
A15 a1
4
a1
3
a1
2
a1
1
a1
0
a9 a8
A7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Khi le nh nay c thc hien th vi ieu khie n se thc hien chng trnh
con tai a ch addr16. Lenh nay co the goi chng trnh con au cung c trong
vung 64kbyte. Addr16 cua chng trnh con co the thay bang nhan (ten cua chng
trnh con).
16 bit a ch A15 A0 c nap vao PC, vi ieu khien se thc hien chng trnh
con tai a ch va nap vao PC. Chu y: Trc khi nap a ch vao thanh ghi PC th a
ch cua lenh ke trong chng trnh chnh c cat vao bo nh ngan xep.
3. Lenh tr ve t chng trnh con :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
Cu phap : RET
Ma lenh :
0 0 1 0 0 0 1 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Lenh nay se ket thuc chng trnh con, vi ieu khien se tr lai chng
trnh chnh e tiep tuc thc hien chng trnh.
Chu y: lenh nay se lay a ch cua lenh ke a lu trong bo nh ngan xep (khi thc
hien lenh goi) tra lai cho thanh ghi PC e tiep tuc thc hien chng trnh chnh. Khi
viet chng trnh con th phai luon luo n ket thuc bang lenh ret.
4. Lenh tr ve t chng trnh con phuc vu ngat :
Cu phap : RETI
Ma lenh :
0 0 1 1 0 0 1 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Lenh nay se ket thuc chng trnh phuc vu ngat, vi ieu khien se tr lai
chng trnh chnh e tiep tuc thc hien chng trnh.
5. Lenh nhay dung a ch tuyet oi :
Cu phap : AJMP addr11
Ma lenh :
a1
0
a9 a8 0 0 0 0 1
a7 a6 a5 a4 A3 a2 a1 a0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Y ngha cua lenh: vi ieu khien se nhay en a ch addr11 e thc hien chng
trnh tai o. Addr11 co the thay the bang nhan. Nhan hay a ch nhay en khong
qua 2 kbyte.
11 bit a ch A10 A0 c nap vao PC, cac bit cao cua PC kho ng thay oi, vi ieu
khien se nhay en thc hien lenh tai a ch PC mi va nap.
Lenh nay khac vi lenh goi chng trnh con la khong cat a ch tr ve. Ni nhay
en kho ng qua 2 kbyte so vi lenh nhay.
6. Lenh nhay dung a ch 16 bit :
Cu phap : LJMP addr16
Ma lenh :
0 0 0 0 0 0 1 0
a1
5
a1
4
a1
3
a1
2
a1
1
a1
0
a9 a8
a7 a6 a5 a4 a3 a2 a1 a0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: vi ieu khien se nhay en a ch addr16 e thc hien chng trnh tai
o. Ni nhay en tuy y nam trong vung 64 kbyte.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'- . | .
7. Lenh nhay tng oi :
Cu phap : SJMP rel
Ma lenh :
1 0 0 0 0 0 0 0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: vi ieu khien se nhay e n lenh co a ch tng oi (rel) e thc hien
tiep. Co the thay the rel bang nhan.
Lenh nay ch nhay trong tam vc 256 byte: co the nhay ti 128 byte va co the nhay
lui 128 byte. Khi tam vc nhay xa hn ta nen dung le nh AJMP hay LJMP.
Chu y: rel [relative: tng oi]: cac lenh co xuat hien rel e u lien quan e n lenh
nhay: ni nhay en c tnh bang cach lay noi dung cua PC cong vi so lng byte
cua cac lenh nam gia lenh nhay va ni nhay en. Chu ng ta khong can quan tam
en ieu nay v chng trnh bien dch cua may tnh se tnh giup chung ta.
8. Lenh nhay gian tiep :
Cu phap : JMP @A + DPTR
Ma lenh :
0 1 1 1 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: lenh se nhay en ni co a ch bang noi dung cua A cong vi dptr e
tiep tuc thc hien chng trnh tai o.
9. Lenh nhay neu c Z =1 (noi dung thanh ghi A bang 0) :
Cu phap : JZ rel (jump zero)
Ma lenh :
0 1 1 0 0 0 0 0
r7 r6 r5 r4 r3 r2 R1 r0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu bit Z = 1 th vi ie u khie n se nhay en thc hien chng trnh tai a
ch rel (thoa ieu kien), neu Z = 0 th vi ieu khien se tiep tuc thc hien lenh ke
(khong thoa ieu kien).
10. Lenh nhay neu c Z = 0 (noi dung thanh ghi A khac 0):
Cu phap : JNZ rel
Ma lenh :
0 1 1 1 0 0 0 0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu Z = 0 th vi ieu khien se nhay en thc hien chng trnh tai a
ch rel.
11. Lenh nhay neu bit carry = 1 :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '
Cu phap : JC rel
Ma lenh :
0 1 0 0 0 0 0 0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu bit carry C = 1 th vi ieu khien se nhay en thc hien chng trnh
tai a ch rel.
12. Lenh nhay neu bit carry = 0 :
Cu phap : JNC rel
Ma lenh :
0 1 0 1 0 0 0 0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu bit carry C = 0 th vi ieu khien se nhay en thc hien chng trnh
tai a ch rel.
13. Lenh nhay neu bit = 1 :
Cu phap : JB bit, rel
Ma lenh :
0 0 1 0 0 0 0 0
b7 b6 b5 b4 b3 b2 b1 b0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu noi dung cua bit co a ch bit [c xac nh bi byte th 2] bang 1
th vi ieu khien se nhay e n thc hien chng trnh tai a ch rel.
14. Lenh nhay neu bit = 0 :
Cu phap : JNB bit, rel
Ma lenh :
0 0 1 1 0 0 0 0
b7 b6 b5 b4 b3 b2 b1 b0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu noi dung cua bit co a ch bit [c xac nh bi byte th 2] bang 0
th vi ieu khien se nhay e n thc hien chng trnh tai a ch bang rel.
15. Lenh nhay neu bit = 1 va xoa bit :
Cu phap : JBC bit, rel
Ma lenh :
0 0 0 1 0 0 0 0
b7 b6 b5 b4 b3 b2 b1 b0
r7 r6 r5 r4 r3 r2 r1 r0
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
' . | .
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: neu bit c xac nh bi byte th 2 bang 1 th bit nay c xoa ve 0 va
vi ieu khien se nhay en thc hien chng trnh tai a ch rel.
16. Lenh so sanh o nh trc tiep vi noi dung thanh ghi A :
Cu phap : CJNE A, direct, rel (compare jump if not equal)
Ma lenh :
1 0 1 1 0 1 0 0
a7 a6 a5 a4 a3 a2 a1 a0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: lenh nay anh hng en c C va thc hien viec nhay nh sau:
Neu noi dung cua A noi dung cua o nh co a ch direct th bit C = 0.
Neu noi dung cua A < noi dung cua o nh co a ch direct th bit C = 1.
Neu noi dung cua A khac noi dung cua o nh co a ch direct th lenh se nhay
en va thc hien lenh tai a ch rel.
Neu noi dung cua A bang noi dung cua o nh co a ch direct th khong nhay
va lam lenh ke.
17. Lenh so sanh d lieu tc thi vi noi dung thanh ghi A :
Cu phap : CJNE A, #data, rel
Ma lenh :
1 0 1 1 0 1 0 0
d7 d6 d5 d4 d3 d2 d1 d0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: lenh nay anh hng en c C va thc hien viec nhay nh sau:
Neu noi dung cua A data 8 bit th bit C = 0.
Neu noi dung cua A < data 8 bit th bit C = 1.
Neu noi dung cua A khac data 8 bit th lenh se nhay en thc hien lenh tai a
ch rel.
Neu noi dung cua A bang data 8 bit th khong nhay va lam lenh ke .
18. Lenh so sanh d lieu tc thi vi noi dung thanh ghi :
Cu phap : CJNE Rn, #data, rel
Ma lenh :
1 0 1 1 1 n2 n1 n0
d7 d6 d5 d4 d3 d2 d1 d0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: lenh nay anh hng en c C va thc hien viec nhay nh sau:
Neu noi dung cua thanh ghi Rn data 8 bit th bit C = 0.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7
Neu noi dung cua thanh ghi Rn < data 8 bit th bit C = 1.
Neu noi dung cua thanh ghi Rn khac data 8 bit th lenh se nhay en thc hien
lenh tai a ch rel.
Neu noi dung cua thanh ghi Rn bang data 8 bit th khong nhay va lam lenh ke.
19. Lenh so sanh d lieu tc thi vi d lieu gian tiep :
Cu phap : CJNE @Ri, #data, rel
Ma lenh :
1 0 1 1 0 1 1 i
d7 d6 d5 d4 d3 d2 d1 d0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: lenh nay anh hng en c C va thc hien viec nhay nh sau:
Neu noi dung cua o nh co a ch lu trong thanh ghi Ri data 8 bit th bit C =
0.
Neu noi dung cua o nh co a ch lu trong thanh ghi Ri < data 8 bit th bit C =
1.
Neu noi dung cua o nh co a ch lu trong thanh ghi Ri khac data 8 bit th
lenh se nhay en thc hie n lenh tai a ch rel.
Neu noi dung cua o nh co a ch lu trong thanh ghi Ri bang data 8 bit th
khong nhay va lam lenh ke.
20. Lenh giam thanh ghi va nhay :
Cu phap : DJNZ Rn, rel (decrement and jump if not zero)
Ma lenh :
1 1 0 1 1 n2 n1 n0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Noi dung cua thanh ghi Rn giam i 1 va neu ket qua trong thanh ghi Rn
sau khi giam khac 0 th vi ieu khien se thc hien chng trnh tai a ch rel, neu
ket qua bang 0 th vi ieu khien se tiep tuc thc hien lenh ke.
21. Lenh giam o nh trc tiep va nhay :
Cu phap : DJNZ direct, rel
Ma lenh :
1 1 0 1 0 1 0 1
a7 a6 a5 a4 a3 a2 a1 a0
r7 r6 r5 r4 r3 r2 r1 r0
Lenh nay chiem 3 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Neu noi dung cua o nh co a ch direct giam i 1 va neu ket qua sau
khi giam khac 0 th vi ieu khien se thc hie n chng trnh tai a ch rel, ngc lai
neu ket qua bang 0 th vi ieu khie n se tiep tuc thc hien lenh ke .
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .
22. Lenh Nop :
Cu phap : NOP
Ma lenh :
0 0 0 0 0 0 0 0
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Noi dung cua PC tang len 1 va tiep tuc thc hien lenh tiep theo.
E.
1. Lenh xoa bit carry :
Cu phap : CLR C
Ma lenh :
1 1 0 0 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Xoa bit C ve 0.
2. Lenh xoa bit :
Cu phap : CLR bit
Ma lenh :
1 1 0 0 0 0 1 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Xoa bit co a ch c xac nh bi byte th 2 ve 0.
3. Lenh at bit carry :
Cu phap : SETB C
Ma lenh :
1 1 0 1 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: at bit C = 1.
4. Lenh at bit :
Cu phap : SETB bit
Ma lenh :
1 1 0 1 0 0 1 0
b7 b6 b5 b4 B3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: at bit co a ch c xac nh bi byte th 2 le n 1.
5. Lenh bu bit carry :
Cu phap : CPL C
Ma lenh :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3
1 0 1 1 0 0 1 1
Lenh nay chiem 1 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Bu bit carry, neu trc o C = 1 th C = 0, ngc lai C = 0 th C = 1.
6. Lenh bu bit :
Cu phap : CPL bit
Ma lenh :
1 0 1 1 0 0 1 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Bu bit co a ch xac nh bi byte th 2, neu trc o bit nay = 0 th ket
qua bit nay bang 1 va ngc lai neu trc o bang 1 th no se bang 0.
7. Lenh and bit carry vi bit :
Cu phap : ANL C, bit
Ma lenh :
1 0 0 0 0 0 1 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Bit C and vi bit co a ch c xac nh bi byte th 2, ket qua cha
bit C.
8. Lenh and bit carry vi bu bit :
Cu phap : ANL C, /bit
Ma lenh :
1 0 1 1 0 0 0 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Bit C and vi bu bit co a ch c xac nh bi byte th 2, ket qua cha
bit C.
9. Lenh or bit carry vi bit :
Cu phap : ORL C, bit
Ma lenh :
0 1 1 1 0 0 1 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Bit C or vi bit co a ch c xac nh bi byte th 2, ket qua cha
bit C.
10. Lenh or bit carry vi bu bit :
Cu phap : ORL C, /bit
Ma lenh :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'7 . | .
1 0 1 0 0 0 0 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Bit C or vi bu bit co a ch c xac nh bi byte th 2, ket qua cha
bit C.
11. Lenh di chuye n bit vao bit carry :
Cu phap : MOV C, bit
Ma lenh :
1 0 1 0 0 0 1 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 1 chu ky may
Chc nang: Bit co a ch c xac nh bi byte th 2 c chuyen vao bit C.
12. Lenh di chuye n bit carry vao bit :
Cu phap : MOV bit, C
Ma lenh :
1 0 0 1 0 0 1 0
b7 b6 b5 b4 b3 b2 b1 b0
Lenh nay chiem 2 byte va thi gian thc hien lenh la 2 chu ky may
Chc nang: Bit C c chuyen vao bit co a ch c xac nh bi byte th 2.
- :~: :~: -|| . -| |-| \'

Data Transfer Instructions.

Mnemonic
Instruction code Hexa
decimal
Explanation
D
7
D
6
D
5
D
4
D
3
D
2
D
1
D
0

MOV A,Rn 1 1 1 0 1 n
2
n
1
n
0
E8 EF (A) (Rn)
MOV A,direct 1 1 1 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

E5
Byte 2
(A) (direct)
MOV A,@Ri 1 1 1 0 0 1 1 I E6 E7 (A) ((Ri))
MOV A, #data 0 1 1 1 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

74
Byte 2
(A) #data
MOV Rn, A 1 1 1 1 1 n
2
n
1
n
0
F8 FF (Rn) (A)
MOV Rn,direct 1 0 1 0 1 n
2
n
1
n
0

a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

A8 AF
Byte 2
(Rn) (direct)
MOV Rn #data 0 1 1 1 1 n
2
n
1
n
0

d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

78 7F
Byte 2
(Rn) #data
MOV direct,A 1 1 1 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

F5
Byte 2
(direct) (A)
MOV direct,Rn 1 0 0 0 1 n
2
n
1
n
0

a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

88 8F
Byte 2
(direct) (Rn)
MOV direct,direct 1 0 0 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

85
Byte 2
(direct) (direct)
(source)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7'
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0
Byte 3 (destination)
MOV direct,@Ri 1 0 0 0 0 1 1 I
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

86 87
Byte 2
(direct) ((Ri))
MOV direct,#data 0 1 1 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

75
Byte 2
Byte 3
(direct) #data
MOV @Ri, A 1 1 1 1 0 1 1 I F6 F7 ((Ri)) (A)
MOV @Ri, direct 1 0 1 0 0 1 1 I
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

A6 A7
Byte 2
((Ri)) (direct)
MOV @Ri, #data 0 1 1 1 0 1 1 i
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

76 77
Byte 2
((Ri)) (data)
MOV dptr,#data16 1 0 0 1 0 0 0 0
d
15
d
14
d
13
d
12
d
11
d
10
d
9
d
8

d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

90
Byte 2
Byte 3
(dptr) #data
15-0
(dpH) #data
15-8
(dpL) #data
7-0

MOVC A, @A +
dptr
1 0 0 1 0 0 1 1 93 (A) ((A) + (dptr))
External Ram
MOVC A, @A +
PC
1 0 0 0 0 0 1 1 83 (A) ((A) + (PC))
External Ram
MOVX A, @Ri 1 1 1 0 0 0 1 i E2 E3 (A)((Ri))
External Ram
MOVX A,@dptr 1 1 1 0 0 0 0 0 E0 (A)((dptr))
External Ram
MOVX @Ri, A 1 1 1 1 0 0 1 i F2 F3 ((Ri)) (A)
MOVX @ dptr, A 1 1 1 1 0 0 0 0 F0 ((dptr)) (A)
PUSH direct 1 1 0 0 0 0 0 0
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

C0
Byte 2
(SP) (SP) + 1
((SP)) (direct)
POP direct 1 1 0 1 0 0 0 0
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

D0
Byte 2
(direct) ((SP))
(SP) (SP) - 1
XCH A, Rn 1 1 0 0 1 n
2
n
1
n
0
C8 CF (A) (Rn)
XCH A, direct 1 1 0 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

C5
Byte 2
(A) (direct)
XCH A, @Ri 1 1 0 0 0 1 1 i C6 C7 (A) ((Ri))
XCHD A, @Ri 1 1 0 1 0 1 1 i D6 D7 (A
3-0
) ((Ri
3-0
))

Mathematical (Arithmetic) Instructions.

Mnemonic
Instruction code Hexa
Decimal
Explanation
D
7
D
6
D
5
D
4
D
3
D
2
D
1
D
0

ADD A,Rn 0 0 1 0 1 n
2
n
1
n
0
28 2F (A) (A) + (Rn)
ADD A,direct 0 0 1 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

25
Byte 2
(A) (A) + (direct)
ADD A,@Ri 0 0 1 0 0 1 1 I 26 27 (A) (A) + ((Ri))
ADD A,#data 0 0 1 0 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

24
Byte 2
(A) (A) + #data
ADDC A, Rn 0 0 1 1 1 n
2
n
1
n
0
38 3F (A) (A) + (Rn) + (C)
ADDC A, direct 0 0 1 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

35
Byte 2
(A) (A) + (direct) + (C)
ADDC A, @Ri 0 0 1 1 0 1 1 I 36 37 (A) (A) + ((Ri)) + (C)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'7 . | .
ADDC A, #data 0 0 1 1 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

34
Byte 2
(A) (A) + #data +(C)
SUBB A,Rn 1 0 0 1 1 n
2
n
1
n
0
98 9F (A) (A) - (Rn) - (C)
SUBB A,direct 1 0 0 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

95
Byte 2
(A) (A) - (direct) - (C)
SUBB A,@Ri 1 0 0 1 0 1 1 I 96 97 (A) (A) - ((Ri)) - (C)
SUBB A,#data 1 0 0 1 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

94
Byte 2
(A) (A) - #data -(C)
INC A 0 0 0 0 0 1 0 0 04 (A) (A) + 1
INC Rn 0 0 0 0 1 n
2
n
1
n
0
08 0F (Rn) (Rn) + 1
INC direct 0 0 0 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

05
Byte 2
(direct) (direct) + 1
INC @Ri 0 0 0 0 0 1 1 I 06 07 ((Ri)) ((Ri)) + 1
INC dptr 1 0 1 0 0 0 1 1 A3 (dptr) (dptr) + 1
DEC A 0 0 0 1 0 1 0 0 14 (A) (A) - 1
DEC Rn 0 0 0 1 1 n
2
n
1
n
0
18 1F (Rn) (Rn) - 1
DEC direct 0 0 0 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

15
Byte 2
(direct) (direct) - 1
DEC @Ri 0 0 0 1 0 1 1 I 16 17 ((Ri)) ((Ri)) - 1
MUL AB 1 0 1 0 0 1 0 0 A4 (B
15-8
),(A
7-0
) (A)(B)
DIV AB 1 0 0 0 0 1 0 0 84 (A
15-8
),(B
7-0
) (A)/(B)
DA A 1 1 0 1 0 1 0 0 D4 Content of A la BCD

Logic Instructions.

Mnemonic
Instruction code Hexa
Decimal
Explanation
D
7
D
6
D
5
D
4
D
3
D
2
D
1
D
0

ANL A,Rn 0 1 0 1 1 n
2
n
1
n
0
58 5F (A) (A) AND (Rn)
ANL A,direct 0 1 0 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

55
Byte 2
(A) (A) AND (direct)
ANL A,@Ri 0 1 0 1 0 1 1 I 56 57 (A) (A) AND ((Ri))
ANL A,#data 0 1 0 1 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

54
Byte 2
(A) (A) AND #data
ANL direct, A 0 1 0 1 0 0 1 0
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

52
Byte 2
(direct)(direct) and (A)
ANL direct, #data 0 1 0 1 0 0 1 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

53
Byte 2
Byte 3
(direct)(direct) and
#data
ORL A, Rn 0 1 0 0 1 n
2
n
1
n
0
48 4F (A) (A) OR (Rn)
ORL A, direct 0 1 0 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

45
Byte 2
(A) (A) OR (direct)
ORL A, @Ri 0 1 0 0 0 1 1 I 46 47 (A) (A) OR ((Ri))
ORL A, #data 0 1 0 0 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

44
Byte 2
(A) (A) OR #data
ORL direct, A 0 1 0 0 0 0 1 0
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

42
Byte 2
(direct)(direct) OR (A)
ORL direct, #data 0 1 0 0 0 0 1 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

43
Byte 2
(direct)(direct) OR
#data
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0
Byte 3
XRL A, Rn 0 1 1 0 1 n
2
n
1
n
0
68 6F (A) (A) XOR (Rn)
XRL A, direct 0 1 1 0 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

65
Byte 2
(A) (A) XOR (direct)
XRL A, @Ri 0 1 1 0 0 1 1 I 66 67 (A) (A) XOR ((Ri))
XRL A, #data 0 1 1 0 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

64
Byte 2
(A) (A) XOR #data
XRL direct, A 0 1 1 0 0 0 1 0
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

62
Byte 2
(direct)(direct) XOR (A)
XRL direct, #data 0 1 1 0 0 0 1 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0

63
Byte 2
Byte 3
(direct)(direct) XOR
#data
CLR A 1 1 1 0 0 1 0 0 E4 (A) 0
CPL A 1 1 1 1 0 1 0 0 F4 (A) (A) lenh not hay
bu 1
RL A 0 0 1 0 0 0 1 1 23 The contents of the
accumulator are rotated
left by one bit.
RLC A 0 0 1 1 0 0 1 1 33 The contents of the
accumulator and carry
are rotated left by one bit.
RR A 0 0 0 0 0 0 1 1 03 The contents of the
accumulator are rotated
right by one bit.
RRC A 0 0 0 1 0 0 1 1 13 The contents of the
accumulator and carry
are rotated right by one
bit.
SWAP A 1 1 0 0 0 1 0 0 C4 (A
3-0
) (A
7-4
)

Control Transfer Instructions.

Mnemonic
Instruction code Hexa
decimal
Explanation
D
7
D
6
D
5
D
4
D
3
D
2
D
1
D
0

ACALL addr 11 A
10
a
9
a
8
1 0 0 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

Byte 1
Byte 2
(PC) (PC) + 2
(SP) (SP) + 1
((SP)) (PC
7-0
)
(SP) (SP) + 1
((SP)) (PC
15-8
)
(PC) page address
LCALL addr 16 0 0 0 1 0 0 1 0
a
15
a
14
a
13
a
12
a
11
a
10
a
9
a
8

a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

12
Byte 2
Byte 3
(PC) (PC) + 3
(SP) (SP) + 1
((SP)) (PC
7-0
)
(SP) (SP) + 1
((SP)) (PC
15-8
)
(PC) addr
15-0

RET 0 0 1 0 0 0 1 0

22 (PC
15-8
) ((SP))
(SP) (SP) - 1
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'7- . | .
(PC
7-0
) ((SP))
(SP) (SP) - 1
RETI 0 0 1 1 0 0 1 0

32 (PC
15-8
) ((SP))
(SP) (SP) - 1
(PC
7-0
) ((SP))
(SP) (SP) - 1
AJMP addr 11 a
10
a
9
a
8
0 0 0 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

Byte 1
Byte 2
(PC) (PC) + 2
(PC) page address
LJMP addr 16 0 0 0 0 0 0 1 0
a
15
a
14
a
13
a
12
a
11
a
10
a
9
a
8

a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

02
Byte 2
Byte 3
(PC) addr
15-0

SJMP rel 1 0 0 0 0 0 0 0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

80
Byte 2
(PC) (PC) + 2
(PC) (PC) + rel
JMP @A + dptr 0 1 1 1 0 0 1 1 73 (PC) (A) + (dptr)
JZ rel 0 1 1 0 0 0 0 0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

60
Byte 2
(PC) (PC) + 2
IF (A) = 0 then (PC)(PC)
+ rel
JNZ rel 0 1 1 1 0 0 0 0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

70
Byte 2
(PC) (PC) + 2
IF (A) = 0 then (PC)(PC) +
rel
JC rel 0 1 0 0 0 0 0 0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

40
Byte 2
(PC) (PC) + 2
IF (C) = 1 then (PC)(PC) +
rel
JNC rel 0 1 0 1 0 0 0 0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

50
Byte 2
(PC) (PC) + 2
IF (C) = 0 then (PC)(PC) +
rel
JB bit, rel 0 0 1 0 0 0 0 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

20
Byte 2
Byte 3
(PC) (PC) + 3
IF (bit) =1 then (PC)(PC)
+ rel
JNB bit, rel 0 0 1 1 0 0 0 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

30
Byte 2
Byte 3
(PC) (PC) + 3
IF (bit) = 0 then (PC)(PC)
+ rel
JBC bit, rel 0 0 0 1 0 0 0 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

10
Byte 2
Byte 3
(PC) (PC) + 3
IF (bit)= 1 then (bit) 0
(PC)(PC) + rel
CJNE A, direct, rel 1 0 1 1 0 1 0 1
a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0


r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

B5
Byte 2
Byte 3
(PC) (PC) + 3
IF (direct) < (A) then (C)0
and
(PC)(PC) + rel
IF (direct) > (A) then (C)1
and
(PC)(PC) + rel
CJNE A, #data, rel 1 0 1 1 0 1 0 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0


r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

B4
Byte 2
Byte 3
(PC) (PC) + 3
IF #data < (A) then (C)0
and
(PC)(PC) + rel
IF #data > (A) then (C)1
and
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '7
(PC)(PC) + rel
CJNE Rn,#data, rel 1 0 1 1 0 n
2
n
1
n
0

d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0


r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

B8 BF
Byte 2
Byte 3
(PC) (PC) + 3
IF #data < (Rn) then (C)0
and
(PC)(PC) + rel
IF #data > (Rn) then (C)1
and
(PC)(PC) + rel
CJNE @Ri,#data,
rel
1 0 1 1 0 1 1 0
d
7
d
6
d
5
d
4
d
3
d
2
d
1
d
0


r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

B6 B7
Byte 2
Byte 3
(PC) (PC) + 3
IF #data <((Ri)) then (C)0
and
(PC)(PC) + rel
IF #data >((Ri)) then (C)1
and
(PC)(PC) + rel
DJNZ Rn, rel 1 1 0 1 1 n
2
n
1
n
0
r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

D8 DF
Byte 2
(PC) (PC) + 2
(Rn) (Rn) 1
IF ((Rn)) = 0 then
(PC)(PC) + rel
DJNZ direct, rel 1 1 0 1 0 1 0 1

a
7
a
6
a
5
a
4
a
3
a
2
a
1
a
0

r
7
r
6
r
5
r
4
r
3
r
2
r
1
r
0

D5
Byte 2
Byte 3
(PC) (PC) + 3
(direct) (direct) 1
IF (direct) = 0 then
(PC)(PC) + rel
NOP 0 0 0 0 0 0 0 0 00 (PC) (PC) + 1

Bit Oriented Instructions.

Mnemonic
Instruction code Hexa
decimal
Explanation
D
7
D
6
D
5
D
4
D
3
D
2
D
1
D
0

CLR C 1 1 0 0 0 0 1 1 C3 (C) 0
CLR bit 1 1 0 0 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

C2
Byte 2
(bit) 0

SETB C 1 1 0 1 0 0 1 1 D3 (C) 1
SETB bit 1 1 0 1 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

D2
Byte 2
(bit) 1

CPL C 1 0 1 1 0 0 1 1 B3 (C) (C)
CPL bit 1 0 1 1 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

B2
Byte 2

(bit) ( bit )
ANL C,bit 1 0 0 0 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

82
Byte 2
(C) (C) AND (bit)
ANL C,/bit 1 0 1 1 0 0 0 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

B0
Byte 2

(C) (C) AND ( bit )
ORL C,bit 0 1 1 1 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

72
Byte 2
(C) (C) OR (bit)
ORL C,/bit 1 0 1 0 0 0 0 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

A0
Byte 2

(C) (C) OR ( bit )
MOV C,bit 1 0 1 0 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

A2
Byte 2
(C) (bit)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'7 . | .
MOV bit,C 1 0 0 1 0 0 1 0
b
7
b
6
b
5
b
4
b
3
b
2
b
1
b
0

92
Byte 2
(bit) (C)
8ong 4-5. 1cm tot top |enh v| d|e u kh|en MC55.



V. 80 |NH 1H0| 1|MEk 1k0NC V| |EU KH|EN MC55
' 3 :|-|
Trong vi ieu khien MCS51 co 2 timer/counter T0 va T1, con MCS52 th co 3 timer / counter.
Cac timer hay counter ch la mot va chnh la bo em co chc nang em xung.
Neu ta s dung che o timer th thi gian nh thi nhan vi chu ky cua moi xung se tao ra
lng thi gian can thiet che o timer vi ieu khien thng em xung noi lay t mach dao
ong ben trong vi ieu khien co chu ky on nh. Che o timer dung e nh thi gian chnh xac e
ieu khien cac thiet b theo thi gian.
Neu chung ta s dung che o counter th ta ch can quan tam en so lng xung em c
khong can quan tam en chu ky cua xung em. Che o counter thng th em xung nhan t ben
ngoai a en ngo vao T0 oi vi timer/counter th 0 va ngo vao T1 oi vi timer/counter th 1.
em xung t ben ngoai con goi la em s kien. Mot ng dung cho che o counter la co the s
dung vi ieu khien lam cac mach em san pham.
en ay ta co the xem timer hay counter la 1 va chu y rang tai moi mot thi iem ta ch s
dung mot trong 2 hoac la timer hoac la counter.
Cac timer / counter cua vi ieu khien s dung 16 flip flop nen ta goi la timer/ counter 16 bit
va so lng xung ma timer/ counter co the em c tnh theo so nh phan bat au t 0000 0000
0000 0000
2
en 1111 1111 1111 1111
2
, neu viet theo so thap luc phan th bat au t 0000H en
FFFFH va neu tnh theo gia tr thap phan th bat au t 0 en 65535.
Khi at en gia tr cc ai va neu co them 1 xung na th bo em se b tran, khi b tran th
gia tr em se t ong ve 0 (gio ng nh mach em nh phan 4 bit khi em len 1111 va ne u co 1
xung na th gia tr em ve 0000) va c tran cua timer/counter len 1 e bao hieu timer/counter a
b tran (trc khi em th phai xoa c tran).
Ngi lap trnh s du ng trang thai c tran len 1 e re nhanh hoat cham dt thi gian can thiet
a nh e chuyen sang lam mot cong viec khac. Va khi c tran len 1 se tao ra ngat cung e re
nhanh chng trnh e thc hien mot chng trnh khac ban se nam ro phan ng dung.
Cac gia tr em c cua timer/counter T0 th lu trong 2 thanh ghi TH0 va TL0 moi thanh
ghi 8 bit ket hp lai thanh 16 bit.
Tng t, cac gia tr em c cua timer/counter T1 th lu trong 2 thanh ghi TH1 va TL1
moi thanh ghi 8 bit ket hp lai thanh 16 bit.
Ngoai cac thanh ghi lu tr so xung em va gii thieu th con co 2 thanh ghi ho tr kem
theo: thanh ghi TMOD va thanh ghi TCON dung e thiet lap nhieu che o hoat o ng khac nhau
cho timer e ap ng c s a dang cac yeu cau ng dung trong thc te.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '77
Bang 4-6 se liet ke te n, chc nang, a ch cua cac thanh ghi lien quan en cac timer/counter
cua vi ieu khien 89C51.

Ten Chc nang a ch Cho phep truy xuat bit
TCON Control 88H YES
TMOD Mode 89H NO
TL0 Timer 0 low-byte 8AH NO
TL1 Timer 1 low-byte 8BH NO
TH0 Timer 0 high-byte 8CH NO
TH1 Timer 1 high-byte 8DH NO
8ong 4-5. Coc thonh gh| t|mer v| d|e u kh|en MC55.
|~|| | || -| ~ .- | :- \- --:-
Thanh ghi tmod gom hai nhom 4 bit: 4 bit thap dung e thiet lap cac che o hoat ong cho
Timer 0 va 4 bit cao thiet lap cac che o hoat ong cho Timer 1.

Cac bit cua thanh ghi TMOD c tom tat nh bang 4-6 :
8|t 1en 1|mer Chuc nong
7 CA1E Neu CA1E = th| 1|mer ch| |om v|e c kh| |N1= .
6 C/1 8|t |uo chcn ccunter hoy t|mer:
C/1 = : dem xung tu be n ngco| duo den ngc voc 1.
C/1 = 0 : d|nh thc| dem xung nc| be n trcng.
5 M 8|t chc n mcde cuo 1|mer .
4 M0 8|t chc n mcde cuo 1|mer .
3 CA1E 0 Neu CA1E = th| 1|mer 0 ch| |om v|ec kh| |N1= .
2 C/1 0 8|t |uo chcn ccunter hoy t|mer: g|cng nhu tren.
M 0 8|t chc n mcde cuo 1|mer 0.
0 M0 0 8|t chc n mcde cuo 1|mer 0.
8ong 4-6. Coc b|t trcng thonh gh| 1M0D.
Hai bit M0 va M1 tao ra 4 trang thai tng ng vi 4 kieu lam viec khac nhau cua Timer 0
hoac cua Timer 1 nh bang 4-7.
M M0 K|eu Chuc nong
0 0 0 Mcde 1|mer 3 b|t (mcde 8048|
0 Mcde 1|mer 6 b|t
0 2 Mcde tu dc ng nop 8 b|t
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'7` . | .
3
Mcde to ch 1|mer ro :
1|mer0 : ducc to ch ro |om 2 t|mer 8 b|t gcm cc:
1|mer 8 b|t 1L0 duc c d|eu kh|en bc| co c b|t cuo mcde 1|mer0.
1|mer 8 b|t 1H0 ducc d|e u kh|en bc| coc b|t cuo mcde 1|mer.
1|mer : khc ng ducc hcot dcng c mcde 3.
8ong 4-7. Coc b|t chcn mcde trcng thonh gh| 1M0D.
|~|| | -| |- | :- |: --:-
Thanh ghi ieu khien tcon cha cac bit trang thai va cac bit ieu khien cho Timer0 va
Timer1.

Hoat ong cua tng bit cua thanh ghi tcon c tom tat nh bang 4-8:

8|t K| h|eu |o ch| Chuc nong
7 1F 8FH Cc tro n 1|mer : 1F = kh| t|mer b| tron vo cc the xco
bong phon mem hcoc kh| v| d|eu kh|en thuc h|e n
chucng tr|nh ccn phu c vu ngot t|mer th| tu dcng xco
|ucn cc tron 1F.
6 1k 8EH 8|t d|eu kh|en 1|mer dem / ngu ng dem:
1k = th| t|mer duc c phep dem xung.
1k = 0 th| t|mer khcng duc c phep dem xung (ngung|.
5 1F0 8DH Cc tron 1|mer 0 (hcot dcng tucng tu 1F|
4 1k0 8CH 8|t d|eu kh|en 1|mer 0 (g|cng 1k|
3 |E 88H Cc boc ngot cuo ngot |N1. Kh| cc ngot xoy ro c ngc voc
|N1 (conh xucng| th| cc |E to c dcng |en muc .
Kh| v| d|eu kh|en thuc h|e n chucng tr|nh ccn phu c vu ngot
|N1 th| tu dcng xco |uc n cc boc ngot |E.
2 |1 8AH 8|t d|eu kh|en chc phep ngot |N1 to c dcng bong mu c
hoy bong conh. |1 = 0 th| ngot |N1 toc dc ng bong muc.
|1 = th| ngot |N1 toc dcng bong conh xucng.
|E0 8H C|cng nhu |E nhung phuc vu chc ngot |N10
0 |10 88H C|cng nhu |1 nhung phuc vu chc ngot |N10
8ong 4-8. Coc b|t trcng thonh gh| 1C0N.
- ~ -| |~: | |~ :- .~ :~|
MCS51 co 2 timer la timer0 va timer1. Ta dung ky hie u TLx va Thx e ch 2 thanh ghi byte
thap va byte cao cua Timer0 hoac Timer1. Nh a trnh bay tren cac timer co 4 kieu hoat ong,
phan nay ta se khao sat chi tiet cac kieu hoat ong cua timer.
1. MODE 0 (Mode Timer 13 bit) :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '73
Mode 0 la mode em 13 bit: trong o 8 bit cao s dung het 8 bit cua thanh ghi Thx, 5 bit con
lai ch s dung 5 bit trong so thap cua thanh ghi TLx, con 3 bit cao cua TLx khong du ng nh hnh
4-11.
mode nay gia tr em ln nhat la 2
13
= 8192 tc em t 0 0000 0000 0000
2
en 1 1111
1111 1111
2
va neu co them mot xung na th bo em se tran va lam cho c tran len 1. Neu trong
chng trnh co cho phep timer ngat th ngat se tac ong.

H|nh 4-. 1|mer hcot dcng c mcde 0.
2. MODE 1 (Mode Timer 16 bit) :
Mode 1 la mode em 16 bit. mode nay gia tr em la ln nhat la 2
16
. Timer hoat ong
mode 1 giong nh mode 0 ch khac la mode 1 em 16bit.
3. MODE 2 (Mode Timer t ong nap 8 bit) :
Mode 2 la mode t ong nap 8 bit, byte thap TLx cua Timer hoat ong nh mot Timer 8 bit
trong khi byte cao THx cua Timer dung e lu tr gia tr e nap lai cho thanh ghi TLx.
Khi bo em TLx chuyen trang thai t FFH sang 00H: th c tran c set va gia tr lu trong
THx c nap vao TLx. Bo em TLx tiep tuc em t gia tr va nap t THx len va cho e n khi co
chuyen trang thai t FFH sang 00H ke tiep va c the tiep tuc. S o minh hoa cho timer hoat ong
mode 2 nh hnh 4-12.

H|nh 4-2. 1|mer hcot dc ng c mcde 2.
4. MODE 3 (Mode Timer tach ra) :
Mode 3 la mode Timer 0 tach ra lam 2 timer cung vi timer 1 tao thanh 3 timer.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .
Khi Timer0 nh cau hnh mode 3 th timer0 c chia la 2 timer 8 bit TL0 va TH0 hoat
ong nh nh ng Timer rieng le va s dung cac bit TF0 va TF1 lam cac bit c tran tng ng nh
hnh 4-13.
Timer 1 khong the s dung mode 3, nhng co the c khi ong trong cac mode khac va
khong the bao tran v c tran TF1 a du ng e bao tran cho timer TH0.
Khi timer 0 hoat ong Mode 3 se cung cap them 1 Timer 8 bit th ba. Khi Timer 0 mode
3 th Timer 1 co the hoat ong nh la mot bo dao ong thiet lap toc o Baud phuc vu cho Port noi
tiep e truyen va nhan d lieu, hoac no co the dung trong cac ng dung ma khong s dung che o
bao tran va bao ngat. S o minh hoa cho timer hoat ong mode 3 nh hnh 4-13.

H|nh 4-3. 1|mer 0 hcot dcng c mcde 3.
~ ||| || -
Timer/counter co the em xung t hai nguon: neu timer/counter s dung che o nh thi
timer th se em xung ben trong (xung noi) a biet tan so, neu timer/counter s dung che o
counter th se em xung t ben ngoai nh hnh 4-14. Bit C/T trong TMOD cho phep chon che o
timer hay counter khi khi tao thanh ghi tmod.

H|nh 4-4. Coc ngucn xung duo de n t|mer / ccunter.
1. em thi gian:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '`'
Neu bit C/T = 0 th Timer hoat ong em noi xung lien tuc lay t dao ong tre n Chip. Tan
so ngo vao tu thach anh c a qua mot mach chia 12 e giam tan so phu hp vi cac ng dung.
Neu du ng thach anh 12MHz th sau khi qua bo chia 12 tan so a en bo em timer la 1MHz.
Timer se sinh ra tran khi no a em u so xung tng ng thi gian qui nh, phu thuoc vao
gia tr khi tao c nap vao cac thanh ghi THx va TLx.
2. em cac s kien ben ngoai (Event Counting):
Neu bit C/T = 1 th Timer hoat ong em xung en t be n ngoai va chu ky cua moi xung do
nguon tao tn hieu ben ngoai quyet nh. Hoat o ng nay thng du ng e em cac s kien. So
lng cac s kien c lu tr trong thanh ghi cua cac Timer.
Nguon xung clock ben ngoai a vao chan P3.4 la ngo nhap xung clock cua Timer0 (T0) va
P3.5 la ngo nhap xung clock cua bi Timer1 (T1).
Trong cac ng dung em xung t ben ngoai: cac thanh ghi Timer se tang gia tr em khi
xung ngo vao Tx chuyen trang thai t 1 sang 0 (tac ong xung clock canh xuong). Ngo vao nhan
xung ben ngoai c lay mau trong suot khoang thi gian S5P2 cua moi chu ky may, do o khi
xung mc H (1) trong mot chu ky nay va chuyen sang mc L (0) trong mot chu ky ke th bo em
tang len mot. e nhan ra s chuyen oi t 1 sang 0 phai mat 2 chu ky may, nen tan so xung ben
ngoai ln nhat la 500KHz neu he thong vi ieu khien s dung dao o ng thach anh 12 MHz.
-| |-| ~ :- |~: |
Bit TRx trong thanh ghi TCON c ieu khien bi phan mem e cho phep cac Timer bat
au qua trnh em hoac ngng.
e bat au cho cac Timer em th phai set bit TRx bang lenh:
SETB TR0 ; cho phep timer T0 bat au em
SETB TR1 ; cho phep timer T1 bat au em
e cac Timer ngng em ta dung lenh Clear bit TRx.
V du Timer 0 bat au bi lenh SETB TR0 va ng ng em bi lenh CLR TR0.
Bit TRx b xoa khi reset he thong, do o che o mac nh khi m may ca c Timer b cam.
Mot phng phap khac e ie u khie n cac Timer la dung bit GATE trong thanh ghi TMOD va
ngo nhap ben ngoai INTx nh hnh 4-15. Phng phap nay c du ng e o cac o rong xung.
Gia s xung can o o rong a vao chan INT0, ta phai khi tao Timer 0 hoat o ng mode
1 la mode Timer 16 bit vi gia tr khi tao ban au la TL0/TH0 = 0000H, bit GATE = 1, bit TR0 =
1. Khi xung a en ngo vao INT0 = 1 th cong c m e cho xung noi co tan so 1MHz vao
timer 0. Qua trnh timer 0 em xung noi se dng lai cho e n khi xung a en ngo vao INT0
xuong mc 0. Thi gian em c cua timer 0 chnh la o rong xung can o.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .

H|nh 4-5. c dc rcng xung tu be n ngco|.
7 | :~ .~ :|. |~: ~ :|~|| | |~ \/
Cac Timer thng c khi tao 1 lan au chng trnh e thiet lap mode hoat ong phuc
vu cho cac ng dung ieu khien lien quan en nh thi hay em xung ngoai. Tuy thuoc vao yeu
cau ieu khien cu the ma ta ieu khien cac timer bat au em, ngng hay khi o ng em lai t
au
Thanh ghi TMOD la thanh ghi au tien can phai khi tao e thiet lap mode hoat o ng cho
cac Timer. V du khi ong cho Timer0 hoat ong mode 1 (mode Timer 16 bit) va hoat ong
nh thi em xung noi ben trong th ta khi tao bang lenh: MOV TMOD, # 00000001B. Trong
lenh nay M1 = 0, M0 = 1 e vao mode 1 va C/T = 0, GATE = 0 e cho phep em xung noi ben
trong ong thi xoa cac bit mode cua Timer 1. Sau lenh tren Timer 0 van cha em va timer 0 ch
em khi set bit ieu khien chay TR0.
Neu ta khong thiet lap cac gia tr bat au em cho cac thanh ghi TLx/THx th Timer se bat
au em t 0000H len va khi chuyen trang thai t FFFFH sang 0000H se sinh ra tran lam cho bit
TFx = 1 roi tiep tuc em t 0000H le n tiep . . .
Neu ta thiet lap gia tr bat au em cho TLx/THx khac 0000H, th Timer se bat au em t
gia tr thiet lap o len nhng khi chuye n trang thai t FFFFH sang 0000H th timer lai em t
0000H le n.
e timer luon bat au em t gia tr ta gan th ta co the lap trnh ch sau moi lan tran ta se
xoa c TFx va gan lai gia tr cho TLx/THx e Timer luo n luon bat au em t gia tr khi gan len.
ac biet neu bo nh thi hoat ong trong pham vi nho hn 256 us th ta nen dung Timer
mode 2 (t ong nap 8 bit). Sau khi khi tao gia tr au cho thanh ghi THx, va TLx, khi set bit TRx
th Timer se bat au em t gia tr a gan trong TLX va khi tran t FFH sang 00H trong TLx, th
c tran TFx t ong c set, ong thi gia tr trong Thx t ong nap sang cho TLx va Timer bat
au em t gia tr khi gan nay len. Noi cach khac, sau moi lan tran ta khong can khi gan lai cho
cac thanh ghi Timer ma chung van em c lai t gia tr a gan.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '`
V du 1: Chng trnh tao xung vuong tan so 1kHz s dung timer mode1:
mov tmod,#01h ;chon mode 1 timer 0 em 16 bit
loop1: mov th0,#0feh ;o rong xung 500us
mov tl0,#0ch ;
setb tr0 ;cho timer bat au em
loop: jnb tf0,loop ;ch bao ngat
clr tf0 ;xoa c ngat
cpl p1.0 ;nghch ao bit p1.0
sjmp loop1 ;quay tr lai lam tiep

V du 2: Chng trnh tao xung vuong tan so 10 kHz s dung timer mode2:
mov tmod,#02h ;chon mode 2 che o t ong nap lai 8 bit
loop1: mov th0,#-50 ;tao o rong xung 50us
setb tr0 ;cho timer bat au em
loop: jnb tf0,loop ;ch bao ngat
clr tf0 ;xoa c ngat
cpl p1.0 ;nghch ao bit p1.0
sjmp loop1 ;tro lai loop1
` \/ |~ . -| |-| | \
Ho vi ieu khien MCS52 co 3 timer T0, T1, T2. Cac timer T0 va T1 co cac thanh ghi va hoat
ong giong nh ho 51. ay ch trnh bay them phan hoat ong cua timer T2.
Cac thanh ghi cua timer/counter T2 bao gom: thanh ghi TL2, TH2, thanh ghi ieu khie n
T2CON, thanh ghi RCAP2L va RCAP2H.
Timer/counter T2 co the dung e nh thi timer hoac dung nh bo em counter e em
xung ngoai a en ngo vao T2 chnh la chan P1.0 cua port 1 nh hnh 4-16.
Timer/counter T2 co 3 kieu hoat ong: t ong nap lai, thu nhan va thiet lap toc o baud e
phuc vu cho truyen d lieu.
Chc nang cua thanh ghi ieu khien T2CON nh bang 4-9:

Bit K hieu a ch Chc nang
7 TF2 CFH C tran Timer 2: hoat ong giong nh cac timer tren (TF2 se
khong c thiet lap len mc 1 neu bit TCLK hoac RCLK
mc 1).
6 EXF2 CEH C ngoai cua timer T2: ch c set khi xay ra s thu nhan hoac
nap lai d lieu bi s chuyen trang thai t 1 sang 0 ngo vao
T2EX va EXEN2 = 1; khi cho phep timer T2 ngat, EXF2=1 th
CPU se thc hien chng trnh con phuc vu ngat Timer T2, bit
EXF2 co the bang phan mem.
5 RCLK CDH Xung clock thu cua timer 2. Khi RCLK=1 th timer T2 cung cap
toc o baud cho port noi tiep e nhan d lieu ve va timer T1 se
cung cap toc o baud cho port noi tiep e phat d lieu i.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'`- . | .
4 TCLK CCH Xung clock phat cua timer 2. Khi TCLK=1 th timer T2 cung cap
toc o baud cho port noi tiep e phat d lieu i va timer T1 se
cung cap toc o baud cho port noi tiep e nhan d lieu ve .
3 EXEN2 CBH Bit ieu khien cho phep tac ong t ben ngoai. Khi EXEN2 = 1
th hoat ong thu nhan va nap lai cua timer T2 ch xay ra khi ngo
vao T2EX co s chuyen trang thai t 1 sang 0.
2 TR2 CAH Bit ieu khien Timer 1 em / ngng em:
TR2 = 1 th timer 1 c phep em xung.
TR2 = 0 th timer 1 khong c phep em xung (ngng).
Dung lenh ie u khien bit TR2 e cho phe p timer1 em hay
ngng em.
1 C/T2 C9H Bit la chon counter hay timer:
C/T2 = 1 : em xung t ben ngoai a en ngo vao T2.
C/T2 = 0 : nh thi em xung noi be n trong.
0 CP/RL2 C8H C thu nhan/nap lai d lieu cua timer T2.
Khi bit nay = 1 th thu nhan ch xay ra khi co s chuyen trang
thai t 1 sang 0 ngo vao T2EX va EXEN2=1; khi bit nay = 0
th qua trnh t ong nap lai khi timer T2 tran hoac khi co s
chuyen trang thai ngo vao T2EX va bit EXEN2 = 1; neu bit
RCLK hoac TCLK = 1 th bit nay xem nh bo.
8ong 4-. Coc b|t trcng thonh gh| 12C0N.

H|nh 4-6. 5c dc cho n cuo 8C52 vc| ngc voc 12 |o P.0 vo 12EX |o P. .
a. Che o t ong nap lai:
Bit thu nhan/nap lai 2 / RL CP la chon mot trong hai che o: t ong nap lai va thu nhan.
Khi 0 2 / = RL CP th timer hoat ong che o t ong nap lai: cac thanh ghi TL2, TH2 se lu tr
so xung em con 2 thanh ghi RCAP2L va RCAP2H lu tr gia tr e nap lai cho TL2, TH2. Gia tr
lu va nap lai la 16 bit.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '`
Khi timer em tran th lam cho c bao tran TF2 bang 1 ong thi t o ng thc hien nap lai
d lieu.
Tng t neu bit EXEN2 = 1 th che o t ong nap lai xay ra khi co s chuyen trang thai t
1 sang 0 ngo vao T2EX ong thi lam cho bit EXF2 = 1. Tng t nh c TF2 th c EXF2 cung
co the c kiem tra bang phan mem hoac tao nga t. Bit EXF2 phai xoa bang phan mem. Hoat
ong t nap cua timer T2 c trnh bay nh hnh 4-17.


H|nh 4-7.Hcot dcng cuo t|mer 12 c che dc tu dcng nop |o|.
b. Che o thu nhan:
Khi 1 2 / = RL CP th timer hoat ong che o thu nhan. Khi o timer T2 hoat ong bnh
thng nh mot timer/counter 16 bit, thanh ghi TL2, TH2 se lu tr xung em va neu co s chuyen
trang thai t FFFFH sang 0000H th se sinh ra tran va lam cho c tran TF2=1. Bit c tran co the
kiem tra bang phan mem hay co the tao ra ngat.

H|nh 4-8. Hcot dc ng cuo t|mer 12 c che dc 1hu nho n du ||eu.
e cho phep che o thu nhan hoat ong th lam cho bit EXEN2 = 1. Neu bit EXEN2 = 1 va
khi co s chuyen trang trai t 1 sang 0 ngo vao T2EX th che o thu nhan se xay ra: gia tr em
c trong thanh ghi TL2, TH2 se c chuyen sang 2 thanh ghi RCAP2L va RCAP2H. C EXF2
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'` . | .
cung c chuyen len mc 1 e bao hieu qua trnh thu nhan a xay ra, c EXF2 co the kiem tra
bang phan mem hoac tao ngat.
Hoat ong thu nhan d lieu cua timer T2 c trnh bay hnh 4-18.
V|. H0A1 0NC 1kUEN DU L|EU CUA V| |EU KH|EN MC55
' 3 :|-|
Truyen d lieu noi tiep cua MCS51 co the hoat ong nhieu kieu rieng biet trong pham vi
cho phep cua tan so. D lieu dang song song c chuyen thanh noi tiep e truyen i va d lieu
nhan ve dang noi tiep c chuyen thanh song song.
Chan TxD (P3.1) la ngo xuat d lieu i va chan RxD (P3.0) la ngo nhan d lieu ve.
ac trng cua truyen d lieu noi tiep la hoat ong song cong co ngha la co the thc hie n
truyen va nhan d lieu cung mot luc.
Hai thanh ghi chc nang ac biet phuc vu cho truye n d lieu la thanh ghi em SBUF va
thanh ghi ieu khien SCON. Thanh ghi em sbuf nam a ch 99H co 2 chc nang: neu vi ieu
khien ghi d lieu le n thanh ghi sbuf th d lieu o se c truye n i, neu he thong khac gi d lieu
en th se c lu vao thanh ghi em sbuf. S o khoi cua he thong truye n d lieu nh hnh 4-19.

H|nh 4-. 5c dc khc| cuo truyen du ||e u nc| t|ep.
Thanh ghi ieu khien truyen d lieu SCON nam a ch 98H la thanh ghi cho phep truy suat
bit bao gom cac bit trang thai va cac bit ieu khien. Cac bit ieu khien dung e thiet lap nhieu
kieu hoat ong truyen d lieu khac nhau, con cac bit trang thai cho biet thi iem ket thuc khi
truyen xong mot k t hoac nhan xong mot k t. Cac bit trang thai co the c kiem tra trong
chng trnh hoac co the lap trnh e sinh ra ngat.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '`7
Tan so hoat ong cua truyen d lieu noi tiep co n goi toc o BAUD (so lng bit d lieu c
truyen i trong mot giay) co the hoat ong co nh (s dung dao ong tren chip) hoac co the thay
oi. Khi can toc o Baud thay oi th phai s dung Timer 1 e tao toc o baud.
|~|| | -| |- | :|.-| | -| | :-:
Thanh ghi scon se thiet lap cac kieu hoat o ng truyen d lieu khac nhau cho MCS51. Cau
truc cua thanh ghi Scon nh sau:

Bang 4-10 tom tat thanh ghi ieu khien Port noi tiep scon nh sau :

Bit Ky hieu a ch Mo ta hoat ong
7 SM0 9FH Bit chon kieu truye n noi tiep: bit th 0.
6 SM1 9EH Bit chon kieu truye n noi tiep: bit th 1.
5 SM2 9DH Bit cho phep truyen ket noi nhie u vi x ly mode 2 va 3; RI
se khong tch cc neu bit th 9 a thu vao la 0.
4 REN 9CH Bit cho phep nhan k t. REN = 1 se cho phep nhan k t.
3 TB8 9BH Dung e lu bit 9 e truyen i khi hoat ong mode 2 va 3.
TB8 bang 0 hay 1 la do ngi lap trnh thiet lap.
2 RB8 9AH Dung e lu bit 9 nhan ve khi hoat o ng mode 2 va 3.
1 TI 99H C bao hieu nay len mc 1 khi truyen xong 1 k t va xoa bi
ngi lap trnh e san sang truyen k t tiep theo.
0 RI 98H

C bao hieu nay le n mc 1 khi nhan xong 1 k t va xoa bi
ngi lap trnh e san sang nhan k t d lieu tiep theo.
8ong 4-0. Coc b|t trcng thonh gh| d|e u kh|en truyen du ||eu.
~ -| :|.-| | -| | :-:
Trc khi truyen d lieu th thanh ghi SCON phai c khi tao ung kieu. V du e khi tao
truyen d lieu kieu 1 th 2 bit: SM0 SM1 = 01, bit cho phep thu: REN =1, va c nga t truyen TI = 1
e san sang truyen, ta dung lenh sau : MOV SCON, # 01010010b.
Truyen d lieu noi tiep cua MCS51 co 4 kieu hoat ong tuy thuoc theo 4 trang thai cua 2 bit
SM0 SM1 c liet ke bang 4-11.
Ba trong bo n kieu cho phep truyen ong bo vi moi k t thu hoac phat se c ket hp vi
bit Start hoac bit Stop.

5M0 5M K|eu Mc to 1cc dc boud
0 0 0 1honh gh| d|ch Cc d|nh (to n sc doc dcng f/2|.
0 UAk1 8 b|t 1hoy dc| (duc c dot bc| 1|mer|.
0 2 UAk1 b|t Cc d|nh (to n sc doc dcng f/2 cr f/64|
3 UAk1 b|t 1hoy dc| (duc c dot bc| 1|mer|.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'`` . | .
8ong 4-. Coc k|eu truyen du ||e u.
a. Truyen d lieu kieu 0 kieu thanh ghi dch 8 bit :
e nh cau hnh cho truyen d lieu noi tiep kieu 0 th 2 bit SM1 SM0 = 00. D lieu noi
tiep nhan vao va d lieu truyen i eu thong qua chan RxD, con chan TxD th dung e dch
chuyen xung clock. 8 bit d lieu e truye n i hoac nhan ve th luon bat au vi bit co tro ng so nho
nhat LSB. Toc o Baud c thiet lap co nh tan so bang
1
12
tan so dao ong thach anh tren
Chip.
Khi thc hien lenh ghi d lieu len thanh ghi sbuf th qua trnh truye n d lieu bat au. D lieu
c dch ra ngoai thong qua chan RxD cung vi cac xung nhp cung c gi ra ngoai thong qua
chan TxD. Moi bit truye n i ch co xuat hien tren chan RxD trong khoang thi gian mot chu ky
may. Trong khoang thi gian cua moi chu ky may, tn hieu xung clock xuong mc thap tai thi
iem S3P1 va len mc cao tai thi iem S6P1 trong gian o thi gian hnh 4-20.
Qua trnh nhan c khi ong khi bit cho phep nhan REN = 1 va c nhan RI = 0. Nguyen
tac chung la khi tao bit REN = 1 au chng trnh e khi ong truyen d lieu, va xoa bit RI e
san sang nhan d lieu vao. Khi bit RI b xoa, cac xung clock se xuat ra ben ngoai thong qua chan
TxD, bat au chu ky may ke tiep th d lieu t ben ngoai se c dch vao ben trong thong qua
chan RxD.


H|nh 4-20. C|on dc thc| g|on.
Bieu o thi gian cua d lieu noi tiep truyen vao vi ie u khien kieu 0 nh sau :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '`3







H|nh 4-2. C|on dc thc| g|on truyen du ||eu mcd 0.
Mot ng dung cu the s dung mode 0 la dung e m rong them so lng ngo ra cho MCS51
vi cach thc thc hien nh sau: mot thanh ghi dch t noi tiep thanh song song c noi e n cac
ng TxD va RxD cua MCS51 e m rong them 8 ng ra nh hnh 4-22. Neu dung them nhieu
thanh ghi dch mac noi tiep vao thanh ghi dch au tien se m rong c nhie u ngo ra.
8 Extra Output




H|nh 4-22. Mct ung dung k|eu 0 de tong them ngc ro bong thonh gh| d|ch.
b. Truyen d lieu kieu 1 Thu phat bat ong bo 8 bit vi toc o Baud thay o i :
Trong mode nay, truyen d lieu noi tiep hoat o ng bat o ng bo UART 8 bit co toc o Baud
thay oi c. UART la bo thu va phat d lieu noi tiep vi mo i ky t d lieu luon bat au bang 1
bit Start ( mc 0) va ket thuc bang 1 bit Stop ( mc 1), bit parity oi khi c ghe p vao gia bit
d lieu sau cung va bit Stop.
Trong kie u nay, 10 bit d lieu se phat i chan TxD va neu nhan th se nhan chan RxD.
10 bit o bao gom: 1 bit start, 8 bit data (LSB la bit au tien), va 1 bit stop. oi vi hoat ong nhan
d lieu th bit Stop c a vao bit RB8 trong thanh ghi SCON.

85

1XD (P3.|

kXD (P3.0|
C|cck
5h|ft keg|ster
Doto

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'3 . | .
Trong MCS51, toc o Baud c thiet lap bi toc o tran cua Timer T1. oi vi ho 52 co 3
timer th toc o baud co the thiet lap bi toc o tran cua timer T1 hoac timer T2 hoac ca 2 timer T1
va T2: mot timer cho may phat va 1 timer cho may thu.
Nguon cung cap xung clock e ong bo cac thanh ghi truyen d lieu noi tiep hoat ong
kieu 1, 2, 3 c thiet lap bi bo em 16 nh hnh 4-23, ngo ra cua bo em la xung clock tao toc
o baud. Xung ngo vao cua bo em co the lap trnh bang phan mem.

H|nh 4-23. Cung cop xung chc truyen du ||e u nc| t|ep.
Khi co mot lenh ghi d lieu len thanh ghi sbuf th qua trnh truyen d lieu bat au nhng no
cha truyen ma ch cho en khi bo chia 16 (cung cap toc o Baud cho truye n d lieu noi tiep) b
tran. D lieu c xuat ra tren chan TxD bat au vi bit start theo sau la 8 bit data va sau cung la
bit stop. Cac c phat TI c nang le n mc 1 cung luc vi thi iem xuat hien bit Stop tren chan
TxD nh hnh 4-24.
Qua trnh nhan d lieu c khi ong khi co s chuyen oi t mc 1 sang mc 0 ngo vao
RxD. Bo em 4 bit c reset ngay lap tc e sap xep bit d lieu ang en t ngo vao RxD. Moi
bit d lieu en c lay mau trang thai em th 8 trong mot chu ky 16 trang thai cua bo em 4
bit.
Khi co s chuyen trang thai t 1 xuong 0 ngo vao RxD cua bo thu th trang thai 0 nay phai
ton tai trong 8 trang thai lien tuc cua bo em 4 bit. Ne u tr ng hp nay kho ng ung th bo thu xem
nh b tac ong bi tn hie u nhie u. Bo thu se reset va tr ve trang thai ngh va ch s chuyen trang
thai tiep theo.


H|nh 4-24. Cc boc phot xcng du ||eu 1|.
Gia s viec kiem tra bit Start la hp le th bit Start se c bo qua va 8 bit data c nhan
vao thanh ghi dch noi tiep.
Khi tat ca 8 bit c ghi vao thanh ghi dch th 3 cong viec sau se c thc hien tiep theo:
- Bit th 9 (bit Stop) c dch vao bit RB8 trong SCON.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3'
- 8 bit data c nap vao thanh ghi SBUF.
- C ngat nhan RI =1.
Tuy nhien, 3 cong viec tren ch xay ra neu hai ieu kie n sau ton tai :
- RI = 0 va
- SM2 = 1 va bit Stop nhan c = 1 hoac SM2 = 0.
c. Truyen d lieu kieu 2 Thu phat bat ong bo 9 bit vi toc o Baud co nh :
Khi SM1 SM0 = 10 th truyen d lieu hoat o ng kieu 2 co toc o Baud co nh. Co 11 bit
c phat hoac thu : 1 bit Start, 8 bit data, 1 bit data th 9 c lap trnh va 1 bit Stop. Khi phat th
bit th 9 c at vao TB8 cua SCON (co the bit parity). Khi thu th bit th 9 c at vao bit RB8
cua thanh ghi SCON. Toc o Baud trong mode 2 bang 1/12 hoac 1/64 tan so dao ong tre n Chip.
d. Truyen d lieu kieu 3 Thu phat bat ong bo 9 bit vi toc o Baud thay o i :
Khi SM1 SM0 = 11 th truyen d lieu hoat ong kieu 3 la kieu UART 9 bit co toc o Baud
thay oi. Kieu 3 tng t kieu 2 ngoai tr toc o Baud c lap trnh va c cung cap bi Timer.
Cac kieu 1, kieu 2 va kieu 3 rat giong nhau, nhng iem khac nhau la toc o Baud (kieu 2 co
nh, kieu 1 va kieu 3 thay oi) va so bit d lieu (kieu 1 co 8 bit, kieu 2 va kieu 3 co 9 bit data).
- | :~ .~ :|. |~: ~ :|~|| | :|.-| | -| | :-:
a. Bit ieu khien cho phep nhan d lieu (Receive Enable) :
e cho phep thu d lieu th chng trnh phai lam cho bit REN = 1 va ieu nay c thc
hien au chng trnh.
Ta co the khi tao cho phep truyen d lieu bang lenh :
Setb ren hoac mov scon, # xxx1xxxxb
b. Bit d lieu th 9:
Bit d lieu th 9 c phat trong kieu 2 va kieu 3 phai c nap vao bit TB8 bang phan
mem co ngha la ngi lap trnh phai thc hien cong viec nay trc khi truyen d lieu i, con bit
d lieu th 9 cua d lieu thu c th t ong at vao trong bit RB8.
Phan mem co the hoac khong oi hoi bit d lieu th 9 tham gia vao qua trnh truye n d lieu
tuy thuoc vao ac tnh cua cac thiet b noi tiep ket noi vi nhau thiet lap ra qui nh. Bit d lieu th
9 ong 1 vai tro quan trong trong truyen tho ng nhie u vi x ly.
c. Bit kiem tra chan le Parity :
Bit th 9 th ng c dung la bit kiem tra chan le. moi chu ky may, bit P trong thanh ghi
trang thai PSW bang 1 hay bang 0 tuy thuoc vao qua trnh kiem tra chan 8 bit d lieu cha trong
thanh ghi A.
V du neu he thong truyen d lieu yeu cau 8 bit data cong them 1 bit kiem tra chan, th cac
lenh sau ay se phat 8 bit trong thanh ghi A cong vi bit kiem tra chan c cong vao bit th 9.
Mov C,P ;chuyen c chan le P sang c C
Mov TB8,C ;chuyen c C sang bit TB8 e chuan b truyen i
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'3 . | .
Mov sbuf,A ;truyen d lieu 8 bit trong A va bit th 9 trong TB8 i.
Neu kiem tra le c yeu cau th cac lenh tren c sa lai la :
Mov C,P ;chuyen c chan le P sang c C
Cpl C ;nghch ao chan thanh le
Mov TB8,C ;chuyen c C sang bit TB8 e chuan b truyen i
Mov sbuf,A ;truyen d lieu 8 bit trong A va bit th 9 trong TB8 i.
Trong kieu 1 ta van co the s dung bit kiem tra chan le nh sau: 8 bit data c phat trong
kieu 1 co the bao gom 7 bit d lieu, va 1 bit kiem tra chan le. e phat mot ma ASCII 7 bit vi 1 bit
kiem tra chan vao 8 bit, cac lenh sau ay c dung:
MOV C, P ; a Parity chan vao C
MOV ACC.7, C ; a Parity chan vao bit MSB cua A
MOV SBUF, A ; Gi bit data cung bit Parity chan
d. C ngat :
C ngat nhan RI va phat TI trong thanh ghi SCON ong mot vai tro quan trong trong
truyen d lieu cua MCS51. Ca hai bit eu c set bi phan cng nhng phai xoa bi phan mem.
ien hnh la c RI c set mc 1 khi ket thuc qua trnh nhan ay u 1 k t va cho biet
thanh ghi em thu a ay. Trang thai cua c RI co the kiem tra bang phan mem hoac co the lap
trnh e sinh ra ngat. Ne u muo n nhan mot ky t t mot thiet b a c ket noi en Port noi tiep,
th chng trnh phai ch cho en khi c RI = 1, sau o xoa c RI va oc ky t t thanh ghi SBUF.
Qua trnh nay c lap trnh nh sau :
WAIT : JNB RI, WAIT : Kiem tra RI xem co bang 1 hay kho ng. Ch neu = 0
CLR RI : khi c RI = 1 th a nhan xong d lieu va xoa c RI
MOV A, SBUF : oc ky t nhan c t thanh ghi Sbuf
C TI len mc 1 cho biet a phat xong ky t va cho biet thanh ghi em sbuf a ro ng. Neu
muon gi 1 ky t en mot thiet b a c ket noi en Port noi tiep th trc tien phai kiem tra
xem Port noi tiep a san sang cha. Neu ky t trc ang c gi i, th phai ch cho en khi ket
thuc qua trnh gi. Cac lenh sau ay dung e phat mot ky t trong thanh ghi A ra :
WAIT : JNB TI, WAIT : Kiem tra TI co bang 1 hay khong va ch neu bang 0.
CLR TI : Xoa c ngat thu TI
MOV SBUF,A : gi noi dung trong thanh ghi A i
|.-| | -| | :-: :| |- :|| ||-| . -| |-|
Kieu 2 va kieu 3 co mot chc nang ac biet cho viec truye n thong a x ly. cac mode 2 va
3, 9 bit d lieu c thu va bit th 9 c lu vao bit RB8. Truyen d lieu co the lap trnh sao cho
khi thu c bit Stop th ngat cua truyen d lieu noi tiep tac ong ch khi bit RB8 =1. Cau truc nay
c phep bi bang cach set bit SM2 = 1 trong thanh ghi SCON. Kie u nay c ng dung trong
mang s dung nhieu MCS51 c to chc theo cau hnh may chu va may t nh hnh 4-25.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3








H|nh 4-25. Ket nc| nh|eu v| xu |y.
Trong cau hnh ket noi tren th moi mot vi x ly t se co mot a ch duy nhat do chung ta
qui nh.
Khi bo x ly chu muon phat mot khoi d lieu en mot trong cac bo x ly t th trc tien vi
x ly chu phai gi ra 1 byte a ch e nhan dien bo x ly t muo n ket noi.
Byte a ch c phan biet vi byte d lieu bi bit th 9: trong byte a ch th bit th 9
bang 1 va trong byte d lieu th bit th 9 bang 0.
Cac vi x ly t sau khi nhan c byte a ch se biet c vi x ly chu muon giao tiep t
nao. Khi co vi x ly t c phe p th no se xoa bit SM2 e bat au nhan cac byte d lieu tiep theo.
Con cac vi x ly khong c phep th van gi nguyen bit SM2=1 e kho ng nhan cac byte d lieu
truyen gia vi x ly chu va vi x ly t ang c phep. Vi x ly t sau khi ket noi vi vi x ly chu
xong th phai lam cho bit SM2=1 e san sang ket noi cho nh ng lan tiep theo.
Sau khi thc hien xong viec trao oi d lieu th vi x ly muon truy xuat mot vi x ly khac th
phai tao ra mot a ch mi va vi x ly t tng ng vi a ch o c phep va hoat ong giong
nh va trnh bay.
:|.-| | -| | :-:
Truyen d lieu noi tiep neu hoat ong kieu 0 va kieu 2 th co toc o truyen co nh. Trong
kieu 0 th toc o truyen bang
1
12
tan so dao ong tren Chip. Neu s dung thach anh 12 MHz th
toc o truyen cua kieu 0 la 1MHz nh hnh 4-26a.
Trong thanh ghi PCON co mot bit SMOD co chc nang lam tang gap oi toc o baud, mac
nhien sau khi reset he thong th bit SMOD = 0 th cac kieu truyen d lieu hoat ong vi toc o qui
nh, khi bit SMOD = 1 th toc o tang gap oi.
V du trong kieu 2, toc o truyen co the tang gap oi t gia tr mac nh 1/64 tan so dao
ong tren Chip (SMOD = 0) len en 1/32 tan so dao ong tren Chip (ng vi SMOD =1) nh hnh
4-26b.
MA51Ek
85
1XD
P0 P P2 P3
85 5|ove # 2
kXD
32 I/O lines 32 I/O lines
P0 P P2 P3
85 5|ove #
kXD
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'3- . | .
Do thanh ghi PCON khong cho phep truy suat bit nen e set bit SMOD ma khong thay oi
cac bit khac cua thanh ghi PCON th phai thc hien lenh sau.
Lenh sau ay set bit SMOD e tang gap oi toc o truyen:
OR PCON, #1000 0000b ;bit Smod v tr th 7
Cac toc o Baud trong kieu 1 va kieu 3 cua MCS51 c xac nh bi toc o tran cua Timer
1. Bi v Timer hoat ong tan so tng oi cao nen phai chia cho 32 khi bit smod = 0 va chia cho
16 neu SMOD = 1 trc khi cung cap xung clock e thiet lap toc o Baud cho Port noi tiep. Toc o
Baud kieu 1 va 3 cua MCS51 c xac nh bi toc o tran cua Timer 1 hoac Timer 2, hoac ca 2
nh hnh 4-26c.

H|nh 4-26. 1h|et |op tcc dc 8oud.
Thiet lap toc o Baud dung timer 1:
Muon co toc o Baud th ta khi tao thanh ghi TMOD kie u t ong nap 8 bit (kieu 2) va
at gia tr nap lai vao thanh ghi TH1 cua Timer 1 e tao ra toc o tran chnh xac e thiet lap toc o
Baud. Thanh ghi tmod c khi tao e thiet lap toc o baud nh sau:
Mov tmod,#0010xxxxB ;ch quan tam en timer 1
Mot cach kha c e tao to c o baud la nhan tn hieu xung clock t ben ngoai a en ngo vao
T1. Cong thc chung e xac nh toc o Baud trong mode 1 va mode 3 la :


BAUD RATE = TIMER 1 OVERFLOW RATE 32


V du1: truye n d lieu can toc o baud la 1200 th ta tnh toan nh sau:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '3
Toc o tran cua timer 1 bang 1200 x 32 = 38,4KHz. Neu he thong s dung thach anh 12 MHz th
xung cung cap cho Timer 1 em co tan so la 1 MHz hay 1000KHz. Vay e at toc o tran 38,4
KHz th ta tnh c so lng xung em cho moi chu ky tran la 1000 KHZ/38,4 KHz = 26,4 xung
(lam tron bang 26).
Do cac Timer em len va thi iem tran xay ra khi chuyen trang thai em t FFH 00H
nen ta phai nap gia tr bat au t (256 26 = 230) e t gia tr nay timer 1 em len 26 xung na th
sinh ra tran. Gia tr 230 c nap vao thanh ghi TH1 e t ong nap lai cho thanh ghi TL1 khi tran
bang lenh: mov th1,#230. Ban co the khong can phai tnh toan ra gia tr 230 ma co the thay bang
lenh : mov th1,#-26 th trnh bie n dch se tnh cho ban.
Bang tom tat toc o Baud ng vi 2 loai thach anh 12 MHz va 11, 059 MHz :
1cc dc
boud
1on sc
thoch onh
5M0D C|o tr| nop chc 1H 1cc dc thu c 5o| sc
600 2MHz - 7 (FH| 823 7X
2400 2MHz 0 -3 (F3H| 2404 0,6X
200 2MHz 0 -26 (E6H| 202 ~0X
200 ,05MHz -3 (FDH| 200 0X
600 ,05MHz 0 -3 (FDH| 600 0X
2400 ,05MHz 0 -2 (F4H| 2400 0X
200 ,05MHz 0 -24 (E8H| 200 0X
8ong 4-2. 1cm to t tcc dc boud.
V du 2: hay khi tao truyen d lieu noi tiep hoat ong nh UART 8 bit toc o Baud 2400,
dung Timer 1 e tao toc o Baud.
Chng trnh sau se thiet lap ung theo yeu cau e ra:
MOV SCON, # 01010010B : Port noi tiep mode 1.
MOV TMOD, # 20H : Timer 1 mode 2
MOV TH1, # -13 : Nap vao bo em toc o 2400 Baud.
SETB TR1 : Start Timer 1.
Trong thanh ghi SCON co: hai bit SM0 SM1 = 01 thiet lap mode UART 8 bit, bit REN = 1
cho phep san sang nhan d lieu, bit TI = 1 bao cho biet thanh ghi em ro ng san sang cho phep phat
d lieu.
Thanh ghi TMOD co: hai bit M1M0 = 10 e thiet lap Timer 1 mode 2 t ong nap 8 bit.
Lenh setb TR1 cho phep Timer lam viec tao toc o baud.
T toc o Baud 2400 ta tnh c toc o tran cho Timer 1 la 2400 x 32 = 76,8 KHz va gia s
Timer 1 em xung noi tan so 1000 KHz (ng vi thach anh 12 MHz).
Vay e at toc o tran 76,8 KHz th ta tnh c so lng xung em cho moi chu ky tran la
1000KHZ/76,8KHz = 13,02 xung (lam tro n bang 13). Nen lenh th 3 se nap gia tr -13 vao thanh
ghi TH1 e tao toc o baud la 2400.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'3 . | .
Thu tuc ch nhan mot k t:
Mainr: jnb ri,$ ;ch cho en khi c bao nhan len 1
Clr ri ;xoa c e nhan k t tiep theo
Mov @r0,sbuf ;cat d lieu vao o nh.

Neu ch nhan mot k t th sau khi nhan xong CPU se thc hie n cong viec x ly khac, con
neu muon nhan na th quay lai.
Thu tuc ch gi mot k t:
Mains: jnb ti,$ ;kiem tra may phat san sang hay cha
Clr ti ;xoa c e chuan b phat d lieu
Mov sbuf,@r0 ;lay d lieu t o nh truye n i.

Viec s dung truyen d lieu toc o baud nao tuy thuoc vao yeu cau thc te. Toc o cang
cao th d lieu truyen cang nhanh. Khi truyen nhieu d lieu th ngoai toc o qui nh thong nhat
gia 2 he thong ket noi vi nhau con phai quan tam en toc o x ly d lieu nhan ve va lay d
lieu e gi i e khong b mat d lieu trong qua trnh truyen va nhan. Mot trong nhng giai phap
e kiem tra xem d lieu co b mat hay kho ng th phai s dung thu tuc bat tay.


V||. H0A1 0NC NCA1 CUA V| |EU KH|EN MC55
' 3 :|-|
Ngat s dung trong vi x ly hay vi ieu khien hoat o ng nh sau: vi x ly hay vi ieu khien
luon thc hien mot chng trnh ma ta thng goi la chng trnh chnh, khi co mot s tac ong t
ben ngoai bang phan cng hay s tac ong ben trong lam cho vi x ly ngng thc hien chng
trnh chnh e thc hien mot chng trnh khac (con goi la chng trnh phuc vu ngat ISR) va sau
khi thc hie n xong vi x ly tr lai thc hie n tiep chng trnh chnh. Qua trnh lam gian oan vi x
ly thc hien chng trnh chnh xem nh la ngat.
Co nhie u s tac ong lam ngng chng trnh chnh goi la cac nguon ngat, trong vi ie u
khien khi timer/counter em tran se tao ra ngat. Ngat ong mot vai tro quan trong trong lap trnh
ieu khien.
Khi s dung ngat se cho phep vi x ly hay vi ieu khie n ap ng nhie u s kien quan trong va
giai quyet s kien o trong khi chng trnh khac ang thc thi. V du: vi ieu khien ang thc
hien chng trnh chnh th co d lieu t he thong khac gi en th vi ieu khien ng ng chng
trnh chnh e thc hien chng trnh phuc vu ngat nhan d lieu xong roi tr lai tiep tuc thc hien
chng trnh chnh, hoac co mot tn hieu bao ngat t ben ngoai th vi ieu khien se ngng thc
hien chng trnh chnh e thc hien chng trnh ngat roi tiep tuc thc hien chng trnh chnh.
Ta co the s dung ngat e yeu cau vi ie u khie n thc hien nhieu chng trnh cung mot luc
co ngha la cac chng trnh c thc hien xoay vong.
Ta co the minh hoa qua trnh thc hie n 1 chng trnh trong trng hp co ngat va khong co
ngat nh hnh 4-27.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '37

H|nh 4-27. V| d|e u kh|en thu c h|en chucng tr|nh ch|nh trcng 2 trucng hcp khc ng vo cc ngot.
Trong o : Ky hieu * cho biet vi ieu khien ngng chng trnh chnh e thc thi chng trnh
con phuc vu ngat ISR. Con ky hieu ** cho biet vi ieu khien quay tr lai thc hien tiep chng
trnh chnh sau khi thc hien xong chng trnh con phuc vu ngat ISR.
|| |~:
Vi ieu khien 89C51 co 5 nguon ngat: 2 ngat ngoai, 2 ngat Timer va mot ngat Port noi tiep.
Vi ieu khien 89C52 co them mot nguon ngat la cua timer T2 nh hnh 4-28. Mac nhien khi vi
ieu khien b reset th tat ca cac ngat se mat tac dung va c cho phep bi phan mem.
Trong trng hp co hai hoac nhie u nguon ngat tac ong ong thi hoac vi ie u khien ang
phuc vu ngat th xuat hien mot ngat khac, th se co hai cach giai quyet la kiem tra lien tiep va s
dung che o u tien.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
'3` . | .

H|nh 4-28. V| d|e u kh|en 8C52 cc 6 ngucn ngot.
| :|- : |~: / ~ |~:
Trc tien chung ta phai hieu cho phep va khong cho phep ngat la nh the nao ? Khi ta cho
phep ngat va khi ngat tac ong th vi ieu khien se ngng chng trnh chnh e thc hien chng
trnh con phuc vu ngat, co n khi kho ng cho phep th du co s tac ong en ngat vi ie u khie n van
tiep tuc thc hien chng trnh chnh khong thc hien chng trnh phuc vu ngat.
Trong vi ieu khien co 1 thanh ghi IE (Interrupt Enable) tai a ch 0A8H co chc nang cho
phep / cam ngat. Ta s dung thanh ghi nay e cho phep hay khong cho phep oi vi tng nguon
ngat va cho toan bo cac nguon ngat.
Hoat ong cua tng bit trong thanh ghi cho phep ngat IE c tom tat trong bang 4-13:

8|t K| h|eu |o ch| b|t Chuc nong (Enob|e = , D|sob|e = 0|
|E.7 EA AFH Chc phep tcon bc hcoc com tcon bc co c ngucn ngot.
|E.6 - AEH Chuo du ng den
|E.5 E12 ADH Chc phep ngot 1|mer 2 (8052|.
|E.4 E5 ACH Chc phep ngot Pcrt nc| t|ep.
|E.3 E1 A8H Chc phep ngot 1|mer .
|E.2 EX AAH Chc phep ngot ngco| Externo| (|N1|.
|E. E10 AH Chc phep ngot 1|mer 0.
|E.0 EX0 A8H Chc phep ngot ngco| Externo| 0 (|N10|.
8ong 4-3. 1cm to t chuc nong coc b|t cuo thonh gh| |E.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '33
Trong thanh ghi IE co bit IE.6 cha dung e n, bit IE.7 la bit cho phe p/cam ngat toan bo cac
nguon ngat. Khi bit IE.7= 0 th cam het tat ca cac nguon ngat, khi bit IE.7=1 th cho phep tat ca cac
nguon ngat nhng con phu thuoc vao tng bit ieu khien ngat cua tng nguon ngat.
V du e cho phep Timer 1 ngat ta co the thc hien tren bit:
SETB EA ;cho phep ngat toan bo
SETB ET1 ;cho phep timer 1 ngat
Hoac co the dung lenh sau:
MOV IE, #10001000B
oi vi yeu cau cua v du tren th 2 cach thc hien tren la xong nhng ta hay so sanh 2 cach
thc hien va chu y mot vai ieu trong lap trnh:
Cac lenh cua cach 1 khong anh h ng cac bit con lai trong thanh ghi IE.
Cach th hai se xoa cac bit con lai trong thanh ghi IE.
au chng trnh ta nen khi gan IE vi lenh MOV BYTE, nhng khi ieu khien cho phe p
hay cam trong chng trnh th ta se dung cac lenh SET BIT va CLR BIT e tranh lam anh hng
en cac bit khac trong thanh ghi IE.
- -| |-| || :-| |~:
Khi co nhieu nguo n ngat tac ong cung luc th ngat nao quan tro ng can thc hien trc va
ngat nao khong quan trong th thc hien sau giong nh cac cong viec ma ta giai quyet hang ngay.
Ngat cung c thiet ke co s sap xep th t u tien t thap en cao e ngi lap trnh sap xep cac
nguon ngat theo yeu cau cong viec ma mnh x ly.
Thanh ghi co chc nang thiet lap che o u tien trong vi ieu khien la thanh ghi IP (Interrupt
Priority) tai a ch 0B8H. Hoat ong cua tng bit trong thanh ghi IP c tom tat trong bang 4-14.

Bit K hieu a ch bit Chc nang
IP.7 - - Cha s dung
IP.6 - - Cha s dung
IP.5 PT2 BDH u tien cho ngat Timer 2 (8052).
IP.4 PS BCH u tien cho ngat Port noi tiep.
IP.3 PT1 BBH u tien cho ngat Timer 1.
IP.2 PX1 BAH u tien cho ngat ngoai External 1.
IP.1 PT0 B9H u tien cho ngat Timer 0.
IP.0 PX0 B8H u tien cho ngat ngoai External 0.
8ong 4-4. 1cm to t chuc nong coc b|t cuo thonh gh| |P.
Khi reset he thong th thanh ghi u tien ngat IP b xoa va tat ca cac ngat mc u tien thap
nhat.
Trong 89C51 co 2 mc u tien thap va 2 mc u tien cao. Neu vi ieu khie n ang thc hie n
chng trnh con phuc vu ngat co mc u tien thap va co mot yeu cau ngat vi mc u tien cao
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | .
hn xuat hien th vi ieu khien phai ngng thc hie n chng trnh con phuc vu ngat co mc u tien
thap e thc hien chng trnh con phuc vu ngat mi co u tien cao hn.
Ngc lai neu vi ieu khien ang thc hie n chng trnh con phuc vu ngat co mc u tien
cao hn va co yeu cau ngat vi mc u tien thap hn xuat hien th vi ieu khien van tiep tuc thc
hien cho en khi thc hien xong chng trnh phuc vu ngat co u tien cao hn roi mi thc hien
chng trnh phuc vu ngat co u tien thap ang yeu cau.
Chng trnh chnh ma vi ieu khien luon thc hien trong mot he tho ng th mc thap nhat,
khong co lien ket vi yeu cau ngat nao, luon luon b ngat bat chap ngat mc u tien cao hay
thap. Neu co 2 yeu cau ngat vi cac u tien khac nhau xuat hien ong thi th ye u cau ngat co mc
u tien cao hn se c phuc vu trc.
- :~ |~: :|- .| .|-: -| :|
Neu 2 yeu cau ngat co cung mc u tien xuat hien ong thi th vong quet kiem tra lien tiep
se xac nh yeu cau ngat nao se c phuc vu trc tien. Vong quet kiem tra lien tiep theo th t
u tien t tren xuong la: ngat ngoai th 0 (INT0), ngat timer T0, ngat ngoai th 1 (INT1), ngat
Timer 1, ngat truyen d lieu noi tiep (serial Port), ngat timer 2. Hnh 4-29 se minh hoa cho trnh t
tren.
Quan sat trong hnh 4-29 chung ta thay co 6 nguo n ngat cua 89C52 va tac dung cua cac thanh
ghi IE hoat ong nh mot contact On/Off con thanh ghi IP hoat ong nh mot contact chuyen mach
gia 2 v tr e la chon 1 trong 2.
Ta hay bat au t thanh ghi IE trc: bit cho phep ngat toan cuc (global enable) neu c
phep se ong toan bo cac contact va tuy thuoc vao bit cho phe p cua tng nguon ngat co c phep
hay khong va chung hoat ong cung gio ng nh mot contact: neu c phep th ong mach va tn
hieu yeu cau ngat se a vao be n trong e x ly, neu khong c phep th contact h mach nen tn
hieu yeu cau ngat se khong a vao be n trong va khong c x ly.
Tiep theo la thanh ghi IP: tn hieu sau khi ra khoi thanh ghi IE th a en thanh ghi IP e sap
xep u tien cho cac nguon ngat. Co 2 mc o u tien: mc u tien cao va mc u tien thap. Neu
cac nguon nao co u tien cao th contact chuyen mach se a tn hieu yeu cau ngat o en vong
kiem tra co u tien cao, neu cac nguon nao co u tien thap th contact chuye n mach se a tn hieu
yeu cau ngat o en vong kiem tra co u tien thap.
Vong kiem tra ngat u tien cao se c thc hien trc va se kiem tra theo th t t tre n
xuong va khi gap yeu cau ngat nao th yeu cau ngat o se c thc hien. Sau o tiep tuc thc
hien cho vong kiem tra u tien ngat co mc u tien thap hn.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | . '

H|nh 4-2. Cou truc ngot cuo v| d|eu kh|en.
Trong hnh con cho chu ng ta thay yeu cau ngat truyen d lieu noi tiep tao ra t to hp OR
cua 2 c bao nhan RI va c bao phat TI. Khi ngat truyen d lieu xay ra va ta muon biet la do c
nhan hay c phat tao ra ngat e thc hien 2 co ng viec khac nhau th ta phai kiem tra c RI va TI
e biet thc hien cong viec nao tng ng.
V du trong truye n d lieu: khi co bao ngat truyen d lieu th ta phai kiem tra xem c RI = 1
hay khong? Ne u ung th he thong khac ang gi d lieu en va ta phai chuyen hng chng
trnh phuc vu ngat sang hng nhan d lieu, neu khong phai th chac chan la c TI=1 bao cho
chung ta biet rang d lieu a truyen i xong va san sang truyen k t tiep theo va khi o ta phai
chuyen hng chng trnh phuc vu ngat sang phat d lieu tiep theo.
Tng t, cac yeu cau ngat cua Timer 2 tao ra t to hp OR cua c tran TF2 va c nhap
ngoai EXF2.
Cac bit c cua cac nguon ngat c tom tat bang 4-15:

|nterrupt F|og 5Fk keg|ster ond 8|t Pcs|t|cn
Externo| 0 |E 0 1C0N
Externo| |E 1C0N 3
1|mer 1F 1C0N 7
1|mer 0 1F 0 1C0N 5
5er|o| Pcrt 1| 5C0N
5er|o| Pcrt k| 5C0N 0
1|mer 2 1F 2 12C0N 7 (8052|
1|mer 2 EXF 2 12C0N 6 (8052|
8ong 4-5. 1cm to t coc b|t cc cuo coc nguc n ngot.
/| . |~:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | .
Khi tn hieu ye u cau ngat xuat hien va c chap nhan bi CPU th CPU thc hie n cac cong
viec sau:
Neu CPU ang thc hien lech th phai ch thc hien xong lenh ang thc hien.
Gia tr cua bo em chng trnh PC c cat gi vao Stack (chnh la a ch cua lenh
tiep theo trong chng trnh chnh).
Trang thai ngat hien hanh c lu vao ben trong.
Cac yeu cau ngat khac se b ngan lai.
a ch cua chng trnh phuc vu ngat tng ng se c nap vao bo em chng
trnh PC.
Bat au thc hien chng trnh phuc vu ngat ISR.
Trong chng trnh phuc vu ngat luon ket thuc bang le nh RETI. Khi gap lenh RETI th CPU
se lay lai a ch cua lenh tiep theo trong ngan xep tra lai cho thanh ghi PC e tiep tuc thc hien
cac cong viec tiep theo cua chng trnh chnh.
Chu y: chng trnh con phuc vu ngat kho ng c lam mat hoac lam sai a ch cua PC a
lu trong ngan xep neu ieu nay xay ra th khi tr lai chng trnh chnh CPU se khong thc hien
tiep cong viec cua chng trnh chnh va chung ta cung khong xac nh CPU ang lam g va au.
Khi o chung ta mat quyen kiem soat vi x ly.
Giong nh ta ang oc mot cuon sach v mot cong viec khac ta phai ngng lai va ta co lam
dau tai trang ta tam ngng, sau khi lam xong co ng viec th ta tiep tuc quay lai e oc tiep cuon
sach tai ni ta a dng. Tat ca eu xay ra nh vay th rat la bnh thng nhng trong khi ta thc
hien cong viec th co mot ngi khac xem cuon sach cua ta va vo tnh lam mat dau th khi ta quay
lai ta se oc khong u ng trang chung ta ang dng lai. Nguyen tac lam viec cua vi x ly hoan toan
giong nh vay.
Trong vi ieu khien th bo nh ngan xep la bo nh RAM noi nen chung san sang hoat
ong cho viec lu tr tam, con oi vi vi x ly th bo nh ngan xep s dung bo nh ngoai nen
ban phai khi tao bo nh ngan xep phai la vung nh RAM e co the ghi va oc lai c, neu ban
khi tao tai vung nh EPROM hoac khi tao tai ni ma bo khong ghi vao c th se lam mat a
ch d lieu lu vao bo nh ngan xep dan e n chng trnh se thc hien sai.
Mot ieu can phai chu y na la trong lap trnh chung ta kho ng c nhay t chng trnh con
sang chng trnh chnh e thc hien tiep chng trnh v lam nh vay sau nhieu lan thc hien th
bo nh ngan xep se b tran va ghi e len cac d lieu khac lam sai chng trnh. Trong trng hp
nay chung ta se thay rang chng trnh chung ta thc hien ung mot vai lan va sau o th sai.
Cac vector ngat (Interrupt Vectors) :
Nh a trnh bay tre n, khi co mot yeu cau ngat xay ra th sau khi cat gia tr a ch trong PC
vao ngan xep th a ch cua chng trnh con phuc vu ngat tng ng con goi bi vector a ch
ngat se c nap vao thanh ghi PC, a ch nay la co nh va do nha che tao vi ieu khien qui nh.
Cac chng trnh ngat phai bat au viet ung tai a ch quy nh o. Cac vector a ch ngat c
cho trong bang 4-16:

Interrupt Flag Vectors Address
System Reset RST 0000H
External 0 IE 0 0003H
Timer 0 TF 0 000BH
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
. | .
External 1 IE 1 0013H
Timer 1 TF1 001BH
Serial Port RI or TI 0023H
Timer 2 TF 2 or EXF2 002BH
8ong 4-6. 1cm to t vectcr d|o ch| ngot.
Vector reset he thong bat au tai a ch 0000H: khi reset vi ieu khien th thanh ghi PC =
0000H va chng trnh chnh luon bat au ta i a ch nay.
Khi ban s dung yeu cau ngat nao th chng trnh con phuc vu ngat phai viet ung tai a
ch tng ng. V du ban s dung ngat timer T0 th chng trnh ngat ban phai viet tai a ch
000BH.
Do khoang vung nh gia cac vector a ch cua cac nguo n ngat ch co vai o nh v du nh
vector a ch ngat cua ngat INT0 tai 0003H va vector a ch ngat cua ngat T0 tai 000BH ch cach
nhau co 9 o nh . Neu chng trnh phuc vu ngat cua ngat INT0 co kch thc ln hn 9 byte th no
se ung en vung nh cua ngat T0. Cach giai quyet tot nhat la ngay tai a ch 0003H ta viet lenh
nhay en mot vung nh khac ro ng hn. Co n neu cac ngat T0 va ca c ngat khac khong s du ng th ta
co the viet chng trnh tai o cung c.
Chng trnh chnh luon bat au tai a ch 0000H sau khi reset he thong, neu trong chng
trnh co s du ng ngat th ta phai dung lenh nhay tai a ch 0000H e nhay en mot vu ng nh khac
rong hn kho ng b gii han e viet tiep.


V|||. 8A| 1AP:
Cau 1: Hay thiet ke mot he thong em san pham hien th ket qua em tre n 3 led 7 oa n
dung vi ieu khien 89C51 hoac 89C52 hoac 89S52.
Cau 2: Hay thiet ke mot he thong em o ng ho so hien th gi phut giay tren 6 led 7
oan du ng vi ieu khie n 89C51 hoac 89C52 hoac 89S52.
Cau 3: Hay thiet ke mot he tho ng o nhiet o dung cam bien LM35 va ADC 0808 hie n
th ket qua tren 3 led 7 oa n dung vi ieu khie n 89C51 hoac 89C52 hoac 89S52.
Cau 4: Hay thiet ke mot he tho ng o nhiet o dung cam bien LM35 va ADC 0808 hie n
th ket qua tren 3 led 7 oa n dung vi ieu khie n 89C51 hoac 89C52 hoac 89S52.
Cau 5: Hay thiet ke mot he tho ng o nhiet o dung cam bien LM35 va ADC 0808 hie n
th ket qua tren LCD du ng vi ieu khie n 89C51 hoa c 89C52 hoac 89S52.





Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| - . -| |-| ` :: `'
- . | .

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M


Chng 5





' 3 :|- |
~ : | | ~ :|| :-| | ~---:-
~| :| | ~ ||| :|| |: ||
- ~ || ~ : -| :|| :| .|~ :|| : -| |
~ | ~| | ~---:-
~ -| |- | | ~ :|| | |: ||
7 ~ : | -| - :
` | | | \~
3 ~ | . -: ||| :|| |: ||


Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
. | .
I. C|0 1H|E U:
Hp ng (assembly language) thay the nhng ma nh phan bang cac t g i nh e lap trnh
de da ng hn. Ma y tnh kho ng hieu hp ng do o trnh bie n dch hp ng assembler va trnh lien
ket linker co ch c nang dch nhng chng trnh viet ba ng hp ng thanh ngon ng may.
Mot so khai niem:
Chng trnh hp ng (assembly language program)
La chng trnh c viet ba ng ca ch dung cac nhan, ca c t gi nh,, trong o mo i pha t
bieu tng ng v i mot lenh cua may. Chng trnh viet bang hp ng goi la ma nguon va
chng trnh nay khong the th c thi ma nham giup ngi lap trnh o c hieu nhng g vi x ly
th c hien va g ro i mo t ca ch de dang.
Chng trnh ngon ng may (machine language program)
La chng trnh gom cac ma nh phan tng ng v i 1 lenh cu a vi x ly. Ca c chng trnh
vie t bang ngon ng ma y thng c go i la ma o i t ng (object code) va thc thi c ba ng
may tnh.
Chng trnh assembler:
La chng trnh lien ke t cac chng trnh oi tng dch mo t chng trnh viet ba ng hp
ng sang chng trnh ngon ng may. Chng trnh ngon ng may co the da ng tuye t o i hoa c
dang ta i nh v.
Chng trnh linker:
La chng trnh ke t h p ca c chng trnh o i tng ta i nh v c e tao ra chng trnh
oi t ng tuye t oi e th c thi c bang may tnh.
Segment:
La mot n v bo nh ch a ma lenh hoa c ch a d lieu. Mot segment co the dang tuye t
oi hoa c tai nh v c.
Segment ta i nh v c se co ten, kieu va ca c thuoc tnh cho phe p chng trnh linker ke t
hp no v i no vi ca c phan cu a cac oan khac neu ca n e nh v u ng oa n. Segment da ng
tuye t oi khong co ten va kho ng the ke t hp c v i ca c oan kha c.
Module:
Cha1 hay nhieu segment hoac mot pha n segment. Mo t module co ten do ngi s dung
a t. Nhng nh ngha module xa c nh tam cua ca c ky hie u cu c bo . Mot tap tin oi tng ch a
1 hay nhieu module. Mo t module c xem nh la mot tap tin trong nhieu tnh huong.
Chng trnh:
Gom nhieu module tuye t o i, tron ta t ca ca c oan tuyet o i va ta i nh v c t ta t ca
ca c module nhap. Mo t chng trnh ch ch a cac ma nh phan cho ca c ch th ma may tnh hieu.
II. H0A 1 0 NC CU A 1kNH 8|E N D|CH A55EM8LEk:
ASM51 la assembler chay tren may tnh do Intel cung cap e bien dch cho ho MCS51.
Ca ch dung ASM51 e bien dch chng trnh viet bang h p ng nh sau: t dau nhay
DOS hay Win commander a nh
ASM51 source_file[assembler_control]
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . 7
Trong o source_file la te n tap tin nguon vie t bang hp ng, assembler_control la nh ng
ieu khien assembler.
Assembler nha n tap tin nguo n (v du example.asm) va tao ra ta p tin o i t ng
(example.Obj) va tap tin kieu liet ke (example.lst) nh hnh sau:

H|nh 5-. 8|en d|ch mct chucng tr|nh ngucn.
Tat ca cac chng trnh bien dch eu quet chng trnh nguon 2 lan e th c hien dch ra
ngon ng may ne n c goi la assembler hai bc. Assembler s dung bo em v tr lam a ch
cu a ca c lenh va cac gia tr cho cac nha n. Hoa t ong cu a tng bc c mo ta nh sau:
B c 1: nhan dien cac nhan va ca c k hieu trong chng trnh nguon, tnh toan ca c a ch
tng o i cu a chung va cat va o ba ng k hieu.
Bang k hieu ch a nh ng v tr nhan, nhng k hieu va nhng gia tr tng ng cu a chung.
Bo em v tr: lu gi a ch cu a ca c lenh va gia tr cu a nhan chng trnh.
B c 2: ta o ra tap tin o i t ng va tap tin lie t ke :
Ca c t g i nh
Ca c toa n hang c nh v va a t sau ca c ma lenh.
Ca c gia tr k hie u c truy cap e tnh ung d lieu hoa c a ch.
Cho phep tham chie u ti.
Tap tin ta i nh v cha thong tin can cho linker va nh v.
Tap tin lie t ke ch a chng trnh nguon va ma le nh.
III. CA U 1kU C CU A CHU0NC 1kNH H0P NCU :
Nhng le nh cu a vi x ly
Nhng ch dan assembler (assembler directive)
Nhng ieu khie n assembler
Ca c chu thch
Dang lenh cu a vi x ly nh sau:
[label:] mnemonic [operand] [,operand] [] [;comment]
Trong o label la nhan theo sau b i dau hai cham :, mnemonic la t g i nh cua lenh,
operand la toan hang tuy thuoc va o lenh co mo t hoa c nhie u toa n hang hoac khong co toan hang,
cuo i cung la chu thch cho lenh o i sau dau cham pha y ;.
K hie u la ten c nh ngha e bieu dien mo t gia tr, khoi van ban, a ch hoa c ten
thanh ghi va cu ng co the bieu dien ca c hang so va ca c bie u th c.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
` . | .
Ca c ten cu a ca c k hie u cho phep toi a 31 k t v i k t a u pha i la ch hoac dau ?
hoa c -, va theo sau phai la cac ch, so, ? hoac -. Ca c k hieu co the s dung ca c k t in
hoa hay thng khong phan bie t. Chu y ca c t k hie u la ca c t a s dung nen ng i lap trnh
khong c dung chung lam k hieu cho cac muc ch khac.
V du:
Bdn equ R2
Nha n la mo t loai k hieu dung e nh ngha v tr trong chng trnh:
Ten nhan tng trng cho mot a ch.
Vung van ban th nha t trong dong hp ng
Theo sau nha n la dau hai cham :
Tre n mo t hang ch co the nh ngha mo t nhan.
Khong c a t ten ca c nhan trung nhau.
V du:
Label1: mov R2,#35h
Mnemonic la ta t ca cac t g i nh cho ta t ca cac lenh va ca c ch da n assembler:
Mnemonic cho le nh: ADD, SUB, MUL, DIV, MOV,
Mnemonic cho ch dan assembler: org, equ, db, bit,
Toan hang operand la oi so hoa c bieu thc c a t ta cung vi lenh hoac ch dan
assembler, toa n hang co the la a ch hoa c d lieu.
Ca c ch da n assembler luon can cac toan hang la hang so hoa c k hieu.
V du:
Biendemngat equ R2
So toa n hang trong lenh tuy thuo c vao lenh co toan hang hay kho ng co:
V du:
Mov R0,#75h
nop
ret
Trong h p ng ASM51 co cac kieu toan hang bang 5-1:
K|eu tcon hong Mc to
Du ||e u tuc thc| K| h|eu hco c hong ducc dung |om g|o tr| sc
|o ch| b|t truc t|e p K| h|eu hco c hong thom ch|eu d|o ch| b|t
|o ch| chucng tr|nh K| h|eu hco c hong thom ch|eu d|o ch| mo
|o ch| du ||eu truc t|ep K| h|eu hco c hong thom ch|eu d|o ch| du ||eu
|o ch| g|on t|e p 1hom ch|eu g|o n t|e p den bc nhc, cc the |o cffset
K| h|eu ossemb|er doc b|e t 1en thonh gh|.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . 3
8ong 5-. Coc k|e u tcon hong
D lie u tc thi (immediate data):
La bie u th c so c ma hoa nh mot phan trong lenh ngon ng ma y. Toa n hang nay pha i
co k hieu # i tr c.
V du:
Mov R0,#75h
Trong v du na y 75h la d lieu t c th i.
a ch bit trc tiep: (direct bit address)::
Kie u nay du ng e truy cap ca c bit cu a ca c o nh cho phep truy xua t bit.
Co 3 ca ch e nh a ch bit:
- Truy xua t tr c tie p a ch bit.
- Truy xua t toa n t cham (byte.bit).
- K hie u assembler c nh ngha trc.
V du:
setb 00h ;bit co a ch 00h
clr acc.7 ;xoa bit th 7 cu a thanh ghi A
clr ea ;xoa bit nga t toa n cuc
a ch chng trnh: (program address)::
La toa n hang cu a lenh nhay.
- Lenh nhay tng o i: trong kieu lenh nay toan hang na y co o da i 8 bit c xem la
offset s du ng cho lenh nhay khong ie u kien sjmp va le nh nhay co ie u kien.
- Lenh nhay va lenh go i tuye t o i: trong kie u lenh nay toan ha ng nay co o dai 11 bit
dung e quan ly trang bo nh cho lenh AJMP va ACALL.
- Lenh nhay va lenh go i co a ch dai: trong kie u lenh nay toan hang na y co o dai 16
bit dung e quan ly toan bo bo nh cho lenh LJMP va LCALL.
Nha y va go i generic::
Lenh JMP co the c dch h p tha nh lenh SJMP, AJMP hoa c LJMP.
Lenh Call co the c dch hp thanh lenh ACALL hoa c LCALL
Ng i lap trnh kho ng can quan tam en a ch tha t khi nhay hay go i. Quy tq81c chuyen
thanh tuy thuo c vao assembler:
Lenh SJMP: khong co tham chie u t i va a ch ch trong vung -128 byte so v i a ch
cu a lenh ke .
Lenh AJMP/ACALL: khong co tham chieu ti va a ch ch trong vu ng nh cung khoi 2
kByte so v i lenh ke.
Lenh AJMP/ACALL: co tham chieu ti a ch ch trong vung nh 64Kbyte.
V du:

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
' . | .
1234 1 org 1234h
2
1234 04 3 start: inc a
1235 80FD 4 jmp start
5
12FC 6 org start+200
12FC 4134 7 jmp start
8
12FE 021304 9 jmp finish
1301 121307 10 call delay
11
1304 14 12 finish: dec a
1305 4134 13 jmp start
14
1307 7F00 15 delay: mov r7,#0
1309 22 16 ret
17 end
Hang le nh th 4 jmp c bien dch thanh sjmp, hang lenh jmp th 7 c bien dch tha nh
Ajmp va hang lenh jmp th 9 c bien dch thanh ljmp.

a ch d lieu tr c tiep (direct data address)::
a ch nay dung e truy xuat bo nh d lieu no i t 00H en 7FH va cac vung nh ch a
ca c thanh ghi a c bie t t 80H en FFH. Cac k hie u c nh ngha eu co the s dung c
cho cac thanh ghi ch c nang.
V du: hai lenh sau la tng ng:
Mov a,90h
Mov a,p1

a ch d lieu gan tiep (indirect data address)::
Kie u nay dung cac thanh ghi e ch a a ch cua cac o nh can truy xua t d lieu. Ca c
thanh ghi s dung cho kieu nay la thanh ghi R0, R1, DPTR va PC.
Ca c k hieu a c bie t cua assembler::
Ca c k hie u nay dung cho ca ch nh a ch du ng thanh ghi nh A, DPTR, R0 en R7, PC,
c C va cap thanh ghi AB.
K hie u dau $ du ng e tham chieu en gia tr hien hanh cu a bo em v tr.
V du: hai lenh sau la tng ng:
Wait: jnb ri,wait
Jnb ri,$

K hie u ; i sau no la ca c chu thch
IV. 1NH 10A N 8|E U 1HU C 1k0NC QUA 1kNH D|CH:
' ~ : -| :|| .~ :~ | :|
Toan t c dung e ke t hp va so sanh cac toan hang trong chng trnh hp ng.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . ''
Bieu th c dung e ke t hp cac so, ca c chuo i ky t, ca c ky hieu va cac toan t e tnh toan
ra so nh phan 16 bit. Du ng bieu th c trong lap trnh se giu p cho chng trnh de o c hn va
uyen chuyen hn.
Ca c toa n hang gom co : so, ky t , chuo i ky t va bo em v tr.
Ca c toa n t gom co : toa n t so hoc, toa n t nh phan, toan t quan he va cac toan t kha c.
So: co the c s dung la :
So tha p luc phan (hexadecimal = hex, co c so 16): H, h.
So tha p phan (decimal, co c so 10): D, d hoa c khong can ghi.
So bat phan (octal, co c so 8): O, o, Q, q.
So nh phan (binary, co c so 2): B, b.
Chu y : v i so hex neu k t so hex au tie n be n trai la ch (t A en F) th pha i co them k
t so 0 trc.
V du: lenh nap d lieu F4H vao thanh ghi R0
Mov R0,#0F4h

Ky t: cho phe p toi a 2 ky t nam gi a 2 dau nha y () co the c dung lam toan ha ng
trong bieu th c.
V du: A co gia tr tng ng 0041H (bang ma ASCII)
AB co gia tr tng ng 4142H
a co gia tr tng ng la 0061H
ab co gia tr tng ng 6162H
Chung ta cung co the s du ng ky t lam toa n hang cho d lie u tc thi.
V du:
Mov R0,#0

|| . :| |~~:- -:|
Co the ke t h p v i ch da n DB e nh ngha ca c thong bao trong chng trnh hp ng.
V du:
Keymsg DB press any key
Ch da n tren se tao ra mot vung nh d lieu cha ca c ma ASSCI tng ng la 50H (ch
P), 72H (ch r), 65H (ch e), , lu vao vung nh ba t a u t a ch keymsg.

- . : ~: | ||:-
Dung e xac nh a ch cua tng ma lenh trong chng trnh bien dch tuy thuo c vao ch
dan ORG.
Ky t $ se tra ve gia tr hien hanh cua bo em v tr.
V du:
LOC OBJ LINE SOURCE
(v tr ma oi tng hang ma nguon)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
' . | .
1234 1 org 1234h
2
1234 04 3 start: inc a
1235 80FD 4 jmp start
5
12FC 6 org start+200
12FC 4134 7 jmp start
8
12FE 021304 9 jmp finish
1301 121307 10 call delay
11
1304 14 12 finish: dec a
1305 4134 13 jmp start
14
1307 7F00 15 delay: mov r7,#0
1309 22 16 ret
17 end

- ~ :~| :| - | ~:|-: :-~: |
Toan t cong + expr + expr
Toan t tr - expr - expr
Toan t nhan expr * expr
Toan t chia /- expr / expr
Toan t chia lay pha n d mod expr MOD expr
Trong o expr la bieu th c.
V du:
Mov a,#10 + 10H ;hai lenh nay tng ng
Mov a,#1AH

Mov a,#25 mod 7 ;hai lenh nay tng ng
Mov a,#4

~ :~| :|
Toan t NOT: NOT expr la y bu ao tng bit
Toan t SHR expr SHR n dch sang pha i n bit
Toan t SHR expr SHL n dch sang tra i n bit
Toan t AND expr AND expr and tng cap bit tng ng
Toan t OR expr OR expr or tng ca p bit tng ng
Toan t XOR expr XOR expr xor tng cap bit tng ng
Trong o expr la bieu th c va x la so v tr can dch.
V du: 3 lenh sau la tng ng
Three equ 3
Minus3 equ -3

Mov a,#(NOT three) +1
Mov a,#minus3
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . '
Mov a,#11111101b

~ :~| :| .|~| |- - ~:| :-~:-
EQ = bang nhau (equal)
NE <> khong bang nhau (not equal)
LT < nho hn (less than)
LE <= nho hn hoac bang (less than or equal)
GT > ln hn (greater than)
GE >= ln hn hay bang (greater than or equal)
Ket qua luon tra ve ung (FFFFH) hoa c sai (0000H).
V du: ca c lenh sau la tng ng
Mov a,#5=5
Mov a,#5 EQ 5
Mov a,#5 NE 4
Mov a,#5 <> 4
Mov a,#0FFH

7 ~ :~| :| |~
Toan t LOW expr co ch c nang lay ke t qua byte thap cua expr.
Toan t HIGH expr co ch c nang lay ke t qua byte cao cua expr.
V du:
Mov dph,#high(1234h) ;hai lenh nay tng tng
Mov dph,#12h

Mov dpl,#low(1234h) ;hai lenh nay tng ng
Mov dpl,#34h

` || :| || :- | | ~ ~ :~ | :|
Danh sach quyen u tien cu a ca c toan t c sap theo th t t cao nha t en thap nha t
nh bang 5-2:
Th t Toan t
1 ()
2 HIGH LOW
3 * / MOD SHL SHR
4 + -
5 EQ NE LT LE GT GE = <> < <= > >=
6 NOT
7 AND
8 OR
9 XOR
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
'- . | .
8ong 5-2. 1hu tu uu t|e n coc tco n tu
Khi cac toan t c s dung co quyen u tien ngang nhau th vie c tnh toan ra gia tr se
ba t au tnh t trai sang pha i.
V. CA C CH| DA N CH0 A55EM8LEk:
La nh ng ch th le nh cho assembler va c chia ra lam ca c nho m nh sau:
- ie u khien trang tha i assembler: ORG, END, USING.
- nh ngha ky hieu: segment, equ, set, data, Idata, Xdata, bit, code.
- Kh i tao hay nh ngha trong bo nh : DS, DBIT, DB, DW.
- Lien ket chng trnh: public, extrn, name.
- Chon oa n: Rseg, Cseg, Dseg, Iseg, Bseg, Xseg.
' ~ | ~ | -| |-| :~| :|~ ~---:-
ORG: co ch c nang thay o i bo em v tr cu a segment hien thi e a t goc chng trnh
mi cho cac phat bieu theo sau org.
Ca ch s dung: Org expr
V du:
2200 1 org 2200h ;khai bao a ch bat au 2200h
2200 7435 2 mov a,#35h
2202 7435 3 mov a,#35h
2204 7435 4 mov a,#35h
5
6
3000 7 org ($+1000H) and 0f000h ;khai bao a ch bang a ch hien tai cong
3000 7435 8 mov a,#35h ;them 1000H va and vi F000H e chuyen
9 end ;sang 4 kbyte ke
Neu bo lenh AND v i F000H th a ch mi se la 3206H = 2206H + 1000H
Ta co the s du ng khai ba o ORG trong ba t ky loai segment nao. Neu segment hie n th i la
tuye t oi th gia tr se la a ch tuye t o i trong segment hien th i. Neu segment hien thi la ta i
nh v c th gia tr cu a bieu thc ORG c x ly nh offset cu a a ch ne n cua segment
hien thi.
USING: co chc nang ba o cho assembler bie t bang thanh ghi tch cc hien thi, nhng no
khong th c s chuyen bang thanh ghi, do o e co the s dung ung th ta phai s dung AR0 en
AR7 sau USING thay v dung R0 en R7. Khi o assembler se t ong s dung u ng thanh ghi
trong bang thanh ghi mong muon o va khi dch assembler se o i Arn sang a ch tr c tiep.
Ca ch s dung: USING expr
V du:
USING 2
Mov AR3,#70H
Mov R0,#22h
Trong lenh th nhat, AR3 chnh la thanh ghi R3 cua bank thanh ghi 2, se c thay the bang cach truy cap a
ch tr c tiep la 13H. Trong lenh th hai, R0 van truy cap trong bank thanh ghi hien tai la bank 0.
END: la phat bieu cuo i cung trong tap tin nguo n, nhng g sau ch dan end se khong c
x ly .
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . '
| ~| || ||~ |-|
Nhng ch dan na y tao ca c ky hieu e bieu die n ca c segment, ca c thanh ghi, so va a ch.
Khong c s du ng nhan cho ch da n. Nhng ky hie u c nh ngha b i ca c ch da n nay la
duy nha t, ngoa i tr ch dan SET cho phe p nh ngha la i.
EQU hay SET: co ch c nang gan 1 ky so hay ky hieu thanh ghi cho ten ky hieu c a t
ta .
Ca ch s dung: symbol equ expr
symbol set expr
Trong o symbol la ky hie u do ng i dung nh ngha va expr la bieu th c.
V du:
Bdn equ R2
Giay set 40

Segment: co ch c nang khai ba o segment ta i nh v c.
Ca ch s dung: symbol segment segment_type
Trong o symbol la ky hieu do ngi dung nh ngha va segment_type la kieu segment.
Co ca c kieu segment nh sau:
- Code: segment ma chng trnh.
- Xdata: segment vung d lie u cha bo nh ben ngoa i.
- Data: segment vung d lie u no i co a ch tr c tiep t 00H7FH
- Idata: segment vung d lieu no i co a ch gian tie p t 00H7FH o i v i 8051 va
00HFFH oi v i 8052.
- Bit: segment vung nh bit nam trong vu ng nh cho phep truy xua t bit t
20H2FH
V du:
eprom segment code
Khai bao ky hieu eprom la segment kieu code. Chu y phat bieu nay ch khai bao EPROM la kieu code, e s
dung segment nay th pha i s dung ch dan RSEG
CODE/DATA/IDATA/XDATA/BIT
Dung e gan a ch cua kieu tng ng vi ky hieu, tuy nhien ne u co s dung th assembler kiem tra kie u.
V du:
LOC OBJ LINE SOURCE

0005 1 flag1 equ 05h
0005 2 flag2 bit 05h
0000 D205 3 setb flag1
0002 D205 4 setb flag2
0004 750500 5 mov flag1,#0
0007 750500 6 mov flag2,#0
*** ERROR #37, LINE #6 (0), DATA SEGMENT ADDRESS EXPECTED
7 end

SYMBOL TABLE LISTING
------ ----- -------
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
' . | .

N A M E T Y P E V A L U E A T T R I B U T E S

FLAG1. . . NUMB 0005H A
FLAG2. . . B ADDR 0020H.5 A

REGISTER BANK(S) USED: 0
ASSEMBLY COMPLETE, 1 ERROR FOUND (6)
Trong chng trnh v du tren ta a khai bao flag1 la o nh co a ch 05H, flag2 la bit co a ch 05H. Hai
lenh setb khi bien dch khong co loi v assembler xem chung la cac bit co a ch 05H.
Lenh th 5 khi bien dch se xem flag1 la o nh co a ch 05H, nhng lenh th 5 th khi bien dch se bao lo i v
lenh MOV khong the thc hien o i vi o nh bit.
| ~| | :~ : :| : ||
.DB (define byte): nh ngha byte, co ch c nang kh i tao vung nh ma v i cac gia tr kieu
byte.
Ca ch s dung: [label:] db expr [,expr][]
Trong o label la nhan do ng i dung nh ngha va expr la bieu th c.
V du:
LOC OBJ LINE SOURCE

---- 1 cseg at 0100h
0100 C0 2 ma7d: db 0c0h,0a4h
0101 A4
3
0102 48656C6C 4 msg: db ' Hello'
0106 6F
5
6 end
SYMBOL TABLE LISTING
------ ----- -------

.DW (define word): nh ngha t, co chc nang kh i tao vung nh ma vi ca c gia tr kieu
word.
Ca ch s dung: [label:] dw expr [,expr][]
Trong o label la nhan do ng i dung nh ngha va expr la bieu th c.
- | ~| ~ || | :| : ||
.DS (define storage): nh ngha vung lu tr , co ch c nang danh vung nh theo byte. Ch
dan nay co the c s dung trong ba t ky loa i segment nao ngoai tr DBIT.
Ca ch s dung: [label:] ds expr
Trong o label la nhan do ng i dung nh ngha va expr la bieu thc khong co tham chieu
t i.
Khi gap ch dan DS trong chng trnh th bo em v tr cu a segment hie n tai c ta ng
them so byte la gia tr cu a expr.
V du: tao vung nh Ram noi 40 byte e lu d lieu:
dseg at 10h ;vung nh d lieu noi
len equ 40
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . '7
buf: ds len ;danh 40 byte ba t au t a ch 10H

V du: tao vung nh Ram ngoai 1000 byte e lu d lieu:
xseg at 2000h ;vung nh d lieu ngoai
xlen equ 1000
xbuf: ds len ;danh 1000 byte bat au t a ch 2000H
;bat au ma chng trnh
Cseg at 0000h
Mov dptr,#xbuf ;nap a ch cu a vung nh ngoa i vao dptr
Loop: clr a
Movx @dptr,a
Inc dptr
Mov a,dpl
Cjne a,#low(xbuf+xlen+1),loop ;so sanh a ch byte thap e so sanh a ch byte cao
Mov a,dph
Cjne a,#high(xbuf+xlen),loop ;so sanh a ch byte cao e ket thuc
Sjmp $
end

.Dbit (define bit): nh ngha vung lu tr d lieu bit, co ch c na ng danh vung nh theo bit
trong segment bit.
Ca ch s dung: [label:] dbit expr
Trong o label la nhan do ng i dung nh ngha va expr la bieu thc khong co tham chieu
t i. Khi gap ch dan DBIT trong chng trnh th bo em v tr cu a segment BIT hien tai c
ta ng them vi so bit la gia tr cua expr.
V du:
LOC OBJ LINE SOURCE

---- 1 bseg ;ch da n vung nh bit mac nhien bat au t 00H
0000 2 kbflag: dbit 1
0001 3 pbflag: dbit 1
0002 4 dbflag: dbit 1
5
---- 6 bseg at 30h ;ch dan vung nh bit bat au t 30H
7
0030 8 F1: DBIT 1
0031 9 F2: DBIT 2
0033 10 F3: DBIT 1
11
---- 12 cseg
0000 D231 13 setb F2
14
15 end
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
'` . | .
SYMBOL TABLE LISTING
------ ----- -------

N A M E T Y P E V A L U E A T T R I B U T E S

DBFLAG . . B ADDR 0020H.2 A ;bit co a ch bit la 02H cua o nh co a ch 20H
F1 . . . . B ADDR 0026H.0 A ;bit co a ch bit la 30H cua o nh co a ch 26H
F2 . . . . B ADDR 0026H.1 A ;bit co a ch bit la 31H cua o nh co a ch 26H
F3 . . . . B ADDR 0026H.3 A ;bit co a ch bit la 32H cua o nh co a ch 26H
KBFLAG . . B ADDR 0020H.0 A ;bit co a ch bit la 00H cua o nh co a ch 20H
PBFLAG . . B ADDR 0020H.1 A ;bit co a ch bit la 01H cua o nh co a ch 20H

~ | ~ | -| -: ||| :||
Cho phe p ca c module (ca c tap tin) c hp dch rieng co the lie n la c v i nhau bang ca ch
cho phep tham chieu gi a ca c module va at te n ca c module.
Public: Lie t ke cac ky hieu co the c s dung trong cac module oi tng kha c.
Ca ch s dung: public symbol [,symbol][,..]
Trong o ky hieu symbol c khai bao trong Public pha i c nh ngha trong module
hien ha nh.
V du: Public inchar, outchar, inline, outstr, extern

Extrn: Lie t ke cac ky hieu c tham chie u trong module nguon hie n hanh nhng chung
c khai ba o trong cac module kha c.
Ca ch s dung: extrn segment_type(symbol [,symbol][,..])
Ca c segment_type la CODE, XDATA, DATA, IDATA, BIT va NUMBER (NUMBER la ky
hieu khong co kieu c nh ngha bang EQU).
V du: Co 2 tap tin MAIN.SRC va message.SRC
;main.src
Extrn code (HELLO, GOOD_BYE)

CALL HELLO

CALL GOOD_BYE

END
;MESSAGE.SRC
PUBLIC HELLO, GOOD_BYE

HELLO;

RET
GOOD_BYE;

RET
Hai module tren khong phai la chng trnh ay u : chung c bien dch rieng va lien ke t v i nhau e tao
chng trnh kha thi. Trong khi lien ket, cac tham chieu ngoai c thay the vi a ch ung cho cac lenh CALL.

Name: dung e at te n cua module o i t ng c sinh ra trong chng trnh hien hanh.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . '3
Ca ch s dung: Name module_name
Trong o module_name la te n module.
~ | ~ | | | --|:
Segment la kho i ch a ma lenh hay vu ng nh ch a d lie u ma assembler tao ra t ma hay
d lieu trong tap tin nguo n hp ng 8051. Khi assembler gap ch dan chon segment th no se
chuyen hng ma hoa c d lieu theo sau vao segment c chon cho en khi gap ch dan chon
segment kha c.
.RSEG (relocatable segment segment tai nh v c): cho phep chon segment tai nh
c ma a nh ngha tr c bang segment.
Ca ch s dung: Rseg segment_name
Trong o segment_name la ten segment a nh ngha tr c o.
Ch dan nay chuyen hng ma va d lieu theo sau vao oan segment_name cho en khi
gap ch dan chon segment kha c.
Ca c kie u ch dan chon segment CSEG/ DSEG/ ISEG/ XSEG Cho phe p chon 1 segment
tuye t oi.
Ca ch s dung: aSEG [at address]
Trong o a co the la C, D, I, B hoa c X va address la a ch.
V du:
LOC OBJ LINE SOURCE

1 onchip SEGMENT data ;cac segment tai nh v c
2 EPROM SEGMENT code
3
---- 4 bseg at 70h ;segment bit tuyet o i
5
0070 6 flag1: dbit 1
0071 7 flag2: dbit 2
8
---- 9 Rseg onchip ;bat au segment d lieu ta i nh v c
0000 10 total: ds 1
0001 11 count: ds 1
0002 12 sum16: ds 2
13
---- 14 rseg eprom ;bat au segment ma tai nh v c
0000 750000 F 15 begin: mov total,#0
16
17 end
VI. CA C |E U KH|E N CUA 1kNH 8|EN D|CH A55EM8LEk:
Ta co the at ca c dong ieu khien trong chng trnh nguon e ieu khien nh ng cong vie t
nh ket xua t (listing). Mo i dong ieu khien bat au bang ky t ch $ va theo sau no la danh
sa ch cac t khoa ieu khien ca ch nhau b i khoa ng trong.
Pha n ln cac t khoa ieu khie n nay co da ng tch c c va khong tch cc va thng t khoa
ieu khien vie t tat bang 2 ky t .
Mot so t khoa ieu khien thong dung c liet ke bang sau:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
. | .


Ten ieu khien Vie t ta t Mac nhien Y ngha
PAGELENGTH(n) PL PL(60) a t so dong to i a cho moi trang cu a tap tin ket xuat (ta m v
tr t 10 en 65536)
PAGEWIDTH(n) PW PW(120) a t so ky t to i a cho mo i dong (ta m tr t 72 en 132)
XREF/NOXREF XR/NOXR NOXR Tao bang liet ke tham chieu cheo cu a ta t ca cac ky hieu s
dung trong chng trnh.
EJECT EJ Tiep tuc ke t xua t trang ke.
GEN/NOGEN GE/NOGE NOGE Tao ra ay u cac khai trien macro.
INCLUDE
(filename)
IC Ch tap tin co ten filename xem nh la mot phan cu a chng
trnh.
LIST/NOLIST LI/NOLI LI In nhng dong ma nguon lien tuc trong tap tin ke t xuat.
SAVE/RESTORE SA/RS Lu tr nhng ca i a t ieu khien cho LIST va GEN, va lay la i
t ngan xep.
TITLE(string) TT a t chuo i ky t string au trang toi a 60 ky t.
VII. H0A 1 0 NC L|E N KE 1 L|NKEk:
Vi nhng ng dung ln ng i lap trnh th ng chia chng trnh thanh nhieu chng trnh
con hay ca c module va co the ta i nh v c.
Ta ca n chng trnh lie n ket va nh v e ket hp ca c module thanh mot module o i t ng
tuye t o i ma co the thc thi c. Ta t ca ca c ky hieu ngoa i c thay the bang ca c gia tr ung
va c a t vao trong ca c tap tin xuat c minh hoa nh hnh 5-2:

H|nh 5-2. Hcot dcng cu o chucng tr|nh ||nker cc ten |o kL5.
VIII. 5U DU NC MACk0:
Phng tie n x ly macro cua ASM51 la phng tie n thay the chuoi ky t. Macro cho phep
ca c phan ma s dung th ng xuyen se c nh ngha mo t lan bang ca ch dung t g i nh n
gian va co the s dung ba t ky cho nao trong chng trnh bang cach chen va o t g i nh o.
Ta co the nh ngha macro ba t ky cho nao trong chng trnh nguon va sau o s dung
nh ca c lenh khac. Cu phap cu a nh ngha macro nh sau:
%*define (call_pattern) (macro_body)
Trong o call_pattern la t gi nh do ng i dung nh ngha va macro_body la tha n macro
chnh la oan chng trnh thng lap lai.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . '
e phan bie t v i ca c lenh that th ng i ta a t them ky hieu % tr c te n macro va khi
hp dch th ta t ca cac lenh trong than macro c thay the vao ni goi chng trnh macro.
V du: Neu nh ngha macro sau au tap tin nguon
%*define (push_dptr)
(
push dph
push dpl
)

Th khi gap pha t bieu %push_dptr trong chng trnh nguon th trnh bien dch se thay the bang 2 lenh tren
push dph
push dpl
trong tap tin .lst

Ca c tie n li khi s dung macro:
- chng trnh nguon co s dung macro th de o c hn v t g i nh cu a macro cho bie t y
ngha cu a cong viec pha i th c hie n.
- Chng trnh ngan gon hn nen t anh may hn.
- S dung macro se lam giam bt lo i.
- S dung macro giup cho ng i la p trnh khong pha i ban ron v i nh ng chi tiet cap thap.
' |.-| :|~ - | ~
Macro v i ca c tham so c truyen t chng trnh chnh co dang nh sau:
%*define (macro_name (parameter_list)) (macro_body)
Trong o macro_name la ten macro, parameter_list la danh sach ca c tham so va
macro_body la than macro.
V du: nh ngha macro co truyen tham so nh sau
%*define (cmpa#(value))
(
Cjne a,#%value, $+3
)

Th khi goi pha i truyen tham so nh sau:
%Cmpa#(20H)
Khi bien dch se tr thanh
Cjne a,#20h,$+3
Chu y : lenh cjne la lenh 3 byte, do o $+3 chnh la a ch cua lenh ke na m sau lenh cjne.

V du: e thc hien lenh nhay neu ln hn th ta co the tao macro nh sau:
%*define (JGT(value,label))
(
Cjne a,#%value+1,$+3 ;so sanh v i gia tr (value+1) roi nhay ti $+3
Jnc %label ;nhay ti label neu c C = 0 co ngha la ln hn
)


\~ . ||~ | | :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
. | .
Ta cung co the s du ng ca c nhan cu c bo trong macro co dang nh sau:
%*define (macro_name [(parameter_list)])
[local list_of_labels](macro_body)
Trong o macro_name la te n macro, parameter_list la danh sach cac tham so,
list_of_labels la danh sach ca c nhan cu c bo va macro_body la tha n macro.
V du: nh ngha macro co nhan cu c bo nh sau
%*define (dec_dptr) local skip
(
Dec dpl
Mov a,dpl
Cjne a,#0FFh,%skip
Dec dph
%Skip:)
Khi macro c goi
%dec_dptr
Th trnh bien dch se thay the lenh goi tren bang cac lenh a nh ngha trong macro file .lst nh sau:
Dec dpl
Mov a,dpl
Cjne a,#0FFh,skip00
Dec dph
Skip00:
Nhan cuc bo khong quan he v i nhan co cu ng ten trong chng trnh chnh v trnh bien dch ASM51 a t
ong them va o ma so i theo sau nhan cuc bo khi bien dch.
Nhng neu chu ng ta nh ngha macro nh sau th khi bien dch ASM51 se khong oi ten nhan cuc bo:
%*define (dec_dptr) local skip
(
Dec dpl
Mov a,dpl
Cjne a,#0FFh,skip
Dec dph
Skip:)

~ .| / ~ : ~
:La mot trong cac macro duo xay dng san trong assembler. Cu pha p:
%repeat (expression) (text)
Trong o expression la bieu thc va text la van ban can lap la i.
V du: e thc hien 100 lenh NOP th ta co the s dung macro repeat nh sau:
%repeat(100)
(
nop
)
Khi bie n dch th trong file .lst se thay hang lenh tren bang 100 lenh NOP.
- ~ :~ .| -| | -|
ASM51 cung cap ca c nh ngha macro luong ieu khien e cho phep h p dch co ieu
kien ca c phan ma. Dang lenh macro nh sau:
Cu phap:
%IF (expression) THEN (balaneced_text)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | .
[ELSE (balaneced_text)] FI
Trong o expression la bieu thc va balaneced_text la van ban ca n thay o i theo ie u kien.
V du: e thc hien 100 lenh NOP th ta co the s dung macro repeat nh sau:

intenal equ 1
; 1= 8051 serial I/O drivers
; 0= 8052 serial I/O drivers

%IF (intenal) THEN
(inchar: ;8051 drivers
;cac lenh nhan ky t hay d lieu

Outchar: ;cac lenh truyen ky t hay d lieu

)ELSE
(inchar: ;8052 drivers
;cac lenh nhan ky t hay d lieu

Outchar: ;cac lenh truyen ky t hay d lieu

)
Neu bien INTENAL = 1 th thc hien viec truyen d lieu theo kieu cua 8051 nhng neu INTENAL = 0 th
truyen d lieu theo kieu cu a 8052.

IX. CA CH V|E1 CHU0NC 1kNH H0 P :
Cach vie t chng trnh h p ng :
Gi i thieu::
- a c iem cua chng trnh la: giam b t m c o phc tap, cong vie c hay tac vu x ly ro
rang tien l i cho viec g ro i va chnh s a.
- Ca c ky thua t e pha t trien chng trnh: lap trnh co cau tru c, mot chng trnh co cau
tru c ch a mo t he pha n cap cac chng trnh con.
- Ch co 3 cau tru c trong chng trnh: ca c pha t bieu, ca c vong lap va cac l a cho n.
- a c tnh cu a ca u truc: mo i ca u tru c co mot iem vao duy nhat va mo t iem ra duy nha t.
- iASM51 cung cap ca c nh ngha macro luong ieu khien e cho phep hp dch co ieu
Pha n tch 3 cau tru c::
Phat bieu (statement):
V du1: Gan gia tr cho 1 bien bang pseudo-code (ma gia) la
[count=0]
V du2: Goi 1 chng trnh con bang pseudo-code
Print_string(select option)
V du3: Khoi pha t bieu bang pseudo-code
Begin
[statement1]
[statement2]
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
- . | .
[statement3]
end
Cau tru c lap (while/do):
V du1: Minh hoa mot cau truc while/do khi c C = 1 th thc thi phat bieu:
pseudo-code:
while [c==1] do [statement]
ma 8051:
enter: JC skip
(phat bieu)
skip: (tiep tuc)
V du2: Yeu cau bai toan: Chng trnh con tnh tong SUM cua mot chuo i so: tong so d lieu da ng byte
lu trong thanh ghi R7 va a ch ba t au cu a vung d lieu lu trong thanh ghi R0, ket qua tong lu trong thanh ghi
A.
Vi pseudo-code:
[summ==0]
while [chieu_dai>0] do
begin
[sum= sum+@pointer]
[pointer=pointer+1] ;tang pointer
[chieu_dai=chieu_dai - 1] ;gia m chieu dai
end
Vi ma lenh 8051 (vie t theo pseudo-code)
Sum: clr a
Loop: cjne r7,#0, continues
Jmp exit
continues: add a,@r0
inc r0
dec r7
jmp loop
exit: ret
Vi ma lenh 8051 (vie t gon hn)
Sum: clr a
continues: add a,@r0
inc r0
djnz r7,continues
ret
Trong kieu th 3 nay chng trnh c viet gon hn nh pha i thoa ieu kien la R7 > 0.
V du 3: Yeu cau bai toan: s dung cau truc while/do e thc thi pha t bieu khi ca thanh ghi A khac
0Dh va thanh ghi R7 khac 0.
Pseudo-code:
While [acc != 0 and R7!= 0] do [statements]
Vi ma lenh 8051 (vie t theo pseudo-code)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | .
enter: cjne a,#0DH, skip
Jmp exit
skip: cjne R7,#0,statement
jmp exit
statement:

Jmp enter
Exit:

Cau tru c vong lap (repeat/until):
V du1: Yeu cau bai toan: vie t chng trnh con tm kie m ma ky t Z trong mot chuoi ky t (ke t thuc
bang ky t rong) co a ch ba t au lu thanh ghi R0. Thanh ghi A c tra ve v i ky t ASCII Z ne u no co trong
chuoi ky t, ngc lai th thanh ghi A cha gia tr 0.
pseudo-code:
repeat
[Acc=@pointer]
[increament pointer]
Until [acc ==Z or Acc=0]
Vi ma le nh 8051 (vie t theo pseudo-code)
statement: mov a,@r0
inc r0
jz exit
cjne a,#Z,statement
exit: ret

Cau tru c la chon (if/then/else):
V du: Yeu cau ba i toan: vie t chng trnh kie m tra thanh ghi A: neu MSB cu a thanh ghi A bang 1 th nap
gia tr FFH va ngc la i nap 00H vao thanh ghi A.
pseudo-code:
if [MSB ACC ==1]
then [ACC = FFH]
else [ACC=0]
Vi ma le nh 8051
begin: rlc a
jnc statement2
statement1: mov a,#0ffh
jmp exit
statement2: clr a
exit:

Cau tru c la chon (case):
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
. | .
V du: Yeu cau ba i toan: Gia s co mo t chng trnh con co ten la INCH khi c goi e oc cac phm
nhan [0, 1, 2, 3] tra ve ma ASCII cua phm c lu trong thanh ghi A. Hay viet chuo i lenh oc phm nhan va
thc thi oan lenh tng ng.
pseudo-code:
case [kytu] of
0: [statement 0]
1: [statement 1]
2: [statement 2]
3: [statement 3]
End_case
Vi ma lenh 8051
Call inch
Cjne a,#0,skip1
Xulyphim0: ;x ly cong viec ng v i phm 0
Jmp exit ;ket thuc
Skip1: Cjne a,#1,skip2
Xulyphim1: ;x ly cong viec ng v i phm 1
Jmp exit
Skip2: Cjne a,#2,skip3
Xulyphim2: ;x ly cong viec ng v i phm 2
Jmp exit
Skip3: Cjne a,#3,exit
Xulyphim3: ;x ly cong viec ng v i phm 3
Jmp exit

Phong ca ch lap trnh:
Chng trnh pha i ro ra ng va nhat quan v ieu nay rat quan trong neu chung ta lap trnh
theo nhom gom nhieu ng i.
Ca ch a t ten nhan trong chng trnh: ca c nhan ch co the c ga n mo t lan trong mo i
chng trnh. S dung cac nha n pha i lam cho vie c oc chng trnh ro rang va de dang hn.
Vi ca c lenh c lap lai th cac nhan nen a t ten la: loop, back, more,
Vi lenh re nha nh chng trnh va nha y qua nhie u lenh th nen at ten nhan la: skip,
ahead,
Vi le nh re nhanh chng trnh khi o c bit trang tha i th nen a t ten nhan la : wait, again,..
Co the s dung cac te n nhan c sap xe p tuan t nh: skip1, skip2, skip3,
Ca ch chu thch lenh va chu thch khoi: khong nen chu thch mo i dong ma lenh ma ch chu
thch nh ng lenh co ngha an.
V du 1: cac dong lenh sau th can chu thch:
Push 00H ;ca t R0 vao ngan xep
Mov R0,#60h ;R0 quan ly vung nh co a ch ba t au t 60h
Mov R7,#31 ;R7 dung la m bo em
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| |: || | . -| | - | `'
. | . 7

V du 2: cac dong lenh sau th khong can pha i chu thch v lenh a ro rang:
Push ACC ;ca t A vao ngan xep
Inc R0 ;tang R0
Dec r7 ;gia m bo e m1

Ca c chu thch kho i co ba t au mo i chng trnh con va ca c chu thch bao gom:
- Ten cu a chng trnh con.
- Chc nang va gi i han cu a chng trnh on.
- Ca c ieu kie n nhap xua t hay con goi la d lieu au va o va d lieu sau khi x ly.
- Ca c chng trnh con kha c s dung trong chng trnh con nay.
- Ca c thanh ghi c s du ng.
V du : Mot chng trnh minh ho a:
;-------------------------------------------------------
;ten chng trnh:
;chc nang:
;d lieu vao:
;d lieu ra:
;cac chng trnh con s dung:
;cac thanh ghi co s dung:
;-------------------------------------------------------

Lu tr noi dung thanh ghi va khoi phu c:
Khi go i mot chng trnh con th chng trnh con o se s dung mo t so thanh ghi e phu c
vu cho viec x ly nen chung ta pha i tien hanh vie c lu d lieu cua cac thanh ghi o va o ngan
xep va tien hanh la y la i tr c khi ke t thuc chng trnh con.
To ch c chng trnh:
Ca c phan chng trnh c sap xep theo th t nh sau:
- Cho bang
- Ca c lenh kh i tao tr.
- Chng trnh chnh.
- Ca c chng trnh con.
- Khai bao vung d lie u hang so.
- Ca c o nh d lieu RAM (s dung ch dan DS).




Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| ~ : :|| | : || | . -| | -| `'
` . | .




Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M






Chng 6





CAC L|NH K|EN 50
' |:
~ ~
-
- || \ .~ /\
CAC L|NH K|EN |C NC0A| V|
' |~ . ~ : :|| `/
|~ . ~ : :|| `73
1H|E1 KE HE 1H0NC V| XU L
' ~ :|-: - - '
~ :|-: - -
8A| 1AP





Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .

|. CAC L|NH K|EN |C 50:
. |C chct:
Trong he thong vi x ly co a hp a ch va d lieu th khi ket noi vi x ly vi he thong phai
tien hanh tach rie ng t ng ng a ch va d lieu. Cac IC thng c dung e tach a ch va d
lieu la IC chot 8 bit 74573.
S o chan va s o k hie u logic nh hnh 6-1:

H|nh 6-. 5c dc chon vo sc dc |cg|c |C chct 74573.
Bang trang thai hoat ong cua IC chot 74573.

8ong 6-. 8ong trong tho| hcot dcng cuo |C chct 74573.
Trong bang trang thai th khi ngo cho phep chot (LE) m c cao (H) th d lieu ngo ra Q phu
thuoc vao d lieu ngo vao D (Q = D), khi LE mc thap d lieu tai cac ngo ra c chot lai khong
phu con phu thuoc d lieu cua ngo vao (khong thay oi) bat chap d lieu ngo vao. Khi o ta xem
d lieu a c chot lai.
Chan ieu khien cho phep OE tch cc mc thap. Khi OE mc cao th 8 ngo ra se trang
thai tong tr cao.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 231
2. |C g|o| mo
Trong he thong vi x ly thng ket noi vi nhieu bo nh va nhieu thiet b ngoai vi khac
nhau. V cac bo nh va thiet b ngoai vi s du ng chung bus a ch va bus d lieu nen phai tien
hanh giai ma e phan chia cac vung nh khac nhau cho cac bo nh va cac thiet b ngoai vi sao cho
mot a ch tng ng vi 1 o nh duy nhat.
Cac IC so th ng c du ng e giai ma bo nh trong cac he thong vi x ly la cac cong logic,
cac IC giai ma va cac IC PROM hay PAL,
Trong phan nay gii thieu cac IC giai ma thng s dung nh IC 74139 giai ma 2 ng sang
4 ng va IC 74138 giai ma 3 ng sang 8 ng:
S o chan va s o k hie u logic nh hnh 6-2:

H|nh 6-2. 5c dc chon vo sc dc |cg|c |C g|o| mo 7438.
Bang trang thai hoat ong cua IC giai ma 74138.

8ong 6-2. 8ong trong tho| hcot dcng cuo |C g|o| mo 7438.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .
Trong bang trang thai th 3 trang thai au th IC khong c phep giai ma tat ca cac ngo ra
eu mc H.
8 trang thai con la i cho phe p giai ma 8 ngo ra theo 3 ngo vao, moi mot trang thai co 1 ngo ra
mc L.
Khi can nhie u ngo ra th ket noi nhieu IC giai ma 74138 v du nh can giai ma 5 ng sang
32 ng th se dung 4 IC giai ma ket noi nh hnh 6-3:

H|nh 6-3. Ket nc| 4 |C g|o| mo 7438 de g|o| mo 5 ducng song 32 ducng.
Cung ho vi IC giai ma 74138 th con IC giai ma 74139 giai ma 2 ng sang 4 ng co s
o chan va s o logic nh hnh 6-4:

H|nh 6-4. 5c dc chon vo sc dc |cg|c |C g|o| mo 743.
Trong IC giai ma 74139 co 2 mach giai ma 2 ng sang 4 ng.
Bang trang thai hoat ong cua IC giai ma 74139.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 233

8ong 6-3. 8ong trong tho| hcot dcng cuo |C g|o| mo 743.
trang thai au tien th chan cho phep E mc H se khong cho phep giai ma, cac ngo ra
mc H. 4 trang thai con la i th cho phep giai ma, ngo ra tch cc mc thap.
3. |C dem:
Cac IC em co dong vao, dong ra, ap vao, ap ra ln e em cac ng a ch va cac ng
d lieu khi vi x ly ket noi vi nhie u IC nh hay IC ngoai vi. Co 2 dang IC em: em 1 chieu va
em 2 chieu.
em 2 chieu thng s dung la IC 75245 co s o chan va s o logic nh hnh 6-5:

H|nh 6-5. 5c dc chon vo sc dc |cg|c |C dem 74245.
Bang trang thai hoat ong cua IC em 74245.

8ong 6-4. 8ong trong tho| hcot dcng cuo |C dem 74245.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
- . | .
Trong bang trang thai th 2 trang thai au OE = L cho phep IC hoat ong va neu Direction =
L th d lieu truyen t bus ngo vao B sang bus ngo ra A, Direction = H th d lieu truyen t bus ngo
vao A sang bus ngo ra B. Khi OE = H th cac bus trang thai tong tr cao.
IC 74245 thng c dung e em bus d lieu hi chieu cua vi x ly.
Ngoai IC em 2 chieu con co cac IC em mot chieu 74244 va IC em ao 74240:
em 1 chieu thng s dung la IC 75244 co s o chan va s o logic nh hnh 6-6:

H|nh 6-6. 5c dc chon vo sc dc |cg|c |C dem 74244.
Trong s o logic hnh 6-7 se thay ro rang hn ve IC em 74244: trong IC 74244 chia ra lam
2 khoi em 4 bit hoat ong oc lap.

H|nh 6-7. 5c |cg|c |C dem 74244 ch|o ro |om 2 khc|.
Bang trang thai hoat ong cua IC em 74244.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 235

8ong 6-5. 8ong trong tho| hcot dcng cuo |C dem 74244.
Trong bang trang thai th 2 trang thai au OE = L cho phep IC em d lieu. Khi OE = H th
cac ngo ra trang thai tong tr cao.
IC 74240 la IC em hoat o ng giong nh IC em 74244 nhng ngo ra b ao.
4. |C nhc EPk0M, 5kAM
Trong cac he thong vi x ly luo n s dung bo nh ROM va bo nh RAM e lu chng trnh
va d lieu x ly. Tuy thuoc vao yeu cau ma he tho ng bo nh c s dung nhie u hay t.
Cac bo nh ROM thng s dung la loai EPROM ho 27xxx, loai EEPROM ho 28xxx, ho
29xxx.
Bo nh RAM s dung nhie u loai va pho bien la ho 62xx.
Khao sat EPROM ho 27xxx: bao gom 2716 en 27512 nh hnh 6-8:

H|nh 6-8. 5c dc chon |C nhc hc 27xxx.
Trong s o tong quat hnh 6-8 th cac IC 2716 va 2732 ch co 24 chan, cac IC t 2764 e n
27512 th co 28 chan, e quan sat ro hn hay xem hnh 6-9. EPROM 2716 co dung lng 2 Kbyte,
2732 co dung lng 4Kbyte, 2764 co dung lng 8Kbyte.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .

H|nh 6-. 5c dc chon |C276, 2732, 2764.
Ten cua cac chan nh bang 6-6:

8ong 6-6. 1en coc chon cuo |C nhc hc 27.
Bang trang thai hoat ong cua cac IC nh 2716, 2732 va 2764: nh cac bang 6-7, 6-8, 6-9.

8ong 6-7. 8ong trong tho| hcot dcng cuo |C276.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 237

8ong 6-8. 8ong trong tho| hcot dcng cuo |C2732.

8ong 6-. 8ong trong tho| hcot dcng cuo |C2764.
Trong bang trang thai liet ke ay u cac trang thai nhng neu s dung trong he thong giao
tiep vi vi x ly th ch can quan tam en cac trang thai sau: Read, Standby va Output
Disable. Cac trang thai con lai danh cho cac nha nghien cu thiet ke bo nap Eprom.
Khi s dung bo nh ket noi vi vi x ly th can phai quan tam en cac thong so cua IC nh
cho ay u trong datasheet cua IC. Co rat nhieu thong so nhng ngi s dung can biet cac thong
so c ban o la thi gian try xuat bo nh . Hnh 6-10 trnh bay dang song truy xuat bo nh cua IC
nh 2716:

H|nh 6-0. Dong scng truy xuot bc nhc 276.
Phan tch dang song: sau khi bo nh 2716 nhan c a ch a xac lap, tiep theo chan ie u
khien CE chuyen trang thai t H xuo ng mc L e cho phep bo nh, tiep theo chan OE chuyen
trang thai t H xuong mc L e cho phe p xuat d lieu.
Nhn vao dang song th ta thay co 3 thong so:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
` . | .
- Thong so t
OE
c tnh t khi chan OE xuong mc L cho en khi d lieu xuat ra bus.
- Thong so t
CE
c tnh t khi chan CE xuong mc L cho en khi d lieu xuat ra bus.
- Thong so t
ACC
c tnh t khi a ch xac lap cho en khi d lieu xuat ra bus (thong so
nay thng c goi la thi gian truy xuat).
Ca 3 thong so c cho bang 6-10. Trong bang nay liet ke cac thong so cho 2 loai bo nh.

8ong 6-0. 8ong thc ng sc thc| g|on hcot dcng cuo |C276.
Khao sat SRAM ho 62xxx: bao gom 6264 va 62256, 62512:
/\ - bo nh nay co dung lng 8 kbyte co s o chan va s o logic nh hnh 6-11:

H|nh 6-. 5c dc chon vo sc dc |cg|c |C nhc 6264.
Ten cua cac chan nh bang 6-11:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 239

8ong 6-. 1en coc chon cuo |C nhc 6264.
Bang trang thai hoat ong cua cac IC nh 6264 nh bang 6-12:

8ong 6-2. 8ong trong tho| hcot dcng cuo |C6264.
unq .onq !or vv |iev v vo nno 1Z^ !! .u rur nonq .o. Tng t nh bo nh EPROM khi s
dung phai quan tam en cac thong so truy xuat bo nh oc va ghi d lieu. Hnh 6-12 trnh bay
dang song oc d lieu t bo nh cua RAM 6264:

H|nh 6-2. Dong scng dcc bc nhc kAM 6264.
Phan tch dang song: sau khi bo nh 6264 nhan c a ch a xac lap, tiep theo cac chan
ieu khien CE1 va CE2 chuyen sang trang thai cho phep bo nh, tiep theo chan OE chuyen trang
thai t H xuong mc L e cho phe p xuat d lieu.
Nhn vao dang song th ta thay co 3 thong so:
- Thong so t
DOE
c tnh t khi chan OE xuo ng mc L cho e n khi d lieu xuat ra bus.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
- . | .
- Thong so t
ACE1
c tnh t khi chan CE1 xuong mc L cho en khi d lieu xuat ra bus.
- Thong so t
ACE2
c tnh t khi chan CE2 xuong mc L cho en khi d lieu xuat ra bus.
- Thong so t
AA
c tnh t khi a ch xac lap cho en khi d lieu xuat ra bus (tho ng so
nay thng c goi la thi gian truy xuat).
Ca 3 thong so c cho bang 6-13.

8ong 6-3. 8ong thc ng sc thc| g|on hcot dcng dcc cuo |C 6264.
unq .onq qni vv |iev .uo vo nno 1Z^ !! .u rur nonq .o. dang song ghi d lieu vao o nh cua
RAM 6264 nh hnh 6-13:

H|nh 6-3. Dong scng gh| du ||eu voc bc nhc kAM 6264.
Phan tch dang song: sau khi bo nh 6264 nhan c a ch a xac lap, tiep theo cac chan
ieu khien CE1 va CE2 chuyen sang trang thai cho phep bo nh, tiep theo chan WE chuyen trang
thai t H xuong mc L e cho phe p ghi d lieu.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 241
Nhn vao dang song th ta thay co 3 thong so:
- Thong so t
PWE
c tnh t khi chan WE xuong mc L cho en khi d lieu c ghi.
- Thong so t
SCE1
c tnh t khi chan CE1 xuong mc L cho en khi d lieu c ghi.
- Thong so t
SCE2
c tnh t khi chan CE2 xuong mc L cho en khi d lieu c ghi.
- Thong so t
WC
c tnh t khi a ch xac lap cho en khi d lieu c ghi (tho ng so nay
thng c goi la chu ky ghi).
Ca 3 thong so c cho bang 6-14.

8ong 6-4. 8ong thc ng sc thc| g|on hcot dcng gh| cuo kAM 6264.
/\ bo nh nay co dung lng 32 kbyte co s o chan va s o logic nh hnh 6-14:

H|nh 6-4. 5c dc cho n vo sc dc |cg|c |C nhc 62256.
Ten cua cac chan va bang trang thai hoat ong nh bang 6-17:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
- . | .

8ong 6-7. 1en coc chon cuo |C nhc 62256.
||. CAC L|NH K|EN |C NC0A| V| :
. |C ngco| v| |op tr|nh 8255A:
Vi x ly khong the trc tiep ieu khien cac thiet b du ch ieu khien 1 bong en Led, moi
thiet b ngoai vi muon giao tiep vi vi x ly eu thong qua cac IC ngoai vi.
Cung vi vi x ly th co rat nhieu IC ngoai vi co cac chc nang khac nhau phuc vu cho vi x
ly nh a trnh bay phan trc. Mot trong nhng IC c s dung pho bien nhat la IC ngoai vi
8255A.
IC ngoai vi 8255A c che tao theo cong nghe LSI du ng e giao tiep song song gia vi x
ly va thiet b ben ngoai.
Vi mach 8255A thng c goi la mach giao tiep ngoai vi lap trnh c (Programmable
Peripheral Interface PPI). Do kha nang a nang trong cac ng du ng thc te nen 8255A la vi
mach giao tiep c dung rat pho bien cho cac he vi x ly 8 bit 16 bit.
(a) S o chan va s o kho i cua 8255A :
IC 8255A co 40 chan nh hnh 6-15 va s o cau truc be n trong nh hnh 6-16:

H|nh 6-5. 5c dc cho n vo sc dc |cg|c cuo |C 8255A.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 243


H|nh 6-6. 5c dc khc| cuo |C 8255A.
Ten cac chan cua IC 8255A nh bang

8ong 6-8. 1en coc chon cuo |C 8255A.
IC 8255A giao tiep vi vi x ly thong qua 3 bus: bus d lieu 8 bit D
7
-D
0
, bus a ch A
1
A
0
, bus
ieu khien RD, WR, CS , RESET.
Ma lenh va d lieu eu c truye n tre n 8 ng d lieu D7-D0. Vi x ly gi d lieu
en 8255A hoac vi x ly oc d lieu t 8255A tu y thuoc vao lenh ieu khien. Cac ng tn hieu
RD, WRcua 8255A c ket noi vi cac ng RD, WRcua vi x ly.
Tn hieu RESET dung e khi o ng 8255A khi cap ien, khi b reset cac thanh ghi cac
thanh ghi ben trong 8255A eu b xoa va 8255A trang thai san sang lam viec. Khi giao tiep vi
vi x ly ngo vao tn hieu RESET nay c ket noi vi tn hieu RESET ngo ra cua vi x ly.
Tn hieu Chip Select ( CS ) dung e chon 8255A khi vi x ly giao tiep vi nhieu 8255A.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
-- . | .
8255A co 3 port xuat nhap (I/O) co ten port A, port B, port C, moi port 8 bit. Port A gom cac
bit PA
0
-PA
7
, port B gom cac bit PB
0
-PB
7
va port C gom PC
0
-PC
7
. Cac port nay co the la cac port
input hoac output tuy thuoc vao lenh ieu khien, lenh ieu khie n do vi x ly gi ti lu trong thanh
ghi ieu khien e nh cau hnh lam viec cho 8255A.
Cac a ch A
1
A
0
cua 8255A dung e la chon cac port va thanh ghi, A
1
A
0
=00
2
du ng e cho n
Port A, A
1
A
0
= 01
2
dung e chon Port B, A
1
A
0
= 10
2
du ng e chon Port C, A
1
A
0
= 11
2
dung e chon
thanh ghi ieu khie n.
Trong s o khoi 8255A, cac port I/O chia lam hai nhom: nhom A gom port A va 4 bit cao
cua port C, nhom B gom port B va 4 bit thap cua port C. e s dung cac port cua 8255A ngi lap
trnh phai gi t ieu khie n ra thanh ghi ieu khien e 8255A nh cau hnh cho cac port ung theo
yeu cau ma ngi lap trnh mong muon.
(b) Cau truc t ieu khien cua 8255A:
Trong 8255 co 2 t ie u khien: Control Word va Bit Set/Reset.
T ieu khien Control Word co cau truc nh hnh 6-17:

H|nh 6-7. Cou truc tu d|eu kh|e n thu nhot cuo |C 8255A.
Do cac port cua 8255A c chia lam hai nhom: nhom A va nhom B tach ri nen t ieu
khien cua 8255A cung c chia lam hai nhom.
~ ::

'

|| - || ~| ||| | ||
Bit D
0
dung e thiet lap 4 bit thap cua port C:
D
0
= 0 port C thap la port xuat d lieu (output)
D
0
= 1 port C thap la port nhap d lieu (input).
Bit D
1
dung thiet lap port B:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 245
D
1
= 0 port B la port xuat d lieu (output)
D
1
= 1 port B la port nhap d lieu (input).
Bit D
2
dung thiet lap Mode ie u khien cua nhom B:
D
2
= 0: nhom B hoat ong Mode 0.
D
2
=1: nhom B hoat ong Mode 1.
~ ::

.
-
.

|| - || ~| ||| | || /
Bit D
3
dung e thiet lap 4 bit cao cua port C:
D
3
= 0 port C la port xuat d lieu (output)
D
3
= 1 port C la port nhap d lieu (input).
Bit D
4
dung e thiet lap port A:
D4 = 0 port A la port xuat d lieu(output)
D4 = 1 port A la port nhap d lieu (input).
Bit D
6
D
5
dung thiet lap Mode ieu khien cua nhom A:
D
6
D
5
= 00: nhom a hoat ong Mode 0.
D
6
D
5
= 01: nhom A hoat ong Mode 1.
D
6
D
5
= 1X: nhom A hoat ong Mode 2.
Coc nhc m A, 8 |om v|ec c cou h|nh c Mcde 0:
T ieu khien thiet lap nhom A & B hoat ong Mode 0 nh hnh 6-18:

H|nh 6-8. 1u d|eu kh|en kh| 2 nhcm A, 8 |om v|e c c mcde 0.
Mode 0 cac port A, port B, port C thap va port C cao la cac port xuat hoac nhap d lieu
oc lap. Do co 4 bit e la chon nen co 16 t ieu khie n khac nhau cho 16 trang thai xuat nhap cua
4 port.
Coc nhc m A & 8 |om v|e c c cou h|nh c Mcde :
T ieu khien nhom A, B hoat ong Mode 1 nh hnh 6-19:

H|nh 6-. 1u d|eu kh|en kh| 2 nhcm A, 8 |om v|e c c mcde .
Mode 1 cac port A & B lam viec xuat nhap co chot (Strobe I/O). Mode nay hai port A
& B hoat ong oc lap vi nhau va moi port co 1 port 4 bit ie u khien. Cac port 4 bit ieu khien
c hnh thanh t 4 bit thap va 4 bit cao cua port C.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
- . | .
Khi 8255A c cau hnh Mode 1, thiet b giao tiep muon 8255A nhan d lieu, thiet b o
phai tao ra tn hieu yeu cau 8255A nhan d lieu, ngc lai 8255A muon gi d lieu e n thiet b
khac, 8255A phai tao ra tn hieu yeu cau thiet b o nhan d lieu, tn hieu ye u cau o goi la tn
hieu Strobe.
|~| :| || / ~ .- ~| ||| \- '
Port A c cau hnh la port nhap d lieu:
Chc nang cua cac ng tn hieu c trnh bay hnh 6-20 va dang song lam vie c cua cac
ng tn hieu ieu khie n nh hnh 6-21:

H|nh 6-20. Pcrt A cuo |C 8255A hcot dc ng c mcde nhon du ||eu.

H|nh 6-2. Dong scng cuo coc duc ng t|n h|eu d|eu kh|e n pcrt A |nput.
Cac ng tn hieu cua port C tr thanh cac ng ie u khien cua port A.
Bit PC
4
tr thanh bit
A
STB (Strobe Input, tch cc mc thap) dung e nhan tn hieu chot t
thiet b gi e bao cho 8255A biet d lieu a c gi e n cac ngo vao PA
7
PA
0
.
Bit PC
5
tr thanh bit IBF
A
(Input Buffer Full, tch cc mc cao), du ng e bao cho thiet b
gi biet bo em a ay ng gi byte tiep theo.
Bit PC
3
tr thanh bit INTR
A
(Interrupt Request, tch cc mc cao), bit nay co mc Logic 1
khi hai bit 1
A
STB , IBF
A
= 1 va bit INTE
A
(Interrupt Enable) ben trong 8255A bang 1. Bit INTE
A

c thiet lap mc logic 1 hay 0 do phan t ieu khien Set/Reset cua 8255A. hnh tre n, bit
INTE
A
= 1 du ng e cho phep tn hieu IBF
A
xuat hien tai ngo ra INTE
A
cua cong AND. Tn hieu
INTR
A
c ket noi vi ngo vao ngat cua vi x ly e bao cho vi x ly biet: d lieu mi a xuat
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 247
hien port A ye u cau vi x ly thc hien chng trnh phuc vu ngat e nhan d lieu tai port A va
xoa yeu cau ngat e ieu khien tn hieu IBF
A
ve mc 0 san sang nhan byte tiep theo.
Cac bit con lai cua port C: PC
6
, PC
7
la cac bit xuat/nhap bnh thng tuy thuoc vao bit D
3

trong t ieu khien. Cac bit c dung e thiet lap cho nhom B.
Port A c cau hnh la port xuat d lieu:
Chc nang cua cac ng tn hieu c trnh bay hnh 6-22 va dang song lam vie c cua cac
ng tn hieu ieu khie n nh hnh 6-23:

H|nh 6-22. Pcrt A cuo |C 8255A hcot dc ng c mcde xuot du ||eu.

H|nh 6-23. Dong scng cuo co c ducng t|n h|e u d|e u kh|en pcrt A cutput.
Bit PC
7
tr thanh bit
A
OBF (Output Buffer Full, tch cc mc thap), khi co d lieu t vi x ly
gi ra port A, tn hieu
A
OBF se yeu cau thiet b ben ngoai nhan d lieu.
Bit PC
6
tr thanh bit
A
ACK (Acknowledge Input, tch cc mc thap) thiet b nhan d lieu
dung tn hieu nay e bao cho 8255A biet tn hieu a c nhan va san sang nhan d lieu tiep theo.
Bit PC
3
tr thanh bit INTR
A
(Interrupt Request, tch cc mc cao), bit nay co mc logic khi
hai bit 1
A
OBF , 1
A
ACK va bit INTE
A
(Interrupt Enable) ben trong 8255A mc 1. Tn hieu
INTR
A
tac ong en ngo vao ngat cua vi x ly e bao cho vi x ly biet: thiet b be n ngoai a nhan
xong d lieu port A va thc hien gi byte d lieu tiep theo.
Cac bit con lai cua port C: PC
4
, PC
5
la cac bit xuat/nhap bnh thng tuy thuoc vao bit D
3

trong t ieu khien. Cac bit dung e thiet lap cho nhom B.
|~| :| || ~ .- ~| ||| \- '
Port B c cau hnh la port nhap d lieu :
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
-` . | .
Chc nang cua cac ng tn hieu c trnh bay hnh 6-24:

H|nh 6-24. Pcrt 8 cuo |C 8255A hcot dcng c mcde nhon du ||eu.
Chc nang cua cac bit ieu khien giong nh nhom A hoat ong Mode 1.
Port B c cau hnh la port xuat d lieu:
Chc nang cua cac ng tn hieu c trnh bay hnh 6-25:

H|nh 6-25. Pcrt 8 cuo |C 8255A hcot dcng c mcde xuot du ||eu.
Chc nang cua cac bit ieu khien giong nh nhom A hoat ong Mode 1.
Cac bit c du ng thiet lap cho nhom A, bit D
0
khong co tac du ng trong trng h p ca
hai nhom cung lam viec Mode 1.
Coc nhc m A |om v|ec c cou h|nh c Mcde 2:
Mode 2 la kieu hoat ong Stable Bi-directional IO, khac vi Mode 1 la port co the xuat
nhap d lieu.
T ieu khien khi hai nhom A hoat ong Mode 2 nh hnh 6-26:

H|nh 6-26. 1u d|eu kh|en hcot dc ng c mcde 2 cuo nhcm A.
Chc nang cua cac ng tn hieu c trnh bay hnh 6-27 va dang song lam vie c cua cac
ng tn hieu ieu khie n nh hnh 6-28:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 249

H|nh 6-27. Nhcm A cuo |C 8255A hcot dc ng c mcde 2.

H|nh 6-28. Dong scng cuo co c ducng t|n h|e u d|e u kh|en nhcm A.
Cac ng tn hieu cua port C tr thanh cac ng ie u khien cua port A.
Bit PC
7
tr thanh bit OBF
A
, PC
6
tr thanh bit
A
ACK , PC
4
tr thanh bit
A
STB , PC
5
tr thanh
IBF
A
va bit PC
3
tr thanh bit INTR
A
. Chc nang cua cac ng tn hieu giong nh Mode 1, ch
khac la tn hieu ngo ra INTR
A
=1, INTE1 = 1 hoac IBF
A
= 1, INTE2 = 1.
Cac bit PC
2,1,0
con lai co the la cac bit I/O tuy thuoc vao bit ieu khien cua nhom B.
Chu y khi nhom A lam viec Mode 2, nhom B ch c phep hoat ong Mode 0.
Cau hnh cua t ieu khien Set/Reset bit INTE khi 8255A hoat ong Mode 1 hoac Mode 2
c trnh bay hnh 6-29:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .

H|nh 6-2. 1u d|eu kh|en thu 2cuo |C 8255A.
Cau hnh nay con cho phep Set/Reset tng bit cua port C. T ieu khien nay khac vi t ie u
khien cau hnh la bit D
7
= 0.
Bit D
0
dung e Set/Reset bit INTE, khi D0 = 1 th INTE = 1 (cho phep ngat), khi D
0
= 0 th
INTE = 0 (kho ng cho phep ngat). Ba bit D
1
, D
2
, D
3
dung e chon mot bit cua port C, gan mc
Logic cua bit D
0
cho 1 bit cua port C a chon.
Trong thc te port A va port B th ng c cau hnh vi nhie u Mode khac nhau. V du nhom
A hoat ong Mode 2 nhom B lam viec Mode 0.
2. IC ngoai vi lap trnh 8279:
IC ngoai vi 8279 la IC chuyen dung e quet led 7 oan va quet ban phm co cau truc nh
sau:
IC 8279 co the hoat ong mode quet ban phm.
IC 8279 co the hoat ong mode cac cam bien.
Hoat ong kieu ngo vao co chot.
Bo nh FIFO co the lu 8 byte ma phm nhan.
Co chc nang khoa 2 phm hoac quet vo ng N phm co chong doi phm.
Co the hien th quet 2 bo quet 4 ng, 8 ng hoac 16 ng.
Quet hien th 8 led 7 oan hoac 16 led 7 oan.
Co cac lenh lap trnh.
Co the lap trnh bo nh Ram cha d lieu hien th 16 byte vi loi vao trai hoac phai.
Co the lap trnh thi gian quet.
Co the tao tn hieu ngat khi co nhan phm.
(a) S o chan va s o kho i cua 8279 :
IC 8279 co 40 chan nh hnh 6-30:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 251

H|nh 6-30. 5c dc chon vo sc dc |cg|c cuo |C 827.
8279 co mot bo nh RAM 16 byte dung e hien th va mot bo nh RAM dung e cha ma
cua phm nhan. Trong 8279 co mot thanh ghi em d lieu, khi d lieu c vi x ly gi ti th no
c chuyen ti thanh ghi nay. Ngoai ra 8279 con co thanh ghi trang thai e ch trang thai cua
8279 tai mot thi iem.
Ten cac chan cua IC 8279 nh bang 6-19:

1en t|n h|eu Hucng t|n h|eu Chuc nong
A0 | Address
D87 D80 |/0 Doto 8us (8|-d|rect|cn|
CLK | C|cck |nput
kE5E1 | keset |nput
C5\ | Ch|p 5e|ect
kD\ | keod |nput
Wk\ | Wr|te |nput
|kQ 0 |nterrupt kequest |nput
5L0-5L3 0 5con L|nes
kL0-kL3 | keturn L|nes
5H|F1 | 5h|ft |nput
CN1L/518 | Ccntrc|/5trcbe |nput
0U1 A3-0 0 D|sp|oy (A| 0utput
0U1 83-0 0 D|sp|oy (8| 0utput
8D\ 0 8|onk D|sp|oy 0utput
8ong 6-. 1e n coc chon cuo |C 827.
IC 8279 ket noi vi vi x ly thong qua 3 bus gom bus d lieu D7-D0, bus a ch co 1 ng
A0, bus ieu khien RD, WR, CS , RESET, IRQ, CLK giong nh 8255A.
Tn hieu cho n CS c ket noi vi mot ngo ra nao o cua IC giai ma a ch IO hoac IC giai
ma a ch bo nh. Neu xem IC 8279 la mot bo nh th bo nh nay co hai o nh .
(b) Cau truc t ieu khien cua 8279:
IC 8279 co 1 ng a ch A0 co chc nang la chon nh sau:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .
- A0 = 0: IC 8279 xem d lieu t vi x ly gi en e cho n d lieu hie n th.
- A0 = 1: IC 8279 xem d lieu t vi x ly gi en la d lieu cua lenh ieu khien 8279.
-|| -.:~/-:~. \- -:
T ieu khien nh hnh 6-31:

H|nh 6-3. 1u d|eu kh|en keybcord/D|sp|oy mcde set cuo |C 827.
Trong o 2 bit DD dung e thiet lap Mode hien th, 3 bit KKK du ng e thiet lap Mode quet
ban phm.
Chc nang cua 2 bit DD:
Ho| b|t DD Chuc nong
00 h|en th| 8 ky tu |c| voc tro|.
0 h|en th| 6 ky tu |c | voc tro|.
0 h|en th| 8 ky tu |c| voc pho|.
h|en th| 6 ky tu |c | voc pho|.
8ong 6-20. Coc mcd h|en th| khoc nhou cuo 827.
Chc nang cua 3 bit KKK:
8o b|t KKK Chuc nong
000 enccde scon keybcord 2 key |cckcut
00 deccde scon keybcord 2 key |cckcut
00 enccde scon keybcord N key rc||cver
0 deccde scon keybcord N key rc||cver
00 enccde scon senscr motr|x
0 deccde scon senscr motr|x
0 strcbe |nput, enccde d|sp|oy scon
strcbe |nput, deccde d|sp|oy scon
8ong 6-2. Coc mcd quet mo tron ph|m khoc nhou cuo 827.
-|| ~
T ieu khien nh hnh 6-32:

H|nh 6-32. 1u d|eu kh|en Prcgrom c|cck cuo |C 827.
Lenh nay co chc nang chia tan so xung clock ngo vao CLK chan so 3, cac bit PPPPP
dung e xac nh so chia nam trong khoang t 1 en 31, tan so hoat ong cua mach quet hien th
va chong doi cua 8279 thng la 100kHz, neu tan so ngo vao la 2MHz th chia cho 20 ta c
100kHz, khi o cac bit PPPPP co gia tr la 10100.
-|| -~ /--|- /\
T ieu khien nh hnh 6-33:

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 253
H|nh 6-33. 1u d|eu kh|en ked F|F0/senscr kAM cuo |C 827.
IC 8279 co 8 byte RAM ben trong e cha ma cua phm an hay ma cua cac sensor c phan
biet bi 3 bit a ch AAA. Trc khi oc d lieu cua o nh nao th ta phai thiet lap t ieu khien
nay cho IC 8279 roi mi tien hanh oc d lieu.
Ta co the thay oi con tro quan ly 8 byte nay bang cach thay oi gia tr cua cac bit AAA
trong t ieu khien. Bo nh thuoc kieu FIFO.
Bit AI (Automatically increment): neu mc 1 th sau khi oc 1 byte th con tro tang len 1 e
co the oc byte ke tiep. Neu AI = 0 con tro se khong thay oi, muon oc byte ke tiep th phai thay
oi a ch bang t ieu khien nay.
-|| -~ -:~. /\
T ieu khien nh hnh 6-34:

H|nh 6-34. 1u d|eu kh|en D|sp|oy kAM cuo |C 827.
IC 8279 co 16 byte RAM be n trong do con tro 4 bit AAAA quan ly, 16 byte RAM nay dung
e cha d lieu can hien th do vi x ly gi ti, vi x ly co the oc lai d lieu cua 1 o nh bat ky
trong vung nh RAM nay bang cach ieu chnh cac bit AAAA tng ng trc khi oc d lieu.
Bit AI (Automatically Increment): mc 1 lam con tro t ong tang len o nh ke e san sang
e oc byte d lieu cua o nh ke. Ne u AI = 0 con tro se khong thay oi.
-|| | |:-|::
T ieu khien nh hnh 6-35:

H|nh 6-35. 1u d|eu kh|en End |nterrupt cuo |C 827.
Bit E = 1 se xoa ngat IRQ ve mc Logic 0.
-|| N:- -:~. /\
T ieu khien nh hnh 6-36:

H|nh 6-36. 1u d|eu kh|en Wr|te D|sp|oy kAM cuo |C 827.
IC 8279 co 16 byte RAM be n trong do con tro 4 bit AAAA quan ly, 16 byte RAM nay dung
e cha d lieu can hien th do vi x ly gi ti, e ghi d lieu vao o nh nao trong vung nh RAM
nay ta co the ieu chnh cac bit AAAA tng ng.
Bit AI (Automatically Increment): mc 1 lam con tro t ong tang len o nh ke e san sang
lu byte d lieu ke. Neu AI = 0 con tro se khong thay oi, do o , byte d lieu sau se ghi e len
byte d lieu trc o.
-|| -~
T ieu khien nh hnh 6-37:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
- . | .

H|nh 6-37. 1u d|e u kh|en C|eor cuo |C 827.
Nhng Bit C
D
trong lenh nay dung e xoa tat ca cac hang cua bo nh RAM hien th sang ma
nhap nhay cho phep la chon nh hnh 6-38:

H|nh 6-38. 8o b|t C
D
cuo tu d|e u kh|en C|eor.
Trong suot thi gian ang xoa bo nh RAM hien th th ta khong c ghi d lieu vao (thi
gian xoa khoang 160s). Bit MSB cua t trang thai FIFO mc 1 trong khoang thi gian xoa va se
tr ve mc 0 sau khi xoa xong. Ngi lap trnh co the kiem tra bit nay e biet khi nao xoa xong e
gi d lieu ra hien th.
Neu nh Bit C
F
tch cc (C
F
=1), t trang thai FIFO se b xoa va ngo ra Interrupt b Reset .
Bit C
A
co chc nang xoa tat ca cac bit, bit nay con ket hp vi bit C
D
va C
F
. Bit C
D
e xoa
ma tren RAM hien th va no cung xoa luon trang thai FIFO.
(c) ng dung IC 8279:
IC 8279 la chuye n ve giai ma hien th Led 7 oan va quet phm theo nhieu phng phap
khac nhau.
D lieu can hien th t vi x ly gi en se c lu trong 16 byte RAM ben trong c goi la
bo nh hie n th. Cac tn hieu nay lan lt gi ra tren 8 ng tn hie u t A[3:0] en B[3:0].
Cac ng tn hieu SL[3:0] dung e quet, d lieu tren ng nay co the c thiet lap theo
kieu ma hoa (Encode) hoac giai ma (Decode) tuy thuoc vao kieu thiet ke phan cng. Cac ng
nay co hai chc nang va quet hien th va quet giai ma ban phm.
Cac ng tn hieu RL[7:0] la cac ng tn hieu input ket hp vi cac ng tn hieu quet
SL[3:0]

tao thanh ma tran phm, phm c nhan se lam cho mot ngo vao RL xuo ng mc 0, ket hp
vi cac ng tn hieu quet se cho biet ma cua phm nhan. Chu y cac ng SL[3:0] phai che o
Decode.
Cac ngo vao SHIFT va CNTL c dung e m rong cac phm to hp.
So lng cac phm thiet ke co the len ti 64 phm ri.
IC 8279 gi d lieu trong vung nh RAM hien th ra cac Led va t ong quet ban phm e
tm phm b tac ong va t cho ng doi, sau o thiet lap ma cua phm b nhan, trc khi s dung IC
8279 ngi s dung phai thiet lap cac t ieu khien gi ra IC 8279 khi bit a ch A0 = 1. D lieu
oc vao hoac gi ra IC 8279 khi bit a ch A0 = 0.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 255
Khi co phm b nhan, IC 8279 se t ong chong doi sau 10,3 ms va kiem tra lai mot lan na
e xem phm o co b nhan hay khong, ne u con th IC 8279 se thiet lap ma cho phm nay va lu tr
ma cua phm vao bo nh RAM be n trong. Sau o se bao cho vi x ly biet a co mot phm tac ong
va yeu cau vi x ly nhan ma cua phm nay bang cach tac ong e n tn hieu ngat IRQ. Tn hieu
IRQ c ket noi e n mot ngo vao ngat cua vi x ly va chng trnh phuc vu cho ngat nay la
chng trnh ngat phm. Nhiem vu cua vi x ly la oc ma cua phm b nhan vao e x ly va Reset
ngat cua IC 8279 e chuan b cho phm tiep theo.
Khung ma d lieu cua phm b nhan nh hnh 6-39:

H|nh 6-3. Cou truc mo ph|m cuo |C 827.
Giai thch khung ma phm: IC 8279 tao ra mot ma tran phm 88 = 64 phm va 2 phm hoat
ong oc lap la CNTL va SHIFT ket noi vi 2 ngo vao cua IC 8279.
To hp 88 bao gom 8 ng RL[7:0] va 3 ng SL[2:0] ( ng SL3 ch phuc vu quet hien
th) sau khi qua mach giai ma 3 sang 8 se c 8 ng.
Neu khong s dung phm to hp th 2 ngo vao CNTL va SHIFT cua IC 8279 phai noi mass
(0V) khi o 2 bit CNTL va SHIFT trong khung ma phm se bang 0.
Khi nhan 1 phm th IC 8279 se biet c phm nhan tng ng vi trang thai quet nao cua 3
bit SL[2:0] va chnh la 3 bit 5CAN, phm nhan tren se lam thay oi trang thai 1 trong 8 ng RL
qua ma hoa 8 sang 3 se tao ra ma nh phan 3 bit chnh la 3 bit kE1UkN trong khung ma d lieu.
Neu khong dung phm CNTL va SHIFT th to hp 6 bit se tng ng vi 64 phm khac nhau
tng ng vi so hex [00:3F], khi nhan them phm to hp gom phm SHIFT va 1 trong 64 phm th
se co 64 ma khac tng ng vi so hex [40:7F], khi nhan them phm to hp gom phm CNTL va 1
trong 64 phm th se co 64 ma khac tng ng vi so hex [80:BF], cuoi cung khi nhan them phm to
hp gom phm CNTL + SHIFT va 1 trong 64 phm th se co 64 ma khac tng ng vi so hex
[C0:FF]. Vay to ng cong co tat ca 256 to hp phm.

|||. 1H|E1 KE HE 1H0NC V| XU L:
Mot mnh vi x ly khong the hoat o ng c ma phai ket hp vi bo nh ROM, RAM e lu
tr chng trnh va d lieu, phai ket hp vi IC ngoai vi e giao tiep xuat nhap tn hieu ieu khien
goi la he thong vi x ly hay kit vi x ly hay he tho ng ieu khien vi x ly .
Trong phan nay trnh bay cach thiet ke mot he thong ieu khien dung vi x ly thong qua cac
yeu cau ieu khien.
. 8o| th|et ke sc
eu cou: Hoy th|et ke he thcng du ng v| xu |y 8 b|t 8085A g|oc t|ep vc| 2 |co| bc nhc cc
dung |ucng 8Kbyte EPk0M vo 8 Kbyte kAM. C|oc t|ep vc| 2 |C ngco| v| 8255A vo |C 827 de
d|eu kh|e n h|en th| 8 |ed 7 dcon vo mo tron bon ph|m gcm 24 ph|m.
|~| :| .-| ~|
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .
He thong s dung vi x ly 8085A co 16 ng a ch nen co the giao tiep vi 64Kbyte bo
nh. Trong he thong nay ch s dung 16Kbyte la ap ng c.
Do vi x ly 8085A khi reset th a ch thanh ghi PC = 0000H nen bo nh EPROM luon c
thiet ke bat au tai a ch 0000H.
Vi x ly 8085A cho phe p giao tiep vi IC ngoai vi tho ng qua 8 bit a ch thap va ng
ieu khien IO/M nen co the giao tiep vi 256 o nh ngoai vi IO.
|| || - |
Sau khi phan tch xong ta chon loai bo nh EPROM loai 2764 co dung lng 8Kbyte va Ram
6264 co dung lng 8Kbyte.
|-: - ~ ~ ~ |
Tong IC giao tiep vi vi x ly la 5 IC gom 2 IC nh va 3 IC ngoai vi. Do ieu kien bai thiet
ke khong bat buoc nen ta co the thiet ke giai ma nh sau:
Lap bang o nh cua vi x ly nh sau:
18 NH| PHAN HEX
A5 A4 A3 A2 A A0 A A8 A7 A6 A5 A4 A3 A2 A A0
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000
2764 0 0 0 FFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2000
6264 0 0 3FFF

8ong 6-22. 8ong dc bc nhc.
Trong bang o nh bat au la IC nh 2764 co 13 ng a ch A[12:0] nen trong bang o a
ch cua vi x ly th o nh co a ch bat au tng ng vi 13 ng a ch A[12:0] eu bang 0. O
nh cuoi cung co a ch tng ng vi 13 ng a ch A[12:0] eu bang 1. a ch tnh theo so
hex cua 8Kbyte EPROM la [0000:1FFF].
Tiep theo la bo nh RAM 6264 cung co 13 ng a ch A[12:0] ne n trong bang o a ch
cua vi x ly th o nh co a ch bat au tng ng vi 13 ng a ch A[12:0] eu bang 0. O nh
cuoi cung co a ch tng ng vi 13 ng a ch A[12:0] e u bang 1. e phan biet 2 bo nh
nay oc lap vi nhau th phai dung cac ng a ch con lai t A[13:15]. Trong bai nay tac gia s
dung ng a ch A13 bang cach cho A13 = 1 oi vi bo nh RAM. a ch tnh theo so hex cua
8Kbyte RAM la [2000:3FFF].
Vi yeu cau nay th ta dung ng a ch A13 e ieu khien chip select phan biet 2 IC nh.
Lap bang o IO cua vi x ly nh sau:
18 NH| PHAN HEX
A7 A6 A5 A4 A3 A2 A A0
8255A 0 0 0 0 0 0 0 0 00
0 0 0 0 0 0 03
8255A 0 0 0 0 0 0 0 04
2 0 0 0 0 0 07
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 257
827 0 0 0 0 0 0 0 08
3 0 0 0 0 0 0 0
8ong 6-23. 8ong dc |0.
Trong bang o nh bat au la IC nh 8255A th 1, moi IC 8255 co 2 ng a ch nen o nh
co a ch bat au tng ng vi 2 ng a ch A[1:0] eu bang 0. O nh cuoi cung co a ch
tng ng vi 2 ng a ch A[1:0] eu bang 1. a ch tnh theo so hex cua 8255A-1 la [00:03].
Tiep theo la IC 8255A th 2, o nh co a ch bat au tng ng vi 2 ng a ch A[1:0]
eu bang 0. O nh cuoi cung co a ch tng ng vi 2 ng a ch A[1:0] eu bang 1. a ch
tnh theo so hex cua 8255A-2 la [04:07].
Tiep theo la IC 8279 th 3, o nh co a ch bat au tng ng vi 1 ng a ch A[0] bang
0. O nh cuoi cung co a ch tng ng vi 1 ng a ch A[0] bang 1. a ch tnh theo so hex
cua 8279-32 la [08:09].
Trong bang o IO th phai dung 2 ng a ch A[3:2] e phan biet 3 IC ngoai vi.
S o nguye n ly cua he thong nh hnh 6-40.

H|nh 6-40o. 5c dc nguyen |y cuo khc| v| xu |y vo |C chct.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
` . | .
Trong he thong s dung vi x ly 8085A va IC chot 74573 e tach a ch va d lieu, ket qua
c 8 ng d lieu D[7:0] va 16 ng a ch A[15:0], cac ng ieu khien IO/M\, RD\, WR\,
RST_O, CLK_O.


H|nh 6-40b. 5c dc nguyen |y cuo khc| bc nhc vc| v| xu |y.
IC4 la Eprom 2764 va IC5 la SRAM 6264: 13 ng a ch A[12:0] cua cac IC nh c ket
noi vi 13 ng a ch A[12:0] cua vi x ly, 8 ng d lieu D[7:0] cua bo nh c ket noi vi
8 ng d lieu cua vi x ly, chan ie u khien oc OE c ket noi vi chan RD cua vi x ly, chan
ieu khien ghi WR cua SRAM 6264 c ket noi vi WR cua vi x ly.
Khi vi x ly truy xuat bo nh th chan IO/M\ = 0, neu truy xuat Eprom 2764 th a ch A13 =
0 nen qua cong OR IC13A lam cho chan CS = 0: cho phep bo nh Eprom 2764. Trong khi o A13
= 0 qua cong ao IC6A va cong OR IC13B lam chan CS1 = 1 se khong cho phep SRAM hoat
ong: bus a ch trang thai tong tr cao.
Tng t khi truy xuat bo nh SRAM 6264 th bo nh EPROM se trang thai khong cho
phep.
Khi vi x ly truy xuat bo nh th ng ieu khien IO/M\ mc L, khi truy xuat IO th mc
H nen ng ieu khien nay dung e cho phep truy xuat bo nh hoac IO.
Trong s o nguyen ly hnh 6-40c a s dung IC 74238 giai ma a ch cho 2 IC 8255A va 1
IC 8279. Cac bus a ch, bus d lieu va ieu khien ket noi vi IC ngoai vi giong nh ket noi vi
bo nh. ng ieu khie n IO/M\ se ieu khien chan G1 cua IC 74138 e cho phep IC nay giai ma
khi vi x ly truy xuat IO, con khi truy xuat bo nh th chan IO/M\ = 0 se khong cho phep IC giai
ma nen cac IC ngoai vi cung khong c phep, bus d lieu se trang thai tong tr cao.


Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 259

H|nh 6-40c. 5c dc nguye n |y cuo khc| 2 |C ngco| v| 8255.

H|nh 6-40d. 5c dc nguyen |y cuo khc| ngco| v| 827 vc| bo n ph|m vo |ed 7 dcon.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .


H|nh 6-40e. 5c dc nguyen |y cuo khc| mo tron ph|m 8x3.

H|nh 6-40f. 5c dc nguyen |y cuo 8 |ed h|en th| quet.
Vi bai toan thiet ke tren th cac IC giai ma a s dung gom 3 loai IC: co ng OR - 7432, cong
Not 7404 va giai ma 74138. Vi kieu giai ma nay th s dung qua nhieu IC va ta co the giai ma
n gian tiet kiem IC hn bang cach xem cac IC ngoai vi 8255A va 8279 nh la cac IC nh co
cung dung lng 8Kbyte nh 2 bo nh trong he thong, bang o nh cua he tho ng nh sau:
Lap bang o nh cua vi x ly nh sau:
18 NH| PHAN HEX
A5 A4 A3 A2 A A0 A A8 A7 A6 A5 A4 A3 A2 A A0
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000
2764 0 0 0 FFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2000
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 261
6264 0 0 3FFF
8255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4000
0 0 5FFF
8255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6000
2 0 7FFF
827 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8000
3 0 0 FFF

8ong 6-22. 8ong dc bc nhc.
Trong bang o nh th e phan biet cac IC nh va ngoai vi ta phai dung 3 ng a ch
A[15:13] va khi o ta ch can dung 1 IC giai ma 74138 va toan bo khoi bo nh va ngoai vi c
thiet ke lai nh hnh 6-41:

H|nh 6-4o. 5c dc nguye n |y cuo khc| bc nhc vo |C g|o| mo.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .

H|nh 6-4b. 5c dc nguye n |y cuo khc| ngco| v| 8255.
Khi thiet ke khoi ngoai vi 8255A theo kie u IO th lenh dung e truy xuat cac IO la lenh IN
va OUT, nhng khi ket noi theo kieu bo nh th lenh dung e truy xuat IO gio ng nh le nh dung
cho cac o nh.
Trong kieu giai ma nay th a ch cua 8255A 1 (IC7) co a ch la 4000H en 5FFFH
nhng do 8255A ch co 2 ng a ch A[1:0] nen ta ch can quan tam en 4 a ch nh sau:
a ch 4000H la a ch cua port A.
a ch 4001H la a ch cua port B.
a ch 4002H la a ch cua port C.
a ch 4003H la a ch cua t ieu khien.
Tng t a ch cua 8255A 2 (IC8) co a ch la 6000H en 7FFFH nhng ta ch can quan
tam en 4 a ch nh sau:
a ch 6000H la a ch cua port A.
a ch 6001H la a ch cua port B.
a ch 6002H la a ch cua port C.
a ch 6003H la a ch cua t ieu khien.
a ch cua 8279 3 (IC9) co a ch la 8000H en 9FFFH nhng do 8279 ch co 1 ng a
ch A1 nen ta ch can quan tam en 2 a ch nh sau:
a ch 8000H la a ch truy xuat d lieu.
a ch 8001H la a ch truy xuat t ieu khie n cua 8279.
2. 8o| th|et ke sc 2
eu cou: Hoy th|et ke he thc ng dung v| xu |y 8 b|t 8085A g|oc t|ep vc| 32Kbyte bc nhc
EPk0M su dung 4 |C 2764 cc dung |ucng 8Kbyte vo 32 Kbyte kAM su dung |C 62256. C|oc t|ep
vc| 2 |C ngco| v| 8255A vo |C 827 de d|eu kh|en h|en th| 8 |ed 7 dcon vo mo tron bon ph|m
gcm 24 ph|m.
|~| :| .-| ~|
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 263
He thong s dung vi x ly 8085A co 16 ng a ch nen co the giao tiep vi 64Kbyte bo
nh. Trong he thong nay bo nh cua vi x ly a s dung het 64 kbyte bao gom 32Kbyte Eprom va
32 Kbyte Ram.
Phan giao tiep vi 2 IC 8255 va IC 8279 th phai giai ma theo kieu IO.
|-: - ~ ~ ~ |
Tong IC giao tiep vi vi x ly la 8 IC gom 5 IC nh va 3 IC ngoai vi.
Lap bang o nh cua vi x ly nh sau:
18 NH| PHAN HEX
A5 A4 A3 A2 A A0 A A8 A7 A6 A5 A4 A3 A2 A A0
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000
2764- 0 0 0 FFF
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2000
2764-2 0 0 3FFF
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4000
2764-3 0 0 5FFF
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6000
2764-4 0 7FFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8000
62256 FFFF

8ong 6-23. 8ong dc bc nhc.
Trong bang o nh th 4 IC nh 2764 c thiet lap a ch giong nh bai 1, bo nh SRAM co
dung lng 32Kbyte se co 15 ng a ch nen a ch bat au tng ng vi 15 ng bang 0 va
a ch cuoi tng ng vi 15 ng bang 1. Nhn vao bang ta se thay 32kbyte EPROM phan biet
vi 32Kbyte Ram chnh la ng a ch A15.
Ta co the xem bo nh Ram 62256 nh la 4 IC nh 6264 khi o bang o nh c thiet lap lai
nh sau:
18 NH| PHAN HEX
A5 A4 A3 A2 A A0 A A8 A7 A6 A5 A4 A3 A2 A A0
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000
2764- 0 0 0 FFF
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2000
2764-2 0 0 3FFF
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4000
2764-3 0 0 5FFF
EPk0M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6000
2764-4 0 7FFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8000
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
- . | .
6264- 0 0 FFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A000
6264-2 0 8FFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C000
6264-3 0 DFFF
kAM 0 0 0 0 0 0 0 0 0 0 0 0 0 E000
6264-4 FFFF

8ong 6-24. 8ong dc bc nhc.
Vi bang o nh nay th ta se dung 3 ng a ch A[15:13] giai ma thanh 8 ng ieu
khien e chon 8 chip select cho 8 IC nh: trong o 4 ng au tien dung e chon CS cho 4
Eprom, 4 ng co n lai ta phai OR lai e ieu khien chon CS cua Ram 62256.
Phan giai ma IC ngoai vi giong nh bai thiet ke so 1.
S o nguye n ly cua he thong nh hnh 6-42.

H|nh 6-42o. 5c dc nguyen |y cuo khc| v| xu |y vo |C chct.


Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 265


H|nh 6-42b. 5c dc nguyen |y cuo khc| bc nhc.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | .

H|nh 6-42c. 5c dc nguye n |y cuo khc| ngco| v|.
|V. 8A| 1AP:
Bai tap so 1: Hay thiet ke mot he tho ng gom vi x ly 8085A giao tiep vi 32 Kbyte bo nh
Eprom s dung 8 IC nh 2732 co dung lng 4 Kbyte cho moi IC va 32Kbyte bo nh RAM dung 4
IC 6264. Cac IC ngoa i vi bao gom 7 IC ngoai vi 8255 va 1 IC 8279.
Bai tap so 2: Hay thiet ke mot he tho ng gom vi x ly 8085A giao tiep vi 32 Kbyte bo nh
Eprom s dung 4 IC nh 2764 co dung lng 8 Kbyte cho moi IC va 16Kbyte bo nh RAM dung 2
IC 6264. Cac IC ngoa i vi bao gom 1 IC ngoai vi 8255 va 1 IC 8279. Thiet ke sao cho toi u nhat.
Bai tap so 3: Hay thiet ke mot he thong gom vi ieu khien 89S52 giao tiep vi 32 Kbyte bo nh
Eprom s dung 4 IC nh 2764 co dung lng 8 Kbyte cho moi IC va 16Kbyte bo nh RAM dung 2
IC 6264. Cac IC ngoa i vi bao gom 1 IC ngoai vi 8255 va 1 IC 8279. Thiet ke sao cho toi u nhat.
Bai tap so 4: Hay thiet ke mot he tho ng gom vi ieu khien 89S52 s dung 8Kbyte bo nh Flash
ROM noi va giao tiep vi 24Kbyte bo nh Eprom s dung 1 IC nh 2764 co dung lng 8 Kbyte
cho moi IC va 32Kbyte bo nh RAM dung 4 IC 6264. Cac IC ngoai vi bao gom 3 IC ngoai vi 8255
va 1 IC 8279. Thiet ke sao cho toi u nhat.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M






Chng 7





V| |EU KH|EN C|A0 1|EP V0| LED
' 3~ :-: . - |
3~ :-: . - 7 ~ |
V| |EU KH|EN C|A0 1|EP V0| PHM NHAN
' 3~ :-: . ' |~ :|' ||~ |
3~ :-: . ~ :~| :|
V| |EU KH|EN C|A0 1|EP V0| ADC, DAC
' 3~ :-: . / `3
3~ :-: . / 7'3
3~ :-: . / ``
V| |EU KH|EN C|A0 1|EP V0| kELA VA 0NC C0 8U0C
' 3~ :-: . -~.
3~ :-: . | :|
V| |EU KH|EN C|A0 1|EP V0| LCD
V| |EU KH|EN C|A0 1|EP V0| LED MA 1kAN
V| |EU KH|EN C|A0 1|EP V0| kEAL1|ME






Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
` . | .

|. V| |EU KH|EN C|A0 1|EP V0| LED:
. C|oc t|ep vc | |ed dcn:
A. Giao tiep phan cng:
Trong cac ng dung co s dung led n e ch th nen phan nay se trnh bay phan giao tiep
vi led n.
Cac thong so cua led n thng s dung la ien ap lam viec cua led khoang 2V, dong qua
led khoang t 10 en 20 mA.
Tra cac thong so lam viec cua moi ngo ra cua vi ieu khien th khi ngo ra mc H dong
chay ra (source) co gia tr rat nho khoang t 10A en 60A oi vi port1, 2, 3 va dong khoang t
80A en 800A oi vi port0. Khi ngo ra mc L dong chay vao (sink) khoang 15mA oi vi
cac port1, 2, 3 (15mA/1 port) va dong khoang 20mA oi vi port0 (20mA/port0).
Khi giao tiep vi led n th se co 2 kieu giao tiep nh hnh 7-1.
LED
Y1
74ABT245
2
3
4
5
6
7
8
9
19
1
18
17
16
15
14
13
12
11
A0
A1
A2
A3
A4
A5
A6
A7
G
DIR
B0
B1
B2
B3
B4
B5
B6
B7
C1
C
330
R1
R C1
C
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
P1.0/T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
R1
R
VCC
LED
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
P1.0/T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
Y1
RESET
VCC
330
VCC
RESET

(o| (b|
H|nh 7-. C|oc t|ep v| d|eu kh|e n vc| |ed dcn.
Vi hnh 7-1a
- Vi hnh 7-1a ket noi trc tiep ngo ra cua port vi led th mc 1 led sang nhng kho ng
u dong cung cap cho led sang nen led sang m, neu muon sang ro th phai dung them
IC em hoac transistor e khuech ai. Hnh 7-1b du ng IC em 74245 e ieu khien
led.
- Vi hnh 7-2 ket noi trc tiep ngo ra cua port vi led th mc 0 led sang va u do ng
cung cap cho led sang nen led sang ro khong can IC em.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 269
470
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2. 0
P2. 1
P2. 2
P2. 3
P2. 4
P2. 5
P2. 6
P2. 7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1.0/T2
P1.1/ T2X
P1. 2
P1. 3
P1. 4
P1. 5
P1. 6
P1. 7
INT0
INT1
T0
T1
VCC
470
470
470
LED
LED
470
LED
LED
470
Y1
470
LED
470 LED
LED
470
LED
470
LED
LED
LED
470
R1
10k
470
LED
LED
VCC
LED
470
470
470
470
470
LED
470
LED
LED
LED
LED
470
VCC
470
VCC
LED
470
470 LED
LED
C1
10
470
LED
470
RESET
LED
LED
470
LED
LED
470
470
470 LED
470
470
LED
LED
470
470
LED
LED

H|nh 7-2. C|oc t|ep v| d|e u kh|en vc| 32 |ed dcn.
ien tr han dong cho led c tnh nh sau:


491
6 . 5
45 . 0 8 . 1 5
mA
V V V
I
V V V
R
LED
OL LED CC

Nen chon loai LED sang ro , dong lam viec kha nho nen ien tr han dong khoang 470.
Chu y: khi s dung vi ieu khien tuy thuoc vao ket noi ta co the dung hoac khong dung ien
tr keo le n nhng theo tac gia th tat ca cac port nen dung ien tr keo len, neu trong s o khong
trnh bay th ban oc hieu ngam la luon co ket noi.
B. Cac chng trnh v du:
V du 1: Vi s o nguyen ly hnh 7-2, hay viet chng trnh ieu khie n led 32 led n chop tat:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dieu khien 4 port 0 dieu khien 32 led chop tat
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
org 0000h ;khai bao dia chi bat dau cua chuong trinh
lb: mov p0,#00h ;sang 8 led cua p0
mov p1,#00h ;sang 8 led cua p1
mov p2,#00h ;sang 8 led cua p2
mov p3,#00h ;sang 8 led cua p3
lcall delay ;goi chuong trinh con delay

mov p0,#0ffh ;tat 8 led cua p0
mov p1,#0ffh ;tat 8 led cua p1
mov p2,#0ffh ;tat 8 led cua p2
mov p3,#0ffh ;tat 8 led cua p3
lcall delay ;goi chuong trinh con delay
sjmp lb ;lam lai tu dau
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r6,#0ffh ;nap hang so delay FFH vao thanh ghi r6
de : mov r7,#0ffh ;nap hang so delay FFH vao thanh ghi r7
djnz r7,$ ;giam thanh ghi r7 di 1 va nhay khi r7 khac 00
djnz r6,de ;giam thanh ghi r6 di 1 va nhay khi r6 khac 00
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
7 . | .
ret ;thoat khoi chuong trinh con
end

V du 2: Vi s o nguyen ly hnh 7-2, hay viet chng trnh ieu khien led 32 led n sang dan va
tat dan:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dieu khien 32 led sang dan len va tat dan
;ket noi 4 port den 32 led bang cap 4 soi cap 8 soi theo dung thu tu
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
org 0000h
mov p0,#0ffh ;tat port 0
mov p1,#0ffh ;tat port 1
mov p2,#0ffh ;tat port 2
mov p3,#0ffh ;tat port 3
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;dieu khien sang dan len
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
lb: lcall delay ;goi chuong trinh con delay
clr c ;lam cho bit C = 1
mov a,p0 ;chuyen noi dung port0 vao thanh ghi A
rlc a ;xoay noi dung thanh ghi A sang trai
mov p0,a ;chuyen noi dung port0 vao thanh ghi A
mov a,p1 ;xoay noi dung p1
rlc a
mov p1,a
mov a,p2 ;xoay noi dung p2
rlc a
mov p2,a
mov a,p3 ;xoay noi dung p3
rlc a
mov p3,a
jc lb ;thuc hien tiep khi C=0
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;dieu khien tat dan
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
lb1: lcall delay ;goi chuong trinh con delay
setb c ;lam cho bit C = 0
mov a,p0 ;chuyen noi dung port0 vao thanh ghi A
rlc a ;xoay noi dung thanh ghi A sang trai
mov p0,a ;chuyen noi dung port0 vao thanh ghi A
mov a,p1 ;xoay noi dung p1
rlc a
mov p1,a
mov a,p2 ;xoay noi dung p2
rlc a
mov p2,a
mov a,p3 ;xoay noi dung p3
rlc a
mov p3,a
jnc lb1 ;thuc hien tiep khi C=1
sjmp lb ;sau khi 8 led sang het thi quay lai tu dau
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r6,#0ffh
de : mov r7,#0ffh
djnz r7,$
djnz r6,de
ret
end
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 271

8o| top : Hay viet chng trnh ieu khien 32 sang tat dan t trai sang phai, t phai sang
trai, t ngoai vao va t gia ra.
8o| top 2: Hay viet chng trnh giong nh bai 1 nhng moi kie u sang c thc hien 5 lan.
2. C|oc t|ep vc | |ed 7 dcon:
A. Giao tiep vi 1 led 7 oan:
Trong ng dung ch co 1 led 7 oan th co nhieu cach giao tiep vi ieu khien vi led:
- Giao tiep trc tiep: kieu nay se dung 1 port cua vi ieu khien ket noi vi led 7 oan
loai Anode chung nh hnh 7-3. Kieu nay dung 8 ng.
- Giao tiep gian tiep: kieu nay ch dung 4 ng ket noi vi IC giai ma led 7 oan nh
hnh 7-4. Kieu nay dung 4 ng.
Y1
VCC
R2 330
C
10
R4 330
RESET
R8 330
R5 330
LED1
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
VCC
R6 330
R1 330
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
R3 330
R
10k
R7 330

H|nh 7-3. C|oc t|ep tru c t|ep v| d|eu kh|e n vc| |ed dcon.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
7 . | .
R2 330
Y1
VCC
R
10k
R3 330
R7 330
C
10
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
VCC
VCC
R6 330
VCC
LED1
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
74247
7
1
2
6
4
5
3
13
12
11
10
9
15
14
D0
D1
D2
D3
BI/ RBO
RBI
LT
A
B
C
D
E
F
G
R5 330
R4 330
RESET
VCC
R1 330

H|nh 7-4. C|oc t|ep g|on t|ep v| d|eu kh|e n vc| |ed dcn.
Co u hc| : Hoy chc b|et uu khuyet d|em cuo 2 coch g|oc t|ep v| d|eu kh|en vc| |ed 7 dco n
c 2 h|nh tren.
ien tr han dong cho led c tnh nh sau:


491
6 . 5
45 . 0 8 . 1 5
mA
V V V
I
V V V
R
LED
OL LED CC

Nen chon ien tr khoang 470:
B. Cac chng trnh giao tiep vi 1 led 7 oan:
V du 3: Hay dung s o hnh 7-3, viet chng trnh ieu khien 1 led 7 oan em len t 1 e n 9.
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dem len tu 0 den 9 hien thi tren 1 led
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
dem equ r2
led equ p1

org 0000h ;bat dau chuong tri nh
mov dptr,#ma7doan ;
main0: mov dem,#00h ;dem=00
main1 mov a,dem
movc a,@a+dptr ;lay ma 7 doan
mov led,a ;goi ra led hien thi

lcall delay

inc dem ;tang gia tri dem
cjne dem,#10,main1 ;ss dem voi 10
ljmp main0 ;lam lai tu dau

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r5,##0ffh
loop1: mov r6,#0ffh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 273
djnz r6,$
djnz r5,loop1
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;khai bao ma 7 doan tu so '0' den so '9'
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ma7doan: db 0C0h,0F9h,0A4h,0B0h,99h,92h,82h,0F8h,80h,90h
end
Trong v du 3 chung ta can quan tam en bien co te n dem vi gia tr ban au bang 00h,
c giai ma sang ma 7 oan va gi ra led e hien th, tiep theo la thc hien chng trnh con
delay 1 giay, sau o tang gia tr em len 1 n v va kiem tra xem ne u cha bang 10 th quay ve
giai ma va hien th so mi, neu bang 10 th xoa ve 00 va bat au lai chu ky mi.
V du 4: Hay dung s o hnh 7-4, viet chng trnh ieu khien 1 led 7 oan em len t 1 e n 9.
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dem len tu 0 den 9 hien thi tren 1 led
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
dem equ r2
led equ p1

org 0000h ;bat dau chuong tri nh

main0: mov dem,#00h ;dem=00
main1 mov a,dem
mov led,a ;goi ra led hien thi
lcall delay

inc dem ;tang gia tri dem
cjne dem,#10,main1 ;ss dem voi 10
ljmp main0 ;lam lai tu dau

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r5,##0ffh
loop1: mov r6,#0ffh
djnz r6,$
djnz r5,loop1
ret

end
Trong v du 4 th yeu cau giong v du 3 nhng trong mach ien a dung IC giai ma led 7 oan
nen trong chng trnh kho ng can phai tien hanh giai ma, ch can gi ma BCD ra port.
C. Giao tiep vi nhieu led 7 oan:
e lam quen vi cach thc giao tiep ieu khien nhieu led 7 oan th nen ket noi theo phng
phap quet. S o nguye n ly cua led 7 oan hnh 7-5.
Tai moi mot thi iem ta ch cho 1 transistor dan va 7 transistor con lai tat, d lieu gi ra se
sang tren led tng ng vi transistor dan. Sau o cho 1 transistor khac dan va gi d lieu hien th
cho led o, qua trnh ieu khien nay dien ra lan lt cho en khi het 8 led.
Vi toc o gi d lieu nhanh va do mat ta co lu anh nen ta nhn thay 8 led sang cung 1 luc.
Ma quet: mc logic 0 th transistor dan, mc logic 1 th transistor ngat c trnh bay bang
7-1.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
7- . | .

MA HEX Mo quet d|eu kh|en coc trons|stcr
FE 0 1rons|stcr 0N
FD 0 1rons|stcr 2 0N
F8 0 1rons|stcr 3 0N
F7 0 1rons|stcr 4 0N
EF 0 1rons|stcr 5 0N
DF 0 1rons|stcr 6 0N
8F 0 1rons|stcr 7 0N
7F 0 1rons|stcr 8 0N
8ong 7-. Mo quet d|eu kh|e n |ed.
Ma 7 oan: trong he thong s dung led 7 oan loai Anode chung nen mc logic 0 th led sang
va mc logic 1 th led tat. Cac ma 7 oan cua cac so thap phan t 0 e n 9 va cac so hex t A e n F
c trnh bay bang 7-2:



5c hex dp g f e d c b o Mo sc hex
0 0 0 0 0 0 0 C0
0 0 F
2 0 0 0 0 0 A4
3 0 0 0 0 0 80
4 0 0 0 0
5 0 0 0 0 0 2
6 0 0 0 0 0 0 82
7 0 0 0 F
8 0 0 0 0 0 0 0 80
0 0 0 0 0 0 0
A 0 0 0 0 0 0 88
8 0 0 0 0 0 83
C 0 0 0 0 0 C2
D 0 0 0 0 0 A
E 0 0 0 0 0 86
F 0 0 0 0 8E
8ong 7-2. Mo 7 dcon cuo coc sc.
Cac ma khac co the t thiet lap.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 275
VCC
b
R10
10k
LED5
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
f a
LED2
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
LED0
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
c
LED3
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
LED6
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
d
Q2
A564
g
Q5
A564
g
f
p27
p24
d
p
b
e
R13
10k
p
b
R8 330
p
P23
VCC
p p c
c
f
c
b
c
d
a
g
LED1
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
p
p22
VCC
c
P24
p26
g
p
d
b
p25
C
10
R6 330
g
VCC
c
a
d
R16
10k
d
R5 330
e
R15
10k
Q7
A564
d
a
P25
p21
RESET
VCC
g
Q3
A564
b
f
g
R1 330
e
LED7
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
e
P20
p23
R
10k
VCC
f
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1.0/T2
P1.1/ T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
R2 330
R14
10k
d
Q4
A564
p
d
LED4
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
b
VCC
P26
R11
10k
R7 330
b
R17
10k
Q1
A564
a
c
e
Y1
p
R12
10k
f
P21
p20
f
R4 330
a
Q6
A564
e
a
g
f
e
VCC VCC
b
e
P27
c
R3 330
VCC
e
a f
Q0
A564
a
g
P22

H|nh 7-5. C|oc t|ep v| d|e u kh|en vc| 8 |ed 7 dco n.
Trong s o hnh 7-5 ta co the giam bt so lng ng ieu khie n bang cach dung them IC
so ong vai tro quet va giai ma nh hnh 7-6.

R13
10k
c
a
Q3
A564
R10
10k
U5 7447
7
1
2
6
4
5
3
13
12
11
10
9
15
14
D0
D1
D2
D3
BI/RBO
RBI
LT
A
B
C
D
E
F
G
R
10k
P10
f
f
P13
Y7
f
a
LED1
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
VCC
e
p
g
Q7
A564
VCC
e
f
P11
c
d
b
a
P11
f
VCC
P15
LED0
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
VCC
c
a
R15
10k
e
P16
Y0
b
LED2
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
b
VCC
Q0
A564
b
R14
10k
LED4
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
VCC
g
VCC
e
R7 330
VCC
a
p
P12
R3 330
a
e
g
RESET
P12
b
R2 330
LED5
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
d R6 330
e
Y1
b
b
c
VCC
LED7
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
d
Y4
U4 74S138
1
2
3
15
14
13
12
11
10
9
7
6
4
5
A
B
C
Y0
Y1
Y2
Y3
Y4
Y5
Y6
Y7
G1
G2A
G2B
f
P16
g
e
P17
b
p
P13
d
p
d
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
P1.0/T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
g
a
R5 330
R11
10k
Q1
A564
d
Y2
g
f
R4 330
R16
10k
p P10
VCC
e
e
Y5
C
10
VCC
d
f
R12
10k
p
a
g
c
Q2
A564
g
c
VCC
Q4
A564
d
P14
p
LED3
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
a
Y3
g
Y1
b
P15
P17
f
P14
Y6
R17
10k
Q5
A564
R1 330
LED6
4
5
9
7
6
2
1
10
3
8
a
b
c
d
e
f
g
dp
A1
A2
c p
d
Q6
A564
c
c

H|nh 7-6. C|oc t|ep v| d|e u kh|en vc| 8 |ed 7 dco n ch| dung pcrt 8 ducng.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
7 . | .
Trong s o hnh 7-6 ta dung IC 7447 o ng vai tro giai ma so BCD sang ma 7 oan, IC 74138
co chc nang giai ma 3 ng sang 8 ng ieu khien 8 transistor. So lng ng ieu khien
can dung la 7 ng.
Trong 7 ng ieu khien cua port1 th vi ieu khien se du ng 4 bit thap e gi ma BCD ra IC
giai ma ieu khien led 7 oan, 3 bit tiep theo dung e ieu khien IC giai ma cho phep 1 transistor
dan. ng tn hieu p17 ieu khien chan cho phep cua IC 74138: khi muon cho phep hien th th
P17 phai mc 0, khi cam th P17 phai mc H.
Trong phng phap quet s dung 8 led th thi gian c phep sang cua 1 led bang 1/8 chu
ky quet, thi gian tat bang 7/8 chu ky quet. Do thi gian led tat kha dai so vi thi gian led sang
nen phai quet nhanh th ta mi nhn thay ta t ca cac led eu sang.
Vi led n sang chu ky va tat chu ky th tan so e mat ta nhn thay led sang lien tuc
(sang luon do mat lu anh) o c trong thc te phai > 40Hz.
Vi 8 led 7 oan du ng phng phap quet th tan so quet o c trong thc te phai ln h n
>60Hz.
Cach tnh toan nh sau: vi dong lam viec bnh thng (khong quet) cho n la mA I
LED
5 , ap
lam viec nh mc V V
LED
8 . 1 . Khi o ien tr cua moi led la :


550
5
45 . 0 8 . 1 5
mA
V V V
I
V V V
R
LED
OL LED CC
BT

Khi dung phng phap quet: th dong tc thi phai bang 40mA gap 8 lan. e tang dong th
co 2 cach: tang ap hoac giam ien tr. e phu hp vi ien ap lam viec nen ta thc hien cach
giam ien tr va ien tr c tnh lai nh sau:


75 , 68
40
45 . 0 8 . 1 5
_
mA
V V V
I
V V V
R
QUET LED
OL LED CC
Quet

Vay ien tr han dong giam va thng c cho n nam trong pham vi t 68 en 220 nham
tang them cng o sang va bao ve qua dong.
Co u hc| 2: Ho y chc b|et uu khuyet d|em cuo 2 co ch g|oc t|ep v| d|eu kh|en vc| 8 |ed 7 dcon
c 2 h|nh tren.
D. Cac chng trnh v du giao tiep vi 8 led 7 oan:
V du 5: Chng trnh em giay hien th tren led 7 oan s du ng phng phap quet.

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dem len tu 00 den 60 hien thi tren 2 led cua 8 led quet
;su dung ngat timer t0 de dem chinh xac ve thoi gian
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
giay equ r2 ;gan bien dem giay la R2
bdn equ r1 ;gan bien dem ngat

quet equ p2
led7 equ p0
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; chuong trinh chinh
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
org 0000h ;bat dau chuong tri nh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 277
ljmp main ;nhay den chtr chinh
org 000bh
ljmp int_t0 ;nhay den chtr con ngat timer0

main: mov tmod,#01h ;timer0: mod 1 - dem 16 bit
mov dptr,#ma7doan ;dptr quan ly vung ma 7 doan

clr tf0 ;xoa co tran
mov IE,#10000010B ;cho phep timer0 ngat
mov TH0,#high(-50000) ;khoi tao timer delay 50ms
mov TL0,#low(-50000)
setb tr0 ;cho phep timer bat dau dem

main0: mov giay,#00h ;giay=00
main1: mov bdn,#00 ;nap bien den so lan ngat
lcall gma
main2: lcall hthi ;goi chtr con hien thi

cjne bdn,#20,main2 ;chua dung 20 lan [tuc 1 giay]
mov a,giay ;chuyen giay sang A
add a,#1 ;tang giay len 1
da a ;hieu chinh so BCD trong A
mov giay,a ;tra lai cho giay
cjne giay,#60h,main1 ;ss giay voi 60h
ljmp main0 ;lam lai tu dau
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; chuong trinh con giai ma
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
gma: mov a,giay
anl a,#0fh ;xoa 4 bit cao hang chuc giay
movc a,@a+dptr ;lay ma 7 doan
mov 27h,a ;cat ma vao o nho 20h

mov a,giay
anl a,#0f0h ;xoa 4 bit thap hang dvi
swap a ;chuyen 4 bit cao xuong vi tri thap
movc a,@a+dptr ;lay ma 7 doan hang chuc
mov 26h,a
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;CHUONG TRINH CON NGAT TIMER0 SAU KHOANG THOI GIAN 50MS
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
int_t0: inc bdn ;tang bien dem giay len 1
mov TH0,#high(-50000) ;khoi tao timer delay 50ms
mov TL0,#low(-50000)
clr TF0
reti
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con hien thi
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hthi: mov a,#01111111b ;ma quet
mov r0,#27h

ht1: mov led7,@r0
mov quet,a
lcall delay1
mov quet,#0ffh
dec r0
rr a ;chuyen sang led ke
cjne r0,#25h,ht1
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay1
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
7` . | .
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay1: mov r7,#0fh
djnz r7,$
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;khai bao ma 7 doan tu so '0' den so '9'
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ma7doan: db 0C0h,0F9h,0A4h,0B0h,99h,92h,82h,0F8h,80h,90h
end

8o| top 3: Hay viet chng trnh em giay cho s o hnh 7-6.
8o| top 4: Hay viet chng trnh em phut giay cho ca 2 mach.
8o| top 5: Hay viet chng trnh em gi phut giay cho ca 2 mach.

||. V| |EU KH|EN C|A0 1|EP V0| PHM NHAN:
. C|oc t|ep vc | hcoc 2 nut nhon:
Trong mot so he tho ng ieu khien luo n co nut nhan hay phm nhan hay switch dung e giao
tiep gia con ngi va thiet b ie u khie n v du nh may vi tnh, may tnh phai co ban phm e
nhap d lieu ma, thong tin, neu so lng nut nhan t khoang 1 en vai nut th nen ket noi 1 nut
nhan trc tiep vi 1 ngo vao nh hnh 7-7:
RESET
VCC
SW1
R2
10k
R1
10k
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
C1
C
SW2
VCC
R1
R
Y1

H|nh 7-7. C|oc t|ep v| d|eu kh|en vc| 2 nut nhon.
Khi cha nhan th cac ngo vao cua nut nhan mc logic H, khi nhan th se lam ngo ra
chuyen sang mc logic L. Trong hnh 7-7, 2 nut nhan c noi en ngo vao P2.0 va P2.1 cua vi
ieu khien. Chng trnh se kiem tra s thay oi trang thai cua mc logic va thc thi cac cong viec
tng ng vi tng nut nhan.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 279
Vi vi ieu khien th cac port thng mc logic 1 nen 2 ien tr keo len la khong can thiet
nhng cac vi mach khac kho ng co tnh nang o neu khong co 2 ie n tr keo le n th khong hoat
ong c.
V du 6: Chng trnh ieu khie n ong c DC co 2 nut nhan Start va Stop.
Lu o ieu khien cho chng trnh nh hnh 7-8:

H|nh 7-8. Luu dc d|e u kh|en.
Giai thch lu o:
Mac nhien khi cap ien th ong c ngng, ch kiem tra phm Start co c nhan hay khong:
neu kho ng nhan th tiep tuc ch, neu nhan th cho ong c chay. Kiem tra xem co nhan phm Stop
hay khong: neu khong nhan th ong c tiep tuc chay, ne u co nhan stop th ket thuc chng trnh
quay lam lai t au.
Chng trnh c viet nh sau:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr dieu khien dong co: khi nhan nut start thi dong co quay
;khi nhan nut stop thi dong co ngung
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

start bit p2.0 ;phim sw1
stop bit p2.1 ;phim sw2
dk_dc equ p1.0 ;ngo ra dieu khien dong co

org 0000h
main1: clr dk_dc ;cho dong co ngung
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con dieu khien dong co quay thuan
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
main: jb start,$ ;cho nhan start
jnb start,$ ;cho buong nut nhan

setb dk_dc ;cho dong co chay
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
` . | .

jb stop,$ ;cho nhan stop
jnb stop,$ ;cho buong nut nhan
sjmp main1

end
Hai nut nhan c ket noi vi 2 ngo vao cua port2. Bit P1.0 cua Port1 ket noi ieu khien
ong c.
2. C|oc t|ep vc | mo tron nh|eu ph|m:
Khi so lng nut nhan nhieu v du nh 16 phm, 20 phm, hoac nhieu hn nh ban phm may
tnh th phai dung cach ket noi kieu ma tran e giam bt so lng ng ket noi. S o ket noi vi
ieu khien vi 16 nut nhan nh hnh 7-9.
VCC
SWC
SW5
C3
H0
SW4
SW7
SW8
SWA
R2 10k
H1
SWE
SWB
SW1
C0
H2
R0 10k
SW3
SWD
C2
R3 10k
SW6 SW2
SW0
C1
R1 10k
SWF
SW9
H3

H|nh 7-. C|oc t|ep v| d|e u kh|en vc| mo tron ph|m 4x4.
Trong ban phm ma tran 44 se co 4 hang t H[0:3] ong vai tro la cac ngo vao bnh thng
mc H, va co 4 cot C[0:3] dung e xuat ma quet.
4 hang va 4 cot c ket noi vi 1 port cua vi ie u khie n, vi ieu khien se xuat ma quet ra
cac cot va sau o oc d lieu cac hang ket qua cua qua trnh thc hien nh bang 7-3:

Mo quet xuo t ro 4 cct Nhop du ||eu cuo 4 hong Nhon Ph|m
C3 C2 C C0 H3 H2 H H0
0 Khcng nhon ph|m
0 0 Nhon ph|m 5W0
0 0 Nhon ph|m 5W
0 0 Nhon ph|m 5W2
0 0 Nhon ph|m 5W3

0 Khcng nhon ph|m
0 0 Nhon ph|m 5W4
0 0 Nhon ph|m 5W5
0 0 Nhon ph|m 5W6
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 281
0 0 Nhon ph|m 5W7

0 Khcng nhon ph|m
0 0 Nhon ph|m 5W8
0 0 Nhon ph|m 5W
0 0 Nhon ph|m 5WA
0 0 Nhon ph|m 5W8

0 0 Khcng nhon ph|m
0 0 Nhon ph|m 5WC
0 0 Nhon ph|m 5WD
0 0 Nhon ph|m 5WE
0 0 Nhon ph|m 5WF
8ong 7-3. Mo quet xuot ro cct vo mo ho ng ducc dcc ve.
Trong cac ng dung l n nen dung IC chuyen dung quet ban phm ma tran va quet led 7 oan
hien th 8279 nh a trnh bay chng trc.
Chng trnh quet phm th ng c viet dang chng trnh con va cac chng trnh khac se
goi chng trnh con quet phm e kiem tra xem co s tac ong t ben ngoai hay khong va neu co
th x ly theo yeu cau.
Khi ta nhan phm th thi gian nhan phm kha dai t vai chuc ms en hang giay, trong khi o
toc o cua vi x ly rat cao nen khi ta nhan phm th vi x ly se thc hien ngay lap tc va co the
thc hien nhieu lan neu thi gian nhan phm dai. ie u nay dan en khi ta nhan mot phm v du nh
ch A th se co nhieu ch A hien th tren man hnh nguyen nhan do doi phm nhan va do toc o
thc hien cua vi x ly nhanh, sau khi thc hien xong cong viec cua phm nhan o roi quay lai phat
hien ra phm van con nhan va tiep tuc thc hien tiep cho en khi nao buo ng phm th ngng.
Lu o xuat ma quet, kiem tra phm nhan va tao ma phm nh hnh 7-10:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
` . | .

H|nh 7-0. Luu dc quet mo tron ph|m 4x4.
Lu o chong doi phm nh hnh 7-11:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 283

H|nh 7-. Luu dc chcng dc| sou kh| quet ph|m.
e chong doi phm ta thc hien nh sau: goi chng trnh con quet phm neu khong co nhan
phm th thoat, neu co th tien hanh goi lai chng trnh con 10 lan roi cat ma phm nhan, sau o
thc hien chng trnh quet ch cho buong phm va phai kiem tra 10 lan sau khi a buo ng phm
nhan, lay lai ma phm va thoat.
V du 7: Chng trnh giao tiep vi ieu khien vi ban phm ma tran 44 va 8 led 7 oan.

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;dung port3 chua qua IC dem ket noi voi ban fim ma tran 4x4
;dung port0 va port 2 ket noi dieu khien 8 led 7 doan quet
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
quet equ p2
led7 equ p0
mtphim equ p3 ;ket noi voi ma tran ban phim
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
`- . | .
maphim equ r5 ;luu tamma phim
maquet equ 11111110B ;FEH

org 0000h
mov dptr,#ma7doan
mov quet,#07fh ;xuat ma quet chi cho 1 led sang
mov led7,#7fh

main: lcall keypres ;goi chtr con quet phim
cjne a,#0ffh,main1
sjmp main

main1: lcall gma_hthi ;goi chtr con giai ma hien thi
sjmp main
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Chuong tri nh con giai ma fim nhan va hien thi ra 1 led 7 doan
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
gma_hthi: movc a,@a+dptr
mov led7,a
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Chuong tri nh con quet phim va chong doi phim
;su dung cac thanh ghi: R4, R5, R6, R7, A
;neu khong nhan thi (A) = FF, neu nhan thi (A) chua ma phim nhan
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
keypres: mov r4,#10 ;nhap so dem 10 lan
keypres1 : lcall KEY ;Neu co phim an thi co c=1
jc pn1 ;kiem tra tiep neu c = 1
ret ;Neu khong co phim nhan thi co c=0

pn1: djnz r4,keypres1 ;Quay ve lap lai chong nay
push acc ;Cat noi dung ma phim trong A

keypres2: mov r4,#10 ;Nhap so dem 10 lan cho nha phim
keypres3: lcall key ;Co phim nhan hay khong
jc keypres2 ;Co thi kiem tra lai
djnz r4,keypres3 ;Khong thi lap lai 50 lan va dam bao
pop acc ;Khoi phuc lai gia tri cho A
ret ;ket thuc mot chuong trinh con
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Chuong tri nh con quet phim
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
key: mov r7,#maquet ;bat dau voi cot so 0(feh)
mov r6,#4 ;Su dung r6 lam bo dem
mov maphim,#00

key1: mov mtphim,r7 ;xuat ma quet ra cot
mov a,mtphim ;Doc lai port1 de xu ly tiep theo
anl a,#0f0h ;xoa 4 bit thap la hang
cjne a,#0f0h,key2 ;co nhan fim thi nhay

mov a,r7
rl a ;xoay de chuyen den cot ke tiep
mov r7,a

mov a,maphim ;chuyen ma fim sang cot ke
add a,#4
mov maphim,a

djnz r6,key1 ;Neu nhu sau moi lan 1 cot ma khong

clr c ;clr c neu nhu khong co phim duoc an
mov a,#0ffh ;thoat voi ma trong a = FFh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 285
ret

key2: swap a
key4: rrc a ;xoay sang phai tim bit 0
jnc key3 ;nhay neu (c)=0
inc maphim ;tang ma fim len cot ke
sjmp key4 ;tiep tuc cho den khi duoc (C)=0

key3: mov a,maphim
setb c
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;khai bao du lieu ma phim
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ma7doan: db 0c0h,0f9h,0a4h,0b0h,099h,092h,082h,0f8h
db 080h,090h,088h,083h,0c6h,0a1h,086h,08eh

end
Giai thch: chng trnh nay thc hien quet ban phm e kiem tra phm nhan, chong doi, tao
ma phm, giai ma phm va hien th tren 1 led 7 oan trong he thong 8 led ket noi theo phng
phap quet.
Ban phm ma tran ket noi vi port3, quet ieu khien 8 transistor dung port2, ieu khien cac
oan dung port0.
Ch dung 1 led e hie n th nen xuat ma quet ra la 7FH = 0111111B e cho 1 transistor dan.
Mac nhien cho dau cham thap phan sang tren led 7 oan.
Goi chng trnh kiem tra xem co nhan phm hay kho ng?
Neu khong nhan phm th ma phm cuoi cung lu trong thanh ghi A =FFH, neu co nhan phm
th tien hanh chong doi bang cach thc hien delay va kiem tra lai xem phm nhan co con tac ong
hay kho ng? Neu khong con th xem nh lan nhan va roi khong co tac dung, neu con nhan th tien
hanh kiem tra xem nhan phm nao va thiet lap ma phm tng ng.
Ma phm se c giai ma sang ma 7 oan va c hien th tren led.
Neu nhan phm mi th ma cua phm mi se hien th con ma phm trc mat.
8o| top 6: Hay m rong chng trnh tre n vi yeu cau nh sau: mac nhien hien th so 0, khi
nhan phm mi th phm cu se c dch sang led ke ben trai nh cach hien th tren may tnh
(calculator).
8o| top 7: Hay dung ban phm ma tran ieu khie n ong c bc vi cac yeu cau sau:
Mot nut Start: khi nhan nut nay th ong c chay.
Mot nut Stop: khi nhan nut nay th ong c ngng.
Mot nut ao chieu: khi nhan nut nay th ong c ao chieu.


|||. V| |EU KH|EN C|A0 1|EP V0| V| MACH CHUEN 0| ADC:
. C|oc t|ep v| d|eu kh|en vc| ADC 080:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
` . | .
Vi mach chuyen oi tn hieu tng t sang tn hieu so co rat nhieu loai phan biet theo so
kenh va so bit. Mot vi mach chuyen oi thng c s dung nhieu la ADC 0808 hoac ADC 0809.
Cac thong so lam viec cua vi mach nh sau:
- ADC 8 bit.
- Thi gian chuyen oi 100s.
- De giao tiep vi vi x ly hoac vi ieu khien.
- Cac ngo ra 3 trang thai co chot.
- Cac ngo vao a ch co chot.
- Dung nguon 5V.
S o chan cua IC ADC 0809 nh hnh 7-12:

H|nh 7-2. 5c dc chon |C ADC 080.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 287

H|nh 7-3. 5c dc khc| be n trcng |C ADC 080.
Bang trang thai IC ADC 0809 nh bang 7-4:

8ong 7-4. 8ong trong tho| chcn kenh ADC.
S o ket noi vi ieu khien vi IC ADC 0809 nh hnh 7-14:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
`` . | .
C1
C
R1
R
IN1
IN5
IN7
VCC
v R1
10k
IN2
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
C2
0. 001
IN3
IN6
IN0
Y1
VCC
RESET
1K R2
IN4
VCC
ADC0809
10 9
7
17
14
15
8
18
19
20
21
6
22
11
12
16
26
27
28
1
2
3
4
5
25
24
23
CLK OE
EOC
D0
D1
D2
D3
D4
D5
D6
D7
START
ALE
VCC
REF+
REF-
IN0
IN1
IN2
IN3
IN4
IN5
IN6
IN7
A0
A1
A2
U2A
7414
1 2
VCC

H|nh 7-4. C|oc t|ep v| d|eu kh|en vc| ADC 080.
Trong he thong hnh 7-14, vi ieu khien s dung 15 ng ieu khien e giao tiep vi
ADC0809, trong o co 8 ng dung e nhan d lieu so sau khi chuyen oi (D
7
:D
0
), 3 ng dung
e xuat a ch chon 1 trong 8 kenh (A
2
A
1
A
0
), 1 ng dung e xuat tn hieu chot a ch (ALE), 1
ng du ng e xuat tn hieu ieu khie n ADC0809 bat au chuye n oi (START), 1 ng du ng e
xuat tn hieu ieu khien cho phep xuat d lieu (OE), 1 ng dung e nhan tn hieu bao qua trnh
chuyen oi ket thuc e tien hanh nhan d lieu.
Gian o thi gian cua IC ADC0809 nh hnh 7-15:

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 289
H|nh 7-5. C|on dc thc| g|on cuo ADC 080.
Trong ng dung ta co the dung 1 ng ieu khie n ca 2 tn hieu ALE va START. Sau khi ra
lenh ADC thc hien qua trnh chuyen oi th vi ieu khien se kiem tra tn hieu EOC xem chung
xuong mc thap la bao hieu qua trnh chuyen oi ang xay ra, va ch cho en khi tn hieu nay len
mc cao tr lai th qua trnh chuyen oi ket thuc mi tien hanh nhan d lieu.
Ta co the khong thc hie n theo cach kiem tra va trnh bay bang cach ch ADC chuyen oi
xong sau 1 khoang thi gian t
CONV
ln nhat la 116s theo so tay tra cu (datasheet) th bat au
nhan d lieu. Khi o ta khong can phai dung 1 ng tn hieu kiem tra chan EOC va chan nay bo
trong.
Lu o ieu khien ADC 0809 chuyen oi 1 kenh theo 2 cach va trnh bay nh hnh 7-16:

(a) (b)
H|nh 7-6. Luu dc d|eu kh|e n ADC 080.
Lu o (a) ieu khien chuyen oi bang cach ch tn hieu tra li EOC, lu o (b) th dung
delay. Thi gian ch phai ln hn thi gian chuyen oi cho trong datasheet.
V du 7: Chng trnh giao tiep vi ieu khien vi ADC 0809 va 8 led 7 oan.
Yeu cau: dung 1 kenh th 0 va d lieu sau khi chuyen oi dang so hex hien th tre n 2 led.
Ket qua hien th nam trong khoang t 00 en FF.
quet equ p0 ;dieu khien quet transistor
led7 equ p3 ;dieu khien cac doan a,b,c,...

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
3 . | .
inadc equ p1 ;nhap du lieu tu adc
control equ p2 ;dieu khien adc

;ALE bit p0.3
start bit p2.3

org 0000h
mov dptr,#ma7doan
main: lcall ctcd_adc ;goi chtr con chuyen doi du lieu
lcall gma_hex_bcd ;goi chtrinh con giai ma so hex sang led 7 doan
lcall delayhthi
ljmp main ;nhay ve chuyen doi tro lai

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtrinh con chuyen so hex thanh so BCD va sau do thi chuyen
;ma BCD thanh ma 7 doan de hien thi so thap phan
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
gma_hex_bcd:
mov b,#10 ;chuyen so hex sang ma BCD
div ab
mov 10h,b ;luu hang don vi BCD

mov b,#10
div ab ;(a) chua so hang tram, (b) chua hang chuc

movc a,@a+dptr
mov 25h,a ;cat so hang tram

mov a,b
movc a,@a+dptr
mov 26h,a

mov a,10h
movc a,@a+dptr
mov 27h,a
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con chuyen doi du lieu analog sang so ket qua luu trong A
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ctcd_adc: mov control,#00000000B ;goi ra port
setb start ;start = 1
nop
nop
clr start
lcall delayhthi ;goi chtr con delay co hien thi
mov a,inadc ;doc du lieu sau khi chuyen doi
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con co ghep chtr con hien thi
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delayhthi: mov 7eh,#20
pqn: mov 7fh,#30
delpqn: lcall hthi ;goi chuong trinh con hien thi
djnz 7fh,delpqn
djnz 7eh,pqn
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con hien thi ket qua dang so hex sau khi giai ma ra led
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hthi: mov led7,27h ;lay du lieu tung kenh
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 291
mov quet,#11111110b ;goi ma quet cho 1 led sang
lcall delay10
mov quet,#0ffh ; tat het de chong lem

mov led7,26h ;lay du lieu tung kenh
mov quet,#11111101b ;goi ma quet cho 1 led sang
lcall delay10
mov quet,#0ffh ; tat het de chong lem
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay10: mov 7ch,#50h
djnz 7ch,$
ret

;khai bao ma 7 doan tu so '0' den so '9'
ma7doan: db 0C0h,0F9h,0A4h,0B0h,099h,092h,082h,0F8h
db 080h,090h,088h,083h,0c6h,0a1h,086h,08eh
end
Trong chng trnh s dung cac khai bao: quet va led7 la 2 port giao tiep ieu khien 8
led 7 oan, inadc la port ket noi vi 8 ng d lieu so ngo ra cua adc 0809, control la port
ket noi ieu khie n chon ke nh va cac tn hieu ieu khien Start va ALE noi chung.
Trong chng trnh s dung delay e ch ADC chuyen oi xong. Trong thi gian ch th vi
ieu khien se thc hien quet 8 led 7 oan e hie n th d lieu.
8o| top 8: Hay viet chng trnh chuyen oi kenh th 0 va ket qua hien th dang so BCD
tren 3 led.
8o| top : Hay viet chng trnh chuyen oi ADC kenh th 0 va kenh th 1 hien th tren 3
led 7 oan.

2. C|oc t|ep v| d|eu kh|en vc| ADC |CL70:
Vi vi mach ADC 0809 ch chuyen oi 8 bit, khi co nhng yeu cau vi so bit nhie u hn th vi
mach hien ang c s dung pho bie n la ICL7109. Cac thong so lam viec cua vi mach nh sau:
- ADC 2 o doc 12 bit.
- Cac ngo ra 3 trang thai tng thch vi TTL to chc theo byte.
- Co cac ngo vao RUN/HOLD va ngo ra STATUS du ng e giam sat va ieu khien qua
trnh chuyen oi.
- Nhieu co gia tr thap 15V
P-P
.
- Dong ngo vao rat nho khoang 1pA.
- Toc o chuyen oi 30 lan / 1giay.
- Mach dao ong tch hp ben trong s dung thach anh 3.58MHz.
- Dung nguon 5V.
S o chan cua IC ADC ICL 7109 nh hnh 7-17
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
3 . | .

H|nh 7-7. 5c dc chon |C ADC |CL 70.
S o giao tiep vi ie u khien vi ADC ICL 7109 nh hnh 7-18:
+5V
C4
0. 33
C2
1M
C5
0. 15
Y1
VCC
R2 1M
C1
C
R3
5K
R4
20K
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
J1
CON2
1
2
R1
R
-5V
C3
1
+5V
RESET
VCC
Y2
3. 58MHz
R5 22K
U1
I CL7109
35
34
16
15
14
13
12
11
10
9
8
7
6
5
36
39
25
23
22
32
21 24
26
27
33
40
28
20
19
18
30
31
37
38
29
17
4
3
2
IN+
IN-
B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
B11
B12
REFIN+
REFIN-
BUFOSCOUT
OSCOUT
OSCI N
INT
MODE OSCSEL
RUN/HOLD
SEND
COM
V+
V-
CE/ LOAD
HBEN
LBEN
BUFF
AZ
REFCAP+
REFCAP-
REFOUT
TEST
OR
POL
STATUS

H|nh 7-8. C|oc t|ep v| d|eu kh|en A18552 vc| |C ADC |CL 70.
Trong s o hnh 7-18, vi ieu khien dung 12 ng ket noi vi 12 ng d lieu so ngo ra
cua ADC 7109 t B12:B1. 4 ng ie u khien bao gom: ng ieu khie n chon chip CE, ng
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 293
kiem tra cc tnh dng khi POL mc H, ng kiem tra trang thai STATUS mc H khi d lieu
a c chot, va ng kiem tra tn hieu OR cho biet tn hieu tng t ngo vao co vt qua
ngng hay kho ng.
oi vi ADC 7109 th qua trnh ieu khien chuyen oi n gian hn v no ch co 1 kenh va
chuyen oi lien tuc. Lu o chuyen oi nh hnh 7-19

H|nh 7-. Luu dc d|eu kh|e n ADC |CL 70.
Khi muon oc d lieu t ADC th ieu khien cho CE xuong mc 0, tien hanh kiem tra xem
tn hieu bao tran e biet co b tran hay khong: neu b tran th d lieu so nhan ve khong co y ngha
v chung khong ung, neu khong b tran th tien hanh nhan d lieu, cho CE len mc 1 tr lai. Ket
thuc 1 chu ky va tien hanh x ly ket qua.
3. C|oc t|ep v| d|eu kh|en vc| DAC 0808:
DAC la vi mach chuyen oi tn hieu so sang tn hieu tng t, vi mach thng c s dung
pho bien la DAC 0808 - 8 bit:
S o chan DAC 0808 nh hnh 7-20:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
3- . | .

H|nh 7-20. 5c dc chon |C DAC 0808.
S o giao tiep vi ie u khien vi DAC 0808 nh hnh 7-21:
-12V
-
+
IC2
OP07
3
2
6
7
4 8
1
R2
5K
C2
C
VCC
R4
5K
Y1
VCC
I C1 DAC 0808
5
6
7
8
9
10
11
12
14
15
16
4
1
3
3
2
D0
D1
D2
D3
D4
D5
D6
D7
VREF+
VREF-
COMP
OUT
V
c
c
-
V
c
c
G
N
D
5V -12V
R3
5K
VCC
R1
R
-12V
+12V
C1
C
J1
ANALOG OUT
1
2
RESET
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1.0/T2
P1.1/ T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
I NT0
I NT1
T0
T1

H|nh 7-2. 5c dc g|oc t|ep v| d|e u kh|en vc| DAC 0808.
Trong s o giao tiep vi ieu khien dung port 1 e xuat d lieu so en DAC 0808, tn hieu
ngo ra cua DAC c a qua mach khuech ai dung op-amp OP07 hoac op-amp 741, bien tr R2
dung e chnh o phan giai cho DAC.
|V. V| |EU KH|EN C|A0 1|EP V0| kELA VA 0NC C0 8U0C:
. C|oc t|ep v| d|eu kh|en vc| kELA:
Trong cac ng giao tiep ieu khien cac thiet b s dung nguon 220VAC th phai dung cac
linh kien giao tiep trung gian. Cac linh giao tiep bao gom relay, SCR, TRIAC,
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 295
Trong phan nay se trnh bay phan giao tiep vi relay.
Khi s dung relay can phai biet cac thong so: ien ap lam viec cua cuon day, dong lam viec
cua cuon day va dong ie n lam viec cua tiep iem e ieu khien tai.
S o mach giao tiep vi ieu khien vi 2 relay nh hnh 7-22:
Trong s o s dung relay du ng nguon 12V, transistor chon co dong lam viec I
C
phai ln hn
dong lam viec cua cuon day relay.
Moi relay thng co 1 tiep iem thng h va 1 tiep iem thng ong.
VCC
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
VCC
D1
Q2
+12V
R1
R
RESET
Y1
C1
C
Q1
R12
10K
R2
RELAY
5
2 1
3 4
5
2 1
3 4
+12V
D2
R1
RELAY
5
2 1
3 4
5
2 1
3 4
10K
R11

H|nh 7-22. 5c dc g|oc t|ep v| d|eu kh|e n vc| re|oy.
Trong s o hnh 7-22 co 1 khuyet iem: neu ta s dung tiep iem th ng h cua relay e
ieu khie n tai la ong c th khi vi ieu khien mi cap ien th mach auto reset se thc hien lam
cac ngo ra cua cac port mc H lam cac transistor dan relay ong ong c chay. Het thi
gian reset th vi ieu khien mi thc hien chng trnh tat ong c, s kien nay se xay ra khi ta
nhan nut reset he thong. ie u nay nguy hiem cho cac oi tng c ieu khien bang ong c.
e khac phuc khuyet iem tren bang cach them 1 co ng ao ngo ra trc khi ie u khie n
transistor hoac dung IC giao tiep ULN2803 nh hnh 7-23:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
3 . | .
VCC
+12V
+12V
+12V
R2
RELAY
5
2 1
3 4
5
2 1
3 4
I C2
ULN2803
10
1
2
3
4
5
6
7
8
18
17
16
15
14
13
12
11
9
COM
I N1
I N2
I N3
I N4
I N5
I N6
I N7
I N8
OUT1
OUT2
OUT3
OUT4
OUT5
OUT6
OUT7
OUT8
GND
R3
RELAY
5
2 1
3 4
5
2 1
3 4
R
10k
R1
RELAY
5
2 1
3 4
5
2 1
3 4
VCC
+12V
R4
RELAY
5
2 1
3 4
5
2 1
3 4
+12V
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/P
TXD
RXD
EA/VP
X1
X2
RST
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
P1.0/T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
RESET
+12V
Y1
RN2
10k
1
2
3
4
5
6
7
8
9
C
10

H|nh 7-23. 5c dc g|oc t|ep v| d|eu kh|e n vc| 4 re|oy quo |C g|oc t|ep ULN2803.
Trong s o tren ULN 2803 la IC giao tiep em ao: vi moi ngo ra chu dong chay vao len
en 500mA, ien ap lam viec len e n 50V hoac 95V tuy theo loai. Ngo vao tng thch vi ho
TTL. Co diode bao ve ngo ra. Trong mach s dung 4 ngo ra cua vi ieu khien c a qua 2
cong NOT nen khi vi ieu khien b reset th trang thai ngo ra cua cac port bang 1, ngo ra cua IC
ULN 2803 cung bang 1 tng ng vi nguon 12V nen relay khong dan. Ngc lai neu ngo ra cua
vi ieu khien bang 0 th relay dan.
2. C|oc t|ep v| d|eu kh|en vc| dcng cc buc c:
ong c bc co chc nang ieu khien cac thiet b di chuyen vi khoang cach ngan v du
nh ie u khie n di chuyen au oc a. Tuy thuoc vao cong suat cua ong c ma s dung cac mach
giao tiep cho phu hp. Thng th cac ong c bc luon i kem vi mach ieu khien va ch nhan
lenh t vi x ly hay vi ieu khien.
Hnh 1 o ng c bc loai nho dung trong o a mem 1.2 MB nh hnh 7-24:

H|nh 7-24. H|nh dcng cc bucc |co| nhc.
S o day ben trong ong c bc nh hnh 7-25:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 297

H|nh 7-25. Coc cuc n doy be n trcng dcng cc bucc.
ong c bc n cc co 5 ngo ra: trong o co 4 au day coil1: coil4 dung e ieu khien co n
au day common dung e noi nguon cung cap. K hieu cac mau day theo qui nh nh hnh 7-26:

H|nh 7-26. Coc cuc n doy be n trcng dcng cc bucc.
ong c bc n cc co 6 ngo ra: trong o co 4 au day coil1: coil4 dung e ieu khien co n
2 au day con lai chnh la day common c tach ra lam 2 khi dung phai noi ca 2 vi nguon cung
cap. Hai day common nay co cung mau.
Cach xac nh ca c cuon day cua ong c bc theo trnh t nh sau:
Nc| cuc n doy ccmmcn vc| ngucn cung cop
Chcn mct dou doy vo nc| dou doy dc
xucng moss vo g|o su dou doy dc |o doy thu 4
hoy xem h|nh ben pho| tucng ung.

C|u nguye n bucc tren
1|ep thec chcn dou doy t|ep thec trcng
3 dou ccn |o| vo nc| vc| moss quon so t thoy
rctcr quoy nhe bucc thec ch|e u k|m dcng hc
th| dc |o cucn doy thu 3.

1hoc cuc n doy thu 3 ro khc| moss
1|ep thec chcn dou doy t|ep thec trcng
2 dou ccn |o| vo nc| vc| moss quon so t thoy
rctcr quoy nhe bucc nguc c ch|eu k|m dcng
hc th| dc |o cuc n doy thu .

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
3` . | .
Cucn do y ccn |o| |o cucn doy thu nhung
to cung cc the k|em tro bong coch nc| nc vc|
moss quon sot thoy rctcr khcng quoy.

Cach xac nh cuon day common bang chon 1 au day tuy y lam day common, o ien tr
vi 4 au day con lai: neu 4 gia tr ien tr eu bang nhau th au day a chon la au common,
neu kho ng bang th chon au day khac va thc hien lai se tm ra c au day common.
Trnh t ieu khien ong c bc gom co 3 kieu: kieu kch 1 cuon day, kieu kch 2 cuon day
va cach ieu khien na bc.
a. Kieu ieu khien kch 1 cuon day nh hnh 7-27:

H|nh 7-27. |eu kh|en k|ch cucn doy.
Trong hnh tren th moi 1 thi iem ch co 1 cuon day trang thai on. ong c se quay moi
bc khi thay oi trang thai ieu khie n.
Muon quay ao chieu th trnh t ie u khien tien hanh theo chieu ngc lai.
b. Kieu ieu khien kch 2 cuon day nh hnh 7-28:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 299

H|nh 7-28. |eu kh|e n k|ch 2 cucn doy.
c. Phoi hp 2 cach ieu khien e ieu khien na bc nh hnh 7-29:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .


H|nh 7-2. |eu kh|e n phc| hcp co ho|.
Trong cach ieu khien na bc co tat ca 8 trang thai khac nhau.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 301
S o mach giao tiep vi ieu khien vi ong c bc nh hnh 7-30:
RN2
10k
1
2
3
4
5
6
7
8
9
VCC
IC2
ULN2803
10
1
2
3
4
5
6
7
8
18
17
16
15
14
13
12
11
9
COM
I N1
I N2
I N3
I N4
I N5
I N6
I N7
I N8
OUT1
OUT2
OUT3
OUT4
OUT5
OUT6
OUT7
OUT8
GND
Coil4
Coil1
Y1
Coil3
+12V
RESET
Coil2
DONG CO BUOC
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
R
10k
C
10
VCC

H|nh 7-30. 5c dc g|oc t|ep v| d|eu kh|e n vc| dcng cc buc c quo |C ULN2803.
Chan common cua ong c bc phai noi vi nguo n ma o ng c lam viec. Neu ong c cong
suat ln vi dong lam viec ln hn dong cua IC ULN2803 th nen dung transistor.
V du 8: Chng trnh ieu khien ong c bc quay lien tuc 1 chieu: du ng kie u ieu khie n
kch 1 cuon day:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr dieu khien step motor quay lien tuc theo 1 chieu, thoi gian delay dai thi dong co quay cham
;thoi gain nho thi dong co quay nhanh, thoi gian qua nho thi dong co khong dap ung duoc se dung yen
;dung 1 port de dieu khien motor qua ic giao tiep 2803 - dung 4 bit thap hoac 4 bit cao
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

outstep equ p1
org 0000h

main: mov outstep,#10001000b
lcall delay
mov outstep,#01000100b
lcall delay
mov outstep,#00100010b
lcall delay
mov outstep,#00010001b
lcall delay
sjmp main
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r6,#0
del: mov r7,#0
djnz r7,$
djnz r6,del
ret
end
V du 9: Chng trnh ieu khie n ong c bc quay 1 vong roi ng ng:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr dieu khien step motor quay 1 vong roi ngung luon
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .
;dung 1 port de dieu khien motor qua ic giao tiep 2803
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
outstep equ p1
sobuoc equ 50 ;loai dco: 50x4=200 buoc

org 0000h

mov dptr,#datastep ;nap dia chi quan ly ma
mov r0,#0
mov r2,#sobuoc ;50 chu ky la vong
main2: mov r1,#4 ;1 chu ky 8 buoc

main1: mov a,r0
movc a,@a+dptr
mov outstep,a
lcall delay

inc r0
anl 00h,#03h ;anl r0 voi 00000011b
djnz r1,main1

djnz r2,main2
sjmp $ ;dung lai
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con delay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r6,#10h
del: mov r7,#0
djnz r7,$
djnz r6,del
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;khai du lieu dieu khien dong co buoc
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
datastep: db 10001000b
db 01000100b
db 00100010b
db 00010001b
end
ong c s du ng trong bai v du nay co thong so la 1 vong 200 bc.

8o| top 0: Hay thiet ke mach ieu khien 1 ong c bc vi cac nut nhan la Start, Stop, Inv
va giao tiep vi 4 led 7 oan. Viet chng trnh ieu khien ong c bc vi yeu cau nh sau: khi
nhan nut Start th ong c chay va hien th so bc tre n led 7 oan, khi nhan nut stop th ong c
ngng, khi nhan nut ao chieu th ong c ao chieu.
V. V| |EU KH|EN C|A0 1|EP V0| LCD:
. C|c | th|eu LCD:
cac phan giao tiep vi led 7 oan co han che v ch hien th c cac so t 0 e n 9 hoac so
hex t 0 e n F khong the nao hien th c cac thong tin k t khac, nhng chung se c hien
th ay u tren LCD.
LCD co rat nhieu dang phan biet theo kch thc t va k t e n hang chuc k t, t 1 hang
en vai chuc hang. V du LCD 162 co ngha la co 2 hang, moi hang co 16 k t. LCD 204 co
ngha la co 4 hang, moi hang co 20 k t.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 303
Hnh cua 1 LCD 162 nh hnh 7-31:

H|nh 7-3. H|nh cuo LCD
2. 5c dc chon cu o LCD:
LCD co nhieu loai va so chan cua chung cung khac nhau nhng co 2 loai pho bien la loai 14
chan va loa i 16 chan, s khac nhau la cac chan nguon cung cap, con cac chan ieu khien th khong
thay oi, khi s dung loai LCD nao th phai tra datasheet cua chung e biet ro cac chan. S o
chan cua LCD nh bang sau:

8ong 7-6. Coc cho n cuo LCD
Trong 14 chan cua LCD c chia ra lam 3 dang tn hieu nh sau:
.:c c|:r c:r r.or: Chan so 1 la chan noi mass (0V), chan th 2 la Vdd noi vi nguon +5V.
Chan th 3 du ng e chnh contrast thng noi vi bien tr.
.:c c|:r c.e. ||.er: Chan so 4 la chan RS dung e ieu khien la chon thanh ghi. Chan R/W
dung e ieu khien qua trnh oc va ghi. Chan E la chan cho phep dang xung chot.
.:c c|:r c. |.e. !7:!: Chan so 7 en chan so 14 la 8 chan dung e trao oi d lieu gia
thiet b ieu khien va LCD.
3. 5c dc moch g|oc t|ep v| d|eu kh|en vc | LCD:
Trong phan nay se trnh bay phan giao tiep vi ie u khien vi LCD. Hnh 7-32 se trnh bay
ket noi dung vi ieu khien 87C751 cua cong ty HANTRONIX t tai lieu tren Internet.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
- . | .


H|nh 7-32. C|oc t|ep v| d|eu kh|en 87C75 vc| LCD.
Co the dung vi ieu khie n AT89S52 e ieu khien LCD vi s o ket noi nh hnh 7-33:
Y1
D6
VCC
VCC
VO
D2
RESET
D4
D7
R3
10K
RS
LCD
VDD
VCC
E
C1
C
D0
D3
R/W
AT89S52
21
22
23
24
25
26
27
28
17
16
29
30
11
10
31
19
18
9
39
38
37
36
35
34
33
32
1
2
3
4
5
6
7
8
12
13
14
15
P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
RD
WR
PSEN
ALE/ P
TXD
RXD
EA/VP
X1
X2
RST
P0. 0
P0. 1
P0. 2
P0. 3
P0. 4
P0. 5
P0. 6
P0. 7
P1. 0/ T2
P1.1/T2X
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
INT0
INT1
T0
T1
D5
R1
R
VSS
D1

H|nh 7-33. C|oc t|ep v| d|eu kh|en A18552 vc| LCD.
Chu y: khi lan au s du ng LCD th phai chnh bien tr sau cho cac k t hien th tren LCD
th dng lai.
4. Coc |enh d|eu kh|en LCD:
e ieu khien LCD th co cac IC chuyen du ng c tch hp ben di LCD co ma so 447801
en cac IC 447809. Trong IC nay co bo nh RAM dung e lu tr d lieu can hie n th va thc hien
viec ieu khien LCD hien th.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 305
Cac ieu khien bao gom cac lenh c liet ke bang sau:

8ong 7-7. H|nh cuo LCD
er| xo: r:r |r| .|e:r !.er|:y: khi thc hien lenh nay th LCD se b xoa va bo em a
ch c xoa ve 0.
er| c. c|.yer cor :ro .e c:. r:r |r| ..reor ore: khi thc hien lenh nay th bo em a
ch c xoa ve 0, phan hien th tr ve v tr goc a b dch trc o. Noi dung bo nh RAM hien
th DDRAM khong b thay oi.
er| :|.e : |:r |o . .:o r:ry roce ee:: lenh nay dung e thiet lap loi vao cho cac k t hie n
th, bit ID = 1 th con tro t ong tang len 1 moi khi co 1 byte d lieu ghi vao bo hien th, khi ID =
0 th con tro se khong tang: d lieu mi se ghi e len d lieu cu. Bit S = 1 th cho phep dch chuyen
d lieu moi khi nhan 1 byte hien th.
er| c.e. ||.er cor :ro |.er :|. !.er|:y .or:ro|: lenh nay dung e ieu khien con tro (cho
hien th th bit D = 1, tat hien th th bit D = 0), tat m con tro (m con tro th bit C = 1, tat con tro
th bit C = 0), va nhap nhay con tro (cho nhap nhay th bit B = 1, tat th bit B = 0).
er| c. c|.yer cor :ro ..reor /!.er|:y |.:: lenh nay dung e ieu khien di chuyen con
tro hien th dch chuye n (SC = 1 cho phep dch chuyen, SC = 0 th khong cho phep), hng dch
chuyen (RL = 1 th dch phai, RL = 0 th dch trai). Noi dung bo nh DDRAM van khong oi.
er| :|.e : |:r c.: c|. c|o ro r|o <! r|:: | :. e: .c<! ccr: lenh nay dung e thiet
lap a ch cho bo nh RAM phat k t.
er| :|.e: |:r c.: c|. c|o ro r|o <! |.er :|. e: !!<! ccr: lenh nay dung e thiet
lap a ch cho bo nh RAM lu tr cac d lieu hien th.
Hai lenh cuoi cung la lenh oc va lenh ghi d lieu LCD.
Dang song cac tn hieu khi thc hien ghi d lieu vao LCD nh hnh 7-34:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .

H|nh 7-34. Dong sc ng d|eu kh|en cuo LCD.
Nhn vao dang song ta co the thay c trnh t ieu khien nh sau:
- ieu khien tn hie u RS.
- ieu khien tn hie u R/W xuong mc thap.
- ieu khien tn hie u E len mc cao e cho phep.
- Xuat d lieu D7:D0.
- ieu khien tn hie u E ve mc thap.
- ieu khien tn hie u R/W len mc cao tr lai.
5. Luu dc d|eu kh|en LCD:
e ieu khien LCD th phai biet trnh t ieu khien c xay dng theo cac lu o nh hnh
7-35:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 307

(a) (b)
H|nh 7-35. Luu dc d|e u kh|en LCD.
Trong lu o hnh (a) o la lu o chnh bao gom qua trnh khi tao LCD, thiet lap a ch
cua vung nh RAM va thc hie n qua trnh gi d lieu ra LCD hien th hang th 1 va tiep theo la
hang th 2.
Lu o hnh (b) la lu o cho biet trnh t thc hien qua trnh khi tao LCD bao gom cac
bc: gi ma ie u khie n 38H ra LCD va thc hien delay t nhat la 4.1 ms. Tiep tuc thc hien lan
th 2 vi thi gian delay 100s. Cuoi cung gi lien tuc cac ma ieu khien 38H, 0CH, va 01H e
thiet lap cau hnh hoat ong cho LCD.
Lu o thc hien gi t ieu khie n hay gi d lieu ra LCD e hien th nh hnh sau:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
` . | .

H|nh 7-36. Luu dc xuot |enh hcoc du ||e u ro LCD.
Trong lu o co 2 au vao: au vao th nhat neu gi t ieu khie n en LCD, au vao th 2
la gi d lieu can hien th en LCD s khac nhau nay the hien qua tn hieu ieu khie n RS.
Cac bc con lai th giong nhau va sau khi tien hanh gi xong t ieu khien/d lieu th oc
lai t ieu khien/d lieu e kiem tra bit DB7 e xem t ieu khien/d lieu a c nhan xong
cha neu cha th phai ch, neu a nhan xong th thoat va san sang e nhan t ieu khien/d lieu
tiep theo.
6. Chucng tr|nh d|eu kh|en LCD dung v| d|eu kh|en 8552:
Chng trnh sau se ieu khien LCD hien th 2 hang vi noi dung c cho cuoi chng
trnh:
V du 10:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dieu khien LCD 16X2 tren kit vi dieu khien
;DUNG PORT 0 KET DOI VOI CAC DUONG DU LIEU CUA LCD P0-7 ->D0-7
;DUNG 3 BIT CUA PORT2: P20,P21,P22 DIEU KHIEN E,R/W,RS
;tren man hinh LCD se hien thi noi dung moi
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
E BIT P2.0
rw BIT P2.1
rs BIT P2.2

byteout equ p0

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 309
ORG 0000H
mov 0a2h,#0
LCALL khtaolcd ;khoi tao lcd
LCALL first_line ;goi chtr con hien thi hang thu nhat
LCALL scond_line ;goi chtr con hien thi hang thu hai
SJMP $

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con khoi tao LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
khtaolcd: setb e ;Enable
clr rs ;RS low
clr rw ;RW low

MOV a,#38h ;tu dieu khien LCD
LCALL KTAO
LCALL ddelay41 ;delay 4.1 mSec

MOV A,#38h ;function set
LCALL KTAO
LCALL ddelay100 ;delay

MOV A,#38h ;function
LCALL KTAO

MOV A,#0ch ;tu dieu khien display on
LCALL KTAO
MOV A,#01h ;tu dieu khien Clear display
LCALL KTAO

MOV A,#06h ;tu dieu khien entry mode set
LCALL KTAO

MOV A,#80h ;thiet lap dia chi LCD (set DD RAM)
LCALL KTAO

MOV A,#0fh ;enable display cursor
LCALL KTAO

RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay 4.1 ms
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ddelay41: mov r6,#90h
del412: mov r7,#200
djnz r7,$
djnz r6,del412
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay 255 microgiay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ddelay100: mov r7,#00
djnz r7,$
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Feed command/data to the LCD module
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
command_byte:
clr rs ;RS low for a command byte
ljmp bdelay

data_byte: setb rs ;RS high for a data byte
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
' . | .
bdelay: clr rw ;R/W low for a write mode
clr e
nop

setb e ;Enable pulse
nop
nop

mov byteout,#0ffh ;configure port1 to input mode

setb rw ;set RW to read
clr rs ;set RS to command
clr e ;generate enable pulse

nop
nop
setb e
lcall ddelay100
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Chuong tri nh con hien thi noi dung hang thu 1 tren LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
first_line: MOV A,#080h ;set DDRAM
LCALL KTAO
MOV DPTR,#FLINE_DATA
lcall Write
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Chuong tri nh con hien thi noi dung hang thu 2 tren LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
scond_line: mov a,#0c0h ;set DDRAM
LCALL KTAO
mov dptr,#sline_data
lcall write
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con khoi tao LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
KTAO: mov byteout,a
lcall command_byte
RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con goi data hien thi ra LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
write: MOV A,#0
MOVC A,@a+dptr
CJNE A,#99h,Writea
RET

Writea: mov byteout,a
acall data_byte
inc dptr
SJMP Write

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; Data bytes
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FLINE_DATA: DB 'NGUYEN DINH PHU ',099h
SLINE_DATA: DB 'DAI HOC SPKT HCM',099h
END
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 311
8o| top : Hay thiet ke mach ieu khien 1 LCD giong nh tren va 1 ban phm ma tran 44
va viet chng trnh sau cho khi nhan phm so nao th tren man hnh hien th so o tai v tr tan
cung ben trai, cac so trc dch sang trai.
8o| top 2: Hay thiet ke mach ieu khien 1 LCD giong nh tre n va 1 ban phm ma tran 44
va viet chng trnh ong ho hien th gi phut giay, ngay thang nam.

V|. V| |EU KH|EN C|A0 1|EP V0| LED MA 1kAN:
. C|c | th|eu |ed mo tron:
Trong quang cao ni cong cong thng s dung led ma tran, trong phan nay se gii thieu
va trnh bay cach giao tiep vi ieu khien vi led ma tran va phan mem ieu khien led ma tran.
Led ma tran la 1 tap cac led n c bo tr theo dang ma tran cho phep hien th c cac k
t bat ky. Cac nha che tao a tch hp theo cac dang ma tran tnh theo led 57 (5 cot va 7 hang)
hoac 88 (8 hang va 8 cot) va tnh theo kch thc va phan biet theo mau cua led, cuoi cung la
phan biet led sang trong nha (indoor) hay led sang ngoai tri (outdoor). Led sang trong nha th
khong the dung c ngoai tri v anh sang mat tri chieu vao lam chung ta khong nhn thay ro,
ch co led outdoor mi thay ro, va d nhien led outdoor se co gia thanh cao hn.
Hnh 7-37 gii thieu led ma tran 57 vi 4 mau khac nhau:


H|nh 7-37. Led mo tron 57.
2. Cou toc vo phucng phop d|eu kh|en |ed mo tron:
Led ma tran la mot tap hp cac led n vi cach ket noi nh sau: theo hang th cac anode noi
chung, theo cot th cac cathode noi chung, vi led ma tran 57 th co 7 hang va 5 cot.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
' . | .
Anode noi vi mc H va cathode noi mc L th led sang, cac trng hp con lai th led tat.
Hay quan sat hnh 7-38 led ma tran 88 ang hien th ch A, co 2 cach hien th ch A:
~ | ' | -| ~ |~|. ~ .|-: ~ :
Moi k t co 5 byte ma:
- Byte ma th nha t gi ra 8 hang (led nao sang th bit tng ng bang 1, led nao tat th
bit tng ng bang 0) va cho cot th nhat xuong mc L, cac cot con lai mc H.
- Byte ma th hai gi ra 8 hang, cot th hai xuo ng mc L.
- Byte ma th ba gi ra 8 hang, cot th ba xuong mc L.
- Byte ma th t gi ra 8 hang, cot th t xuong mc L.
- Byte ma th nam gi ra 8 hang, cot th nam xuong mc L.
Do toc o thc hien nhanh ne n chung ta se nhn thay ca ch A sang.

H|nh 7-38. 5ong chu A.
Neu hang tren cung la H0 va hang di cung la H7 th ma cua ch A va ma quet c tom
tat nh bang sau:
MA |EU KH|EN HANC MA QUE1 |EU KH|EN C0 1
H7 H6 H5 H4 H3 H2 H H0 5c
hex
C0 C C2 C3 C4
0 0 0 F8 0 Cct thu nhot song
0 0 0 0 0 0 24 0 Cct thu ho| song
0 0 0 0 0 0 22 0 Cct thu bo song
0 0 0 0 0 0 24 0 Cct thu tu song
0 0 0 F8 0 Cct thu nom song

8ong 7-8. Quet thec co ch .

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 313
e thc hien quet cot va de dang cho viec m rong cot khi ket noi nhie u led ma tran vi
nhau th thanh ghi dch thng c s dung.
Vi ma tran 88 th thi gian sang cua 1 cot la 1/8 chu ky quet, thi gian tat la 7/8, chnh v
the led se sang khong ro neu lam viec vi dong va ap nh mc. e tang c ng o sang th phai
tang ap va dong. Bnh thng led lam viec vi dong t 10mA en 20mA va ap la 2V, nhng vi
phng phap quet cho 1 led 88 th dong phai tang len khoang 8 lan t 80mA en 160mA tng
ng.
Cach tnh toan nh sau: vi dong lam viec bnh thng (khong quet) chon la 20mA, ap lam
viec nh mc 2V. Khi o ie n tr cua moi led la:
100
20
2
mA
V
I
U
R
LED

Khi dung phng phap quet th dong tc thi phai bang 160mA, khi o dong trung bnh:
mA
mA
I
TB LED
20
8
160
_

Suy ra ien ap cung cap cho led:
V mA V
LED LED
16 100 160
_

Thng th chon dong thap hn ne n ap cung cap se thap khoang t 9V en 12V.
S o mach ieu khien led ma tran nh hnh 7-39:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
'- . | .

H|nh 7-3. 5c dc mo ch d|eu kh|en.
u iem:
- Khi m rong them led th ch can ket noi song song 8 hang va cot dung thanh ghi
dch rat n gian do so lng ng ieu khien t.
- Moi mot thi iem ch co 1 cot sang nen dong tieu thu thap.
- Mach ien n gian.
Khuyet iem:
- B gii han so cot v khi m rong cang nhieu cot th thi gian tat cua led tang them
tang dong qua ln nguy hiem cho led va led sang khong ro sinh ra hien tng
chap chn. Gia s co 100 cot th cho du quet nhanh hay quet cham th thi gian sang
cua moi cot van khong oi bang 1/100, thi gian tat bang 99/100.
~ | | -| ~ :. ~ .|-: ~ |~|
Moi k t co 5 byte ma giong nh cach 1, nhng trnh t thc nh sau:

MA |EU KH|EN HANC MA QUE1 |EU KH|EN C0 1
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 315
H7 H6 H5 H4 H3 H2 H H0 5c
hex
C0 C C2 C3 C4
0 0 0 0 0 0 0 Hong thu 0 so ng
0 0 0 0 0 0 0 0 Hong thu so ng
0 0 0 0 0 0 0 0 0 Hong thu 2 so ng
0 0 0 0 0 0 0 0 0 Hong thu 3 so ng
0 0 0 0 0 0 0 0 0 Hong thu 4 so ng
0 0 0 0 0 0 0 0 0 0 0 0 Hong thu 5 so ng
0 0 0 0 0 0 0 0 0 Hong thu 6 so ng
0 0 0 0 0 0 0 0 0 Hong thu 7 song
HEX 07 D8 DD D8 07
8ong 7-. Quet thec co ch 2.
- Cho 5 bit th 0 cua 5 byte ma ch A c dch ra cac cot, cho hang H0 dan khi o
hang H0 sang tuy thuoc vao d lieu.
- Cho 5 bit th 1 cua 5 byte ma ch A c dch ra cac cot, cho hang H1 dan khi o
hang H1 sang tuy thuoc vao d lieu.
- Cho 5 bit th 2 cua 5 byte ma ch A c dch ra cac cot, cho hang H2 dan khi o
hang H2 sang tuy thuoc vao d lieu.
-
- Cho 5 bit th 70 cua 5 byte ma ch A c dch ra cac cot, cho hang H7 dan khi o
hang H1 sang tuy thuoc vao d lieu.
Vi cach quet theo hang th thi gian sang cua led la 1/8 thi gian tat la 7/8.
Khi tang them led so cot tang len th thi gian sang va tat cua moi led van khong oi.
Nhng chu ky quet se tang v phai ton them thi gian gi d lieu ra cac cot, cang nhieu cot th
cang ton thi gian gi d lieu.
e ap ng c toc o quet khi bang quang bao co nhieu led th phai s dung cac IC co toc
o lam viec cao khoang vai chuc MHz nh CPLD phu trach cong viec nay.
3. Chucng tr|nh d|eu kh|en |ed mo tron:
Cac chng trnh v du s du ng s o nguye n ly hnh 7-39 va ieu khien theo cach 1.
V du 11: Chng trnh ieu khien hien th ch A:
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh hien thi ki tu chu A tren ma tran led dung yen
;ma chu A = 007H,0DBH,0DDH,0DBH,007H
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hang equ p3 ;dieu khien hang
cotx equ p0 ;dieu khien cot mau xanh
cotd equ p2 ;dieu khien cot mau do

org 0000h
mov cotx,#0 ;tat quet neu co ket noi
mov cotd,#0 ;tat quet neu co ket noi

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
' . | .
main: mov hang,#007h ;goi du lieu ra hang 1
mov cotx,#00000001b ;goi ma quet cho 1 transistor dan
lcall delay
mov cotx,#00h ;chong lem

mov hang,#0DBh ;goi du lieu ra hang 2
mov cotx,#00000010b ;cot thu 2 dan
lcall delay
mov cotx,#00h ;chong lem

mov hang,#0ddh ;goi du lieu re hang 3
mov cotx,#00000100b
lcall delay
mov cotx,#00h

mov hang,#0DBh ;goi du lieu ra hang 4
mov cotx,#00001000b
lcall delay
mov cotx,#00h

mov hang,#007h ;goi du lieu ra hang 5
mov cotx,#00010000b
lcall delay
mov cotx,#00h

sjmp main
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay nho
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay: mov r5,#10
de: mov r6,#20
djnz r6,$
djnz r5,de
ret
end
8o| top 3: Hay thiet ke mach ieu khien 4 led ma tran 88 dung thanh ghi dch 6B595 e
quet cot. Viet chng trnh hien th 1 chuoi k t tuy y.

V||. V| |EU KH|EN C|A0 1|EP V0| kEAL 1|ME 2C887:
. C|c | th|eu kEAL1|ME:
Real-time la bo em thi gian thc thng c s dung trong cac ng dung ieu khien theo
thi gian, v du may tnh luon co 1 bo em thi gian thc va can c vao thi gian thc e biet cac
thong so ve thi gian cua 1 tap tin hay cac thong tin c tao ra, trong he thong van hanh cua ngan
hang cac giao dch xay ra eu phai xac nh thi gian, trong he thong to ng ai luon co ong ho
thi gian thc e quan ly thue bao ve thi gian goi, thi iem goi e sau nay tnh tien cac cuoc
goi, moi hoat ong cua chung ta eu ong bo theo thi gian thc, qua nhng g trnh bay ta thay
c tam quan tro ng cua bo em thi gian thc nen trong phan nay trnh bay phan giao tiep vi IC
thi gian thc la DS12C887 do hang Dallas san xuat.
2. Coc thc ng sc cou truc kEAL1|ME D52887:
Cau truc cua Real-time bao gom:
- Chan IC tng thch vi MC146818B va DS1287.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 317
- D lieu kho ng b mat sau khoang 10 nam khong can nguon cung cap cho IC.
- Tch hp ben trong nguon pin va nguon dao ong.
- Bo em giay, phut, gi, th trong ngay, ngay, thang, nam co gia tr en 2100.
- Cac thong so thi gian c nh hnh 2 dang BCD hoac nh phan.
- Co 2 mode hoat ong: mode 12 gi va mode 24 gi.
- Cho phe p la chon ket noi theo kieu Motorola va Intel.
- a hp bus a ch va bus d lieu e tiet kiem chan IC.
- Co 128 byte o nh RAM: trong o 14 byte au dung o ng ho va thanh ghi ieu
khien, 114 byte con lai dung lam bo nh RAM a dung. Cac d lieu lu vao cac o
nh nay cung khong b mat khi mat nguon cung cap.
- Co chan tn hieu ngo ra tao xung vuong co the lap trnh.
- Co tn hieu bao ngat.
- Co 3 nguon bao ngat co the che c va co the kiem tra c:
3. 5c dc chon vo chuc nong coc chon kEAL1|ME D52C887:
Hnh 7-40 la s o chan cua IC real-time DS12C887:

H|nh 7-40. 5c dc chon cuo reo|-t|me D52C887.
Ten cac chan cua IC nh sau:
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
'` . | .

Chc nang cac chan:
AD0-AD7 (Multiplexed Bidirectional Address/Data Bus): Bus a h p tiet kiem chan bi v
thong tin a ch va thong tin d lieu c dung chung ng tn hieu. a ch c xuat ra trong
khoang thi gian th nhat cua chu ky oc hoac ghi va sau o d lieu c xuat ra trong khoang
thi gian con lai cua chu ky oc/ghi. a hp a ch/d lieu kho ng lam cham thi gian truy cap.
GND, VCC: Nguon cung cap cho Real-time. VCC la ien ap ngo vao +5 volt .
Khi ien ap ung bang 5V th real-time cho phep truy cap ay u va d lieu co the oc va
ghi.
Khi VCC thap hn 4.25 volts th qua trnh oc va ghi se b cam. Tuy nhien, hoat ong em
thi gian ben trong van c tiep tuc kho ng b anh hng bi ien ap b sut giam ben ngoai.
Khi VCC giam xuong thap hn 3V th bo nh RAM va hoat ong em thi gian c chuyen
sang s dung nguon nang lng ben trong.
Chc nang hoat ong em thi gian vi o chnh xac vao khoang 1 phut/thang nhiet o
25
0
C bat chap ien ap ngo vao chan Vcc.
MOT (Mode Select): Chan MOT la chan co chc nang la chon gia hai loai bus.
Khi c noi len VCC, bus nh thi Motorola c chon la.
Khi c noi xuong GND hoac bo trong th bus nh thi Intel c la chon.
Chan co ien tr ke o xuo ng be n trong co gia tr vao khoang 20K.
SQW (Square Wave Output): Chan SQW co the xuat ra 1 tn hieu trong 13 loai tn hieu
c cung cap bi cac bo chia 15 cap ben trong cua Real Time Clock.
Tan so cua chan SQW co the thay oi bang cach lap trnh cho thanh ghi A nh a trnh bay
bang 7-10.
Tn hieu SQW co the m hoac tat s dung bit SQWE trong Register B. Tn hieu SQW kho ng
xuat hien khi Vcc thap hn 4.25 volts.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 319

8ong 7-0. Coc thcng sc t|n h|eu ngc ro 5QW.
AS (Address Strobe Input): ngo vao nhan xung chot a ch. Sn xuong cua AS/ALE lam
cho a ch b chot lai ben trong cua DS12C887. Sn len tiep theo khi xuat hien tren bus AS se
xoa a ch bat chap chan CS co c chon hay khong.
DS (Data Strobe or Read Input): Chan DS/RD co 2 kie u s du ng tuy thuoc vao mc cua
chan MOT.
Khi chan MOT c ket noi len Vcc, bus nh thi Motorola c la chon. Trong kieu nay
DS la xung dng trong khoang thi gian con lai cua chu ky bus va c goi la Data Strobe. Trong
cac chu ky oc, tn hieu DS xac nh thi gian DS12C887 xuat d lieu. Trong cac chu ghi, xung
DS se ieu khien DS12C887 chot d lieu e ghi vao ben trong.
Khi chan MOT c noi xuong GND, bus nh thi Intel c la chon. Trong kieu nay,
chan DS c goi la Read( RD). RD xac nh chu ky khi DS12C887 ieu khie n bus oc d lieu.
Tn hieu RDcung co the xem la tn hieu Output Enable ( OE ) thng thay trong cac bo nh .
R/ W\ (Read/Write Input): Chan W R/ cung co 2 cach hoat ong.
Khi chan MOT c ket noi len Vcc cho che o nh thi Motorola, th mc ie n ap cua
chan W R/ dung e xac nh chu ky hie n tai la chu ky oc hoac ghi.
Chu ky oc oi hoi chan W R/ phai mc cao khi chan DS mc cao.
Chu ky ghi oi hoi chan W R/ phai mc thap trong suot qua trnh chot tn hieu cua DS.
Khi chan MOT c noi GND cho che o nh thi Intel, tn hieu W R/ la tn hieu hoat ong
tch cc mc thap con c goi la WR. Trong che o nay, chan W R/ c nh ngha nh tn hieu
Write Enable (WR) nhng trong cac bo nh RAM.
CS (Chip Select Input): Tn hieu cho n la tch cc mc thap e cho phep truy xuat
DS12C887. CS phai trang thai tch cc trong khoang thi gian DS va AS cua che o nh thi
Motorola va trong suot RD va WR cua che o nh thi Intel. Cac chu ky Bus xay ra ma CS
khong trang thai tch cc th khong c phep truy xuat.
Khi ien ap nguon cung cap Vcc thap hn 4.25 volts th DS12C887 se khong cho phep truy
xuat d lieu bang cach khong cho phep ieu khien chan CS nham bao ve d lieu cua ong ho thi
gian thc ben trong cu ng nh d lieu RAM trong suot qua trnh nguon khong ung hay mat nguon.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .
IRQ (Interrupt Request Output): Chan IRQ la ngo ra tch cc mc thap cua DS12C887
dung e bao ngat ti bo x ly . Ngo ra IRQ mc thap khi bit trang thai sinh ra ngat xuat hien
cung vi bit cho phep ngat tng ng mc 1. e xoa chan IRQ th vi x ly phai tien hanh oc
thanh ghi C hoac tac ong en RESET .
Khi khong sinh ra ngat th IRQ trang tong tr cao xem nh h mach. Nhieu thiet b ngat
co the noi ti mot IRQ bus. Tn hieu
IRQ
vi ngo ra cc thu e h ne n phai dung 1 ien tr ke o
len ben ngoai.
RESET (Reset Input): Chan RESET khong anh hng en ong ho , lch, hoac la RAM.
che o cap nguon, chan RESET co the b keo xuong trong thi gian cho phep e on nh nguon
cung cap. Thi gian ma chan RESET b keo xuong mc thap phu thuoc vao ng dung. Tuy nhien
neu chan RESET c s dung che o cap nguon, thi gian RESET mc thap co the vt qua
200ms e chac chan bo nh thi ben trong DS12C887 che o power-up a het. Khi RESET
mc thap va nguon cung cap VCC tre n 4.25 volts, nhng ieu sau dien ra:
E. Bit cho phep ngat nh ky ((Periodic Interrupt Enable (PEI)) c at mc 0.
F. Bit cho phep ngat chuong (Alarm Interrupt Enable (AIE)) c at mc 0.
G. Bit c cho phep ngat ket thuc cap nhat ((Update Ended Interrupt Flag (UF)) c xoa
ve 0 zero.
H. Bit c trang thai ye u cau ngat (Interrupt Request Status Flag (IRQF)) c at mc
0.
I. Bit c cho phep ngat nh ky (Periodic Interrupt Flag (PF)) c at mc 0.
J. Thiet b khong s dung c cho ti khi chan RESET\ tr lai mc logic 1.
K. Bit c cho phep ngat chuong (Alarm Interrupt Flag (AF)) c at mc 0.
L. Chan IRQ\ trong trang thai tong tr cao.
M. Bit cho phep xuat song vuong (Square Wave Output Enable (SQWE)) c at
mc 0.
N. Bit cho phep ngat ket thuc cap nhat (Update Ended Interrupt Enable (UIE)) b xoa ve
mc 0.
Trong cac ng dung thong thng chan RESET co the c noi len VCC. Ket noi nh vay se
cho phep DS12C887 hoat ong va khi mat nguon se khong lam anh h ng e n bat ky thanh ghi
ieu khien nao.
4. Hcot dcng cu o reo|-t|me c|cck kh| cop nguc n hcoc kh| mot d|en:
Chc nang cua ong ho thi gian thc se tiep tuc hoat o ng va tat ca RAM, thi gian, lch va
thi gian bao gi va nhng vung nh se kho ng mat d lieu con lai bat chap ien ap ngo vao VCC.
Khi ien ap VCC c cung cap cho DS12C887 va at ti ien ap ln hn 4.25 volts, thiet b
co the s dung c sau 200 ms, dao ong c cung cap, no cho phep bo dao ong hoat ong va
qua trnh dao ong kho ng anh h ng bi chan RESET . Tiep theo he thong a i vao o n nh sau
khi nguon c cung cap.
Khi VCC rt xuo ng di 4.25 volts, ngo vao la cho n CS b bat buoc chuyen sang trang thai
khong hoat ong bat chap gia tr tai ngo vao chan CS . V vay DS12C887 c hoat ong che o
chong ghi. Khi DS12C887 ang trang thai chong ghi, moi ngo vao eu b bo qua con cac ngo ra
eu trang thai tong tr cao.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 321
Khi VCC rt xuo ng ien ap khoang 3 volts, ien ap VCC cung cap ben ngoai c cat i va
nguon pin lithium ben trong DS12C887 se cung cap nguo n cho Real Time Clock va bo nh
RAM.
Ban o a ch cua DS12C887 c trnh bay hnh 7-41.


H|nh 7-4. 1c chuc bc nhc ben trcng cuo keo|-t|me D52C887.
Ban o a ch bao gom 114 bytes RAM thong dung, 14 bytes RAM ma thanh phan bao gom
ong ho thi gian thc, lch, d lieu bao gi va 4 bytes thanh ghi c s dung cho viec ieu khien
va thong bao tnh trang. Tat ca 128 bytes co the c ghi hoac oc trc tiep tr nhng trng hp
sau:
Thanh ghi C and D la hai thanh ghi ch oc.
Bit th 7 cua thanh ghi A la bit ch oc.
Bit cao cua byte th 2 la bit ch oc.
Cac thong so ve thi gian va lch co c bang cach oc cac bytes bo tng ng. Thi gian,
lch va bao gi c cai at hoac khi ta o bang cach ghi gia tr bytes RAM thch hp.
Noi dung cua 10 bytes cha thi gian, lch va bao gi eu co the nh dang 1 trong 2 dang nh
phan (Binary) hoac BCD (Binary-Coded Decimal).
Trc khi ghi cac d lieu vao cac o nh thi gian, lch, va cac thanh ghi bao gi ben trong th
bit SET thanh ghi B phai c thiet lap mc logic 1 e ngan chan viec cap nhat co the xay ra
trong qua trnh truy xuat.
Ngoai ra e ghi len 10 thanh ghi ch thi gian, lch, va thanh ghi bao gi mot nh dang a
la chon (BCD hay nh phan) th bit chon kieu d lieu (Data mode (DM)) cua thanh ghi B phai
c at mc logic thch hp.
Tat ca 10 bytes thi gian, lch va bao gi phai s dung cung kieu d lieu. Phai xoa bit SET
trong thanh ghi B sau khi thc hie n xong viec cap nhat thi gian cho ong ho, lch va hen gi.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .
Moi lan khi ong, ong ho thi gian thc cap nhat tat ca cac thay oi mot kieu a la
chon. D lieu se khong the thay oi neu khong khi o ng lai 10 bytes d lieu.
Bang 7-11 trnh bay nh dang nh phan va BCD cua ca thi gian , lch, va bao gi.

|o ch| Chuc nong C|o tr| thop phon Doy |u o chcn
Nh| phon 8CD
0 C|o y 0-5 00-38 00-5
C|o y hen 0-5 00-38 00-5
2 Phut 0-5 00-38 00-5
3 Phut hen 0-5 00-38 00-5
4 C|c: mcde 2 g|c -2 0-0C AM, 8-8C PM 0-2 AM, 8-2 PM
C|c: mcde 24 g|c 0-23 00-7 00-23
5 C|c hen: 2 g|c -2 0-0C AM, 8-8C PM 0-2 AM, 8-2 PM
C|c hen: 24 g|c 0-23 00-7 00-23
6 Ngoy cuo tuo n,
chu nhot =
-7 0-07 0-07
7 Ngoy cuo tho ng -3 0-F 0-3
8 1hong -2 0-0C 0-2
No m 0- 00-63 00-
8ong 7-. Coc d|nh dong cuo coc thcng sc thc| g|on.
Bit la chon kieu hien th 2412 khong the thay oi neu khong khi ong lai thanh ghi gi.
Khi nh dang 12 gi c la chon, bit cao cua bytes gi xac nh PM khi no c at mc logic
1. Moi giay mot lan, 11 bytes c cap nhat va kiem tra tnh trang bao gi.
Neu le nh oc d lieu thi gian va lch dien ra trong qua trnh cap nhat, mot van e phat sinh
la gi, phut, giay, co the kho ng chnh xac. Xac xuat oc khong chnh xac d lieu thi gian va
lch la rat thap. Co vai phng phap tranh sai so co the xay ra khi oc thi gian va lch c e
cap sau.
3 bytes bao gi co the s dung bang 2 cach:
Cach th nhat, khi thi gian bao gi c ghi vao cac thanh ghi gi, phut, giay, tac ong bao
gi c bat au tai thi gian chnh xac trong ngay khi bit cho phep bao chuong c at mc
cao.
Cach th hai s dung e at trang thai bat chap vao mot hoac nhieu bytes bao chuong. Ma
bat chap la bat ky ma so hex nao nam trong gia tr t C0 en FF. Hai bit MSB cua nhng byte tren
at vao trang thai bat chap khi mc logic 1. Bao gi se c sinh ra moi gi khi bit bat chap
c at vao bytes gi. Tng t, bao gi se sinh ra moi phut neu ma bat chap co cac byte gi
va cac byte phut. Neu ma bat chap co trong ca 3 bytes bao gi th no se tao ra tn hieu ngat moi
giay.
5. Hcot dcng cu o coc thonh gh| d|eu kh|en:
DS12C887 co 4 thanh ghi ieu khien c s dung vao moi luc ke ca trong qua trnh cap
nhat.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 323
|~|| | /
M58 L58
8|1 7 8|1 6 8|1 5 8|1 4 8|1 3 8|1 2 8|1 8|1 0
U|P DV2 DV DV0 k53 k52 k5 k50

UIP - Update In Progress (UIP) la bit c trang thai co the theo doi c.
Khi bit UIP mc 1 th cho phep qua trnh cap nhat xay ra ngay sau o.
Khi bit UIP mc 0, qua trnh cap nhat se khong xay ra sau khoang thi gian t nhat la 244
s. Nhng thong tin ve thi gian, lch, va bao gi trong RAM co hieu lc cho viec truy cap khi bit
UIP mc 0.
Bit UIP la bit ch oc va khong b anh hng cua chan RESET . Khi ghi bit SET thanh ghi B
len 1 se ngan chan moi qua trnh cap nhat va xoa cua bit tra ng thai UIP.
DV2, DV1, DV0 3 bit nay c s dung e bat hoac tat bo dao ong va cai at lai qua trnh
em xuong. Khi [DV2DV1 DV0] = [010] co chc nang duy nhat la bat bo dao ong len va cho
phep RTC em thi gian thi gian. Khi [DV2DV1 DV0] = [11] se cho phep dao ong nhng gi
qua trnh em xuong mc reset. Qua trnh cap nhat tiep theo se sm dien ra sau 500ms sau khi
kieu 010 c ghi vao DV0, DV1 va DV2.
RS3, RS2, RS1, RS0 - 4 bit la chon toc o dung e chon mot trong 13 loai cua bo chia 15
trang thai hoac khong cho phep xuat tn hieu chia ra ngoai. Loai c la chon co the dung e
phat ra song vuong (chan SQW) va/hoac ngat theo chu ky. Ngi s dung co the s dung 1 trong
nhng cach sau :
Cho phe p ngat vi bit PIE
Cho phe p xuat ngo ra chan SQW vi bit SQWE
Cho phe p ca hai hoat ong cung mot luc va cung mot loai.
Khong kch hoat ca 2
Bang 7-10 liet ke chu ky ngat va tan so song vuo ng ma co the chon la vi bit RS. Ca 4 bit
oc/ ghi eu khong b anh hng bi chan RESET.
|~|| |

M58 L58
8|1 7 8|1 6 8|1 5 8|1 4 8|1 3 8|1 2 8|1 8|1 0
5E1 P|E A|E U|E 5QWE DM 24/2 D5E

SET Khi bit SET mc 0, thong thng qua trnh cap nhat bang cach tang gia tr em 1 lan
1 giay. Khi bit SET c ghi vao mc 1, moi qua trnh cap nhat eu b cam. Cac chu ky oc co the
thc thi cung mot kieu. Bit SET la bit oc/ghi va khong chu anh hng cua reset hoac cac chc
nang ben trong cua DS12C887.
PIE Periodic Interrupt Enable: Bit cho phep ngat theo chu ky la bit oc/ghi, bit nay cho
phep bit c ngat theo chu ky (PF) trong thanh ghi C e ie u khien chan IRQ xuong mc thap.
Khi bit PIE c at len mc 1, chu ky ngat c tao ra bang cach ieu khien chan IRQ
xuong mc thap tuy thuoc va o t le phan bo cua bit RS3-RS0 thanh ghi A.
Khi bit PIE = 0 se ngan chan tn hieu ngo ra IRQ khoi viec ieu khien bang ngat theo chu ky
nhng bit c chu ky PF van thiet lap toc o cho chu ky.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
- . | .
Bit PIE khong chu anh hng cac hoat ong be n trong cua DS12C887 nhng b xoa ve 0 khi
real-time b reset.
AIE Alarm Interrupt Enable: Bit cho phe p ngat bao gi la bit cho phep oc/ghi, khi bit
AIE bang 1 se cho phep bit c bao gi (AF) thanh ghi C tao tn hieu ngat IRQ.
Tn hieu ngat bao gi xay ra tat ca cac giay khi ca 3 bytes bao gi cha ma bao gi dont
care c the hien nh phan nh sau 11XXXXXX.
Cac chc nang ben trong cua DS12C887 khong b anh h ng bi bit AIE.
UIE Update Ended Interrupt Enable: Bit cho phep ket thuc qua trnh ngat cap nhat la bit
oc/ghi, bit nay cho phep c ket thuc cap nhat UF thanh ghi C kch ngat IRQ. Khi real-time b
reset hoac chan SET mc 1 se xoa bit UIE.
SQWE Square Wave Enable: Khi bit SQWE c at len mc 1, mot tn hieu song vuo ng
co tan so c la chon bi cac bit RS3 en RS0 se xuat ra chan SQW. Khi bit SQWE c at
mc thap, chan SQW se c gi mc thap. SQWE la bit oc/ghi va c xoa khi real-time b
RESET.
DM Data Mode: Bit kie u d lieu DM quy nh khi nao th tho ng tin lch va thi gian nh
dang nh phan hoac BCD. Bit DM c at bi chng trnh e co nh dang thch hp va co the
oc khi c yeu cau. Bit nay khong b thay oi bi cac hoat ong ben trong cua real-time hoac
khi b reset. Mc 1 cua DM se nh dang d lieu nh phan con mc 0 se nh dang d lieu BCD.
24/12 Bit ieu khien 24/12 xac nh kieu cho cac byte gi . Khi mc 1 th no ch th che o
hien th 24 gi, con mc 0 th ch th che o hien th 12 gi. Bit nay la bit oc ghi va khong b
anh hng bi hoat ong ben trong cua real-time cung nh khi real-time b reset.
DSE Daylight Savings Enable: Bit cho phep nh cong khai DSE la bit oc\ghi.
Bit nay cho phep 2 cap nhat ac biet khi DSE c at len 1.
Vao Chu Nhat au tien cua thang 4, khi thi gian tang en gia tr 1:59:59 AM th se oi
thanh 3:00:00 AM.
Vao Chu Nhat cuoi cung cua thang 10, khi thi gian lan au tien at en 1:59:59 AM th no
se oi thanh 1:00:00 AM.
Chc nang ac biet nay se khong c thc thi neu bit DSE mc 0. Bit nay khong b anh
hng bi cac hoat ong ben trong cua real-time cung khi b reset.
|~|| |

M58 L58
8|1 7 8|1 6 8|1 5 8|1 4 8|1 3 8|1 2 8|1 8|1 0
|kQF PF AF UF 0 0 0 0

IRQF Interrupt Request Flag: Bit c yeu cau ngat c at len 1 khi thoa mot trong nhng
ieu di ay:
PF = PIE = 1
AF = AIE = 1
UF = UIE = 1
ieu o co ngha la IRQF = (PF . PIE) + (AF . AIE) + (UF . UIE)
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 325
Bat c luc nao bit IRQF c at len 1, chan IRQ c keo ve mc thap. Tat ca cac c b
xoa sau khi thanh ghi C c oc bi chng trnh hoac khi real-time b reset.
PF Periodic Interrupt Flag: Bit c ngat theo chu ky la bit ch oc, neu bit nay c at len
mc 1 khi co 1 canh xung c phat hien tn hieu la chon cua bo chia. Cac bit RS3 en RS0
thiet lap toc o cho chu ky. C PF c at len 1 kho ng phu thuoc vao trang thai cua bit PIE.
Khi ca PF va PIE eu mc 1, tn hieu IRQ se chuyen sang trang thai tch cc va se at bit
IRQF le n mc 1. Bit PF se b xoa bi reset real-time hoac bi lenh oc thanh ghi C.
AF Alarm Interrupt Flag: neu AF = 1 se xac nh thi gian hien tai trung vi thi gian hen
gi. Neu bit AIE cung mc 1, chan IRQ se xuong mc thap va bit IRQF se bang 1. Bit AF se b
xoa bi reset real-time hoac bi lenh oc thanh ghi C.
UF Update Ended Interrupt Flag: Bit c ngat ket thuc cap nhat c set sau moi chu ky cap
nhat. Khi bit UIE c set len 1, mc 1 UF se lam cho bit IRQF len mc 1 se kch IRQ bao
ngat. Bit UF se b xoa bi reset real-time hoac bi lenh oc thanh ghi C.
T bit 3 en bit 0- ay la nhng bit khong s du ng cua thanh ghi trang thai C. Nhng bit nay
luon luon mc 0 va khong the ghi.
|~|| |

M58 L58
8|1 7 8|1 6 8|1 5 8|1 4 8|1 3 8|1 2 8|1 8|1 0
Vk1 0 0 0 0 0 0 0

VRT Valid RAM and Time: Bit thi gian va RAM co hieu lc cho biet tnh trang cua pin
c ket noi chan VBAT. Bit nay khong cho phep ghi c va khi oc luon co gia tr bang 1. Neu
khi oc bit nay mc 0 th se cho biet nguon nang lng lithium ben trong a can va ca hai thong
so thi gian thc va d lieu RAM eu co the sai. Bit nay khong chu anh hng bi chan RESET.
BIT 6 EN BIT 0 Nhng bit c e cap tren cua thanh ghi D khong c s dung.
Chung khong ghi c va khi oc th luon co gia tr bang 0.
6. Moch g|oc t|ep v| d|eu kh|en vc| reo|-t|me:
Mach giao tiep vi ieu khien vi real-time DS12C887 nh hnh 7-42. Trong s o nay vi ie u
khien ch giao tiep vi 1 real-time vi muc ch trnh bay cach giao tiep ch trong thc te th vi
ieu khien phai giao tiep vi nhieu thanh phan khac th he thong mi hoat ong v du giao tiep
them led 7 oan hoac LCD mi hien th c cac thong so thi gian.
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .

H|nh 7-42. C|oc t|ep v| d|eu kh|en vc| keo|-t|me.
Trong mach ien hnh 7-42, vi ieu khie n giao tiep vi real-time theo kieu bo nh d lieu, vi
ieu khien co 4 le nh giao tiep vi bo nh d lieu m rong ben ngoai:
Lenh oc d lieu: movx a,@dptr (1)
Lenh xuat d lieu: movx @dptr,a (2)
Lenh oc d lieu: movx a,@Ri (3)
Lenh xuat d lieu: movx @Ri,a (4)
Hai lenh (1) va (2) thng c s dung khi dung lng bo nh m rong ln hn 256 byte,
port 2 dung e tai a ch byte cao. Hai lenh (3) va (4) c s dung khi dung l ng bo nh nho
hn hay bang 256 byte, port 2 khong dung co the ieu khien cac thiet b khac. Vay trong trng
hp nay ne n dung 2 lenh (3) va (4).
7. Chucng tr|nh khc| toc reo|-t|me:
Mach giao tiep vi ieu khien vi real-time DS12C887 nh hnh 7-42. Neu real-time cha s
dung lan nao th phai chay chng trnh khi tao e thiet lap ngay gi hien tai cho real-time va ch
chay 1 lan. Trong chng trnh ben di cac thong so giayhientai, phuthientai va giohientai
la cac thong so thi gian hien tai khi khi ong.
Chng trnh khi tao cho real-time:
org 0000h
mov r0,#0bh
mov a,#0
movx @r0,a ;cho phep chinh cai dat gio

mov a,#giayhientai
mov r0,#0
movx @r0,a ;nap giay hien tai
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 327

mov a,#phuthientai
mov r0,#02
movx @r0,a ;nap phut hien tai

mov a,#giohientai
mov r0,#04
movx @r0,a ;nap gio hien tai

mov r0,#0ah
mov a,#020h
movx @r0,a ;cho phep bo dao dong chay
lcall delay50
sjmp $
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

delay50: mov 7eh,#0fh
del12: mov 7dh,#0ffh
del11: djnz 7dh,del11
djnz 7eh,del12
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
end
Chng trnh v du la chng trnh ong ho so du ng vi ieu khie n, realtime va LCD: trong
chng trnh nay vi ieu khie n ket noi vi realtime s dung port2 va 3 bit ieu khien cua port1.
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh dieu khien LCD hien thi chuoi thong tin
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;dinh nghia cac bien

E BIT P1.0
rw BIT P1.1
rs BIT P1.2
byteout equ p2
dklcd equ 70h

bdn equ r6
giay equ r5
phut equ r4
gio equ r3

org 0000h
mov sp,#68h

; lcall khoitao_rt ;goi chtr con khoi tao real time
lcall khoitao_lcd ;goi chtr khoi tao LCD
lcall xoavnh_hthi ;xoa vung nho hien thi

main1: lcall doc_gpg ;doc thong so gio-phut-giay tu real time
lcall bcd_maascii ;chuyen so bcd sang ma ASCII
lcall hienthichung ;goi ra LCD de hien thi
sjmp main1 ;tiep tuc

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con doc gio phut giay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
doc_gpg: mov r0,#00
movx a,@r0
mov giay,a
mov r0,#02
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
` . | .
movx a,@r0
mov phut,a
mov r0,#04
movx a,@r0
mov gio,a
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con chuyen so bcd sang ma ASCII
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
bcd_maascii: mov a,giay
anl a,#0fh
add a,#30h
mov 4fh,a

mov a,giay
anl a,#0f0h
swap a
add a,#30h
mov 4eh,a

mov a,phut
anl a,#0fh
add a,#30h
mov 4ch,a

mov a,phut
anl a,#0f0h
swap a
add a,#30h
mov 4bh,a

mov a,gio
anl a,#0fh
add a,#30h
mov 49h,a

mov a,gio
anl a,#0f0h
swap a
add a,#30h
mov 48h,a
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Chuong tri nh con hien thi noi dung tren LCD cua2 vung nho
;40H->4Fh hang 1
; 50H-> 5Fh hang 2;
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hienthichung: mov A,#080h ;set DDRAM
lcall ktao

mov r1,#16
mov r0,#40h ;bat dau tu 0
fline: lcall Write
djnz r1,fline

mov a,#0c0h ;set DDRAM
lcall ktao
mov r1,#16
sline: lcall Write
djnz r1,sline
ret
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 329
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con goi data hien thi ra LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
write: mov a,@r0
write1: mov byteout,a
lcall data_byte
inc r0
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con khoi tao LCD
;chuong trinh dieu khien LCD 16X2 tren kit vi dieu khien LOAI NHO
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
khoitao_lcd: mov dklcd,#0
lcall khtaolcd ;khoi tao lcd
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chtr con khoi tao LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
khtaolcd: setb e ;Enable
clr rs ;RS low
clr rw ;RW low

mov a,#38h ;tu dieu khien LCD
lcall ktao
lcall ddelay41 ;delay 4.1 mSec

mov A,#38h ;function set
lcall ktao
lcall ddelay100 ;delay

mov A,#38h ;function
lcall ktao

mov A,#0ch ;tu dieu khien display on
lcall ktao
mov A,#01h ;tu dieu khien Clear display
lcall ktao

mov A,#06h ;tu dieu khien entry mode set
lcall ktao

mov A,#80h ;thiet lap dia chi LCD (set DD RAM)
lcall ktao
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con khoi tao LCD
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ktao: mov byteout,a
lcall command_byte
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;Feed command/data to the LCD module
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
command_byte:
clr rs ;RS low for a command byte
ljmp bdelay

data_byte: setb rs ;RS high for a data byte
bdelay: clr rw ;R/W low for a write mode
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| 7 3~ :-: || || . - | |-|
. | .
clr e
nop

setb e ;Enable pulse
nop
nop

mov byteout,#0ffh ;configure port1 to input mode

setb rw ;set RW to read
clr rs ;set RS to command
clr e ;generate enable pulse

nop
nop
setb e
lcall ddelay100
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay 4.1 ms
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ddelay41: mov 7eh,#90h
del412: mov 7fh,#200
djnz 7fh,$
djnz 7eh,del412
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con delay 255 microgiay
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ddelay100: mov 7fh,#00
djnz 7fh,$
ret

delay1giay: mov 7ah,#20
xx2: mov 7bh,#0
xx1: mov 7ch,#0
djnz 7ch,$
djnz 7bh,xx1
djnz 7ah,xx2
ret

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con khoi tao real time
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

khoitao_rt: mov r0,#0bh
mov a,#0
movx @r0,a ;cho phep chinh cai dat gio

mov r0,#0
movx @r0,a ;giay = 0

mov r0,#02
movx @r0,a ;phut = 0

mov r0,#04
movx @r0,a ;gio = 0

mov r0,#0ah
mov a,#020h
movx @r0,a ;cho phep bo dao dong chay
Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M
||| |-: - |- :|| . | .
. | . 331
lcall delay1giay
ret
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;chuong trinh con xoa vung nho hien thi
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xoavnh_hthi: mov r0,#40h
mov a,#' '
xoac: mov @r0,a
inc r0
cjne r0,#60h,xoac
ret

end
8o| top 4: Hay hieu chnh lai chng trnh e co ay u cac thong so th, ngay, thang, nam
hien th tren LCD.

V|||. 10 M 1A1:
Trong chng nay trnh bay giao tiep vi ie u khien vi cac thiet b ngoai vi c ban thng
c dung trong cac he thong ieu khie n thc te.
Mot he thong ie u khien tuy thuoc vao mc o phc tap ma mach giao tiep vi nhie u thiet
b khac nhau, chng nay trnh bay tng phan ri rac, tac gia hy vo ng rang sau khi hieu c
tng phan ngi hoc hay ngi oc co the thiet ke cac he thong giao tiep theo ye u cau.

Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M

1A| L|EU 1HAM KHA 0

[]. AV1Ak 5|NCH WAL1Ek A 1k|E8EL, 1he 8088 M|crcprccesscr
Prcgromm|ng, |nterfoc|ng, scftwore, hordwore, ond App||cot|cns ,
Prent|ce Ho|| |nternot|cno| Ed|t|cns.
[2]. D0UCLA5 V. HALL, M|crcprccesscr ond |nterfoc|ng Prcgromm|ng, ond
hordwore, McCrow H||| |nternot|cno| Ed|t|cns.
[3]. Jchn Uffenbeck, 1he 8088/8086 fom||y : Des|gn|ng, prcgromm|ng ond
|nterfoc|ng, Prent|ce Ho||, 87
[4]. Jomes L. Antcnokcs, 1he 68000 M|crcprccesscr: hordwore ond
scftwore pr|nc|p|es ond opp|| cot|cns, Prent|ce Ho|| f|fth ed|t|cn 2004.
[5]. Jock L. Dov|es, 1he |nncvot|ve 80x86 Vc|ume |: the 80286
M|crcprccesscr, orch|tecture, Prent|ce Ho||.
[6]. Jock L. Dov|es, Z80 Fom||y CPU user monuo|, www.z||cg.ccm.
[7]. MetoL|nk Ccrpcrot|cn Chond|er Ar|zcno, 805 Crcss Assemb|er Users
Monuo|, 6
[8]. MC55 M|crcccntrc||er Fom||y Users Monuo|, 4
[]. M68332 Users Monuo|
[0]. Hc 1rung My, V| Xu Ly, Nho xuot bo n o| Hcc Quc c C|o 1p HCM
[]. Ngc D|e n 1op, Ky 1huot AVk, Nho xuot bon KH & K1 2003.
[2]. Vo n 1he M|nh, Ky 1huot v| xu |y, Nho xuot bo n g|o c duc 7.



Truong DH SPKT TP. HCM http://www.hcmute.edu.vn
Thu vien DH SPKT TP. HCM - http://www.thuvienspkt.edu.vn
B
a
n

q
u
y
e
n


T
r
u
o
n
g

D
H

S
u

p
h
a
m

K
y

t
h
u
a
t

T
P
.
H
C
M