P. 1
Chuang 1 ; Gidi Thieu

Chuang 1 ; Gidi Thieu

|Views: 41|Likes:
Published by DIEP1212

More info:

Published by: DIEP1212 on Mar 16, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/27/2011

pdf

text

original

Chuang 1 ; Gidi thieu

1~

1

-r-

GIOI THIEU

.

1.1 Md DAU

(,I

I

Vao nam 1971 t~p dean Intel da: gioi thieu 8080, bi? vi xu ly ( microprocessor) thanh corig dau tien. Sau do khcng lau Motorola, RCA, ke den Ia MaS Technology va Zilog da gidi thieu cac bi? vi xu ly tuong hI ; 6800, 1801, 6502 va Z80. Ban than cac vi mach ( IC ; integrated circuit) nay tuy khong co nhieu hieu qua su dung nhung khi Ia mi?t phan cua mot may tinh dan board ( single-board computer ), chung trd tha nh thanh phan trung tam trong cac san pham co Ich dung M nghie n cuu va thie't ke'. Cac may t.inh dan board nay, trong do co D2 cua Motorola, KIM-1 cua MaS Technology va SDK-85 cua Intel la dang ghi nhri nhat, da nhanh chong xarn nhap vao cac phong thi nghiern thiet ke' cua triro ng' trung hoc, trunng da i hoc va cac cong ty dien tu.

Vao narn 1976 Intel gidi thieu bi? vi dieu khien ( microcontroller ) 8748, mot chip tucng tI! nhu cac bi? vi xu ly va la chip d:lu tien trong ho vi dieu khien MCS-48. 8748 la mot vi mach chua tre n 17000 transistor bao g6m mot CPU, 1 K byte EPROM, 64 byte RAM, 27 chan xua t nhap va mot bi? dinh thai 8-bit. IC nay va cac IC khac tiep theo cua ho MCS-

~.?*t m .nhanh chong trd' thanh chuan cong nghiep trong cac irng dung

, trong di~u khi~n ( control-oriented application ). Viec thay the' cac thanh phdn co' dien trong cac san pharn nhu cac may gi~t va cac bi? dieu khien den giao thong la mot ung dung ph6 bien ban d§u. Cac san phdm khac rna trong do bi? vi di~u khien duoc tim thay bao g6m xe 0 to, thiet hi cong nghie p, cac san pham tieu dung va cac ngoa i vi cua may tfnh ( ban phirn cua IBM-PC Ia mo t thi du su dung bi? vi dieu khien trong cac thie't ke' toi thi~u thanh phan ).

f)Q phirc tap, kich thirdc va kha nang cua cac bi? vi di~u kh ien diroc tang them mi?t bac quan trong vao narn 1980 khi Intel cfmg bo chip 8051,bQ vi di~u khien d§u tien cua ho vi di~u khien MCS-51. So vdi 8048, chip 8051 chua tren 60000 transistor bao g6m 4 K byte ROM, 128

r!j2

H<;> vi dieu khian 8051

byte RAM, 32 dinrng xuat nhap, 1 port n6i tiep va 2 bi? dinh thoi 16-bit - mot so' hrong mach dang chii y trong mot IC don. Cac thanh vieri moi diroc them vao cho ho MCS-51 va cac bian th~ ngay nay g~n nhl1 co ga'p d6i cac d$c trung nay. Tap doan Siemens, ngudn san xua't thli' hai cac bi? vi dieu khien thuoc ho MCS-51 cung cap chip SAB80515, mot ca i tieri cua 8051 chua trong mo t vo 68 chan, co 6 port xuat nhap 8-bit, 13 nguon tao ra ngilt va mot bi? bien d6i AID 8-bit vdi 8 kenh ng6 vao. H<;> 8051 Ia met trong nhirng bi? vi di~u khidn 8-bit manh va linh ho~tnha't da trd thanh bi? vi die u khi~n hang dilu trong nhirng narn gan day.

Quy~n sach nay khao sat ho vi di~u khien MCS-51. Cac chirong tiep theo gidi thieu cau true pha n cirng va phan mern cua ho MCS-51, dong thci chirng minh qua nhieu th i du thiat ke', each rna ho vi dieu khien nay c6 the tham gia vao cac thie't ke' dien ttl.' vci so' thanh phdn them vao t6i thieu.

Trong cac muc sau cua chuong nay, thong qua viec gidi thieu vat t<'it v~ cau true may tinh, ta se phat trien tit vung cua nhieu tit diroc cau tao tit chtr dau cua cac tit khac va cac tit dang dl1qc stl.' dung ph6 bieri nhung d~ nharn hln trong linh vue nay. Do nhieu thuat ngtr co dinh nghla me ho va trung la'p phu thuoc vao dinh kien cua cac tap doan len va cac y chot nay ra cua nhieu tac gia khac nhau, each gia i quyet cua chung ta mang tinh thuc tien hon la triru tircng. M6i met thuat ngir duoc gidi thieu trong khung canh chung nhat cling vdi su gia i thich ro rang.

1.2 THU~T NGU

Mi?t may tinh ( computer) duoc dinh nghia bdi hai diem chinh :

kha nang duoc l~p trinh de thao tac tre n du Iieu rna kh6ng can den su can thiep cua con ngmri.

kha nang luu tru va kh6i phuc dtr lieu

T6ng qua t hon, mot h~ may tinh ( computer system ) cung bao g6m cac thiet bi ngoai vi ( peripheral device) de truyen thong vci con nguui cung nhir cac chirong trrnh ( program) de xtl.' ly du Iieu. Thie't bi la pha n cirng ( hardware ) va chucng trinh la pha n mern ( software ). Chung ta hay bat dau vdi phdn cirng cua may tinh bang each khao sat hinh 1.1.

Hinh 1.1 la so do khdi don gian, khong chi tie't mot each co' y nharn muc dich tieu bieu cho ta't ca cac loai may Hnh. Nhu ta da tha'y, mo t M may tinh bao g6m mot don vi xtl.' ly trung tam CPU ( central processing unit ), don vi nay ke't n6i vdi bi? nho truy xuat ng<lu nhien RAM ( random access memory ) va bi? nho c.hi doc ROM ( read only memory ) thong qua bus dia chi ( address bus ), bus dir Iieu ( data bus) va bus dieu

Clnnrng 1 : Gidi thieu

3 ~

khien ( control bus ). Cac mach giao tie'p ( interface circuit) ke't nfii cac . bus cua M thong ( go i tat hi bus M thong ) vdi cac thiet bi ngoai vi. Ta SEl d~ cap chi tie't cac dan vi vita neu tren.

CPU

Hmh 1.1 : Sa db khoi cua mi?t h~ may vi tinh CPU: dan vi xi'ily trung tam

RAM: bi? nho truy xuilt ngtiu nhie n ( hay bi? uho doc / ghi ) ROM: bi? nho chi doc

Interface circuitry: mach giao tiep Peripheral devices: ca c thiet b] ngoa i vi Address bus: bus dia chi

Data bus: bus da lieu

Control bus: bus dieu khien

1.3 DdN vr xrr LV TRUNG TAM

CPU, tra i tim cua h~ may tfnh, quan ly ta't ca cac heat do ng cua h~ va thuc hien ta't ca cac thao tac tren dft lieu. H§u he't cac CPU chi bao g6m mot t~p cac mach logic tht!c hi~n lien t1,lc hai thao tac : tim nap l~nh va tht!c thi l~nh. CPU co kha nang hi~u va thuc thi cac lenh dira tren mot hlp cae rna nhi phan, mlii mo t rna nhi phan bi~u thi mo t thao tac d5n gian. Cac l~nh nay thinrng la cac lenh s6 hoc ( nhir cong, trir, nhan, chia ), cac l~nh logic ( nhu AND, OR, NOT, v.v ... ), cac l~nh di chuyen da Iieu hoac cac lenh re nhanh, dircc bi~u thi bdi mo t t~p cac rna nhj phan va duQ'c goi la t~p lerrh ( instruction set ).

Hinh 1.2 cho ta mot cai nhin ra't dan gia n ben trong cua CPU. Hinh nay trinh bay mo t tap cac thanh ghi ( register) co nhiern V1,l hru gift tarn tho-i cac thong tin, mo t dan vi s6 hoc logic ALU ( arithmetic-logic unit)

r

~4

H9 vi dieu khien 8051

co nhie m vu thuc hien cac thao tac tran cac thong tin nay, mot don vi gia i rna lenh va dieu khien ( instruction decode and control unit) co nhiern vu xac dinh thao tac can thuc hien va thiet lap cac hoa t di?ng can thiet M thuc hie n thao taco CPU con co hai thanh ghi 1lI1a : thanh ghi le nh IR ( instruction register) luu giu rna nhi phan clla lEmh de diroc thuc thi va bi? dem chuong trinh PC ( pro'iram counter) luu giu dia ~i cua l~nh ke tiep trong bi? nhc c§n dinrc thuc thi.

CI'U

lustrur-nou n~gH;rl~1' ([H)

Progr-am counter fPC)

Register-s

lnst.rucr ion uccodo and cont rol unit

Aritfnuet.ic and logic um t

Hinh 1.2 : Ban V! xi11y trung tam CPU CPU: dan V! xi11y trung tam

Instruction register ( IR ) : thanh ghi leuh ( IR )

Instruction decode and control unit: dan V! giiii ma Ie nh va di€u khi€n Program counter ( PC ) : hi? de'm chuang trlnh (PC)

Registers : ca c thanh ghi

Viec tim nap mot lenh tit RAM h~ thcng 13 mot trong cac thao tac cd ban nhat rna CPU thuc hien. Vi~c tim nap l~nh diroc thuc hien theo cac bunc sau :

ndi dung cua PC duoc dat len bus dia chi

tin hieu dieu khien READ dtt<;fc xac l~p ( chuyen sang- tra ng thai tich cue )

du lieu ( opcode cua lenh ) duoc d9C tir RAM va dira len bus dir Iieu

opcode dtt<;fc ch6t vao thanh ghi l~nh ben trong CPU PC diroc tang M chuan bi tim nap lenh ke' tlt bi? nho

Chuang 1 : Gidi thieu

5 (!l

-----

Hinh 1.3 minh hoa luong thong tin cho viec tim nap l~nh.

CPU
Address bus (
/ '\
I N I I
Program
counter Data bus (
'"
: I Opcode I 1 ~
Instruction
register
RAM
N+Z
Cont rol N + 1
bUH Opcode
Clock N
Read ~ N·l
~ Hinh 1.3 : Hoa t dQng cua bus cho chu ky tim na p l~nh CPU: dan vi xii ly trung tam

Program counter: bi? de'm chuang trinh Instruction register: thanh ghi l~nh Clock: chan xung clock

Read: chan di~u khign doc

Address bus : bus dia chi

Data bus: bus du Iieu

Control bus: bus di~u khien

Giai doan thuc thi l~nh bao g6m viec gia i rna opcode va tao ra cac tin hieu di~u khieri, cac tin hieu nay di~u khien vi~c xuat nhap giiia cac thanhghi noi vdi ALU va thong bao d~ ALU thuc hien thao tac da dU<;1c xac dinh. Do cac thao tac co t§m thay d6i rong, pharn vi da nh cho cac giai thich vira neu tren co phan nao bi gioi han, chi ap dung dU<;1c cho cac thao tac dan gian nhu tang noi dung cua mo t thanh ghi. Cac lenh phirc tap han doi hoi them nhieu birdc ntra, chang han nhu d9C byte du li~u thii hai va byte dir Iieu thii ba M thuc hien thao taco

Mi?t chu6i cac lenh duoc ke't hop M thuc hien met cong viec co y nghia dU<;1C goi la mot chuang trinh ( program) hay phan me m ( software ). Mire di? rna nhirng cong viec duoc thuc hien dung va co hieu qua phan Ion dU<;1C xac dinh bdi c4i.t.l!!<;1ng cua ph§n1Jl~m, khcng phai bo i su phirc tap cua CPu. V~y thi cac chuang trmh " dieu khian " CPU trong khi lam viec do i khi d:in den sai nhdrn, chinh Ia do nhtrng nhinrc di~m cua cac tac gia chuang trinh. Cac diu nhu la " may tinh tao ra mot 16i " la sai. Mac du thiet bi co su c6 la di~u khong the tranh dU<;1C, cac 16i dU<;1C tao ra thinrng la dau hieu cua cac chuang trinh t6i hoac 16i cua ngu'di dieu khi~n.

H<;> vi dieu khign 8051

1.4 BQ NHO BAN DAN: RAM vA ROM

Cac chucng trinh va du Iieu duoc hru giu trong bi? nho. Cac bien the cua bi? nho may tinh va cac thanh phan kern then rat da dang, va co ng nghe thinrng bi dot pha nen viec nghien ciru lien tuc va bao quat bi? nhri do i hoi pha i theo kip cac phat trien mo i nhat. Cac bi? nho duoc truy xuat true ticp bdi CPU bao g6m cac IC ban d§n go i Ia RAM va ROM. Co hai dac trung dung d{'; phan biet RAM va ROM: thir nha t RAM la bi? nho doc / ghi trong khi ROM la bi? nhc chi doc, tha hai RAM khong tie'p tuc liru giu no i dung khi bj mat ngucn cap dien trong khi ROM thi nguoc la i.

Hau he't cac he may tinh d~u co 6 dla va mot dung lucng ROM nho chi can du dg hru giu cac chuong trmh ngan, thinrng sit dung, nharn thuc hien cac thao tac xuat nhap, Cac chuong trinh va du Iieu cua nguni sit dung duoc hru tren dla va duoc na p van RAM M thuc thi. Vdi gia thanh lien tuc duoc giarn thap, cac M may ttnh nho thinrng chua bi? nho RAM tu hang tr ieu byte de'n hang tram trieu byte.

1.5 cAc BUS: DJA CHi, DU Ll¢U vA DIEU KHIEN

Mi?t bus la mot t~p cac day mang thong tin co cung mot muc dich.

Viec truy xuat tdi mot mach xung quanh CPU sit dung ba bus: bus dia chi, bus du lieu va bus dieu khien. Vdi mbi thao tac doc hoac ghi, CPU xac dinh ro vi tri cua du Iieu ( hoac lenh ) bang each dat mot dia chi len bus dia chi, sau do tich cue mot tin hieu tre n bus dieu kh ien M chi ra thao tac la doc hay ghi. Thao tac doc lay mot byte du Iieu tir bi? nhri fJ vi trf da xac dinh va dat byte nay len bus du lieu. CPU doc dtr Iieu va da t dir lieu van mot trong cac thanh ghi no i cua CPU. Vdi thao tac ghi, CPU xua't du lieu len bus dtr lieu. Nho' van tin hieu dieu khien, bi? nho nhan biet day la thao tac ghi va hru du Iieu van vi tri da ducc xac dinh.

Hau he't cac may tinh nho co tir 16 den 32 dinrng dia chi va co kha

-- .,

nang truy xuat 2" vi tri nho. Mi?t busdta chi 16-blt do v~y co thL!ruy

xuat mot bi? nho co 64 K vi tri nhd, mot bus dia chi 20-bit co kha nang truy xuat 1 M vi tri"'nhd va mo t bus dia chi 32-bit co kha nang truy xua t den 4 G vi tri nho ( 1 K = 1024, 1 M = 1024 K va 1 G = 1024 M ).

Bus du lieu mang thong tin giua CPU va bi? nho ciing nhu giua CPU va cac thiet bi xuat nhap. Viec c6 gang' nghien ciru bao quat phan Ion diroc dung vao viec xac dinh loa i cac hoat dong' lam mat nhieu thoi gian thuc thi dang gia cua may t.inh. Hien nhien la may tinh sit dung den 2/3 t.h<'ti gian vao cac viec di chuyen du Iie u. Vi da s6 cac thao tac di chuyen dif Iieu xay ra giira mo t thanh ghi cua CPU vdi ·ROM va RAM ngoa i, s6 duorig ( hay dQ rong ) cua bus dif Iieu rat quan trong d6i vdi hieu suat, tong' the cua may tinh. Gidi ha n b6'i di? rong nay co dang c6 chai : co the

Chucng 1 : GiO'i thieu

7~

co mot lucng n1t len bi? nhd trong he thdng' va CPU co th~' co kha nang tfnh toan nit 1O'n nhung viec truy xuat dv lieu - di chuyen dv lieu gifra bi? nho va CPU thong qua bus dv lieu - thinrng bi nghen nhu c6 chai do de;> re;>ng cua bus dv lieu.

Di~u nay ra't quan trong nen ngudi ta thucng them mot. tieu to ( trong tieng Anh ) d~ chi ra su md rcng d~ tranh hie n tucng c6 chai. Cau may tinh 16-bit chi ra ding may tinh co 16 dUdng dv lieu. Cac may Huh duoc pha n loa i 4-bit, 8-bit, 16-bit, 32-bit co kha nang tmh toan t6'ng the tang khi de;> re;>ng cua bus dv lieu tang.

Liru y la bus dv lieu, nhu ducc chi ra trong hinh 1.1, In ~us hai chieu con bus dia chi lil_ bus me;>t chieu. Cac thong tin dia chi luon luon duoc cung cap bdi CPU ( dircc chi bdi miii ten trong hinh 1.1 ) trong khi dv lieu di chuyen theo c3. hai hirdngtuy thue;>cVao thao tac du<,1C du dinh la doc hay ghi. Cung hru y In thua t ngir " dv lieu "du<1c sU' dung thea nghia t6ng quat: " thong tin" di chuyen tren bus dv lieu eo th~ la lerih cua chuong trinh, dia chi theo "au lenh hoac dtr lieu ducc ,sU' dung bdi chiro'ng trinh.

Bus dieu khieri la me;>t h6n hcp cae tin hieu, mo imfrt tin hieu co mot vai tro rie ng trong viec dieu khien co trat tl/ heat di?ng cua h~ thong. Theo l~ thircng, cac tin hieu dieu kh ieri la cac tin hieu dinh thc i duo'c eung cap bdi CPU M d6ng bi? viec di chuyen thong tin tre n cac bus dia chi va dv lieu. Mac du thong thinrng co ba tin hieu nhu la CLOCK, READ va WRITE d6i vdi viec di chuyen dv lieu cd ban giva CPU va be:> nhd, ten va hoat dong cua cac tin hieu dieu khien phu thuoc nhieu VaG CPU cu the. Ta can nghien cuu chi tiet cac tham khao ky thuat cua eric nha san xuat.

1.6 cAc THlET BJ XUAT NH~P

Cac thie't bi xuat nhap hay cac thiet bi ngoai vi cua may tfnh cho ta dUdngtruy~n thong giira he may tinh vdi the' gici ben ngoai. Kho ng eo cac thiet bi ngoa i vi, cac he may tinh chi la nhirng chiec may bi thu hE?P va it diroc sU' dung. T6ng quat co ba loai thie't bi xuat nhap la cac thief bi hru tru 1O'n, cac thief bi giao tiep vO'i con nguo i va cac thiet bi dieu khien ! kiem tra.

1.6.1 Cac tb ie't hi hru trii' Ion

Cung nhu cac bi? nhd ban dan RAM va ROM, cac thiet bi hru tru ldn luon luon tang trucng va phat tri~n. Nhu te n go i, cac thiet bi luu tru IO'n luu trv cac hrcng 1O'n thong tin ( chucng trmh hoac dv lieu ) rna cac thong tin nay khorig the chua du trong RAM tirong doi nho ( con goi Iii bi? nhd chinh ) cua may tinh. Thong tin nay pha i ducc nap vao trong bo

HI? vi di~u khien 8051

nhd chinh trudc khi CPU truy xUllt chiing. Neu ta phan loai theo su truy xuat, cac thiet hi hill tru kin hoac thuoc loai online hoac thuoc loai archival. BQ hru tru loai online thinrng la dla tU thich hop vdi CPU khong co sir can thiep cua con ngtnri khi yeu du mo t chuong trinh, hQ hru tru archival thinrng la dia hoac hang tli mac du cac dia quang nhu la CD-ROM hoac cong nghe WORM hien dang du«?,c ua chuong va co the thay the cac hq hru tru archival do dq tin cay, kha nang luu tru va gia thanh thap.

1.6.2 Cac thie't hi giao tie'p v6'i eon ngu'oi

Viec lien ket con nguni va may du«?,c thuc hien qua nhieu thiet hi giao tiep vdi con nguci rna thong thinrng nhat la thiet hi dau cudi hien th] video VDT ( video display terminal ) va may in. May in la thiet hi xuat con cac VDT thuc ra la 2 thiet hi vi chung chua mot han phim M nhap va met den tia am eire CRT ( cathode ray tuhe ) M xuat, Mqt llnh vue ky thuat, ducc goi la " cac nhan to' con nguci ", da phat trien tli nhu cau thiet ke cac thiet hi ngoai vi cho con ngtnri vci rnuc dich la an toan, tien nghi va hieu qua cung vdi cac dac tinh cua con nguO'i d6i vo i nhirng may rna con nguui sa dung. Tli do ta thay co nhieu cong ty san xuat ra cac thiet hi ngoai vi hen la cac cong ty san xuat ra may tinh.

D6i vdi hau het cac he may tinh, thutrng' ta co t6i thieu 3 thiet hi : mot ban phim, mot CRT va mot may in. Cac thiet hi khac giao tiep vdi con nguO'i hao g6m : dn dieu khien tro chrri, but sang, con chuot, 6ng noi, loa v.v ....

l

1.6.3 Cac thie't bi di~u khie'n I kie'm tra

NhO' vao cac thiet hi di~u khien I kiern tra, cac may tinh co the thuc hien vo sO' cac tac vu hucng dieu khien cling nhu thuc hien chung khong ngoi nghi, khong met mo i va dieu nay vU«?'t xa kha nang cua con nguni. Nhieu li'ng dung trong dO'i song hoac trong cong nghiep sa dung cac thiet hi nay.

Cac thiet hi di~u khien la cac thiet hi xuat hoac cac hi? kich thich ( actuator ), cac thiet hi kiem tra la cac thiet hi nhap hoac cac cam bien bien d6i cac dai hrcng phi dien nhu nhiet, anh sang, ap suat, v.v ... thanh cac dai hrong dien nhu dien ap hay dong dien de may tinh doc. Mach giao tiep bien d6i dien ap hay dong dien nay thanh cac rna nhi pha n hoac nguoc la i va thong qua phan mem, mot quan h~ du«?,c thiet lap giua cac thiet hi nhap va cac thiet hi xuat.

Vi~c giao tiep bang phan cling va phan mem gitra cac thiet hi nay voi cac hi? vi dieu khien la mot trong cac chu de chinh cua quyen sach nay.

Chirong 1 : Gidi thieu

9 ~

1.7 CHUONG TRINH: LON vA NHO

Cac thao luan a trim t!%p trung vao phan cang ciia cac h~ may tinh, cac chuong trinh hoac phan mern dieu khien phan cung lam viec chi mdi d~ cap qua. T§.m quan trong tuong doi ciia phan ciing so vdi pQ;ln mern da dl19'c dich chuyen mot each ro r~t trong thap nien cudi cua the ky 20. Trong khi truce day gia san xUllt va bao trl phan cirng cua may tinh rat diit so vdi gia thanh cua phan mern, ngay nay vdi cac chip co di? tich hcp cao LSI ( large scale integrated) va ra't cao VLSI ( very large scale integrated) gia thanh cua phan cirng giarn di i-a't nhieu. Cac cong viec doi hoi nghien ciiu sau nhu viet, cung cap va thu th!%p tu Iieu, bao tri, cap nhat va phan phoi phan mern chiem phan kin gia thanh trong viec W dQng h6a qua trinh str dung cac may tlnh.

system

Operating

(command language, utilitios I

Input/output subroutines (access to hardware i

Hardware

Hinh 1.4 : Cac ca'p phdn m~m Applications software : phan m~m (fng dung

User interface: giao dien v6'i nguoi sli dung

Operating system : h~ di~u hanh

~ Command language, utilities: ngon ngu l~nh, cac ti~n ich Input/output subroutines: cac chuong trlnh con xuti't nhap Access to hardware: troy xua't de'n phrin crrng

Hardware: phdn cung

Ta hay khao sat cac loai phan mern khac nhau. Hinh 1.4 minh hoa ba cap phdn mern co vi tri a giira ngtnri sli' dung va phdn cung cua mi?t h~ may tinh : phan mern ang dung ( application software ), h~ dieu hanh ( operating system) va cac chuong trinh con xua't nhap ( input/output subroutine ).

d cap thap nhat cac chuong trinh con xuat nhap true tiep quan ly phan cirng cua M th6ng, doc cac ky tlf tit ban phim, dua cac ky W len CRT, doc mi?t khdi thong tin tit dla va V.v ... Do cac chuong trinh con nay

~10

HC? vi dieu khien 8051

lien ket mat thiet vdi phan cirng, chung ducc viet bdi cac chuyen gia thiet ke phan cirng va thinrng diroc hru gift trong ROM ( cluing la h~ xuat nhap co ban BIOS [ basic input output system] tren may tfnh ca nha n PC [ personal computer] cua IBM chang han ) .

..

Be giup cho ngmri lap trinh de dang truy cap den phan cirng cua he thong, cac dieu kie n nhap va thoat duoc xac dinh met each ro rang doi vdi cac chuang trmh con xuat nhap. Ngu<'1i lap trinh chi dn khci'i di?ng cac gia tri cho cac thanh ghi cua CPU va go i chuang trinh con: thao tac can thiet dircc thuc thi va ket qua se tra v~ trong cac thanh ghi cua CPU hoac hru la i trong RAM he thong.

Be b6' sung day du cho cac chuang trinh con xuat nhap, ROM chua mot chuang trmh kho i di?ng ( start-up program ), chuang trmh nay diroc thuc thi khi h~ thong diroc cap dien hoac diroc thiet l~p lai ( reset) bang tay. Ban chat khong bi mat ni?i dung cua ROM duqc sU' dung & day vi chuang trmh khdi di?ng pha i hien hiru trong thc i gian khdi do ng h~ thong. Chuang trinh nay kiem tra cac tuy chon, khc i di?ng bi? nhc, thuc hien viec kiem tra chan doan hu hong V.v ... Cudi cung nhung khong kern quan trong, mot trinh nap bootstrap ( bootstrap loader) doc track dau tien ( mot chuong trinh nho ) tir dia vao RAM va chuyen dieu khien t.ai chuang trinh nho nay de nap phan thmrng tru trong RAM cua h~ dieu hanh ( mo t chirong trinh 16'n ) tlt dia va chuyen dieu khien tdi chuang trinh krn nay, hoan tat viec khdi di?ng he thong.

H~ dieu hanh la mo t tap hop Ion cac chuang trinh dircc nap vao trong mdt h~ may tinh nharn cung cap cac co che truy xuat, quan If va sti' dung mot each co hieu qua cac tai nguyen ( resource) cua may tinh. Cac khd nang nay diroc the hien thong qua ngo n ngir l~nh dieu khien ( command language) va cac chuang trinh ti~n ich ( utility program) cua h~ dieu hanh, va den luqt chiing tao dieu kien thuan lei cho viec phat trien cac phan mern ung dung. Neu mot phan mern ti'ng dung diroc thiet ke' tot, nguni su' dung tac dong tutrng h6 vdi may tinh rna khcng can co kien thirc nhieu ve h~ dieu ha nh. Cung cap mot giao dien vrii ngire i sti' dung mot each an toan, hieu qua la mo t trong cac muc tieu co ban cua viec thiet ke cac phan mern ti'ng dung.

1.8 MICRO, MINI vA MAINFRAME

Chung ta phan loa i may tinh dua theo d(l Ian va kha nang tinh toan : may vi tinh ( microcomputer ), may tinh mini ( minicomputer) va may tinh mainframe ( mainframe computer ). Bi(tc diem chinh cua may vi tinh 13. kich thutic va kha nang dong go i cua CPU. May vi tinh chua ben trong met vi mach tich hop dan go i la bi? vi xU' If ( microprocessor ). Cac may tinh mini va mainframe thi phirc tap hon trong tang chi tie't cau

- '

Chuong 1 : Gidi thieu

11(!l

true, chung co cac CPU chu~ nhieu vi mach tich hop, tit vai IC doi vdi may tinh mini de'n va i board chua cac IC doi vdi may tinh mainframe. Day la dieu can thie't M co duoc toc di? cao va khd nang tinh toan manh.

Cac may vi tinh di~n hinh cua IBM-PC, Apple Macintosh va Commodore Amiga sU' dung met bi? vi xU' If lam cpu. RAM, ROM va cac mach giao tie'p yeu cau nhieu vi mach vrii so thanh phan thuong tang theo kha nang tinh toano Cac mach giao tie'p co dq phiic tap thay d6i tuy thuoc vao cac thie't bi xua't nhap, Chang han M di~u khien met loa chua trong hau he't cac may vi tinh ta chi can mot cap c6ng logic, tuy nhieri mach giao tie'p va dieu khi~n 6 dia can nhieu vi mach d dang LSI.

Co met dac trung khac phan biet may vi tinh, may tinh mini va may tinh mainframe. Cac may vi tinh hl cac h~ thong chi danh cho mot nginri sU' dung va thuoc loa i don tac vu, cluing chi tac do ng tuong h6 vdi mot nguui sU' dung va cluing chi thuc thi met chuong trinh d met th<'1i diern. Cac may tinh mini va mainframe la cac he thong danh cho nhieu ngu'<'1isU' dung va thuoc loa i da tac vu, chung co th~ tac dong tuong h6 vrii nhieu nguo i sU' dung va thuc thi d6ng th<'1i nhieu chucng trinh. Thuc te', viec thuc thi d6ng tho i nhieu chuong trinh la ao giac ducc tao ra tit viec chia xe thrri gian sU' dung cac ta i nguyen cua CPU. Tuy nhie n, vdi cac h~ <1! xU' If co nhieu CPU, cac tac vu diroc thuc thi d6ng th<'1i.

1.9 TU Be) VI xU" LY DEN Be) VI DI~U KHIEN

Nhu da de cap d cac phan tren, cac bi? vi xU' If la cac CPU don chip dugc sU' dung trong cac may vi tinh. Vl%y thi da u la su kha c nhau giira mot bi? vi dieu khi~n va mo t bi? vi xU' If. Cau hoi nay co thg duoc trri lc i ttl 3 pho i ca nh : cau true phan cirng ( hardware architecture ), cac ung dung va cac dac trung cua tl%p I~nh ( instruction set feature ).

1.9.1 Ca'u true pha n eung

. De chi ro su khac nhau gitra cac bi? vi dieu khien va cac bi? vi xU' If, hinh 1.2 duoc ve lai chi tie't hen d hinh 1.5.

Trong khi bq vi xU' If Ia mot CPU don chip, mo t bi? vi dieu khien In. mot vi mach don chua ben trong mo t CPU va cac mach khac de tao ne n met he may vi tinh day du. Cac thanh phan d ben trong cua khung ve ba ng cac duong kh6ng lien tuc d hinh 1.5 la phan chu yeu cua hau he't. cac bi? vi dieu khien.

Ngoa i CPU, cac bi? vi dieu khign con chua ben trong chung cac RAM, ROM, mach giao tiep noi tiep, mach giao tie'p song song, bq dinh th<'1i va cac mach dieu khien ngat, ta't ca hien dien ben trong mot vi mach. Di

I!J 12

H<;> vi dieu khien 8051

nhien dung hnrng cua RAM trong chip khOng th~ dat ~iing voi dung lucng RAM (1 cac may vi tinh nhung nhu ta se khao sat sau, dieu nay kh6ng pha i la mot han che' vi cac hI) vi dieu khien dircc thiet ke' vdi du dinh danh cho nhirng ling dung hoan toan khac.

CPU

Internal clocks

External

ROM

RAM

Hmh 1.5 : Sc dB khlli chi tie't cua m(lt h~ may vi tinh

CPU: den vi xii ly trung tam

Timers: cac b(l diuh thO'i

Interrupt control : dieu khi~n ngdt Serial interface : giao tie' p nlli tie' p Parallel interface: giao tie'p song song

Address, data and control buses: ca c bus dia chi, di1li~u vii dieu khien RAM: b(l nhd doc / ghi

ROM: b(l nhri chi doc

External clocks: cac xung clock ben ngoa i External interrupts: cac ngdt ngoa i Serial device: thie't bi nlli tie'p

Parallel device : thie't bi song song

Ml)t dac trung quan trong cua hI) vi dieu khien la h~ thong ngll t du<?,c thiet ke ben trong chip. Cling nhu cac thiet hi hudng di~~ khi~n, cac bi? ~idi~u khi~n dap ling vdi cac tac dong hen ngoai ( cac ngat ) theo tho-i gian thuc. Chung phai thuc hien viec chuyen d6'i ngfJ canh nIt nha~

~

Chuang 1 : Gici thieu

13 ~

treo mot qua t.rinh trong khi dang thuc t.hi met qua t.rinh khac t.heo yeu cau cua mot su kie n. DI nhien hau het. cac hi? vi xU' If d~u co kha nang hien thuc cac so d6 ngiit ( int.errupt. scheme) nhung pha i sU' dung eric thanh phan ben ngoai t.rong khi do mach ben t.rong cua mo t chip vi dieu khien hao g6m cac mach qua n If ngdt can thiet.

1.9.2 Cac ung du ng

Cac hi? vi xU' If hau het. duoc dung lam cac CPU trong cac he may vi tmh t.rong khi cac hi? vi dieu khien diroc tim t.hay trong cac th iet ke nhc, vdi so thanh phan thern vao t.oi thieu nharn thuc hien cac hoat di?ng hucng di~u khien. Trong qua khu cac thiet ke nhu v~y yeu cau hang cli1,lc hoac t.h~m 'chi hang tram vi mach so. Bi? vi dieu khien giiip t.a gia m thieu so hrcng t.i5ng t.h~ cac thanh phan. Tat. ca chi can mo t hi? vi dieu khien, mot so it. cac tha nh phan h6 tro va mot chuang t.rinh dieu khie n chua t.rong ROM. Cac hi? vi di~u khi~n t.hich hop vo i cac ting dung di~u khi~n t.hiet. hi xua't. nh~p trong cac thiet ke yeu cau so tha nh phan t.6i thieu, trong khi do cac hi? vi xU' If thich hop vdi cac iing dung xU' If thong t.in t.rong cac h~ may t.inh.

1.9.3 Cac d*e bung ella t~p l~nh

Tli cac khac nhau v~ ting dung, cac hi? vi dieu khien co cac yeu cau khac doi vdi tap le nh cua chiing so vo i cac hi? vi xV If. Cac tap l~nh cua cac hi? vi xV If bao gom cac le nh xV If hao quat. nen chiing ~Iilnh v~ cac ki~u dinh dia chi vdi cac l~nh cung cdp cac h0lil-t di?ng t.ren cac Iugng da ~. Cac lenh cuachung co th~ heat di?ng tre n cac ~ hyt.e, hyt.e, t.tt_ tV kep. Cac ki~u djnh dia chi cung cap kha nang t.ruy xua't cac day da Iieu len hiing each sv dung cac con trl> di; chi xa cac offset.~ Cac kigu t.fugVa giam tv dong lam don gian hca cac birdc thuc thi tre n cac day du Iieu i!J cac gidi h~n hyte, tV va tV kep. Cac I~nh dac quye n kho ng th~' thuc thi trong cac chuang trinh cua ngul11 sV dung va viec Iie t ke con tiep tuc nira neu ta muon.

Cac hi? vi di~u khien co cac tap lenh cung cap cac diflu khi~n xua't ~~ch giao tiep cho nhieu ngo nhap va ngo xuat chi sU' dung mot . bit. Thi du met d(lng co co th~ durrc dieu khien chay hoac dung hMng each cung cap tin hieu dieu khieri tli mo t port I-hit. Cac hi? vi di~u khign co cac l~nh set va xoa cac hit don va thuc thi cac thao tac huring hit ( hit oriented operation ) nhu Ia AND, OR, XOR, nhay neu hit ducc set hoac ducc xoa, v.v ...

Dac trung manh nay hiern khi tha'y trong cac hQ vi xV If thinrng dugc thiet ke M hoa t dQng tren cac hyte hoac cac don vi da li~u len han.

~14

H<;> vi di~u khi~n 8051

Trong cac thiet bi dieu khien va kiern tra, cac bi? vi dieu khi~n co

~ trong va cac l~nh danh cho cac thao tac xUllt nhap, dinh th<'1i sl,l kien, cho phep va thiet l~p ClrCmuc ~tii'm chocic ng!'it du~o ia bdi cac kfch thich ben ngoai. Cac bi? vi xtily thuong yeu cau them ~c mach p 1,1 iao iep n6i tiep, cac IC dieu khien ngat, cac bi? dinh th . ~) de thuc hie n cling cac thao taco Tuy nhien, khd nang xU' ly tuyet 6i cua bi? vi dieu khien khong bao gi<'1 tiep can duoo vdi khd nang xU'ly' a bi? vi xtily do mot lucng kin IC can dircc cung cap cho cac chuc nang tr n chip - dieu nay tra gia cho kha nang xtilY.

Do 'et cau phan cung ben trong cac bi? vi dieu khien, cac lenh pha i that co dQng va hau het ducc thuc thi tre n tung byte. Mi?t t.ieu chuan thiet ke la chuang trinh dieu khidn can pha i da t vita trong ROM nQi vi viec them ROM ben ngoai se lam tang gia thanh cua san pharn thiet ke tren bi? vi dieu khien. Mi?t So' do rna ho a chat che luon luon can thiet cho t~p l~nh. Dac trung nay hiem thay <1 cac be? vi xu Iy: cac kieu dinh dia chi manh cua cluing lam cho viec rna hoa cac l~nh it chat che han.

1.10 KHAI NI~M MOl

Cac bi? vi dieu khien khong duqc dung trong cac may tinh nhirng la i duoc sti dung trong cac san pham tieu dung va cac san pharn corig nghie p. Nhung ngu<'1i sil dung cac san pham nay thinrng khong nhan biet sl,l hien dien cua cac bQ vi dieu khierr; vdi ho, cac thanh phan ben trong la nhirng chi tiet khong quan trong trong thiet ke.

Khong gi6ng nhu cac h~ may tinh duqc xac dinh bdi kha nang duqc l~p trinh va duoc ta i lap trmh cua chung, cac bi? vi dieu khien diroc lap trinh thinrng true cho mqt cong viec. Sl,l so sanh nay dan den su khac biet hoan toan ve cau true gitra cac h~ may tmh va cac bq vi dieu khien. Cac M may tfnh co tI l~ RAM-ROM ra't cao sao cho cac chuang trmh cua nguni sil dung duoc thuc thi trong mot khorig gian RAM tutrng d6i ldn va cac chuang trinh giao tiep vdi phan ciing duqc thuc thi trong mot khcng gian ROM nho. Cac bi? vi dieu khien nguoc la i co ti so' ROM-RAM cao. GPuo'ng trinh di~u khien tUo'ng ilili ldn d~ trong ROM trong khi ~9'C 51'( d!.lUg nhu me?t bi? nh<i tam th<'1i. RP chuang trinh dieu khien duqc luu thucng true trong ROM, chuang trinh nay con duoc go i la fJrmware. Dua vao rmrc de? Mn virng, chuang trinh di~u khien chua trong ROM nam <1 khoang giua phan mern - cac chuang trinh trong RAM bi mat khi ta kh6ng cung cap dien ntra - va phdn cung - cac mach v~t ly. Sl,l khac nhau ve phan mern va phan cirng hoi gi6ng vdi sl,l khac nhau giua mot trang giay ( phdn cirng ) vdi cac chir duqc viet len trang giay nay ( phan mern ). Ta co th~ xem firmware nhu Ia mot birc thu co dang chuan, dircc thiet ke va duqc in ra cho mot muc dich duy nhat.

'r

Chuong 1 : Gidi thieu

15 !J

1.11 tru vA KHuYET DIEM: MQT THi Dl,T THIET KE

Cac cong viec du'<;1c thuc hien bdi cac bi? vi di~u khien thl khong maio Di~u mdi la cac thiet ke du'<;1c hien thuc vdi it thanhphan han so vdi cac thiet ke tru'ac do. Cac thiet ke truce do yeu cau va i chuc hoac tha m chi vai tram IC d~ hien thuc nay chi co met it tha nh phan trong d6 bao g6m bi? vi dieu khien. Sci thanh phan duoc giarn bat, hau qua true tiep cua tinh kha lap trinh cua cac bi? vi dieu khien va di? tich hop cao trong cong nghe che tao vi mach, thinrng chuyen thanh thai gian phat tri~n ngdn hon, gia thanh khi san xua't thap hon, cong' sua't tieu thu tha'p han va di? tin cay cao han. Cac hoat dong logic yeu cau vai IC thinrng ducc hien thuc ben trong bi? vi di~u khien cling vdi mQt chircng trinh dieu khien them vao.

Va'n d~ d day la tcic di? Cac giai phap dt!a trim bi? vi di~u khi~n khong bao gia nhanh b~ng giIi phap dt!a tren cac thanh phan rai rac. _, ~hi1ng tlnh huong doi hOI phai dap ang th~t nhanh dcii vdi cac su kien ( chism thi~u sci trong cac ling dung ) se du'<;1c quan If t6i khi dua vao cac bi? vi di~u khien.

Lay thi du ta khao sat hinh 1.6, thi du nay hien thuc mot c6ng NAND b~ng each sli dung chip vi di~u khien 8051. Khong phai hoan . toan hi~n nhien rna mQt bQ vi di~u khien co th~ dircc s11 dung cho mot hoat di?ng nhu vay, nhung lai co th~ duoc. Phan mern pha i thuc hien cac thao tac diroc chi ra trong luu d6 d hmh 1.7. Chuong trinh hop ngu cua 8051 cho hoat di?ng logic nay nhu sau :

LOOP: MOV C, Pl.4 ; doc bit PIA V8.0 cCi nh6'
ANL C, Pl.5 ; AND v6'i Pl.5
ANL C, Pl.6 ; AND v6'i P1.6
CPL C ; d6i thanh ket qua NAND
MOV Pl.7, C ; giYj ket qua V8.0 Pl. 7
SJMP LOOP ; li!-p lai Neu chuong trinh nay du'<;1c thuc thi tren bQ vi di~u khien 8051, chtic nang c6ng NAND 3-ngo vao du'<;1c thuc hien. Thai gian tri hoan truyen tinh tli khi co su chuyen trang thai d ngo vao cho den khi xua't hien logic dung d ngo ra kha lau, it nhat ciing ~i sanh du'<;1c ~ai cac mach TTL tuong duong. Tuy thuoc vao hie ngo vao du'<;1C chuyen trang thai va chuong trinh biet du'<;1c st! chuyen trang thai nay, thai gian tri hoa n co tM tli 3 den 17 nsec ( gia S11 8051 hoat dQng vdi tan sci xung clock chuan la 12 MHz ). Thai gian tri hoan truyen cua mach TTL tinrng duong' vao

It!] 16

H9 vi dieu khie'n 8051

khoring 10 nsec. Hign nhien Ia khOng co tranh ca i khi so sanh toc dq cua cac hQ vi dieu khien vdi cac hien thuc TTL co cung chirc nang.

Trong nhieu ling dung, dac biet la cac ling dung co lien quan den con ngu<'1i, cac khoang th<'1i gian tn~ tinh bang nsec, usee hoac tharn chi msec In khorig quan trong. Thi du v~ cong logic d tren minh hoa rang cac bo vi dieu khien co the' h ien thirc cac thao tac logic. Han nita, khi cac th iet ke' t,rd ne n plurc tap han, cac lo'i Ich cua thie't, ke' dira trim cac hf;l vi dieu khi81l diroc tha'y ro han. Viec gia m bdt cac tha nh phan la mo t dieu I<;fi nhu d:'i de cap, cac thao tac trong chuang trinh di~u khien lam cho t.hie't ke' co the thay doi bang' each thay doi phan mern. Ui~u nay co a nh huorig t.6i thieu deri chu ky san xuat.

8051
PH r-,

1'1.5 J 1'1.7
P1.G Hmh 1,6 : Hien thuc mQt dIng logic dung hi) vi di~u khi~n 8051

Enter : di~lll nhap

Read hit PIA: doc hit PIA

AND with hit P1.5 : AND v6'i hit P1.5 AND with hit P1.6 : AND voi hit P1.6 Complement result: lily bu ke't qua Send hit to P1,7 : g&i hit den P1.7

Hinh 1.7 : LU\J db cho chuang trinh hien thuc c6ng logic

Chuong 2 : Tom tilt phdn cirng

17 ~

2

TOM TAT PHAN CUNG

2.1 TONG QUAT

MCS-51 la ho vi dieu khien cua Intel. Cac nha san xuat IC khac nhu Siemens, Advanced Micro Devices, Fujitsu va Philips dttgc cap phep lam cac nha cung cap thu hai cho cac chip cua ho MCS-51.

Chuong nay gidi thieu ve cau true phan cirng cua ho MCS-51. Tham khao ky thuat cua Intel cho cac chip cila ho MCS-51 dinrc tim thay i1 phu luc E. Ta can bie't them nhieu chi tie't trong phu luc nay, thi du nhu cac d~c tinh dien chang han.

Nhieu dac trung phan cirng duoc minh hoa bang cac chu6i lenh ngan, cac mo ta van t3:t cling diroc cung cilp cho tirng thi du con cac chi tie't dily du cua tap l~nh duoc danh lai cho chuong 3. Ta co th~ tham khao phu luc A ( tom t3:t t~p lenh cua 8051 ) va phuIuc C ( cac dinh nghia cho tirng lenh cua 8051 ).

Vi mach t6'ng quat cua ho MCS-51 la chip 8051, linh kien dilu tien cua ho nay dttgc dua ra thj trinrng, Chip 8051 co cac dac trung ducc tom t3: t nhu sau :

4KB ROM

128 byte RAM.

4 port xuilt nhap ( I/O port) 8-bit. V'

2 bQ dinh thai 16-bit.

Mach giao tie'p n6i tie'p. ,/

Khong gian nhd chuong trinh ( rna ) ngoai 64K.

Khong gian nho dfl Iieu ngoai 64K. /

BQ xli ly bit ( thao tac tren cac bit r ieng re )

210 vi tri nhd dttgc dinh dia chi, m6i vi tri 1 bit. .,/

Nhan/chia trong 4~s.

,1.,1.';' • .1. U.I.-C;;U R.I..l.l.-C;;.l.l ....,.V....,..L

Cac thanh visn khac ella ho MCS-51 co cac to' hop ROM ( EPROM ), RAM tren chip khac nhau hoac co them bi? dinh thai thu ba ( xem bang 2.1 ). Moi mot IC ella ho MCS-51 cling co phien ban CMOS cong suit thap.

Chip

8051

8031

8751

8052

8032

8752

- T2EX"

Be? nhd ehlidng trinh Be? nhd dii Ii~u
tren chip iI'en chip
4KROM 128 byte
OK 128 byte
4 KEPROM 128 byte
8KROM 256 byte
OK 256 byte
8 KEPROM 256 byte Bang 2.1: So sanh cac chip cua ho MCS-51

r----iNTi· ,------ iNTt'J •

1- - - - Til1JN :l18u:'I2!8052)

128 bytos R,\~I

1-------

1 HOM

1

Cae be? djnh thiti

2 2 2 3 3 3

EA liST

Al.E

II

Hrnh 2.1 : Sa d6 khfii cua chip 8051

Chuong 2 : Tom tl'it phan cling

19 ~

Interrupt control: dii\u khien ngl1t Other registers: ca c thanh ghi kha c 128 bytes RAM: RAM 128 byte Timer 2, 1, 0 : bQ dinh tho'i 2, 1, 0 CPU: don vi dii\u khi~n trung tam Oscillator: mach dao dQng

Bus control: dii\u khi~n bus

I/O ports: cac port xua't!nh4 p

Serial port: port n6i tie'p Address/data : dia chi/du li~u

Thuat ngu " 8051 " dli<!c dung M chi rong' rai cac chip cua ho MCS-

51. Khi viec thao luan t~p trung vao mot cai tien tlt chip 8051 ca ban, chip cui tien duoc chi ra r6 rang. Cac dac trimg vua neu tren diroc trinh bay trong so' do khni d hinh 2.1.

40

I'SI",:\
.\1,1':
r:'\
I(ST
1m 1'::'7
\V!{ \'::1;
'1'1 1''''·
TO I~:! .j
INTI I J: I : ~
INT" J':i:1
rxn 1':1 I
I(XI) P;UI vee

."\()!il

vss

20

Hinh 2.2 : So'd5 chan cua 8051

.\ 11 ,\ 1:1 i\l~ All .\111 AU AH

~20

H<,> vi di~u khien 8051

2.2 cAc CHAN ( PINOUT)

Hinh 2.2 cho ta so d6 chan cua chip 8051. Mo ta tom t~t chirc nang cua tling chan nhu sau.

Nhu ta thay trong hinh 2.2, 32 trong so 40 chan cua 8051 co cong dung xuat/nha p, tuy nhien 24 trong 32 dUdng nay co 2 muc dich ( cong dung ) [ 26/32 d6i vci 8032/8052 ]. M6i mo t dUdng co th~ hoat dong' xuat/nhap hoac hoat dong nhu mdt dUdng di~u khien hoac hoat do ng nhu mot dUdng dia chi/dir Iieu cua bus dia chi/dir lieu da hop.

Cac dac trimg da d~ cap CJ tre n duqc trinh bay trong so d6 khoi CJ hinh 2.1.

32 chan neu tren hinh thanh 4 port 8-bit. VOi cac thiet ke yeu c§.u mot rruic t6i thieu bi? nhd ngoai hoac cac thanh phan ben ngoai khac, ta co the sli dung cac port nay lam nhie m VI;! xuat/nhap. 8 dudng cho m6i port co th~ duqc xli If nhu mot dan vi giao tiep vdi cac thiet bi song song nhu may in, bi? bien d6i D-A, v:v ... hoac m6i dUdng co the hoat dqng dQc lap giao tiep vdi mqt thiet bj dan bit nhu chuyen mach, LED, BJT, FET cuon day, di?ng co, loa, v.v ...

2.2.1 Port 0

Port 0 ( cac chan tli 32 den 39 tren 8051 ) co 2 cong dung. Trong cac thiet ke co t6i thieu thanh phan, port 0 duqc sli dung lam nhiern VI;! xuat/nhap. Trong cac thiet ke IOn han c6 bi? nho ngoai, port 0 trCJ thanh bus dia chi va bus du Iieu da hop [ byte thap cua bus dia chi neu la dia chi] ( xem 2.6 : Bi! nho ngoai ).

2.2.2 Port 1

f!..ort 1 chi co mi)t C@g dl;!ng la xua't/nhap ( cac chan tli 1 den 8 tren 8051 ). Cac chan ciia port 1 duqc kf hieu la P1.0, Pl.l, ... , P1.7 va duoc dung de giao tiep vdi thiet bi ben ngoai khi c6 yeu c§.u. Khong co chirc nang nao khac nira gan cho cac chan cua port 1, nghia la cluing chi duoc sli dung de giao tiep voi cac thiet bi ngoai vi.I Ngoai 1~ : vdi 8032/8052, ta co the sli dung PLO va Pl.l hoac lam cac dUdng xuat/rihap hoac lam cae ngo vao cho mach dinh thoi thir ba ].

2.2.3 Port 2

Port 2 ( cac chan ttl 21 den 28 tren 8051 ) co 2 cong dung, hoac lam nhiern VI;! xuat/nhap hoac la byte dia chi cao cua bus dia chi 16-bit cho cac thiet ke co bi? nho chuang trmh ngoai hoac cac thiet ke co nhieu han 256 byte bi! nhd du lieu ngoai.

Chuong 2 : Tom tAt phan cung

21 ~

2.2.4 Port 3

Port 3 ( cac chan tli 10 de'n 17 tren 8051 ) co 2 cong dung. Khi khong hoat dQng xuat/nha p, cac chan cua port 3 co nhieu chuc nang rierig ( m6i chan co clurc nang rieng lien quan den cac dac trirng cu th~ cua 8051 ).

Bang 2.2 dUdi day cho ta chirc nang cua cac chan cua port 3 va 2 chan Pl.O, Pl.l cua port 1.

Bit Ten Dia chi hit Chuc ming
P3.0 RxD BOH Chan nha n da li~u cua port niJi tie'p
P3.I TxD BIH Chan phat da Iieu cua port n6i tie'p
,
P3.2 INTO B2H Ngo vao ngilt ngoa i 0
P3} INTI B3H Ngo VIlO ngilt ngoa i 1
P3.4 TO B4H Ngd vao cua hi? dinh thci/dsm 0
P3.5 Tl B5H Ngo va o cua hi? dinh thO-ilMm 1
P3.6 WR B6H Dieu khien ghi hi? nho da lieu ugoai
P3.7 RD B7H Di~u khi~n doc hi? nho da lieu ngaa/,
Pl.O T2 90H Ngo vao cua hi? dinh thitilMm 2
Pl.! T2EX 9lH Nap lai/thu nha n cua hi? dinh thiti 2 Bang 2.2 : Chuc ni1ng cua cac chan cua port 3 va port 1

2.2.5 Chan cho phe p be') nha chuang trinh PSEN

8051 cung ca'p cho ta 4 tin hieu dieu khien bus. Tin hieu cho phep bq nho chuang trinh PSEN ( program store enable) la tin hieu xuat tren tren chan 29. Bay la tin hieu dieu khien cho phep ta truy xuat bQ nho chuang trinh ngoai, Chan nay thunng noi vdi chan cho phep xuat OF ( output enable) cua EPROM ( hoac ROM) M cho phep doc cac byte lenh.

Tin hieu PSEN fJ logic 0 trong suot th6'i gian tim-nap lenh. Cac rna nhi phan cua chuang trinh hay opcode ( rna thao tac ) duoc doc tlt EPROM, qua bus dil' lieu va dugc chot vao thanh ghi lenh IR cua 8051 d~ dinrc gia i rna.

Khi thuc thi mQt chuong trinh chu-a fJ ~OM nQi, PSEN dugc duy tri fJ logic kh6ng tich clfc ( logic 1 ).

~22

Ho vi dieu khien 8051

2.2.6 Chan cho phe p ch6t dja chi ALE

8051 sii dung chan 30, chan xuaf tin hieu cho phe p chot dia chi ALE

address latch enable ) M gia i da hop ( demultiplexing ) bus du lieu va bus dia chi. Khi port 0 dircc sii dung lam bus dia chi/du lieu da ho'p, chan ALE xudt tin hieu M ch6t dia chi ( byte thap cua dia chi 16-bit ) van mot thanh ghi ngoai trong suo t V2 dau cua chu ky bi? nhd ( memory cycle ). Sau khi die u nay da ducc tlnrc hie n, cac chan cua port 0 se xuat/nhap dli lieu hop he trong suo t V2 thtr hai cua chu ky bi? nhc.

Tin hieu ALE co tan so' bang 116 tan so' cua mach dao df?ng ben trong chip vi dieu khien va co th~ diroc dung lam xung clock cho phan con la i cua h~ thong. Neu mach dao dQng co tan so 12 MHz, tin h ieu ALE co tan so' 2 MHz. Ngoai M duy nhat la trong thai gian thuc thi lenh MOVX, mot xung ALE se bi bo qua ( xem hinh 2.10 ). Chan ALE con duoc dung d~ nhan xung ngo vao lap trinh cro EPROM tre n chip d6i vdi de phie n ban cua 8051 co EPROM nay. Vi

2.2.7 Chan truy xua't ngoa i EA

Ngo van nay ( chan 31 ) co the' diroc noi vrii 5 V ( logic 1 ) hoac vo i GND ( logic 0 ). Neu chan nay Hoi len 5 V, 805118052 ~uong trinh trong ROM nqi ( chuong trlnh nho hen 4K18K l. Neu chan nay n6i vdi GN~n PSEN cung d logic 0 ), chuong trinh can th!.Jc thi

chira d-riQ nha ngQai. Doi vdi 803118032 c an -,- ic VI c ung

- . -. .

khoug co bo nho chuong trinh tre n chip. Neu chan L;\ d logic 0 doi vdi

805118052. ROM no i ben t.rong chip dircc vo hieu hoa va clurong trinh cau tlurc th i chua d EPROM ben ngoai.

l ( Cac phie n ban EPROM cua 8051 con sil dung chan E;\ lam chan nha n die n rip cap dien 21V ( Vpp ) cho viec lap trinh EPROM no i ( nap EPROM ).

2.2.8 Chan RESET ( RST )

Ng» vao RST ( chan 9 ) la ngo vao xoa chmh ( master reset) cua 8051 dung d~ th ie t lap lai trang thai ban dau cho M thong hay go i t.dt. Ia reset he thong. Khi ngo van nay duoc treo d logic 1 t6i thieu hai chu ky may, cac thanh ghi ben trong cua 8051 diroc nap cac gia t.ri thich hop cho viec kho i df?ng lai M thong ( xem muc 2.8 ).

2.2.9 Cae chan XTALI va XTAL2

Nhu diroc ve tren hmh 2.2, mach duo df?ng ben trong chip 8051 duoc ghep vrri thaeh anh ben ngoa i d hai chan XTAL1 va XTAL2 ( chan 18 va

Chuong 2 : Tom tih phan cling

23 ~

19 ). Cac tu tin dinh cling diroc yeu cau nhu tren hinh nay. Tan so danh dinh cua thach anh la 12 MHz cho hau het eric chip cua ho MC'S-51 ( 80C31BH-l sii dung thach anh 16 MHz ben trong, mach dao dong t.rong chip kho ng can thach anh ben ngoa i ). o hinh 2.3, 1 ngucn xung clock TTL co th~ ducc noi vdi cac chan XTALI va XTAL2 .•

IX

HOG]

L-------i X 1'/\ Lz

Hinh 2.3 8051 ghe p v6'i mach dao dong TTL ben ngoa i TTL oscillator: mach dao do ng TTL

2.3 CAU TRUC CVA PORT XUATINH~P

Sa db mach ben trong cho eric chan cua port xuat/nha p duoc ve dan gian nhu o hinh 2.4. Viec ghi den 1 chan cua port se nap du Iieu va o bo chot cua port, ngo ra Q cua bi? chot dieu khien mot transistor trirong vii transistor nay no i vdi chan cua port. Kha nang fanout cua cac port 1. 2 va 3 la 4 ta i vi mach TTL loa i Schottky cong suat thap ( LS ) con cua port. 0 la 8 ta i loa i LS ( xem them chi tiet d ph", luc E ).

Luu y la die n trd keo len ( pull up ) se _!iliopg c6_d port 0 Urit khi port nay lam nhiern VI,I cua bus dia chi/du lieu da hop )~ do vay mot dien t.rd keo len ben ngoai pha i duoc can den.

Gin tri cua die n tro Bay ph", thuoc vao dac tinh ngo vao cua tha nh pha n ghep noi vdi chan cua port.

Read latch

POl'!.

Write latch

to latcll

L-- __ -'

., Open drain out.put lor Port 0 when operating as an I/O port

Hinh 2.4 : Mach ben trong cua cac port xuat nhap

1

~24

H<;> vi di~u khien 8051

8051 internal bus: bus nQi cua 8051 Read latch: doc bi? ch6t

Internal pull up : keo len ben trong Read pin; doc chan port

Port pin; chan port .

Port latch; bi) chtit cua port Write to latch; ghi vao bi? chfit

a day ta thay co ca 2 kha nang: "doc hi) chot" va "doc chan port".

Cac lerih yeu cau thao tac dcc--sua=ghi ( nhu l~nh CPL P1.5) doc hi) chdt M tra nh su hieu nharn mire dien ap do st! kien dong tai tang. Cac lenh nhap 1 hit cua port ( nhu MOV C, P1.5 ) doc chan port. Trong trudng hop nay hi) ch6t cua port phai chira 1 neu khong FET se dircc kich bao hoa va di~u nay keo ngo ra xu6ng rmrc thap. Vi~c reset hE\! thong se set tat ca cac hi) chot port, do v~y cac chan port co th~ du<!c dung lam cac ngo nhap rna khong can phai set cac hi? chdt port mot each tUdng minh. Tuy nhien neu mi?t hi) ch6t art hi xoa ( nhu CLR P1.5 ), chan port khong th~ Iamh lEi!m vu tie'p theo la ng6 nhap tnt khi trudc tien ta phai set hi) chdt ( nhu SETB P1.5). -.....

Hinh 2.4 khong trinh hay mach cho cac chirc nang khac ciia cac port 0, 2 va 3. Khi cac chirc nang khac du<!c sli dung, cac mach kich ng6 ra ducc chuyen den mot dia chi ni)i ( port 2 ), dia chi/dir li~u ( port 0 ) hoac tin hieu dieu khien ( port 3 ) tuong ling.

2.4 TO CHUC BQ NHd

Hau het cac hi) vi xli If ( CPU ) d~u co khong gian nho chung cho du lieu va chuang trinh. Di~u nay ciing hop If vi cac chuang trinh thuong duoc btu tren dla va ducc nap vao RAM M thuc thi; vay thi ca hai, du lieu va chuang trinh, d~u btu tru trong RAM.

Cac chip vi dieu khign hiem khi diroc sli dung gi6ng nhu cac CPU trong cac h~ may tinh, thay vao do cluing diroc dung lam thanh phan trung tam trong cac thi~t k~ hudng dieu khien, trong do hi) nho co dung lucng gici han, khorig co 6 dia va h~ dieu hanh. Chuang trmh di~u khien phai thUdD,g tru trons: ROM.

Do If do t.re n, 8051 co khong gian hi) nh6 rieng cho chuang trinh va

du li~u. Nbu ta dB. thay trong hang 2.1 a muc 2.1, ca 2 hi) nhd chuang

trinh va du Iieu d~u dl)t hen trong chip, tuy nhien ta co thg rna rong hi) nho chuang trinh va hi) nhd du lieu bang each sli dung cac chip nhd hen ngoai vdi dung lu<!ng t6i da la 64 K cho he? nhd chuang trinh ( hay he? nhd rna ) va 64 K cho hi) nhd du lieu.

---~

Chuang 2 : Tom tl'it phan cling

25 l¥l

Bi? nho no i trong chip hao gorn ROM ( chi co d 805118052 ) va RAM.

RAM treri chip bao g6m vung RAM da chirc na ng ( nhieu cong dung l. vung RAM vdi tirng hit diroc dinh dia chi ( go i td t la vung RAM dinh dia chi hit ), cac day ( hank) thanh ghi va cac t.hanh ghi chirc nang da c bie t SFR ( special function register ). Hai da c tinh dang luu y la :

(a) cac thanh ghi va cac ort xuat/nha duac dinh dia chi theo kieu ann: ~ hi? nha ( memory mapped) va du<?,c truy xutlt nlnr '""ii"i9i vi tri nha tron ho

(h) vung stack thinrng trti ~ tre n chip ( RAM no i ) t.hay VI d trong RAM ngoa i nhu d6i vo i cac hq vi xU' IY.

Hinh 2.5 tom tdt cac khcng gian nhd chochip 8031 khorig ('0 ROM nci, khOng trmh hay chi tiet ve hi? nho di1 lieu tren chip ( eric cai tie n cua 8032/8052 se dune tom tl'it. sau ).

Hmh 2.6 cho ta chi tiet cua hi? nho di1 Iieu tre n chip. Ta th:J."y ding khong gian nhc noi nay dune chia tha nh : cac day thanh ghi ( OOH "'" 1FH ), vung RAM dinh dia chi hit ( 20H -7 2FH ), vung RAM du muc dich ( 30H "'" 7FH ) va cac thanh ghi chirc nang dac biet ( SOH"," FFH ).

I
I FFFF FFFF
Code Data
nieruorv memory
enabled enabled
via PSEN via RD
and WR
"'0
00 DODO 0000

On-chip Ext.en",1
IllPIlIOI)' mCIIlOl)' Hiuh 2.5 : T6m Hit cac kh6ng gian nho cua chip 8031

On-chip memory: bi> nho tre n chip External memory: bi> nho ngoa i

Code memory: bi> nh6" chuong trlnh ( mil ) Enabled via PSEN : duoc eho phep b&i PSEN Data memory: bi> nhc du lieu

Enabled via RD and WR : dLlc;fe cho phep b&i RD va WR

~26

1

2.4.1 Vung RAM da muc dich

M~e du hinh 2.6 trinh bay vung RAM da muc dich co 80 byte dat i!J dia chi tlt 30H den 7FH, ben dirdi vung nay tlt dia chi OOH de'n 2FH la vung nhd co th~ diroc sil dung tuong tv ( mac du cac vi tri nhd nay co cac muc dich khac nhu se thao luan dudi day). Ba't ky vi tri nho nEW trong vung RAM da muc dich d~u co th~ dl1~c truy xuat tv do bling each sli dung cac kieu dinh dia chi true tie'p hoac gian tie'p. Thi du M doc n(li dung ta i dia chi 5FH ella RAM n(li vao thanh chua A, ta dung lenh sau :

MOV A,5FH

Lenh tren di chuyen 1 byte dtr lieu bang each dung ki~u dinh dia chi true tie'p de xac dinh vi trf nguon ( nghta 13. dia chi 5FH ). Bich cua da lieu ducc xac dinh ro rang trong opcode cua lenh la thanh chua A ( cac ki~u dinh dia chi se dl1~c d~ cap trong chuong 3 ).

Byte
address
7~
80 byte
30
21'
28
20
2C
2B
2A
29
28
27
26
25
24
23
22
21
20
1~
18
17
10
32 byte OF
08
07
00 Bit address

General
purpose
RAM
7~ 7E 707D 7B 7A 79 78
77 76 75 74 73 72 71 70
6F 6E 606C 6B 6A 69 68
67 66 65 64 63 62 61 60
5F 5E 505C 5ll 5i\ 59 58
57 56 5554 ti3 52 51 50
4F 4E 404C 4B 4A 49 48
47 46 45 14 43 42 41 40
3F 3E 303C 3B 3A 39 38
37 36 3534 33 32 31 30
2f' 2E Z02C 2B 2A 29 28
27 26 2524 23 22 21 20
IF IE 10 IC 1B IA 19 18
17 16 15 14 13 12 11 10
OF DE omoc all Oil 09 08
07 06 0504 03 02 ()l on
Bank ".
Bauk 2
Bank 1
Defaut register I
bank for RO·R7 RAM

ByLe
address
1'1'
Fa
EO
DO
B8
BO
AB
AO
99
98
90
80
8C
BB
8i\
S\)
8
88
87
83
82
8 81
80 Bit address

F71 F61F51 F41F31F21FIIFO
Ej ESI r~51 J<;4l E3JE2JE IIEO
071061 051D41031021o liDO

- 1- I - IBC IBBIBAj B91BS
B71 B61 651641631B21B lisa

AFI- 1-IIICIABIAAlA9IAS

A7IAGIA5IA4IA3IA2IAIIAO

110t bit addreeaable
9FI9EI9DI9cI9BI9AI99198

97196195194193192191190
not bit addressable
not bit addressable
not bit addressable
not bit addressable
not bit addressable
SFISEISDISCj BBISAI89 188
not bit addressable

not bit addressable
not bit addressable
not bit addressable
87186185184183182181180 SPECIAL FUNCTION REGISTERS

Hinh 2.6 : Bi? nho da lieu tren chip 8051

B

ACC

PSW

IP

1'3

IE

P2

SBUF SCON

1'1

THI THO TLI TLO TMOD TCON PCON

DPH OPL SP Po

Chuang 2 : Tom tlit phan cirng

27 ~

Byte address, bit address: dia chi' byte, dia chi' bit General purpose RAM : vung RAM da muc dich Bank: day

Default register bank for RO - R7 : day thanh ghi mac dinh RO - R7 Special function registers: cae thanh ghi chuc na ng dac bi~t

Not bit addressable: kho ng dinh dia chi' bit

Vung RAM da muc dich con co thg du(_1c truy xml't bang each dung kigu dinh dia chi gian t.iep ~ac thanh ghi RO. Rl. Thi du hai l~nh sau thuc hi€n cungC6ng viec nhu lenh a thi du tren :

MOV RO, #5FH MOV A, @RO

Lenh d§u tien sii dung kigu dinh dia chi Wc thai di chuygn gia tri 5FH VaG thanh ghi RO, lenh tiep theo sii dung kigu diIlh dia chi gian tiep di chuyen dir Iieu tro Edi RO VaG thanh chira A..

2.4.2 Vung RAM dinh dja chi bit

8051 chira 210 vi tri bit du(_1c dinh dia chi trong do 128 bit chira trong cac byte a dia chi tli 20H den 2FH [ 16 byte x 8 bit = 128 bit] va phan con la i chira trong cac thanh ghi chirc nang dac biet,

Y tUdng truy xuilt cac bit r'ieng re thong qua phan mern la met d~c trimg manh cua hau het cac bq- vi di~u khien. Cac bit co thg dlf<,>'c set, xoa, AND, OR, v.v ... biing mqt lenh, H§.u het cac bq vi xii If yeu cdu mc t chuci lenh doc--sira=ghi dg nhan dU(_1c cung mi?t ket qua. Ngoai ra 8051 con co cac port xuat/nhap co thg dinh dia chi tirng bit, di~u nay lam dan gian viec giao tiep bang phan mern vdi cac thiet bi xuiltJnh~p dan bit.

Nhu vira d~ c~p d tren, 8051 co 128 vi trf bit dU(_1c dinh dia chi va co nhieu muc dich d cac byte co dia chi tit 20H den 2FH. Cac dia chi nay dU(_1c truy xuilt nhu la cac byte hay cac bit tuy VaG l~nh C\l th~. Thi d\l dg set bit 67H bAng 1 ta dung l~nh sau :

5')SETB 67H

r

Tham chieu hinh 2.06 ta thily bit d dia chi 67H la bit co y nghia kin

nha't cua byte a dia chi 2CH. Lenh vita neu tren khong anh huang den cac bit khac trong byte nay. Hdu het cac bi? vi xii ly muon thuc hien cong viec nhu tren phai dung cac lenh co dang tuong tlJ nhu sau :

( MOV A, 2CH' ; doc cli byte

( ORL ~,~lPOOOOOOB ; set bit c6. y nghia 16'n nha't

MOV 2CH, A ; ghi trO' la i ca byte

~28

2.4.3 Cac day thanh ghi

g2 vi trf thap nhat cua bo nhc no i chua cac-day thanh ghi. Cac l~nh cun 8051 hb tro 8 t.hanh ghi t.li RO den H7 thuoc day 0 ( bank 0 ). Day la day n!:;J.c djnh sau khi reset hq thong:.. Cac thanh ghi nay d ca c dia chi. tli OOH den 07H. Lenh sau day doc no i dung ta i din chi 05H vao thanh

~

MOV A, R5

Le nh Hay la lenh I-byte dung kip·u.--dinh~· chi thanh ghi. DI nhie n thno tac tircng tl/ co thp' dircc thuc hien vci 1 lenh 2-byte biing erich dung kieu dinh dia chi tntc tiep :

'MO~ OSH . ,

Cac le nh sti dung cac thanh gh i tli RO den R7 la eric lenh ngan va tlnrc hien nhanh han sovo i cac Ie nh tirong dirong sir dung kieu dinh dia chi true t.iep. Ca c gia tr] dtt Iieu tlnnrng dinrc sir dung ne n chua d mo t trong eric thanh ghi nay. Day thauh ghi dang dl1<;1c sti dung dl1ac goi la dl~l) -v- ,_t.l.,..la_l_ll~c. D~y thanh ghi tich cl/c co the' dl1~ng cndLthny dOi de bit ch~n day trong tli trl?ug thai chl1ang trinh PSW ( sf> de cap sau l, Gin sti rang day thanh ghi 3 ( bank 3 ) ttch cue, lenh sau day ghi no i dung cua thanh chua A vao vi trf 18H :

MOV RO, A

Y t.l1dng "eric day thanh ghi" cho phep "chuyen dO'i ngir crinh" nhanh va co hieu qua d nhtmgnci rna cac phan rieng re cua phd n me m sli dung mo t t~p thanh ghi rieng, dQc l~p vdi cac phrin khac cua phan memo

2.5 cAe THANH GHI CHUC NANG D~e BI~T ( SFR )

eric thanh ghi no i cua hd u het cac b(> vi xli ly deu dl1<;1c t.QJx....!.uu·!_ ro ~~nh. Thi du Ienh INCA cua chip 6809 tang no i dung cua t11:lilh chua A bo i 1. Thao tac dinrc xri c dinh ro rang t.rong opcode cua le nh. Viec truy xuat cac thanh ghi cung' dl1<;1c sti dung tre n 8051. Le nh INC A thuchien cung cong viec tre n.

Cric t.hallh ghi no i cua 8051 diroc cau hinh t.l~allh mo t phan cua RAM t.ren chip, do vay mo i mot thanh ghi cung co mot dia chi. Dieu nay ho p ly vdi 8051 vi chip nay co iflt. nhieu thanh ghi. Cung nhu eric thanh ghi t.li RO den R7, ta co 21 thanh ghi clnrc nang dac biet SFR chiern pha n tre n cua RAM nQi t.li dia chi 80H den FFH ( xem hinh 2.6 ).

Luu Y la khOng pha i tu't. ca 128 dia chi t.li 80h den FFH dell dl1<;1C dinh nghla rna chi co 21 diachi dl1<;1c dinh ughia [ 26 t.re n 8032/8052 J.

Chinrng 2 : Tom tilt phan cirng

29 ~

Thanh chua A co thg dtic;>'c truy xuatro rang nhu dti<?,c minh hoa trong cac thi du d cac phdn tren. Hdu het cac thanb ghi cbqc nang dl)c bi~t dti<?'c truy xuat biing kigu dinh dia chi tr1!C tiep. Trong hinh 2.6 ta cAn htu y 13. mQt s6 thanh ghl chac nang d~c bie t dti<?,c dinh dia chi tltng bit va.dinh dia chi titng byte. Thfdu ta c6 l~nh sau :

SETB OEOH

se set bit 0 cua thanh chua A len 1, cac bit khac cua thanh chua khong thay d6i. Ta nhan thay riing ta i dia chi EOR c6 thg la : dia chi byte cho ca thanh chira V'a dia chi bit cua bit c6y nghla thap nhat trong thanh chua. VI l~nh SETB thao tac tren cac bit va khong thao tac tren cac byte, chi co bit dti<?,c dinh dia chi bjanh htidng. LUu' Y Ia cac bit dti<?,c dinh dia chitrong IllQt thanh ghi chtic nang d~c bi~t co 5 bit cao cua dia

j!i gio~ nha~i:~~ta~~'t~ca~? ~c~ac~bFtit~c~u~a~th~a;n~h:;g~h;..i z: Lay thf dl;! port 1 co diu chi byte Ia 90R ( hay 10010000B ) va cac bIt trong port nay c6 cac

dia chi tit 90R tdi 97R hay 10010xxxB.

Tit trang thai chuong trinh PSW ( program status word ) se dti<?,c thao luan chi tiet trong muc sau. Cac thanh ghi chuc nang dac biet khac ciing dti<?,c gidi thieu van blt, cac chi tiet v~ cluing se dti<?,c d~ cap trong cac chuong tiep theo.

2.5.1 TU tr~ng thai chuong' trinh PSW

Bit Ky hi~u Dja chi Md ta bit
PSW.7 CY D7H CO- nho
PSW.6 AC D6H C(t nho phu
PSW.5. FO D5H ce 0
PSW.4 { RS1 D4H Chon day thanh ghi ( bit 1 )
PSW.3 RSO D3H Chon day thanh ghi ( bit 0 )
00 = bank 0 : dia chi tU OOH de'n 07H
01 = bank 1 : dia chi tU 08H de'n OFH
, .
10 = hank 2 : dia chi tU 10H de'n 17H
11 = bank 3: dia chi tU 18H de'n 1FH
I PSW.2 OV D2H CO- tra n
r PSW.1 D1H Du trO
r
\ PSW.O P DOH C(t ki~m tra chdn Ie
(
,
Bang 2.3: Thanh ghi PSW
'.
l ~30

PSW co dia, chi la DOH chu-a cac bit. trang tha i co chtrc nang diroc tOrl tift t.rong bang 2.3. Tang bit cua PSW diroc khao sat. dirdi day:

" Co rrho'

Cd nho CY ( carry flag ) co 2 cong dung. Cong dung truyen thong trong eric phep toan so' hoc 1a duoc set bilng I neu co so' nhd t.tt phep co ng bit. 7 hoac c6 so' muon mang de'n bit 7. Thi du neu thanh chu-a A c6 Hoi dung la FFH, lenh :

ADD A, #1

sf> him cho thanh chu-a A co no i dung la OOR va cd CY t.rong PSW diroc set bang 1. Cd nho CY con 10. thanh chira logic dugc dung nhu mot thanh ghi I-bit d9i~ le,uh legi<:.. t.hao t:h.' t.r~.u. cae bit Lay thi du l~nh sau doy sf>, AND bit. 25H vcii Cd nho CY va d~t. ket. qua tri:1 v~ cO nho :

ANL C, 25H ; AND bit i1 dia chi 25H v6'i co 11M

J Co nha phu

Khi cong cac gia t.ri BCD, cd nhri phu AC ( auxiliary carry flag) dugc set hilng 1 neu co mot so nho dugc tao ra ta bit. 3 chuyan sang bit. 4 hO:,l<2 Ilf'U ket. qua trong d~-cat thap narn t.rong tam t.tt O_~R den OFH. Neu cac giri tr] ducc co ng 10. giti tr] BCD, lenh cong phai duge t.iep t.heo bi:1i l~nh DA A ( h ieu chinh tha p phon thanh clnra A ) M dira cac ket. qua 16'n lion !) ve gia t.ri dung.

CoO

Day 10. cd co nhieu muc dieh da nh cho cac ung dung cua ngtnri lap trmh.

Cac bit cbo n day thanh ghi

Cue bit. chon day thanh ghi RSO, RS1dung M xac dinh day thanh ghi t.ich cue. Ctic bit. nay duge xoa sau khi co thao tac reset M thong va doi nnrc logic bci phan me m khi can. Thi du ba lenh sau cho phep day thanh ghi 3 (ban~ 3 ) tfch cue, sau do di chuyen noi dung cua R7 ( dia ell! byte IFH ) vao thanh chua A :

SETB RSI SETB RSO MOV A, R7

/' Khi doan chuong trinh tren dime dich, cac diu chi bit. se thay t.he cho cac ky hieu RSO va RSl, va y t.hi lenh SETB RS1 tirong dirong vdi l~nh SETB OD4H.

_·T

Chllang 2 : Tom tiit phan cttng

31 ~

Co t ra n

CC1 trim OV ( overflow flag) dircc set b}lng 1 suu phep toan cong hoac tnt neu co xua t hie n mat tran so hoc. Khi cac so co dau duoc .cong hoac dll9'c tnt, pha n me m co the'kiern tra bit tra n OV de xac dinh xem ket qua co na m trong tam hay khong.

Vdi phep cong eric so kh6ng dau, CC1 tra n OV diroc b6 qua. Ket qua ldn hun +128 hoac nho han -127 se set cC1 OV bang 1. Thi du phep cong sau day gay ra 1 tra n va set CC1 OV trong PSW :

86 hex:

S6 tha p pha n :

OF

15

+ 7F BE

142

8EH bieu dien s6 am -116. nhu vay khong dung vdi ket. qua rnong muon la 142 nen CC1 OV dinrc set bang' 1.

Co ch~n. Ie

Bit. chan le P tll do ng ducc set bang 1 hay xoa bang 0 0 m6i chu ky may de thiet l~p 1i!em tra chiin cho thanh chtta A. S6 eric bit 1 trong thanh chua co ng' vo'i bit. P luon luo n la ~o chan. Thi du neu thanh clnru co no i dung 10101101B. bit P se Ia 1 de co s6 bit 1 la 6. Bit. chan le diroc stt dung nhieu de ket. h9'P vdi eric chucng trmh xuat/nhap noi tiep trudc khi truyen da Iieu hoac de kiern tra chan le sau khi nhan da lieu.

2.5.2 Thanh ghi B

Thanh ghi B i1 dia chi FOH diroc dung chung vo i thanh clura A t.rong eric phep toa n nha n. chiao Le nh MUL AB nha n 2 so8-bit kho ng dau clnra trong A va B va chira ke't qua Ifi-bit VaG C:;1P thanh ghi B:A ( thanh clu1a A cat byte thap va thanh ghi B edt byte cuo ),

Lenh chia DIV AB chia A bo i B. thuong so' d't trong thanh clnra A va du so' ca't trong thauh ghi B. Thanh ghi B con ducc xii ly nhu 1 thauh ghi nhap. Cac bit diroc dinh dja chi cua thanh ghi B co dia chi: til FOH den F7H,

2.5.3 Con tro stack

Con tr6 stack SP ( stack pointer ) la 1 thanh gh], 8-bit. fJ dia chi 81H,

. SP chira 9!a chl ~~~ da lie '. n dang fJ dinh cua stack, Cac lenh lien

quan 'de'n stack bao g6m l~nh ca.'t da Iieu VaG stack va E;luh lay dir lieu ru khoi stack, Viec cat VaG stack lam tang SP truac ~i ~l}i da lieu va vih' la -" ra _" - 'am SP. Vung stack cua 8051 diroc gia trong RAM nqi va diroc gioi han den cac dia chi truy xuat dll9'c bdi .kieu dinh

~32

H9 vi dieu khien 8051

dia chi gian tiifp. Vung RAM ni?i c6 128 byte tren 803118051 hoac 256 byte tren 8032/8052; neu ta khdi di?ng SP de bat dfiu vung stack a dia chi 60H bang' lenh :

MOV SP, #5FH

vung stack diroc gidi han la 32 byte tren 803118051 vi dia chi cao nhat cua RAM tren chip 1a 7FH. Gia tri 5FH dugc dung a day vi SP tang len 60H tru6c khi thao tac cat VaG stack dau tien diroc thuc thi.

Neu ta khcng khdi di?ng SP, ni?i dung mac dinh cua thanh ghi nay Ia 07H nham duy tri sir tuong thich vdi 8048, bi? vi dieu khien tien nhiern cua 8051. Ke't qua la thao tac cat vao stack dau tien se luu dt11i~u VaG vi tri nhd c6 dia chi 08ll. Nhtr vay neu phdn mern ung dung khong khdi df?ng SP, day thanh ghi 1 ( va c6 Ie 2 va '3 ) khong con hop I~ vi vung nay dugc sii dung lam stack. Cac I~nh PUSH va POP se cat dt1 Iieu vao stack va lay dt1 Iieu ttl stack, cac lenh goi chuong trinh con ( ACALL, LCALL ) va lenh tra ve ( RET, RETI ) cling cat va phuc h6i nci dung cua bi? dem chuong trinh PC ( program counter ).

2.5.4 Con tro dilli~u DPTR

Con tro dft Iieu DPTR ( data pointer) dugc dung de truy xuat bi? nhd chuong trinh ngoa i hoac bi? nho dft lieu ngoai. DPTR Ia 1 thanh ghi 16- bit c6 dia chi Ia 82H ( DPL, byte thap ) va 83H ( DPH, byte cao ). Ba lenh sau day ghi 55H vao RAM ngoai a dia cui 1000H :

MOV A, #55H )' , MOV DPTR, #1000H MOV @DPTR,A

Lenh dl1u tien stl dung kieu dinh dia chi tuc thdi de n~u: Iieu ~thanh chua A. Lerih thu hai cling sa dung kieu dinh dia chi tuc thdi, Ifin nay nap hang dia chi 16-bit 1000H cho con tro dir Iieu DPTR. Lenh thu ba sa dung kieu dinh dia chi gian tiep di chuyen gia tr] 55H chua trong thanh chua A de'n ~ ta i dia' chi chua trong DPTR ( 1000H ).

2.5.5 Cac thanh ghi port

Cac port xuat nhap cua 8051 bao g6m p~ tai dia chi 80H, port 1 tai dia chi 90H, port 2 ta i dia chi AOH va port 3 tai dia chi BOll. Cac port 0, 2 va 3 khong dinrc dung de xuat/nhap neu ta stl dung them bi? nho ngoa i hoac ne'u c6 mi?t s6 dac tinh dac biet cua 8051 dugc sa dung ( nhu Ia ngat, port n6i tiep, .. , ). P1.2 de'n P1.7, nguoc lai, luon luon la cac dinrng xuat/nhap da muc dich hop I~.

I

\

t

I

l

Chirong 2 : Tom Hit phan cirng'

33 ~

Tat ca port d~u dUdc dinh dia chi bIng bjt nham cung_cap cac kha nang giao tie'p manh. Thi du ta co met di?ng ca noi qua mot cuon day va ~g transistor noi tdi bit 7 ciia port 1, dQng co co th~ ngirng hay chay chi nhc vao mQt lenh don cua B051 :

SETB Pl.7

lam dQng ca chay va CLR P1.7

him dong co ngirng.

Cac l~nh tren stl dung toan ttl . ( dot) dg dinh dia chi 1 bit trong 1 byte, cho phep dinh dia chi tung bit.

Trinh dich hop ngft thuc hien bien d6i dang ky hieu thanh dia chi thuc te', nghia la hai l~nh sau tirong dirong :

CLR Pl.7 CLR 97H

Vi~c stl dung cac ky hieu du<!c dinh nghia trudc ( ti~n dinh nghia ) cua trinh dich hop ngir se du<!c thao luan chi tie't trong cac tai Iieu v~ l~p trinh hop ngft tren ho MCS-51 hoac <1 chucng 7.

Thf du sau day khao sat viec giao tie'p vdi 1 thie't bi co bit tra ng thai goi la BUSY, bit nay du<!c set bang 1 khi thie't bi dang ban va ducc xoa khi thie't bj da sifn sang. Neu BUSY diroc noi vdi bit 5 cua Port I, vo ng l~p sau day du<!c dung dg chc cho den khi thiet bi siin sang:

WAIT: JB Pl.5, WAIT

Lenh tren co nghla la ne'~, nhay den nhan WAIT ( cting co nghia la nhay v~ va ki~m tra lfin nila ).

2.5.6 Cac thanh ghi djnh thai

B051 co 2 bQ de'mldinh thO'i ( timer/counter ) 16-bit dg dinh cac khoang thO'i gian hoac M de'm cac stf kien. BQ dinh thci 0 co dia chi BAH ( TLO, byte thap ) va BCH ( THO, byte cao ); bQ dinh thoi 1 co dia chi BBH ( TLl, byte thap ) va BDH ( THl, byte cao ).

Hoat dQng ella bQ dinh thO'i du<!c thiet lap bdi thanh ghi che dQ dinh thO'i TMOD ( timer mode register) d dia chi B9H va thanh ghi dieu khien dinh thci TCON ( timer control regist~ dia chi BBH. Chi co TCON du<!c dinh dia chi tUng bit.

Cac bQ dinh thO'i se du<!c thao luan chi tiet sau.

IMJ 34 H<;> vi di~u khiell 8051

2.5.7 Cac thanh ghi eua port A'. .' p

Ben trong 805i co mot port noi tiep de truyen thong vdi eric thiet bi noi tiep nhu cac thiet bi dau cuo'i hoac modem, hoac de gino tiep vci eric l C khac co mach giao tiep neli tiep ( nhu cac t.hanh ghi dich chang han ). MQt thanh ghi duoc go i lit bi? de m dil lieu no'i tiep SBUF ( serial data buffer ) 0- dia chi 99H hru giil du lieu truyeu di va dil lieu nha n ve. Viec ghi len SBUF se nap dil lieu de truyeu va vie c doc SBUF se lay du lieu da nha n diroc.

Cac che dq hoat dong khac nhau diroc lap trinh thong qua thanh ghi dieu khien port noi tiep SCON ( serial port control register ) d din chi 98R. thanh ghi nay duoc ofnh din chi tirng' bit.

Hoat dong chi tie't cua port n6i tiep se mo tri sau,

2.5.8 Cac thanh ghi ng1'it

8051 co mot cau true ngdt vo i 2 mire IiU t.ie n va 5 nguyen nhan ngat, ( 5 source, 2 priority level interrupt structure ). Cac ngd t bi vo hieu hoa sau khi reset he thong va sau do dinrc cho phep bang erich ghi V:lO thanh ghi cho phep ngat IE ( interrupt. enable register) d din chi A8H. Mtic IiU tieu llgd t duoc thiet 14 p qua thanh ghi uu t.ien ngat IP ( int.errupt prior ity register ) d dja chi B8H. Ca 2 thanh ghi nay deu duoc dinh din chi hl'ng bit.

Cac ngdt se duoc d~ cap chi tiet snu. 2.!'i.H Thanh ghi di~u khi~n ngu6n

Thanh ghi dieu khie'n nguou peON ( power control register ) co diu r ln R7H clura erie bit dieu khie n diroc tom tdt. trong bang 2.4.

Bit SMOD tang gap do i toe dq baud ella port no i t ie p khi port nay hon t do ng d eric che' dq L 2 hone :3. Cric bit 4. 5 va (i ella PCON khorig dlJ'O'C dinh nghia. Cric bit 2 va 3 l:l eric bit cd da mile dich da nh cho de Itllg dung cua nguo i sti dung.

, ~c ?it dieu khien nguon, uguon girirn PD va nghi IDL, hcp l~ t.rong tAt. c:J>c.b:_ip thuoc ho MCS-51, nlnrng chi dune hien tlnrc trong cac phie n ba n CMOS cua MCS-5L PCON khOng diroc dinh din chi bit.

Che'dc) ngu6n ghim

Lenh thiet lap bit PD bang 1 se la l~nh sau cung ducc thuc thi trudc

, khi di vao che' d(l nguon giarn. d che d(l ngu6n gia m :

l (_l)_,m~Ch dao d(lng tren chip ngitng hoat d(lng

Chirong 2 : Tom tiit phan cirng

35 ~

(2) mo i chirc nang ngitng hoat d(lng

(3) nQi dung cua RAM tren chip duoc duy tri (4) cac chan port duy tri mac logic cua chung

(5) ALE va PSEN diroc giu d mire thap. Chi ra kh6i che' dQ nay bang

each reset h~ thong.

Trong su6t th<'1i gian d che' dQ nguon giarn, Vcc co dien ap In 2V. Can phai giu cho Vcc khong thap hon sau khi dat duoc che dQ nguon giarn vii clin phuc h6i Vcc = 5V t6i thieu 10 chu ky dao dQng truoc khi chan RST dat muc thap Ian nua.

Bit.

Ky hi~u

Mota

7

SMOD

Bit tang gap do i t6c dt:> baud, bit nay khi set Hun cho t6c dt:> baud tang 2 o' eric che' dt:> 1, 2 va 3 cun port n6i tie'p

6

Khong dinh nghra Khong' dinh nghia Khong diuh nghia Bit co' da muc dich 1 Bit cit da muc dich 2

5

4 3 2 1

GFI GFO

PD

Ngucn giiim; thie't 14p M tich cue che' dt:> ngue n gidm, chi ra kho i che' dt:> bling reset.

o

IDL

Che' dt:> nghi: thie't 14 p M tich cue che' dt:> ughi, chi ra kho i che dt:> bling 1 ngat hoac reset h~ thong.

Biing 2.4 : Thanh ghi PCON

Che' dQ nghi

Lenh thiet lap bit IDL bang 1 se In lenh sau cling dl1<;1c thuc t.hi ~dC khi di va o che dQ nghi. d che'dQ nghi, 'tin hieu clock nci dU.<;1C khoa kh6ng cho den CPU nlumg kh6ng khoa d6i vdi cac clnrc nang ngdt, dinh th<'1i va port n6i tie'p. Trang thai -cua CPU duoc duy tri va no i dung cua

t.:ft. C3 cac thanh ghi cung duoc giu kho ng d6i.

Cac chan port cung dl1<;1c duy t.ri eric mire logic cua chung, ALE va

PSEN dl1<;1c gift d rrnrc cao. .

Che di? nghi ke't. thiic bang each cho phep ngat hoac bang each reset h~ thong. Ca hai each vira neu deu xoa bit IDL.

l!J36

H<;> vi di~u khien 8051

2.6 BQ NHd NGOAI

Cac b6 vi di~u khien cdn c6 kha nang md i'i?ng cac tai nguyen tren chip ( bi? ~hd, 110, v.v ... d~ tranh hien tuo/ng c6 chai trong thie't ke'. Cau true cua MCS-51 cho ta kha nang md rong kh6ng gian bi? nhd chinrng trinh den 64K va kh6ng gian bi? nhd du Iieu de'n 64K. ROM va RAM

ngoai dU"g"c theIrl vao khi c.in.

Cac IC giao tie'p ngoai vi cling c6 th~ duo/c them vao M md rong' kha nang xuat/nh4p. Chung trd thanh 1 phan cua kh6ng gian bi? nhd dir Iieu ngoai barig each sa dung each dinh dia chi ki~u 110 anh xa bi? nhd. Khi bi? nhd ngoai duo/c sa dung, port 0 kh6ng lam nhiern vu cua port xuat/nhap, port nay trd thanh bus dia chi ( AO - A7 ) va bus du lieu ( DO - D7 ) da hop. Ngo ra ALE ch6t byte thap cua dia chi d thci di~m bllt d§u m6i mi?t chu ky bi? nhd ngoai. Port 2 thinrng ( nhung kh6ng pha i luon lu6n) dircc dung lam byte cao cua bus dia chi.

'I'rudc khi thao luan cac chi tie't cu th~ v~ cacbus dia chi va dtr Iieu da hop, y tUdng t6ng quat duo/c trinh bay d hinh 2.7.

_---Memory cycle ---+

AO-A1G

Address

DO-D7

Data

(a) Nonmultiplexed (24 pins)

_--- Memory cycle ---+-

AB-A15

Address

Data

(b) Mult.iplexed (1(; pills)

Ifinh~Da hcp bus dia chi ( byte .Up ) v' bus da Ii,u chan) (b) da hop ( 16 chan)

(a) khong da hop ( 24

Memory cycle : chu ky bQ nhe Address : dia chi

Data : dtt li~u

Nonmultiplexed : khong da hop Multiplexed: da hop

-- .-------------

J

Chuong 2 : Tom t.iit phan cling

37 ~

Slip xep kh6ng da hop sli dung 16 duang din chi va 8 dinrng du lieu, tong cong 24 ducug. Sli p xep da hop ket hcp 8 dinrng' cua bus du Iieu va byte thap cua bus dia chi. do vay ta chi ca n 16 dinrng.

Viec tiet kiern eric chan cho phep ta dong go i bo vi dieu khie n ho MCS-51 trong 1 vo 40 chan.

Sap xep da hop co hoa t do ng nhu sau : trong 1/2 chu ky dau cua chu ky bi? nhd, byte thap cua din chi ducc cung cap bc i port 0 va duoc chof nho tin hieu ALE. Mach chot 74HC373 giu cho byte thap cua dia chi on _ dinh trong ca chu ky bi? nhd. Trong 112 sau cua chu ky bo nho, port 0 duoc sli dung lam bus du lieu va du Iieu diroc doc hay ghi.

2.6.1 Truy xua't be) nhd chuang trinh ngoa i

Bi? nhd chuong trinh ngoa i la bo nhd chi doc, duQ'c cho phep bo i tin hieu PSEN. Khi co 1 EPROM ngoa i duoc sli dung, ca hai port 0 va port 2 deu kh6ng con la cac port xuat/nhap,

Ket n6i pha n cling vdi bi? nhd ngoa i EPROM dinrc trinh bay 6' hinh 2.8.

/" -.
Port 0 DO-D7
8051 -, / EPROM
~"IIC'"
EA 1- ~
/D Q AO-A7
- V ~
ALE G
-,
Port 2 A8-A15
v/
PSEN OE
Hinh 2.8 : Truy xwi't b<} uhd chuo'ng trinh ngoa i

Mi?t chu ky may cua 8051 co ~ chu ky dao dong, Neu bi? dao dong tren chip co tan so 12 MHz, mot chu ky may dai 1 us. Trong 1 chu ky may di~n hinh, ALE co 2 xung va 2 byte cua I~nh' diroc doc tlt bi? nho chuong trinh ( neu I~nh <;.hl co 1 byte, byte thir hai duoc loa i bo ). Gia n do thai gian cua chu ky may nay, du~c goi la chu ky tim-nap I~nh dune trinh bay 0' hinh 2_9_

~38

H<;> vi di~u khien 8051

Is 1 1 821 83 1 84 1 85 1 86 1 81 1

Pl I P2 r i I P2 Pl I P2 Pl I P2 Pl I P2 Pl I P2 Pl I P2

Osc.

Port 2

PCB (Program counter high byte)

PCH

Port. 0

Hinh 2.9 : Gian d6 tho-i gian cua chu ky tun-nap l~nh lJ bQ nhd ngoai

One machine cycle: mQt chu ky may Program counter high byte: byte cao cua PC

2.6.2 Truy xua't bQ nha dtl li~u ngoai

BQ nho di1 li~u ngoai la bQ nhd doc/ghi du'<_1c cho phep bdi cac tin hieu RD va WR <1 cac chan P3.7 va P3.B. Lenh dung M truy xua't bi? nho di1 lieu ngoai la MOVX, sil dung hoac con tro di1 Iieu IB-bit DPTR hoac RO, Rl lam thanh ghi chira dia chi.

RAM co the giao tiep vdi 8051 theo cung each nhu EPROM ngoai tru dinrng RD ndi vdi dinrng' cho phep xua't ( OE ) cua RAM va WR n6i vdi du'<'tng ghi (W) cua RAM. Cac ket noi vdi bus di1 lieu va bus dia chi giong nhu EPROM. Bang each sil dung cac port 0 va port 2 nhu <1 phan ~, ta co 1 dung lu'<_1ng RAM ngoai len den SJ.-K-du'<_1c ket n6i vdi 805l.

Gmn do thoi gian cua thao tac doc di1 lieu <1 bQ nhd di1 lieu ngoai du'<_1c trinh bay <1 hlnh 2.10 cho lenh MOVX. A, @DPTR. Luu Y la ~ ~ng ALE.."li PSEN dl(gC bO qua <1 nai rna xllog RD cho phep dr;!C RAM [

,..!leu lenh MOVX va RAM ngoai khong bao gi<'t du'<_1c dung, cac xung ALE luon co tAn so' b~n~ lIB...wn so cua mach dao dQng ].

Gian do th<'ti gian cua chu ky ghi ( l~nh MOVX @DPTR, A ) cling tirong tv ngoai trir cac xung WR <1 mac tha'p· va di1 lieu duoc xua't ra <1 port 0 (RD vl1n <1 mac cao ).

Clurong 2 : Tom tilt phan ciing

39 ~

l

I

r

1_ Ou« macluu.: cycho -1- One niachiuc cycle -I

ALE

PSEN

1 1 ) 1

1 1 1 1

-,- __ ~I-, 1 1 ,...1 __ -

Port 2 ==x PCB: X ~p~ (data pointer high byteJX '

f I '""'1---

1 1 1 1

1 1

RD

Port 0

Hmh 2.10 : Gian d8 thoi ginn cua l~nh MOVX

One machine cycle: ml?t chu ky may

Data pointer high byte: byte cao cua DPTR External~n : nha p da lieu tu bl? nhc ngoai Opcode : ma thao ta c

v -,
Port 0 DO-D7
r-, /
8051 ~"HC'" ' RAM
U OK, byte)
Eft. ~
/D Q~ AO-A7
v
ALE G
------ ---~ ~ A
I '. \
/ I A~\
2.1 VI
\
RD OE
- W
WR
PSEN t--NC r CS
- Hinh 2.11 : Giao tie'p v6'i 1 K RAM

~40

H9 vi dieu khien 8051

Port 2 giarn brit duoc chuc nang lam nhie m vucung cap byte cao cua diu chi trong cac he thong toi t.h ieu t.ha nh pha n. h~ thong kh6ng dung bo nho chuang tri nh ngoa i va chi co 1 dung luang nho bo nhri du Iieu ngoa i. Cac dia chi 8 bit co the truy xuat bo nho du Iieu ngoa i vo i can hinh be'? nhri nho hudng trang ( page-oriented ). Neu co nh ieu han 1 t.rang 256-byte RAM, 1 va i bit tir port 2 ( hoa c 1 port kh::ic ) co the cho n 1 trang. Thi du vrii 1 RAM IKE ( nghla--ra--4 trang 256 byte )~ta co the ketn6i'RAM nay vdi 8051 nhu rJ hinh 2.11.

Cac bit 0 va 1 cua port 2 pha i diroc khrii do ng de chon 1 trang, ro i lerih MOVX diroc dung M d9C hoac ghi tren trang nay. 'I'hi du ta gia sti' P2.0 = P2.1 = 0, cac lenh sau co the dung de d9C eric no i dung cua RAM ngoai rJ dia chi 0050H van thanh chira A :

MOV RO, #50H MOVX A,@RO

De d9C rJ dia chi cudi cung cua RAM nay, 03FFH, trang 3 duoc chon nghia la ta pha i set cho cac bit P2.0 va P2.1 bang 1. Chuo i lenh sau dircc dung:

SETB P2.0 SETB P2.1

MOV RO, #OFFH MOVX A,@RO

Me'?t dac trimg ella thiet ke' nay In cac bit tlt 2 de'n 7 cua port 2 kho ng

J.' n can him bit dia chi nira, cac bit con la i nay co the sit dung cho muc ch xuat/nhap.

6.3 Giai rna dja chi

Neu co nhieu EPROM hoac nhieu RAM hoac ca 2 giao tie'p vdi 8051 ta can phai giai rna dia chi. Viec giai rna nay cling can cho ,hilu he't cac be'? vi xir Iy.

~I;I neu cac RAM va ROM 8 KB duc;>'C sa dung, dia chi pha i ducc gia i rna M chon cac IC nhd nay tren cac gidi han 8 K : OOOOH - IFFFH, 2000H - 3FFFH, '"

Mdt IC giai rna dien hinh la 74HC138 ducc dung vci cac ngo ra duoc noi vdi cac ng6 van chon chip CS cua cac IC nho nhu diroc mo ta rJ hinh 2.12 cho mot bo nho co nhieu EPROM 2764 ( 8K ) va RAM 6264 (

. . .

8K ). Cdn luu y la do cac dinrng cho phep rieng re (PSEN eho hi? nho

Chuang 2 : Tom tAt phan cung

41 k!J

chuong trmh, RD va WR cho hi? nho da lieu ), 8051 co thg quan If khong gian nhd de'n 64K cho hi? nhd EPROM va 64K cho hi? nhri RAM.

( (
I I
( (
I I j t
-- - DO·D7 DO-D7
PSEN- OE 2764 f- RD- OE 6264 f-
EPROM ,-- WR- Vi RAM f-
~ (8K bytes) ~ (8K bytes)
,...
AO·A12 AO-A12 AO·A12
~ V -
Cil r- CS
rics rics
rlcs rics
74HC138
A15 0
L....- C-
.___E1 B 1"
A13 A "2
, :~\ .
L
E 5 f-- Select other .
_f~ _ j EPROMIHAM
6f--
"if-- Hmh 2.12 : Gia i mil diu chi

2.6.4 Cac khong gian nho chrrong t.rrnh va du l ie u go'i nhau

VI hi? nhd chuang trinh la hi? nhd chi doc, mot tlnhhuO"ng kho xU' duoc phat sinh trong qua trmh phat tri~n phan mern cho 8051. ham the' ,~ phan m~m duac vie't cho mi?t h~ thO"ng dich de fJ rO"i ';;;U phan chi co thg du<:>,c thl!c thi ttl khong gian hi? nhd chuang trinh chi doc.

Gia i phap t6ng quat la cho cac khorig gian hi? nho chuang trlnh va da lieu ngoa i go"i len nhau. VI PSEN diroc dung de doc hi? nho chuang trinh va RD diroc dung de doc hi? nhd da lieu, mot RAM co th~ chiern khOng gian nho chuang trinh va da lieu bang each nO"i chan OE tdi ngo

ra cfing AND co cac ngo vao 13. PSEN va _,RI) . r--

"-----

M~ trmh hay d hinh 2.13 cho phep Ie RAM diroc ghi nhu la hi? nho da Iieu va dinrc doc nhu la hi? nho chuang trinh hoac da Iieu. V~y thi mQt chuang trinh co th~ du<:>,c nap vao RAM ( bang each ghi vao

1!j42

H9 vi dieu khien 8051

RAM nhu la bi? nho du lieu ) va diroc thuc thi (bang each truy xuat nhu bo nho chuang trinh ).

RD PSEN

)O------;-iOE

RAM

WR ----------1 W

Hinh 2.13 : G6i 2 kh6ng giqn nhc chuo'ng trinh viI dli lieu

2.7 cAc CAl TIEN CUA 8032/8052

-Cric vi mach 8032 1 8052 ( va cac phien ban CMOS) co hai ca i tien so vdi 803118051. Mi?t la co them 128 byte RAM tre n chip tl1 dia chi 80H den FFH. Dieu nay kh6ng xung di?t vrii cac thanh ghi chirc nang dac biet ( co cung dia chi) vi 128 byte RAM them V[1O chi co th~ truy xw)'t, bang erich dung ki~u dinh dia chi gian tiep. Mi?t le nh nlnr sau :

MOV A,OFOH

di chuyeri no i dung cua thanh ghi B t6i thanh chira A tren de IC cua ho MCS-51. Chuo i le nh :

MOV RO, #OFOH MOV A, @RO

d9C vao thanh ghi A no i dung ta i din chi FOH tre n cac IC 8032/8052 nhung kho ng duoc dinh nghia t.ren 803118051. T6 chirc bo nho no i cua 8032/8052 diroc tom ti'it d hinh 2.14.

FFIlI--------- FFll
I
I Accexs rhlt- AC(,l~ssibie
UPP'" I
128 I by indn-et-t by direct
I
hvtos I addressing addressrng
I uu lv only
I
80HI SOH
7FH \__ Special
Accessible
Lower bv d nect
funct.ion
l~!l and uidirect
registers
bytes addressing
OOH Hinh 2.14 : Khong' ginn nho nQi cua 8032/8052

Chuong 2 : Tom tAt phan cling

43 ~

Upper 128 bytes: 128 byte tre n Lower 128 bytes : 128 byte du6'i

Accessible by indirect addressing only: chi duQ'c truy xua't bAng ki~u dinh dia chi gia n tie'p

Accessible by direct addressing only: chi duoc truy xua't bAng ki~u di nh dia chi true tie'p

Accessible by direct and indirect addressing: truy xua't bAng ki~u dinh dja chi true tie'p va gian tie'p

Thanh ghi Dia chi Me) ta Dia chi bit
T2CON C8H £)i~u khien C6
RCAP2L CAH Nh4 n byte tha' p Khnng
RCAP2H CBH Nhan byte cao Khong
TL2 CCH Byte tha'p cua b(l dinh thO'i 2 Khang
TH2 CDH Byte cao cua b(l dinh thai 2 Khnng'
Bang 2.5 : Cac thanh ghi cua b(l dinh tho i 2 Ca i tie'n thli 2 la co them be? dinh thdi 16-bit. Be? dinh thdi 2 nay duQ'c lap trinh nhc vao 5 thanh ghi chiic nang dac biet them vao. Chung duQ'c tom tiit trong bang 2.5 va se diroc mo ta chi tie't sau trong chirong 4 ( hoa t dong djnh thdi ).

2.8 HO~ T DONG RESET

8051 duQ'c reset bang each giu chan RST d mire cao t6i thi~u 2 chu kL may va sau do chuy~n v~ miic thap, RST co th~ duQ'c t5c dQng b~ng tay

.---

hoac ducc tac de?ng khi cap nguon bang each dung mot mach RC nlur

ducc trinh bay d hinh 2.15. Trang thai cua ta't ca cac thanh ghi sau khi reset h~ th6ngduQ'c to m tiit trong bang 2.6.

Quan trong nhat trong cac thanh ghi nay co Ie la thanh ghi PC ( bo de'm chirong trinh ), diroc nap OOOOR. Khi RST trd lai mire thap, viec thuc thi chirong trinh luon luon biit dau d vi tri dau tien trong be? nho chirong trinh: dia chi OOOOR. Ne?i dung, cua RAM tren chip khong bj auh huong bdi ~. ---- -

Thanh ghi

B(I de'm chuang trlnh Thanh chua A

NQidung

Thanh ghi B PSW

OOOOH OOH OOB OOB

l!J44

H9 vi dieu khian 8051

SP DPTR

07H OOOOH FFH

Port 0 - 3

Cac thanh ghi dinh thai

xxxOOOOOB ( 8031/8051 ) xxOOOOOOB ( 8032/8052 ) OxxOOOOOB ( 8031/8051 ) OxOOOOOOB ( 8032/8052 ) OOH

OOH

OOH

IP

IE

SCON SBUF

PCON (HMOS) PCON (CMOS)

OxxxxxxxB

OxxxOOOOB

Bang 2.6 : Gia tricua cac thanh ghi sau khi reset M th6ng

+5 V +5 V

IooL _h

n :rf.-IO~F

RST

8.2K

(1

(a! Mam.il rrst-'t

+5 V

1.

fIOf'F

RST K

(1

(b) Power-on reset

Hinh 2.15 : Hai mach dung reset M thong' (a) reset bAng tay (b) reset khi cilp ngudn

Reset : nut nhilt reset

L· Manual reset: reset bAng tay

••• ._~ _ powe':_O_D _~et , reset khi df._p_n_gu_d_n __

Chuong 3 : Tom tdt {-4p l~nh

45 ~

3

TOM TAT TAP LENH

. .

., . ~

3.1 MO DAU

Chuong nay gidi thi~u t~p lenh I instruction set) cua ho MCS-51 thong qua viec khao sat cac ki~u dinh dia chi ( addressing mode) va cac thi du dua tren cac tinh huong lap trinh di~n hinh. Phu luc A cho ta bang tom tdt ta't ca cac l~nh cua 8051. Phu luc C mo ta chi tiet tung l~nh. Cac phu luc diroc dung d~ tham khao khi d9C chirong nay. Chuong nay chua ban den ky thuat l~p trinh cung nhu hoat di?ng cua trlnh dich hop ngi1 ( assembler ), chuong trinh ducc dung d~ chuyen d6i chuong trinh ngucrrviet bang hcp ngir thanh chuong trinh ngo n ngfr may. Cac chu de nay du(_)'c thao luan sau trong cac chuong 7 va 8.

T~p lenh cua MCS-51 diroc t6i uu hca cho cac U'~ dung dieu khi~n 8- ~Nhieu ki~u dinh dia chi co dong va nhanh dung M truy xuat RAM no i duoc dung den nham tao thuan l(_)'i cho cac thao tac tren cac cau triic dl1 lieu nho. Tap l~nh ciing he, trd cac bien I-bit cho phep quan ly bit true tiep trong cac he logic va dieu khien co yeu C§u xlily bit.

-

Cling nhu cac bi? vi xli ly 8-bit, cac l~nh cua 8051 co cac o_pcode 8-bi'S_. do vay so' lenh co th~ len den 256 lenh ( thuc te co 255 l~nh, 1 lenh khOng duoc dinh nghia ) . Ngoai opcode, mot so' l~nh con co them 1 hoac 2 byte mra cho dtr Iieu hoac dia chi. Tap l~llh co 139 lenh 1 byte, 92 lenh 2-byte va 24 lenh 3-byte. Phu 11,lC B trinh bay opcode cua cac lenh. Phu luc nay cho ta thay, vci tling l~nh mot : opcode, rna g(_)'i nhd, so' byte cua l~nh va so' chu ky may can M thuc thi l~nh.

3.2 cAc KIEU DJNH DJA CHi

Khi mot l~nh du(_)'c thuc thi va lenh nay c§n dl1 lieu, mot diu hoi diroc diilt ra l~' :" Du lieu chua d dau ". Cau tra loi cho cau hoi nay tao ra cac ki~u din dia chi cua 8051. Conhieu ki~u dinh dia chi do vay co nhieu cau tra ldi cho cau hoi neu tren, chang han nhu : trong byte thir 2

~46

R<;> vi dieu khien 8051

CUf1 1 lqnh, trong thanh ghi R4, trong dja chi tfl!C tiep hoac c6 the trong be? nho ngoa i (1 dia chi chua trong con tr6 dir lieu.

Cac ki~u dinh dia chi.la phd n can thiet cho toan bi? tap l~nh cua m6i rno t bi? vi xi/ly, bi? vi dieu khien. Cac ki~u dinh dia chi cho phe p ta xac dinh ro nguon va dich cua du li~u theo nhieu each khac nhau phu thuoc VaG tinh hu6ng lap trinh. Trong phan nay chung ta se khao sat tat ca cac kieu dinh dia chi cua .. 51 va neu thi du cho tung ki~u. Co 8 ki~u dinh dia chi:

Thanh ~hi ( register ). Tn!c tiep ( .irect ). Gian tiep ( indirect ). Tlic th_i ( immediate ). Tusng' dii ( relative ). Tuye t i.ii ( a.s.lute ) . • ai ( l.n~ ).

Chi si ( in.exe. ).

3.2.1 Djnh dja chi thanh ghi

Ngmri lap trinh tren 8051 co the' truy xuat 8 thanh ghi " lam yjec '~ duoc danh so' tli RO de'n ~7. Cac l~nh si/ dl,lngki~u dinh dia chi thanh gUt diroc rna hoa bang each dung 3 bit t.hap nhat C;lla opcode ( cua lenh ) de' chi ra mo t thanh ghi ben trong khOng gian dia chi logic nay. V~y thi mot rna chirc nang va dia chi toa n hang co th~ ke't hop M 'hinh tha nh mot lenh ngan ( L-byte ] [ xem hinh 3.1.a ].

Hop ngu cua 8051 chi ra ki~u dinh dia chi thanh ghi bang kyhieu Rn. trong d6 n c6 gia tri tir 0 deri 7. 'I'hi du M cong nci dung cua thanh gh i R7 vrii thanh chira A, ta dung l~nh sau :

ADD A, R7

va leuh nay c6 opcode la 00101111B. Na m bit cao J0101 cho biet day la leuh cong va 3 bit thap 111 chi ra thanh ghi R7. Ta co th~ tham khao phu Iuc C M xac nha n dieu vira de cap.

C6 4 day thanh ghi " lam vi~ " nhung (1 1 thdi di@m chi. c6 1 day ~c.-Cac day thanh ghi chi~m 32 byte d5.u tien cua RAM du lieu ~ ( dia chi tu OOR de'n 1FH ) va ta dung cac bit 4 va 3 cua tu t.ra ng thai ChliJ~ trinh PSW d~ chi ra day thanh ghi tich cue. Mot reset

C Ct/' ~(\ -: LUlYv~

l.

Chuang 3 : Tom tilt t4P Ierih

bllng phdn cirng cho phep day • tich cue con cac day khac dinrc chon bang each sii'a d6i cac bit .( va 3 cua PSW sao cho phu hop. Thi d1,1 l~nh :

Mev P.SW, # ... 11 ....

se tich CIfC day thanh ghi 3 bang each set cac bit chon day thanh ghi ( RSI va RSO ) trong PSW len 1 ( cac bit nay & vi tri 4 va 3 ).

I a I Register addressing I c-.g . :\ •• :\. H.:--)!

Ibl Direct. addressiug I".g .... \])]) .. \. dirt-c-t.r

Ie) Indirect. addressmg le.g .. A •• A. @l't.,

«l rImniediat.e addressing I".g .. A •• A. #55H)

leJ Relative adcit'esslIIg le.g., SJMI' <des!.»

If) Absolute addressing le.g .. AJMP -cdcstc-

Ig) Long .uklress nig le.g .. LJl\1P <des I> I

Base register

Offset. Efl'ect.i vc address

1+1 ACC I=L -,

1 PC lor DPTRI

Ih) Indexed addressing Ic.g .. MOVC A.@A + PCI

Hinh 3.1 : Cac kiii'u dinh dia chi cua 8051 (a) dinh dia chi thanh ghi (b) dinh dia chi true tie'p (c) diuh dia chi gia n tie'p (d) dinh dia chi Wc thai (e) di nh dia chi tl1O'ilg d6i (f) dinh dia chi tuyet d6i (g) dinh dia chi da i (h) dinh dia chi chi so'

Register addressing: dinh dia chi thanh ghi ( thi du : ADD A, R5 ) Direct addressing: dinh dia chi true tie'p ( thi du : ADD A, direct ) Indirect addressing: dinh dia chi gian tie'p ( thi du : ADD A, @RO ) Immediate addressing: diuh dia chi Wc thai ( thi du : ADD A, #55H ) Relative addressing: dinh dia chi hieing deli ( thi du : SJMP -cdest» Absolute addressing: dinh dia chi tuyet d6i ( thi du : AJMP -cdest» Long addressing: dinh din chi da i ( thi du : LJMP -cdest»

Indexed addressing: dinh dia chi chi so' ( thi du : MOVC A, @A+PC )

<.

_ _ , - .. _ _ _------

l¥J48

R9 vi dieu khi€n 8051

MQt so lenh dac bi~t lien quan den 1 thanh ghi xac dinh nao do nhu Iii thanh chua A, con tro du lieu, ... kh6ng can cac bit dia chi, ban than opcode cua lenh da chi ra thanh ghi dn thiet. Cac l~nh dac biet lien quan den thanh ghi nay tham chieu den thanh chira b~ng ky hieu " A ", con tre du Iieu b~ng ky hieu " DPTR ", bQ dem chirong trinh bang ky hieu "PC''', cd nhd bang ky hieu "c" va cap thanh ghi AB bang ky hieu "AB". Lli'y thi du :

INC DPTR

la l~nh I-byte, lenh nay cong 1 vao no i dung cua con tre du lieu 16-bit. Opcode cua lenh nay duoc cho a phu 11,1c C.

3.2.2 Djnh dja chi trq'c tie'p

Ki€u dinh dia chi true tiep ducc sir d~ng M truy xuli't cac bien nho hoac cac thanh ghi tren chip. MQt byte them vao tiep thea opcode dung d~ xac dinh dia chi ( xem hinh 3.I1) ).

~

Phu thuQc vao bit co gin tri vi tri ( hay trong so ) cao cua dia chi true tiep, mQt trong hai kh6ng gian nhd tre n chip dti<;>'c chon. Khi bit 7 bang 0, dia chi true tiep a trong tllrn tl.i 0 den 127 ( OOR - 7FR ) va 128 byte thap cua RAM tren chip dtt<;>,c tham chieu. Tli't eli cac port xuat/nha p va cac thanh ghi chirc nang dao biet, dieu khien, trang thai diroc gan dia chi trong tllm tl.i 128 den 255 ( 80R - FFR ). Khi byte dia chi theo sau opcode co no i dung narn trong gidi han nay ( vdi bit 7 bang 1 ), thanh ghi chirc nang dac bie t diroc truy xuat. Thi du port 0 va port 1 duoc gan dia chi true tiep la 80R va 90R.

Ta khong nhli't thiet phai biet dia chi cua cac thanh ghi nay, trinh dich hop ngi1 cho phep ta sir dung rna g<;>,i nho viet tlit d~ hieu nhu la " PO " cho port 0, " TMOD " cho thanh ghi che dQ dinh thdi ( timer mode register) v.v... Lenh sau day la mo t thi du cho kigu dinh dia chi trvc tiep :

~

MOV PI, A

lenh nay chuyen nQi dung cua thanh chira A vao port 1. Dia chi true tiep cua port 1 la 90R dtt<;>,c xac dinh bai trinh dich hop ngu va trinh dich nay da t 90R vao byte 2 cua lenh, Ngudn cua du lieu, thanh chua, diroc xac dinh ro rang trong.opcode.

Bang each sir dung phu 11,1c C, ta thay lenh vira neu tren co rna la :

10001001 10010000

byte 1: opcode

byte 2 : dia chi cua PI ( 90R )

\

j

Chuong 3 : Tom tfit t4p lenh

49 IMi

3.2.3 Djnh dja chi g'ia n tie'p

Lam each nao nha n biet 1 bien khi dia chi cua bien d5 du'<?,c xric dinh, du'<?,c tinh toan hoac du'<?,c sua d6'i trong khi mQt chuong f.rinh dang chay ? Tinh huong nay phat sinh khi ta qua n ly cac vi tri nhO' lien t.iep, c~c di~m nhap du'c,1c djnh chi so' trong cae ba;;g chaa trong RAM, cac ~. chinh xac holitc cac chu6i ky tu, Cac ki~u dinh dia chi thanh ghi hoac true tie'p khOng sit dung duoc cho cac tinh huong nay, do vay ta can co cac dia chi toan hang diroc biet trong thO'i gian hop dich.

Gia i phap cua 8051 la dung kieu dinh dia chi gian tiep. Cac thanh ghi RO va Rl co thg hoat dQng nhu la cac con tro ( pointer) va nQi dung cua chung chi ra dia chi trong RAM, noi rna du lieu du'<?,c doc hay diroc ghi. Bit co y nghla tha'p nha't cua opcode ( cua lenh ) xac dinh thanh ghi

..--

nao ( ~ hay Rl ) du'q.c sit dung lam con tro ( xem hinh 3.1.c ). Trong

1Wp ngi1 cua 8051, kigu dinh dia chi gian tiep duoc nhan biet nhc vao ky tv @ d:)it tnJO'c RO hoac R1. Lay thi du neu Rl chira 40H va dia chi 40H cua b(> nho no i chira 55H, lenh :

MOV A,@Rl

nap 55H cho thanh chua A.

Ta can Mn kigu dinh dia chi gian tiep khi ta duy~t cac vi tri liEm tie'p trong b(> nho. Thi du sau thuc hien viec tuan tv xo a RAM no i tit diu chi 60H de'n 7FH :

MOV RO, #60H LOOP: MOV @RO, #9 INC RO

CJNE RO, #80H, LOOP

( tie'p tuc )

Lenh dau tien kho i dong RO vci no i dung la 60H, dia chi bat dau ella kh6i nho trong RAM; lenh thu hai sit dung ki~u dinh dia chi ~n t.ie p d~ nap o.Q_H cho vi tri duoc tro bo i RO; lenh thu ba tang con tro ( ffij ) de tro den dja chi tiep thea va lenh cudi cung kiern tra con tro xem d5 ke't. tluic khni nho chua. Viec kiern tra sit dung hang so' 80H thay vi la 7FH vi lenh tang xuat hien sau lenh di chuyen. Di~u nay dam bao vi tri nho sau cung ( 7FH ) duoc ghi OOH truce khi ke't thuc.

3.2.4 Dinh dja chi hlc thai

Khi toan hang ngucn 13. mo t h~ay vi In mot bien ( nghia In lenh sit ~ia tri da: bie't truric 6' thrri gian hop dich ), hang so' nay

(!J50

Ho vi dieu khien 8051

co the dua vao lenh va day la byte dfi Iieu Wc thai ( byte them vao nay co gia trj biet trirdc ) ( xem hmh 3.1.d ).

Trong hop ngtr, cac toan hang Wc thai diroc nhan biet nho vao ky tI! # dat trudc chung, Tolin hang nay co the Ia mot hang so hoc, mQt bien hoac mot bieu thirc s6 hoc slt dung cac hang s6, cac ky hieu va cac toan tir. Trinh dich hop ngfi t.inh gia tr] va thay the dfi Iieu tac thai vao trong lenh, Thi du l~nh :

MOV A, #12

na p gia trj 12 ( OCH ) vao thanh chua A.

Ta't ca cac le nh slt dung kieu dinh dia chi Wc thai deu Slt dung hAnll dir lieu 8.bit lam dfi Iieu tuc thaI Co mot ngoa i l~ khi ta khci dong con tr& dfi lieu 16-bit DPTR, hilng dia chi 16-bit dli<;>'c can den. Thi d", :

MOV DPTR, #8000H

la mot lenh 3-byte, l~nh nay nap hang dia chi 8000H vao con tro dfi Iieu DPTR.

3.2.5 Djnh dja chi tu'dng do'i

Kieu dinh dia chi tuong d6i chi duoc Slt dung cho cac lenh nhay. Mot diu chi turrng d6i ( hay con go i la offset) la mo t gia tri 8-bit co da'u. Gia trj nay diroc cong vdi bi? dem chlidnii trinh Mtao ra dia chi clla l~nh tiep thea dn dli<;>'c thl!c thi. Do ta slt dung mo t offset 8-bit co dau, tam Iiliay dli<;>'c gidi han la -128byte den 127 byte Byte dia chi tirong d6i la byte them vao tiep thea byte opcode clla leW ( xem hinh 3.1.e ).

. ,.-

204;)
0109 2042
0108 2041 :~ } SJMP 2038H
tH07 s l R.,""" "IT,,,
0106 4 fro III address -3
0105 3 0102H is '5' ·4
0104 2 -5
0103 1 -6
0102 203B -7
0101 }SJMP 0107H -8 Relative offset
from address
0100 2039 ·9 2042H is "-10"
2038 -10 ofF6H Code memory

Code memory

I aJ Short jump aitt' ld lil uiornorv

fbi Short jump back in memory

Hinh 3.2 : Tinh offset cho ki~u dinh dia chi tl1C1ng Mi (a) nhay t6'i va ngiin (b) nhay lui va ngll.n

Chuong 3 : Tom tat t4p lenh

51 ~

Short Jump ahead in memory: nhiiy t6'i vii ngdn Short jump back in memory: nhiiy lui va ngan

Relative offset from address 0102H is 5 : offset tllO'ng d6i tlJ dia chi 0102H Iii 5 Relative offset from address 2042H is -10 ( F6H ) ; offset tllO'ng d6i tlJ diachi 2042H la -10 ( F6H )

Nh<'1 vao phep cong, hi? de'm chuong trmh du<;>,c ~ng de'n dja chi theo sau l€mh nhay; vay thi dia chi mdi lien quan den le nh ke tiep, khong lien quan de'n dia chi cua lenh nhay ( xem hinh 3.2 ).

Thong thinrng chi tiet nay khcng lien quan den nguni lap trinh do bdi cac dich nhay thmrng du<;>,c xac dinh bang cac nhan va trinh dich hop ngu se' xac dinh offset nrong doi ttrong irng. Thi du neu nhan THERE dl). t truce l~nh i1 dia chi 1~4_0!f, lerih :

SJMP ,THERE v,

i1 trong hi? nhc tai dia chi 1000H va 1001H, trmh dich hcp ngu se gan offset tinmg doi la 3EH cho hyte 2 cua lenh ( 1002H + 3EH = t040HJ

..

Dinh dia chi tirong doi co diem l<;>,i la cung cap cho ta makhong phl,l thuoc vao vi tri ( vi cac dia chi tuyet doi khong du<;>,c dung ), nhirng .lai c6 diem bat 1<;>'1 1a cac dich nhay hi gidi h~n trong tilm.

3.2.6 Dinh dja chi tuy~t d6i

Kieu dinh dia chi tuyet doi chi du<;>,c sit dung vdi cac l~nh ACALL va AJMP., Bay la cac l~nh 2-hyte cho phep re nhanh chudng trinh trong trang 2K hien hanh cua hi? nho chuong trinh bang each cung clip 11 hit thap cua dia chi dich, trong do 3 hit cao ( A8 - A10 ) dua vao op~va 8 hit thap ( AO - A7 ) thanh l~p hyte thii 2 cua lenh, ( xem hinh 3.1£).

~

5 hit cao cua dia chi dich hi 5 hit cao hien hanh trong hi? de'm chuong

trinh, do v~y l~nh theo sau l~nh re nhanh va dich cua lenh re nhanh phai (} trong cling 1 trang 2K, hi1i vi All - A15 khong thay d6i ( xem hmh 3.3 ). Lay thi du ne'u nhan THERE d*t trudc l~nh d d!a chi OF46H, lenh :

AJMPTHERE

i1 trong hi? nho tai dia chi 0900H va 0901H, trinh dich hop ngu se rna hoa l~nh nhu sau :

11100001 01000110

hyte 1 ( A10 - A8 + opcode ) hyte 2 ( A7 - AO) (V )

Cac hit du<;>,c gach dirci la 11 hit thap cua dia chi dich, OF46H = 0000l11101000110B. Narn hit cao i1 trong hi? dem chuong trinh se kh6ng

\~

~52

Ill? vi dieu kh ie n 8051

thay d6i khi lenh tren diroc thuc thi. 1.1I't1 Y 1<1 le nh A,TMP va dich nhay den dE'\u c1 trong 1 trang 2K girii han bo i 0800H OFFFH (xern hinh 3.3), va do vay co 5 bit dia chi cao nhu nhnu.

Dia chi tuye t d6i co diern lei la le nh ngan i. 2-byte ) nhung co diem bat lo i la dich bi gidi han tam din chi va cung cap cho ta rna phu thuoc vao vi tri.

F!"!"I~ r-: -

,.),.' \

FbOU t-----1

(ll) Th« Up}J('J' fi bits in th(' program counter remain t hc sarno. The lower hits a1"1' fl'plac"d by th« bits supplied in thr- instruction.

32 x 2K = 641{

A15 A10 'AO

" I I I I I I I I I I I I I I

'----v------"

[, bits determine 11 bits determine the

the ~K pago address within a ~K pagp

Witlun any 2K l~\~.\~.t-----1

page, Dilly {, 2K pHg"t' z

the lower 1 1 Jllilil f----I

liit.s change tIFFI<' ~J-; I'''gc' 1

IH)()flL- __ --'

Hiuh :J.3 : Ma h6a le nh dung ki~u di nh dia chi tuyet d6i (a) bi? nhrr duo'c chia tha nh uhieu trang 2K (b) Ben trong mot trang 2K, 5 bit dia chi cao khoug thay d6i.

Within any 2 K page, only the lower 11 bits change: trong mot trang 2 K ~li't ky chi c6 11 bit tha'p thay d6i

.5 bits determine the 2 K page: 5-bit xa c di nh trang 2 K

11 bits determine the address within a 2 K page: 11-bit xac di nh dia chi trong trang 2 K.

G4 K memory map divided into 32 x 2 K pages: ban d6 bi? nho 64 K duo'c chia tha nh 32 trang 2 K.

The upper 5, bits in the program counter remain the same. The lower bits are replaced by the bits supplied in the instruction: 5-bit cao trong thanh ghi PC giU kho ng d6i. Ca c bit thap dLiQ'C thay bang' cac bit cung cdp b&i Ie nh.

3.2.7 Djnh dja chi diti

Kieu dinh dia chi da i chi ducc dung cho cacIanh LCALL va LJMP. eric le nh 3-byt.e nay chua dia chi dich I6-bit ( 2 byte: byte 2 va byte 3 )

cua I~~nh 3.l.g ). .

Lo i ich cua kieu dinh din chi nay Ia sti' dung he't toa n be? khcng gian uho chuang t.rinh 64K nlurng la i co diem bat lo i la .lenh dai den 3-byte va phu thuoc vao vi tri.

Chuong 3 ; Tom tilt t4P l~nh

53 l!J

/

Viec phu thuoc vao vi tri duqc xem la ba't lo i do bdi chuong trinh kh6ng thg duqc thuc thid mot dia chi khac. La'y thi du neu chirong trinh bdt dliu d 2000H va co mot l~nh nhu la LJMP 2040H, chirong trinh nay khfmg thg di chuyen de'n 4000H chang han. Lenh LJMP se v§.n nhay de'n dia chi 2040H va day khOng pbai la vi tri dung sau khi chu<1ng

trinh dii duqc di chuygu... .

.

3.2.8 Dinh dja chi chi s6

.

Kigu dinh dia chi chi 56 sli dung mi;lt thanh ghi nen ( hoac bi;l de'm

chirong trinh hoaccon tro dtr Iieu ) va mot offset ( thanh chua A ) tao thanh dang dia chi hieu dung cho lenh JMP hoac MOVC ( xem hmh 3.1.h ). Trong nhieu U'ng dung, cac bang nhay hoac cac bang tim kiern duqc tao ra d~ dang bIing each sli dung kigu dinh dia chi chi so. Cac thi du duqc cho d phu luc C vdi cac lenh MOVC A, @A+<base-reg> va JMP

@A+DPTR •

3.3 cAc LO~I ~NH

Cac lenh cua 8051 duqc chia lam 5 nho m ; Nhom lenh 56 hoc

Nhom lenh logic /

/ Nhom l~nh di chuyen dft Iieu

Nhom lenh xli ly bit

Nhorn lenh re nhanh

Phu luc A giup ta tham chieu nhanh ta't cd cac lenh cua 8051 dii duqc phan nhom,

3;3.1 Cac l~nh s6 hoc

Cac l~nh s6 hoc cling duqc phan loa i nhu trong phu luc A. Do co the' co 4 kha nang dinh dia chi, lenh ADD A con duqc vie't dudi cac dang

sau :
ADD A,7FH
ADD A,@RO '\
ADD A,R7 .
ADD A, #35H [dinh dia chi true tie'p] [dinh dia chi ginn tie-p] [dinh dia chi thanh ghi] [dinh dia chi Wc thai]

Ta't ca cac lenh 56 hoc gU<1C thuc thi trong mi;lt chu ky may ngoai trlt lenh INC DPTR dinrc thuc thi -trong hai chu ky may, cac lenh MUL

~54

HQ vi dieu khien 8051

AB va DIV AB thuc thi trong 4 chu ky may ( luu Y la met chu ky may dai 1 usee neu 8051 hoat dqng vdi xung clock 12 MHz ).

8051 cung cap ki~u dinh dia chi linh hoat cho khcng gian nhd nqi.

Mqt vi tri bilt ky deu co th~ tang hay giam biil!KSa~h__~yng kieu dinh dia chi ~iep rna khong' can qua trung gian thanh chua A. Lily thi du' ueu vi tri 7FH ciia RAM noi chua gia tr] 40H, lenh :

--

INC 7FH

tang gia tr] tai dia chi 7FH thanh 41H va d~ t ket qua ta i 7FH.

8051 cling co lenh INC thao tac tren con tro dir lieu 16-bit.. Do con tro du Iieu chua Ifi-bit dia chi cua bq nho ngoai, viec tang no i dung con tro nay la mot dac trung thuong sii dung. Khorig may cho ta In khong co lenh giarn noi dUI1g con tro' du lieu va ta pha i dung den mo t chuo i lenh sau thay cho (lieu thieu sot nay:

DEC DPL MOV R7, DPL

CJNE R7, #OFFH, SKIP DEC DPH

; gia m byte thap ella DPTR ; eat va o R7

SKIP: (tie'p tuc )

; so sa nh v6'i OFFH ?

; bang' : giiim byte eao cua DPTR ; khong bang : be qua

Cac byte thap va byte cao cua DPTR pha i duqc giarn rieng re trong do byte cao ( DPH ) chi du<jc giarn 1 neu byte thup ( DPL ) tran tli OOH qua FFH.

Lenh nha n MUL AB nha n du lieu 8-bit dura t.roug thanh ghi B vdi 119i dung cua thanh chua-A vu da t ke t qua Hi-bit t rong cap t.hnnh ghi BA (thanh ghi B chira byte cao. t.hauh cluru A dill:! bvtr- thap l.

Lenh DIV AB chin no i dung tlianh ghi A rho .hr 1t1'11 chun t roug thanh g11i B, t.huong sci H bit cat t.rong thalli, clttf.! .\ \':1 dlf :-0 R·hit 1',1t t.rong thanh ghi B, Lay thi du IIPII no i dung ('lIa t hn uh dlll.l I:t ;:.-, ( l'lll I va thanh ghi B chua G ( 06H ). lenh suu :

DIV AB

chin 25 eho 6, ket qua 4 efit. trong thanh clura A va du so 1 cat t.rong thanh ghi B,

Vai s6 BCD, eric lenh ADD vn ADDC pha i dinrc hep thea bo i l~nh DA A M dam bao ding ke't qua a trong f,:im s6 BCD. Luu Y In lenh hieu dinh DA A se khong bien doi s6 nhi phan thanh BCD rna chi tao ra 1 , kef, qua hop le va ta thinrng go i la hieu dinh trong phep cong 2 s6 BCD.

Chuang 3 : Tom t.at. U!p I$IlI!

Lay thi du neu thanh clnra A clura gia t.ri BCD Ia 59 (59H), chuoi lenh sau :

ADD A, #1 DA A

trirdc tien cong 1 vdi no i dung t.hanh chua A, ket qua Iii 5AH. Ket qua nay duoc lenh t.hti hai hieu dinh thanh gia tr] BCD hop l~ la 60 ( 60H ) va cat. vao thanh chua A.

3.3.2 Nhom l~nh logic

Nhom lenh logic cua 8051 thuc hien cac pheptoan logic ( AND, OR.

XOR va NOT) tren cac byte du Iieu va thuc hi~n tren tirng bit co cung gia trj vi tri ( trong so ). Neu thanh chua A chira gia tr] 001l0101B, lenh AND logic sau day :

ANp A, #01010011B

se tao ra ket qua Ia 00010001B cat trong thanh chua A. Di€u nay duoc minh hoa nhu sau :

01010011

AND 00110101

00010001

( dli li~u We thdi )

( gin tri ban dii u chua trong A ) ( ke't qua chua trong A )

Cac ki~u dinh dia chi cho cac lenh logic cting giong nhu cac kieu dinh dia chi cho cac l~nh so hoc, lenh AND logic co th~' co cac dang sau :

ANfJ._A, 5SH ( dinh dia chi true tie'p ) \

,

ANb:-A, @RO ( dinh dia chi ginn tie'p )

ANb- A"R6 . ( dinh dia chi thanh ghi ) ,

ANtr A, #33H ( dinh dia chi' tac thO"i ),

Tat ca cac l~nh logic sU' dung thanh chua A M luu mot toa n hang se dinrc thuc thi trong 1 chu ky may, ngucc la i neu sU' dung thanh ghi khac hoac byte nhd thay cho thanh chua A, l~nh phai duoc thuc thi trong 2 chu ky may,

Cac phep toan logic co t.h~· du<,1c thuc hien tren met byte ba't ky trong bt) nho du Iieu no i rna khong dn qua trung gian thanh chira A. Lenh "XRL direct, #data "giup ta nhanh chong va d~ 'dang dao rmrc logic cdc bit cua port. Thi du lenh sau :

XRL P1, #OFFH

~56

H<,> vi dieu khi~n 8051

thuc hien mot thao tac doc - sua - ghi. 8 bit cuaport 1 ducc doc, sau do tung bit dircc XOR vdi cac bit tuong ang ( cung vi tri ) cua du Iieu Wc thO'i. Vi 8 bit cua du lieu Wc tho i d~u la 1, ket qua la tung bit cua port 1

du<,>'c Iay bu ( vi A EB 1 = A ). Ket qua nay dircc ghi trd Iai port 1.

Cac l~nh qua RL' A va RR A) dich thanh chaa A qua trai hoac

qua phai I-bit. V6'i lenh quay trai ( RL l co gia tri vi tri 16'n nhat MSB du<,>'c dua vao vi tri co gia tri thip nhat LSB. V6'i lenh quay phai ( RR A), bit co gia trj vi tri thap nhat LSB ducc dira vao vi tri co gia tr] Mn nhat MSB. Cac lenh RLC ~ A la cac l~nh quay 9-bit stt dung, thanh chaa A va cO' ~h6' CY trong·thanh ghi PSW. Thi dl,l neu ci1 n116' CY chaa 1 va thanh chaa A chaa OOH, l~nh sau :

RRC A

.i->:

se cho ket qua la : cO' nhd CY chua 0 va thanh chua A co noi dung la

80H. Dieu nay c6 nghla la cO' nhd CY diroc dua den ACC.7 va ACC.O du<,>'c dua den cO' nhd,

Lenh SWAP A trao dBi mra thap 4-bit v6'i ntta cao 4-bit trong tha~h chira A ~i nhau. Lenh nay thu~ dung trong cac phep toan s6 BCD. L~thanh chua A chua mot so nhi phan da biet va c6 gia trj nho hen 100(10), ta c6 th~ bien d6i so nhi phan nay thanh so BCD bang cac dong lenh nhusau :

! MOV B, #10
,
DIV AB
SWAP A
ADD A,B Vi~c chia mot so cho 10 trong hai l~nh d~u tien tao ra digit chuc trong nira thap cua thanh chua A va digit don vi trong thanh ghi B. Lenh SWAP va ADD di chuy~n digit chuc den mra cao cua thanh chua A va digit don vi vao mra thap cua thanh chua nay.

3.3.3 Cac l~nh di ehuy~n dillit$u

'I'rong RAM DQi

Cac lenh di chuyen du lieu hen trong khong gian nhd nQi duoc thuc thi trong 1 hay 2 chu ~ may. Dang cua lenh nhu sau :

~

MOV <destination>, <source>

Lenh treri cho phep du lieu dircc di chuyen giira cac vi tri cua RAM nqi hoac cac thanh ghi chiic nang d~c bi~t SFR rna khong c~n qua trung . gian thanh chira A. Cdri nhd la 128 byte cao cua RAM du lieu ( doi vci

Chirong 3 : Tom tilt t4p I~nh

57 I!l

8032/8052 ) chi dtf<;1c truy xuat bang ki~u dinh dia chi gian tiep va cac thanh ghi chirc nang dl)c bi~t chi dtf<;1c truy xuat bling ki~u dinh dia chi true tie'p.

MQt dac trung lam cho cau true cua MCS-51 khac vdi cau true cua M.u het cac bq vi xli If la vung stack thtf<'1n~ tiu -trong RAM tren chip ( RAM nQi ) va. tang dan v~ Pb~ !d.h. ij nh6,~ cac dia chi cao h<1n. Lenh PUSH irtf6c tien tang con tro stack ( SP ) r6i sao. chep byte vao trong stack. Cac l~nh PUSH va POP sli' dung ki~u dinh dia chi true tie'p M nhan biet byte dtf<;1c cat hoac dtf<;1c phuc h6i nhirng ban than stack dtf<;1c truy xUllt bdi ki~u dinh dia chi gian tiep sli' dung con tro stack SP.

f)i~u nay co nghia la vung satck co th~ sli dung 128 byte cao cua bq nho RAM nQi tren 8032/8052. 128 byte cao nay khong c6 trong 803118051. V6i cac bq vi di~u khien nay, neu nQi dung cua SP vuot qua 7FH ( 127 ) ( nghta la nqi dung cua SP len hen 7FH ), cac byte duoc PUSH se bi mat con cac byte dtf<;1c POP khong dtf<;1c xac dinh:

Cac I~nh chuyen du li~u con bao g6m lenh MOV 16-bit dung M kho'i dQng con tro dir Iieu DPTR cho muc dich tirn kie'm cac hang trong bi? nho chuong trinh hoac cho muc dich truy xuat bQ nho du Iieu ngoa i 16- bit.

Lenh hoan d6i nQi. dung XCH dtf<;1c sli dung-de hoan d6i ni?i dung cua thanh chua A vdi ni?i dung cua byte dtf<;1c chi ra trong I~nh. Dang I~nh nhu sau :

XCH A, <source>

Lenh tren lam cho thanh chira A va byte duoc dinh dia chi trao d6'i ~~ li~u v6j nhau. Viec trao d6'i mQt digit sli' dung I~nh co dang :

XCHD A, @Ri

cung heat dqngttf<1ng t1;f, tuy nhien chi co cac nli'a thap cua cac byte dtf<;1c trao d6i vdi nhau. Thi du neu thanh chira A chua F3H, Rl chua 40H va ta i dia chi 40H trong RAM nqi chira 5BH, Ierih :

XCHD A,@Rl

cho ketqua la A C;,hl1a FBH va tai dia chi 40H trong RAM nQi chua 53H.

Trong RAM ngoa i

V6i cac I~nh rna viec di chuyen du lieu cho phep du lieu diroc di chuyen giua RAM no i vdi RAM ngoa i, ta phai sli <!yng ki~u dinh dia chi gian tie'p. Cac dia chi gian tie'p dtf<;1c xac dinh bling each dung dia chi..l:-._ byte (~htf @Ri, trong do Ri la RO hoac Rl cua day thanh ghi dtf<;1c chon )

~ --

~58

H<;> vi dieu khieu 8051

hoac dia chi 2-hyte ( nhu @DPTR ). Diem bat lei khi dung dia chi 16-bit la tat ca 8 hit cua port 2 phai duQ'c dung nhu hyte cao cua hus dia chi va dieu nay se kho ng cho ta sil dung port 2 lam port xuat/nhap. Nguoc lai cac dia chi 8-hit cho phep ta truy xua't den mot vai KB cua RAMma khOng can sil dung toan hi? port 2 ( xern muc " Truy xuathQ nhd dir Iieu ngoai d chuang 2 ).

Tat ca cac le nh di chuyen da lieu hoat di?ng treu hi? nhd ngoai diroc thuc thi trong 2 chu ky may vii Sit dung thanh chira lam toan hang nguon hoac toan hang dich.

Cac tinhi~u dung M truy xuat. RAM ngou i II~I-i vn Wl{ ) chi tich cue trong khi l~nh VX dlfQ'C thl!c thi, Binh thmrng eric tin hieu nay khong tich cue ( nurc cao ) vii neu hi? nhrr ngoa i kho ng duQ'c sti' dung, eric d,/an~ va WR co chtic U:lllg uhu eric d~ xllat.! nhq.p. C fo-A-V

Cac bang tim k ie'm

Co hai lenh di chuyen du lieu da nh cho viec doc eric bang tim kiem trong hi.> nhd chuang trinh. Do bo i eric le nh nay truy xua t, hi,> nho chuang trmh, cac hang tim kie m chi co the duoc d9C va khong duoc cap nhut.. Ma go i nhd cua lenh la Move ( move constant : di chuyeu hfing ). MOVC sil dung hoac hi? dern chuang t.rmh hoac con tro du Iieu lam thanh~ .. hi neu va thanh chira A chtia dia chi offset. Leuh suu :

\( Move A, @A+DPTR '

co the truy xuat mot h~ns 256 diem Ilh~p. duQ'c danh so tii 0 den 255. b"O' ClJa diem nh~p yell du dudc n~p cho thanh ('htia A va con tro dli lieu duoc khdi dong'de chlla dia chi dau hung, Le nh sau :

Move A, @A+PC

rung hoa t d(>ng tUQ'ug W. ngoai t.rir d day hi? dern clurong t.rinh duQ'c dimg de chtia dja chi Ben va bung dune truy xuat nho va o mot chuang trinh COIL Trude t.ien so cua diem nhap yeu cau duoc uap clio thanh chua A sau do chuang trinh con dt.!Q'c g<;>i, Chuo i lenh cho phep khdi di?ng va go i co the la :

MOV A, ENTRY-NUMBER CALL LOOK-UP

LOOK-UP:

INC A

MOVC A, @A+PC

Chirong 3 : Tom Mt t4p lenh

59 I!J

TABLE:

RET

DB data, data, data, ....

Bang dugc dinh nghta ngay sau lenh RET trong chuong trmh. Lenh tang dugc dn den do PC tro tdi lenh RET khi lenh MOVC dugc thuc thi. Vi~c tang nQi dung thanh chua se bO qua l~nh RET ..

3.3.4 Cac It$nh xu Iy bit

BQ xli ly cua 8051 chua 1 bQ xli ly logic tren bit cho phep ta thuc hien cac phep toan don bit. RAM nci chua 128 bit duoc dinh dia chi -va kh6ng gian SFR h6 tro them den 128 bit dugc dinh din chi. Ta't ca cac dUdng port d~u co dia chi bit va m6i duong co th~ diroc xli Iy nhir Iii mot port don bit rieng reo Cac lenh truy xuat cac bit nay khong chi Ia cac lenh re nhanh co di~u kien rna con Ia cac lerih di chuyen, set, xoa, la'y bu, OR va AND. Cac thao tac tren bit nhu v~y ( me;>t trong cac d~c trung manh cua MCS-51 ) kh6ng d~ dang co dugc trong.cac cau true khac, cac cau true sli dung cac thao tac huang byte.

Mci thao tac truy xua't bit d~u sit dung ki~u dinh dia chi true tiep vdi cac dia chi bit tlt OOH den 7FH trong 128 vi tri thap, va tit dia ch~ den FFH trong kh6ng gian SFR. Cac dia chi bit d 128 vi trf thap thuQc caCbYte co dia chl tit ~OH dful2FH dugc danh s6 lien tl;lctit bit 0 cua byte d dia chi 20H (bit OOR) den bit 7 ciia byte d dia chi 2FH (bit 7FH).

Cac bit c6 th~ dugc set va xoa bdng 1 lenh. Di~u khien don bit dugc dung cho nhieu thiet bi xua'tJnhl%p, bao g6m xuat ra rd le, dqng CO', ct,&n day, cac LED, mach coi bao dQng,loa hoac nhap tit cac chuyen mach ~c cac be;> chi thi trang thai. Neu co mot m~c6'coibd'() dQng n6ivdi bit 7 ella port 1, ta co t.he tac dong mach coi bang each set bit cua port:

SETB P1.7

houc tdt co i bilng erich xoabit cua port:

CLR P17

Trinh dich hop ngir se bien doi ky hieu P1.7 thanh din chi bit. la ~)7It Th] dl;l sau eho phep ta di chuyen 1 cd vao mot chan cua port:

MOV C, FLAG MOV Pl.O. C

Trong thi du tren, FLAG la ten cua 1 bit dugc dinh dia chi trong 128 vi tri thap hoac trong khong ginn SFR. Mqt duong xuat/nhap ( d thi dl;l tren la bit 0 cua port 1 ) dugc set hoac xoa phu thuoc vao bit cd co gia tri 1 hay O. Bit nhd t.rong PSW diroc dung nhir mQt thanh chua don bit .

.......-------.

!J60

HQ vi di~u khien 8051

cua bi? xit ly logic tren bit, cac lenh dan bit lien quan de'n bit nhd ky hieu la C la cac lenh d~c biet lien quan de'n C(J nho ( nhu la CLR C). Bit nhd cling co mi?t dia chi true tiep vi bit nay dU<;1c luu trong .thanh ghi PSW, thanh ghi nay diroc dinh dia chi tirng bit.

Cling giong nhu cac thanh ghi khac dU<;1c dinh dia chi tl'rng bit trong kh6ng gian SFR, cac bit cua PSW co rna g<;1i nhd rna trlnh dich hcp ngu se chap nhan thay cho dia chi bit. Ma .g<;1i nho cua co nho la CY diroc dinh nghta thay cho dia chi bit OD7H. Ta hay khao sat 2 lenh sau :

CLR C CLR CY

Ca 2 d~u co cung c6ng dung, tuy nhien dang lenh trudc la lenh 1- byte trong khi dang l~nh sau la lenh 2-byte. Trong dang l~nh sau byte thu 2 la dia chi true tie'p cua bit dU<;1c xac dinh - co nho.

Cac lenh logic tre nbit bao g6m £a l~nh ANL va ORL nhl1n~ bao g6m l~nh XRL. Neu ta dn XOR 2 bit, BITI va BIT2, va ke't qua cat (rong c(J nhd, cac l(rib. sau diroc sit dung :

MOV JNB CPL

C, BITl BIT2, SKIP

C '. ~.t... tr.._- c, I /--d

SKIP:

Trude tien BITI dU<;1C nap cho co nho. Nsu BIT2 = 0, thi C dii chira ke't qua ( nghia la BITI liB BIT2 = BIT 1 neu BIT2 = 0 ). Ne'u BIT2 = 1, C chira ke't qua la btl cua co nhd, Vi~c lay btl C hoan tat phep toan XOR

Chirong trinh trong thi du tren sit dung l~, mot trong chuo i lenh kiem tra bit. Cac lenh nay se nhay neu bit duqc djnh dja chi dUdC set bllng ~u la l~nh JC, JB, JBC ) h.g~c ne'u bit dU<;1c dinh dia chi kh6ng dU<;1c set ( JNC, JNB ). Trong thi du d t~n neu BIT2 = 0, l~nh CPL C dU<;1C bo qua. L~nh JBG r nhiiy ne'u bit dU<;1c set, sau do xoa bit) thvc hien viec nha~ bit dU<;1C dinh dia chi dU<;1c set va cling xoa bit;

vay thi mi?t co co thi"" dU<;1c ki~m tra va xoa bAng 1 lenh.

Tilt ca cac bit cua PSW d~u co thg dinh dia chi tn!c tie'p, do v~y bit chii~ hoac cac co da muc dich cling hop l~ doi vdi cac I~nh kiern tra bit.

3.3.5 Cac .I~nh re nha nh

Trong t~p l~nh cua 8051 co nhieu lenh di~u khien luong chirong trinh, bao g6m cac lenh goi mi?t thii tuc va quay v~ ttr mot thu tuc, re

Chuang ~l : T6m dt t.lp le nh

61 ~

nha nh co dieu kie n hoac khong ('6 dieu kie n. Cric kha nang nay duqc ca i tien han ntra bo i 3 kieu dinh dia chi cho cac lenh re nhanh chuang trinh.

C6 3 bien t.h~ cua lenh nluiy : SJMP, LJMP va AJMP ( sa d\,mg kieu dinh dia chi tuong doi, da i va tuyef doi ).

Trinh dich hop ngu cua Intel ( ASM51 ) cho phep sa dung rna gqi nho JMP ne'u nguc i lap trinh kh6ng quan tam deri cac bien t.he. Trinh dich~ngu cua cac cong t.y khac co t.he' khong h6 tro dac tinh nay. JMP t.6ng quat. dich thanh AJMP neu dich nhay den khong chua tham chieu thuan va i1 trong mot trang 2K. Nguoc lai, JMP diroc dich tha nh LJMP. Le nh CALL ciing.hoa t dong theo each nay.

Lenh SJMP xac dinh dia chi dich la offset tuong doi nhu da ban de'n trirdc day khide c~p de'n cac kieu dinh dia chi. Vi lenh da i 2-byt.e ( bao g6m mot opcode cong vci mQt.offset tuong doi 8-bit. ), khoa ng each nhdy duqc girii han t.li -128 byt.e de'n +127 byt.e so v6'i dia chi cua li;!nh theo sau le nh SJMP.

Lenh LJMP xac dinh dia chi dich la hting s6 16-bit. Vi li;!nh dai 3- ~yte ( bao _g6m 1 opcode cong vdi 2-byte dia chi ), dia chi dic~ co th~ 6'

bat. cv dau trong khong gian nh6' chuang trinh 64K. .'

--.~

Lenh AJMP xac dinh dia chi dich la mot hfing so ll-·bit. Cung nhu lenh SJMP, l~nh AJMP cung da i 2 byte nhung diroc rna hoa khac. Byte_ opcode se chua 3 trong 11 bit dia chi va byte 2 chva 8 bit thdp cua dia ~l I~nhduqc thTc thi, Ilbit nay j;hay cho cho 11 bit thdp' t~C con 5 bit cao cua PC van gift nguyen. Dia chi dich do vl1.y php,i 6' ~d;'ng m~Ttrang 2 K vdi lenh theo sau lenh AJMI'". Do ta co khong gian nh6 chuang trinh la 64 K, ta c6 32 trang va m6i trang bii t dau i1 dia chi la bien cua 2 K ( nhu la aOOOR, 0800R, 1000R, 1800R, ...

cho de'n F800R: xem hinh 3.3)( ~) .'

Trong moi tnnrng hop ngufri, lap trinh xac dinh dia chi dich cho trinh dich hop ngft thea each thong thinrng nhu la nhan hoac la 1 hang s6 16- bit. Trinh djch hop ngft se dat dja chi dich theo dung khuon dang cua tltng l~nh. Ne'u khuon dang yeu cau bi1i l~nh khong duqc h6 trc khoang each dung de xac dinh dia chi dich, mQt thong bao " dia chi dich ngoai tam" se dinrc dua ra.

Cac bang nhay

Lenh JMP @A+DPTR h6 tro cac thao tac nhay phu thuoc vao

trinrng hop CI,I the cho cac bang nhay. ~ chi dich duqc tinh i1 thdi diem thl!c thi l~nh la t6ng cua n9i dung thanh ghi DPTR 16-bit v6'i n9i dung

,

J

~62

H<;> vi dieu khien 8051

clla thanh chua A. DPTR dllgc n~p dia chi clla bang nhay va thanh chua 'Ad6ng vai tro-clla mQt thanh ghi ChI s{ Thi dl,l nifu co 5 trlldng h~ d\tgc yeu cau, mo t gia tr] tit 0 den 4 dllgc nap cho thanh chua A va mot nhay cho trinrng hop tirong thich dllgc thuc hien nhu sau :

MOV MOV RL JMP

DPTR, #JUMP _TABLE

A, INDEX_NUMB~R V Q,J-,. ... !;4-

A) 1 ~ t<i!J-.t' 7'~)

@A+DPTR

Lenh RL A d tren bien d6i chi s6 ( 0 ~ 4 ) thanh 1 s6 chiin trong tam tit 0 den 8 vi mlii digm nhap trong hang nhay la 1 dia chi 2-byte :

_-

JUMP_TABLE;

AJMP CASEO AJMP CASEl AJMP CASE2 AJMP CASE3

* Chuong trinh con va ng~t

Co 2 bien thg cho l~nh CALL: ACALL va LCALL sit dung kigu dinh dia chi tuyet d6i va dai. Cling nhu lenh JMP, rna ggi nho CALL co th~ dllgc sit dung vdi trmh dich hop ngu cua Intel neu nglfdi la p trinh khong quan tam den each dinh dia chi. Ca 2 lenh tren d~u cilt nQi dung Clla bi? dem chlldng trinh vao stack va n~p cho bi? d~m Chlld; trinh dia chi dil

I1.gc xac dinh trong l~nh. LllU y la PC se chtia dia chi cua l~nh !heo sau I~nh CALL khi nQi dung thanh ghi nay dllgc cilt vao stack, Khi n~p vao ~te t~ilp n~p trlldc va. by~ cao, n~p s~u. ~ac byte dll<;1c lily ra ~it stack thea tnnh tlf nguoc lai. Lay thi du neu lenh LCALL dllgc chua t!ong bi} nhd chltavg trlllh is cae d~a chi Ii) lQ.00H-1Q02H va con tro stack chira ~' l~nh LCALL se :

(a) nap dia chi quay v~ ~H vao stack ( dat 03H tai dia chi 21H v~OH tai dia chi 22H ).

l') no; du~g cu. con tro stack bay gill Ia 22H .

(c) nhay de'n chuong trinh con bang' each nap cho PC dia chi chira trong byte 2 va byte 3 cua lenh.

Cac lenh LCALL va ACALL cling co cac han che tren dia chi dich nhu cac l~nh LJMP va AJMP ( da d~ cap d tren ).

Cac thu tuc can dllgc ket thuc bang l~nh RET, lenh nay tra viec thuc thi chuong trinh trd v~ lenh theo sau l~nh "CALL. Kh6ng co di~u gi bf lin v~ each rna l~nh RET tra di~Q khien v~ cho chutrng trinh chinh. Don

Chuong 3 : Tom tiit t4P l~nh

63 ~

gian hi l~nh nay la'y la i ( pop ) 2 byte sau cimg ra kho i stack va nap cluing cho bi? de'm chirong tricll. Mi?t qui luat chu ye'u cho viec lap trinh vdi cac thu tuc la cluing luon luon diroc goi bo-i l~nh CALL va luan luan tra di~u khi~n v~ chuO'ng trinh g<;>i bo-i l~nh RET. Cac thao tac nhay vao ho(lc nliay ra kho i 1 thu tuc bang' 1 each khac nio do thinrng lam roi vung stack va lam cho chuong trinh bi dling.

Lenh RETI tra dieu khien v~ chuong trinh go i ttl' 1 trinh phuc vu ngdt ( ISR : interrupt service routine ). Digm khac nhau giira RET va RETI hi RETI bao hieu cho h~ thong' di~u khien ngiit rang' qua trinh xii ly ngat da xong. Neu khong co mot ngiit nao duy tri trong th<'1i gian RETI duQ'c thuc thi, RETI hoat di?ng giong RET. Cac ngat va l~nh RETI se duQ'c d~ cap chi tie't trong chuong 6.

Nhiiy co dMu ki~n

B051 cung cap cho ta nhieu lenh nhdy co di~u kien. Ta't eli cac Ierih nay xac dinh dia chi dich bang ki~u dinh dia chi tuong doi va cung bi gici han 0- khoang each nhay ttl' -12B byte den +127 byte k~ ttl' lenh thea sau lenh nhay co dieu kien. Tuy nhien cdn luu y la ngmri l~p trinh se xac dinh dia chi dich theo cung cac~~nh nhay khac biing cach dung nhan hoac biing so' 16-bit. Trinh dich hop ngtr se thuc hien cac viec can lai. Khong co it a trong ~. Cac l~nh JZ va JNZ kiern t1ru lieu trong thanh chira cho di~u kien nay.

Lenh DJNZ ( ~. neu khac khcng ) danh cho dieu khien lap vong, D~ thuc thi 1 vong lap N lan, ta nap met byte so' de'm N cho mot thanh ghi va ke't thuc vong lap vdi DJNZ tro toi di~m hilt dau vong lap. Thi du dudi day co N=10 :

MOV R7,#10 LOOP: (bAt dftu yang lap )

( ke't thuc yang ll)p ) DJNZ R7, LOOP

I

Lenh CJNE so sanh va nhliy ne'u khong biing ) cling danh cho viec dieu khien vorig lap. Hai byte duoc xac m ron U<'1ng toan hang cua lenh va viec nhay chi duQ'c thuc thi neu 2 byte khac O. Thi du neu 1 ky tv vira duQ'c doc vao thanh chira A ttl' port n6i tie'p va ta muon nhay de'n

f!j64

H<;> vi dieu khien 8051

1 lenh ducc nhan biet bdi nhan TERMINATE neu ky W la Control-C ( 03H ), cac dong l~nh sau duoc sU' dung :

CJNE A, #03H, SKIP SJMP TERMINATE

SKIP:

Vi thao tac nhay chi xuat hie n neu thanh chua A chua rna cua Control-C, met nha n SKIP ( bo qua) diroc dung M bo qua vise ket thiic lenh nhay ngoa i tnt khi rna yeu cau ducc doc. Lerih tren con diroc ling dung trong cac phep so sanh 16'n han hay nho han. Hai byte trong trurrng' toan hang la cac so nguyen khong dau, Neu byte dllu nho han byte thli hai, cd nh6' ducc set len 1. Neu byte dtlu 16'n han hOiilc bilng byte thli ~ cd nh6' duac xria. Lay thi du neu ta muon nhay den ~ neu gia tr] trong thanh chua A kin han hoac bang 20H, cac chi thi sau duoc dung:

CJNE A, #20H, $+3 I . ' L. L

, , ~ .f r> ~ c- ?'fL- ~ ex- :: L

JNC BIG \ ~ -\r V --'

/

Dich nhay cho lerih CJNE duoc xac dinh la~ Dau $ la 1 lIT hi~u

c~ trinh dich hap ngiJ bi~u thj ~a li~nh hi~n~h. Vi CJNE .lal~nh 3-byte, $+3 la dia chi cua l~nh tiep theo, iNC. Miilt khac, l~nh cJNE theo sau bo i lenh JNC kh6ng quan tam den ket qua so sanh. M':Ic dich duy nhat cua viec ~nh la d~ ~t hay xo~ cd nh6' va l~nh JNC quyet dinh nhay hay khOng nhdv. Thf du nay la mQt thEf hi~n trong do 8051 tiep can vdi 1 tinh huong ~p trinh tong quat mot each vung v~ han so vci htlu het cac bi? vi xii Iy, tuy nhien, nhu se duoc trinh bay trong chuang 7, viec sii dung cac macro cho phep ta co cac chudi le nh manh, nhu thi du tren chang han, duoc cau true va thuc thi bang each dung mot rna go i nhd duy nhat,

Chuang 4 : Heat dC)ng dinh thdi

65 ~

4

HOAT DONG DINH THOI

. . .

4.1 Md HAU

NC)i dung cua chuang nay khao sat cac bC) dinh thdi ( timer) cua chip 8051. Ta hay bat d§u tU quan di~m dan gian v~ cac bi? dinh thai thuong dU<;1c sU' dung cho cac bi? vi xU' If hoac cac bi? vi dieu khien.

Mi?t bC) dinh tho i lit mi?tchlloi cae flipflop vci m6i flipflop la mot mach chia 2, chu6i nay nhan mot tin hieu ngo vao lam ngucn xung clock. Xung clock dat VIlO flipflop thli nhat, flipflop nay chia do i t§n sO' xung clock. Ngo ra cua flipflop thu nhat trd thanh ngudn xung clock cho flipflop thli hai, ngudn xung clock nay ciing dU<;1c chia cho 2, v.v ... Vi mo i mot t§ng ke' tie'p nhau deu chia cho 2 nen mot bi? dinh thoi co n t§ng se chia t§n sO' xung clock d ngo vao cua bi? nay cho 2n.

Ngo ra cua t§ng cufii cung lam xung clock cho mot flipflop bao tra n bi? dinh thci hay con goi la co tran ( ov;;now flag ), co tran nay ~c ~ tra bai phan mem holitc tao ra mot ngat. Gia tri nhi phan trong cac flipflop cua bQ dinh thdi la s6 dern cua cac xung clock tU khi bQ dinh thoi bat d§u de'm. Thi du mot bQ dinh thdi I6-bit se de'm tU OOOOH de'n

FFFFH. Cd tran duoc set bang 1 khi xay ra tran sO' Mm tU FFFFH xu6ng OOOOH.

Hoat di?ng cua mot bQ dinh thdi dan gian ducc minh hoa trong hinh 4.1, bi? dinh thdi 3-bit. Moi mot t§ng 10. met D.FF kich khci canh am hoat dQng nhu mot mach chia cho 2 do ta n6i ngo ra Q vci ngo vao D.

Flipflop Co' dan gian la mot mach chot D duoc set bang 1 bdi tang cu6i cua bQ dinh thdi. Gia n db thdi gian d hinh 4.1.b cho thay h~ng thli nhat (Qo) chia 2 tan sO' xung clock, tang thli hai chia 4 t§n sO' xung clock va v.v ... SO' de'm (count) diroc ghi d dang thap phan va duoc kiem tra d€l dang b~ng each khao sat trang thai cua 3 flipflop. Thi du sO' de'm 10. 4 xuat hien khi Q2 = 1, QJ = 0 va Qo = 0 ( 410 = 1002), Cac flipflop d hinh 4.1 la cac flipflop tac dQng canh am ( nghla la ngo ra Q cua cac flipflop d6i trang thai theo canh am cua xung clock ). Khi sO' de'm tran tU 1112

_j

1!J66

H<;> vi dieu khien 8051

xuong 0002, ngo ra Q2 CO canh am ( 1 -7 0 ) lam cho trang thai cua flipflop c(j d6i tli 0 IEm 1 ( ngo vao D cua flipflop nay luon luon d logic 1 ).

Bi? dinh thci diroc sa dung trong hau het cac ling dung hudng dieu khien va 8051 vdi cac bi? dinh tho i tren chip khong phai la trucng ho p ngoai I~. 8051 co hai bi? dinh tho i 16-bit, moi bi? co bon che. di? hoa t dorig. Bi? dinh thci thu ba vdi ba che di? hoat di?ng dune them vao doi vdi chip 8052. Cac bi? dinh thni ducc dung M :

(a) Binh th(ji trong mot khoang thci gian.

(b) Dem su kien.

(c) Tao toc di? baud cho port noi tiep cua chip 8051.

Timer fl i p flops (31

Flag" flip-flop

-u

~_'

"Flag'

Flag

_____ ______l)

Flag is set on 7-to-O timer ove-rflow

Hinh 4.1 : Mqt bq dinh thai 3-bit (a) so' db logic (b) giiin db thai gian

Timer flipflops : ca c flipflop di nh thai « Flag" flipflop: flipflop co'

Count: s6 de'm

Flag is set on 7 -to-O timer overflow : co' duo'c set khi c6 tra 11 bi? di nh thai ( s6 dern trail W 7 xu6ng 0 )

V6i bi? dinh th(ji~-bit, tang cuoi cung ( tang thli 16 ) chia tan so "Xun~ock d ng6 vao clla bi? dinh th(ji cho 2lb = 65§6.

Chuang 4 : HOI;l t d(lng dinh thO'i

67 ~

Trong cac ling dung dinh tho i trong met khoang thO'i gian, b(l dinh thO'i duoc lap trinh sao cho se tra n sau 1 khoang thci gian qui dinh va set cO' tran cua bi? dinh thci bang' 1. Cd tran dU<1C sa dung M d6ng b(l chuang trinh nham thuc hien mo t c6ng viec nhu la kiem tra trang thai cua cac ngo nhap hoac gdi du Iieu de'n cac ngo xuat. Cac ling dung khac co the sa dung xung clock qui dinh cua b(l dinh tho i M do khoang thO'i gian gitra 2 sl! kien ( thi du do dQ rong xung ).

Viec dern sir kie n duoc dung M xac dinh s6 Ian xuat hieri cua mot su kien han la do thO'i gian giira cac su kien. Tli "su kie n" la mot kich thich ben ngoai cung clip met chuyen trang thai tli 1 xuong 0 tdi m(lt chan cua chip 8051. Cac bi? dinh tho.i cling co the cung clip xung clock t6c di? baud cho port n6i tiep ben trong 8051.

Cac bi? dinh tho i cua 8051 duoc truy xuat bilng each sa dung 6 thanh ghi chlic nang dac biet ( xem bang 4.1 ). V6'i bi? dinh thO'i thu ba cua chip 8052, ta co them 5 thanh ghi chirc nang dac biet nira de truy xuat bi? dinh thO'i nay.

SFR cua bi; M~c dich Dia chi Dinh. d;a
dinh thili chi bit
TCON £)i~u khien 88H C6
TMOD Cl19l1 ehe' di) 89H Kh611g
TLO Byte tha'p cua bi) dinh thci 0 8AH Kh611g
TL1 Byte thfi p cua bq dinh thCii 1 8BH Kh6ng
THO . Byte eao cua bq dinh thai 0 8CH Kh6ng
TH1 Byte eao cua bi) dinh thai 1 8DH Kh6ng
I T2CON £)i~u khien bi) dinh thCii 2 C8H C6
¥6'S RCAP2L Nh411 byte tha' p cua bq dinh thai 2 CAH Kh6ng
I RCAP2H Nh411 byte eao cua bi) di nh thai 2 CBH Kh6ng
TL2 Byte thfip cua b6 di nh thai 2 CCH Kh6ng
TH2 Byte eao cua bi) dinh thai 2 CDH Kh611g Bang 4.1 : Cac thanh ghi chuc na ng da c biet cua bi) dinh thai

4.2 THANH GHI eHE DQ DINH THOI ( TMOD )

Thanh ghi TMOD ( timer mode register) chira hai nh6m 4-bit dung

_______.

d~' thie't lap che' di? hoat di?ng cho bi? dinh thO'i 0 va bi? dinh thO'i 1 ( xem bang 4.2 va 4.3 ). TMOD khong dU<1c dinh dia chi tling bit va di~u nay cling kh6ng can thiet. Mi?t each t6ng quat, TMOD dinrc nap mot Ian bdi

---------------

j

I!!J ••

H~ vi .i~u khi~n •• 51

phan mem d thai di~m bdt d~u cua mot chuong trmh d~ khdi dQng che di? hoat di?ng cua bQ dinh thai. Sau do, bi? dinh thai co th~ duoc dang, bat d~u, V.v ... bang each truy xuat cac thanh ghi chu-c nang d~c bi~t khac

'C'"uab6dinh thai. . - . 0

Bit Ten BQ dinh thui M6 td
7 GATE 1 Bit di~u khi~n clIng. Khi dliQ'C set len 1,
be? dinh thai chi hoat de?ng trong khi
iNTI & muc cao.
6 CIT 1 Bit chon chtrc nang d€!'m hoac dinh thb'i :
1 = Mm su kien
o = dinh thb'i trong me?t khoang thai gian.
5 M1 1 Bit chon ch€!' de? thu nhat, (xem bang 4.3)
4 MO 1 Bit chon ch€!' de? thu hai (xern bang 4.3)
3 GATE 0 Bit di~u kh ien c6ng cho be? dinh thoi 0
2 CIT 0 Bit chon chrrc na ng d€!'m hoac dinh thai
cho be? dinh thai 0
1 M1 0 Bit chen ch€!' de? thu nha t
0 MO 0 Bit cho n ch€!' de? thu hai. Bang 4.2 : Thanh ghi chon ch€!' de? dinh thai

Ml MO css s« M6 td

o 0 0 Ch€!' de? dinh thai 13-bit

o 1 1 Ch€!' de? dinh thai 16-bit

1 0 2 Ch€!' de? tv de?ng nap la i 8-bit

1 1 3 Che' de? dinh thoi chia xe

Be? dinh thai 0 : TLO IiI me?t be? dinh thai 8-bit diroc di~u khi~n b&i ca c bit chen ch€!' de? cua be? dinh thai O. THO, ttrong tv TLO chi khac 13. dlic;tc di~u khi~n b&i ca c bit chon ch€!' de? cua be) dinh thai 1.

Be) dinh thai 1 : dung, khOng hoat de)ng.

Bang 4.3 : Ca c ch€!' de? dinh thai

I

j

Chuang 4 : Hoat dong dinh thci

Thanh ghi TCON chua cac hit,...di~u kbif}u ya trang thai :lla hi? dinh th<'1i ° va hi? dinh thci 1 ( xem hang 4.4 ). B6n hit cao trong TCON ( TCON.4 - TCON.7 ) duoc dung dE-} disu khif}n cac hi? dinh thci hoat di?ng hoac ngung ( TRO, TRI ) hoac dE-} bao cac hi? dinh th<'1i tran (TFO, TFl). Cac hit nay duoc dung ro ng rai trong cac thi du cua chuang nay.

B6n hit tha'p cua TCON ( TCON.O - TCON.3 ) khong dung dE-} di~u khierr cac hi? dinh thoi, chung duoc dung df} phat hien va khdi di?ng cac ngat ngolli. Viec thdo luan v~ cac hit nay duoc hoan la i cho de'n chticmg 6, chuang d~ cap de'n cac ngat.

Bit

TCON.7

TCON.6

TCON.5 TCON.4

V TCON.3

TCON.2

TCON.O

KY hifu Dia chi bit Mo td

TFI 8FH Cfi tran cua b(> dinh thdi 1. Co' nay dU«;fC set bdi phd n cung khi c6 tra n, duo'c x6a bo-i phd n m~m, hoac bdi ph~n cltng khi b6 vi xi11y tro de'n trinh phuc vuJi.g1{t.

t

TRI 8EH Bit dieu khien hoat di)ng cua b(> dinh tho-i 1. Bit nay dU«;fC set hoa c dU«;fC x6a bdi pha n me m M di~u khien bi) dinh thaiho~t d(>ng hay ngung hoa t dQng.

TFO 8DH Cfi tra n cua b(> di nh thai 0

TRO 8CH Bit di~u khien hoat dQng cua bi) dinh tho i O.

IE 1 8BH c:;fi ngil"_t benp.goai 1 ( kich kMi ca nh ). CO~. du~c set bdi phftn cltng khi c6 canh am ( xu6ng ) xuilt hi~n tren chan IN1'J, duQ'c x6a biii phfin m~m, hoac hiin cltn khi

C tro en tnn ph~c v~ ngil"t.

ITI 8AH CO- ngil"t ben ngoa i 1 ( kich khdi canh hoac muc ). CO- nay duo'c set hoa c x6a ~l m~m khi xay ra c~nh am ( xu6ng ) hoac mac thilp t~i chan ngil"t ngoiiI.

lEO 89H ce ngil"t ben ggoai 0 ( kich khiii ca nh ).

ITO 88H Co ngil"t ben ngoai 0 ( kich khci ca nh hoac moe ).

Bang 4.4 : Thanh ghi dieu khien dinh thci TCONI'

~70

H9 vi dieu khien 8051

4.4 CAC CHE D(> DlNH THOI vA co THAN

Tung hI? dinh thai se dtroc d~ cap Mn trong muc nay. Do ta co hai hI? dinh thai tren chip 8051, ky hieu " x " ducc sil' dung M chi hoac hI? dinh tho i 0 hoac hI? dinh thni 1; thi dy THx co nghia la THO hoac THI tuy theo hi,) dinh thoi la 0 hay 1. Su sap xep cac thanh ghi TLx, THx va ca c cd tra n TFx cua hI? dinh thdi duoc trinh hay trong hinh 4.2 eho tung che d(> dinh thui.

Timer I TFx I
clock
Overflow
in) Mud" 0 f1ug
Tilller_1 TLx TUx .j TFx I /1 t l}cJ
clock
Overflow
(al Mode 1 flag
Timer I 1'Fx I
clock
Over now
nag iul Mod,,:2

Timer

dock -- .....

TLx

THx

Timer TLO I-----r;;.l

dock ------. - d

'------' Overflow

flag

1-----6

'-------' Overflow

(a) Mode 3 nag

THO

Hinh 4.2 : Cric che' d(> di nh thai (a) che d(> ° (b) che d(> 1 (c) chii d(> 2 (d) chii

d(> 3

Timer clock: xung clock cho b(> diuh thai Overflow flag: cii tra n

Mode 0, 1, 2. 3 : che'd(> 0, 1, 2, 3 Reload: 111,lP la i

1112 F"".: 1112 tiin so' cua mach dao d(>ng tren chip

l

Chuang 4 : Hoat di?ng dinh thai

71~

4.4.1 Che'do dinh thai 13-bit ( che'do 0) vi·

.. ,-- .

Che di? 0 la che di? dinh thai 13-bit cung cap kha nang tirong thich vdi bi? vi dieu khien t.ie n nhiern 8048. Che di? nay kh6ng diroc dung cho eric thiet ke mdi ( xem hinh 4.2a ). Byte cao cua bi? dinh thai TH?'- duoc ghep cascade vo i 5bit thap cua byte thap cua bi? dinh thai TLx d~ tao thanh mot be'? dinh thai 13-bit. Ba bit cao cua TLx kh6ng sif dung.

4.4.2 Che'dQ djnh ~hai 16_jlit ( che'dQ 1 )

Che di? I la che di? djnh thai I6-bit va co cau hinh gi6ng che di? dinh thai I3-bit, chi khac nhau (1 cM bay gia la be'? dinh thai I6-bit. Xung clock dat vao cac thanh ghi dinh thai cao va thap ket ho p ( TLxlTHx ). Khi co xung clock den, bi? dinh thai <iem len : OOOOH, OOOIH, 0002H, .... Mi?t tran se xuat hien khi co su' chuyeri so' dern tir FFFFH xu6ng OOOOH. sir kien nay se set co' tran b~n_g} va bi? dinh thai tiep tuc demo Co' tran la bit TFx trongihanh ghi dieu khien dinh thai TCON, bit nay ducc doc hoac ghi bdi phan mern ( xem hinh 4.2.b ).

Bit co y nghia 1<1n nhat ( MSB ) cua gia tri trong cac thanh ghi diuh thai la bit 7 cua THx va bit co y nghra thap nhat ( LSB ) la bit 0 cua TLx. Bit LSB thay d6'i tra ng thai va chia 2 tan so' xung clock dinh thai d ngo va o trong khi bit MSB thay doi trang thai va chia cho 65536 ( Wc la 2lfi ) tan s6-;_ung clock dinh thai d ngo vao. Cac thanh ghi dinh thai ( TLx / THx ) co th~ duoc doc hoac ghi (1 mot thai diem bat ky bdi phan mern,

4.4.3 Che'dQ tt! ria p Ia i 8·bit ( che' dQ 2) - .TH?<...

I

Che de? 2 la che de? tv nap lai 8-bit. Byte th~/p cua b6 dinh thai ( TLx ) ho~t de?ng dinh thai 8-bit trong khi by~e cad Clla b6 dinh thai lu'u giu gia tri n~p Iai. Khi so' dem trim tli FFH xuong OOH, kh6ng chi co' tra n cua bi? dinh thai duqc set len I rna gia tri trong THx con duac nap va o

~ ; viec dern ·se tiep tuc tli gia tri nay cho den khi xay ra I tran ( FFH ~ OOH ) ke tiep, v.v ... Che de? nay kha tie n lo i do bdi viec tra n be'?

.---- --

dinh thai xay ra a nhirng khoang thai gian xac dinh va twin hoa n mot

khi ca c thanh ghi TMOD va THx da diroc khdi dong ( xem hinh 4.2c ).

4.4.4 Che' dQ d inh thai chia xe ( che' dQ 3 )

Che de? 3 la che' de'? dinh thai chia xe va co hoa t dong khac nhau cho tirng be? dinh thai. Be? dinh thai 0 a che de'? 3 duqc chia thanh 2 bi? d1nh thai 8-bit ho~t de?ng rieng re TLO va THO, mci bi? dinh thai se set eric co' '"trllrrtVong dng 'I'F'O va TFI khi xay ra tra n.

Bi? dinh thai I kh6ng ho~ t dong a che de'? 3 nhung co the' duo c kho i dong ba ng cachrchuy~n be'? din'll thai nay vao mot trong cac che dQ khac.

~72

H9 vi dieu khien 8051

Gidi han duy nhat la eel tra n TFI cua hi? dinh thai 1 khong hi anh hudng bdi hi? dinh thfri 1 khi hq nay xay ra tran vi TFI du<?,c n6i vdi hi? dinh thai 8-hit THO.

Cl),€ do 3 chu ye""u cung cap them mi?t hi? dinh thai 8 hit mia ; nghla la ~051 co them hi? dinh thai tha ba. Khi bi? dinh thai 0 ci' che"" di? 3, hi? dinh thdi 1 co th~ hoat don hol)c ngttng h~ng cach chuygn bi? nay ra kl10i che"" dq 3 hol)c vao che"" di? 3. Bi? dinh thai 1 co t e duoc Stl dung hci'i port n6i_tie""p ( hie nay hi? dinh thai 1 lam nhie m vu cua hi? tao xung ~c di? haud ) hoac du<?,c str dung theo mot each nao d6 nhirng

~g yeu du ngdt ( vi hi? dinh thai 1 hie nay khong con n6i v6'i TFI ).

4.5 NGUON XUNG CLOCK DINH THOI

Hinh 4.2 khong trinh bay each tao ra ngucn xung clock dinh thai cho cac hi? dinh thai. C6 2 kha nang tao ra nguon xung clock nay, viec hra chon kha nang nao do ta thie""t l:%p hit CIT" ( counter I timer) cua thanh ghi TMOD bang 1 hay 0 khi bi? dinh thai du<?,c khdi di?ng. Mi?t nguon xung clock diroc dung M dinh thai trong mot khoang thai gian, nguon xung clock con lai du<?,c dung dg de""m sir kien.

~.5.1 Djnh thai mQt khoang thai gian

Neu CIT" = 0, hoat di?ng dinh thai du<?,c chon va nguon xung clock cua hi? dinh thai do mach dao di?ng ben trong chip t~o ra. Mi?t mach chia 12 tang duoc them v~o M giarn t:~n sO' xung clock de""n' mo t gia trj thich hop vdi hau he""t cac ang dung. Luc nay hi? dinh thai du<?,c dung M dinh thdi trong mot khoang thai gian. Cac thanh ghi dinh thai ( TLx I THx ) de""m len vdi tan s6 xung clock bang 1112 tan sO' cua mach dao d(mg tre n chip [ nghia la neu thach anh la 12MHz, tan sO' xung clock la IMHz ]. Be dinh thai se tran sau mot sO' xung clock cO' dinh phu thuoc vao gia tr]

/ ban dau nap cho cac thanh ghi dinh thai ( TLx I THx ).

~ 4.5.2 De'm sq ki~ n

Neu CIT" = 1, bi? dinh thai du<?,c cung cap xung clock ttt 1 ngu6n tao xung hen ngoa{ Trong da sO' cac irng dung, ngudn xung clock nay cung cip cho hi? dinh thai mo t xung dua tren viec xay ra mot sI! ki~n - hi? dinh thai bay gia de-m sl! kien, SO' cac su' kien diroc xac dinh trong phan mem bang each doc cac tanh ghi dinh thai ( TLx I THx ), gia tri 16-hit trong cac thanh ghi nay tang thea moi su kien. Hai chan cua port 3 ( P3A va P3.S ) bay gia trci' thanh ngo vao xung clock cho cac hi? dinh thai. Chan P3.4 la ngo vao xung clock cho hi? dinh thai 0 ( ta con go i la chan TO ci' ngu canh nay), chan P3.5 hoac Tl la ngo vao xung clock cho hi? dinh thai 1 ( xem hinh 4.3 ).

I

Chuang 4 : Heat do ng dinh thci

73 ~

On-cbij. osril lntur

----1---+ Timor clock

TO or 1'1 ---'

o ; Up (interval timing) 1; Down (event counting)

!Jill

CIT f-------------'

Hinh 4.3 : Ngudn xung clock Crystal: tinh th~ thach anh

On-chip oscillator: bi? dao di?ng ben trong chip TO or Tl pin: chan TO hoac Tl

o = Up ( interval timing) : 0 = vi tri tren ( dinh thai mot khoiing thai gian ) 1 = Down ( event counting) : 1 = vi tri duci ( de'm su kien )

Timer clock: xung clock di nh thai

Trong cac ling dung dern su kien, cac thanh ghi dinh thO'i tang moi khi xay ra ehuygn trang thai tit 1 xu6ng 0 !j ngo vao Tx ( TO ho~e T1 ). Ngo vao Tx dinrc la'y mau.trong suot thO'i gian S5P2 ella m6i mi?t ehu ky ~ vay thi khi ngo vao i1 mire eao trong mot ehu ky va mlie tha'p trong e~, so' dem duqe tang. Gia tri mo i xuat hien trong cac thanh ghi dinh thO'i trong sufit thO'i gian S3P1 cua ehu ky tiep theo ehu ky pha t hien su chuyen trang thai. Ta do ta tha'y phai ma't 2 ehu ky may ( 2~lS ) M nh~n hiet sl! ehuygn trang thai tit 1 xuong 0, tan so' cue dai eua ngubn xung clock ben ngoai la 500 KHz ( vdi gia sit chip vi dieu khierr ~t d~ng::!61 th~eh anh 12 MHz ).

4.6 KHdI DQNG, DUNG vA DIEU KHIEN cAc BQ DINH THOI

Hinh 4.2 minh hoa cac cau hmh khac nhau ella cac thanh ghi dinh thO'i TLx, THx va cac cd tran ella hi? dinh tho i, TFx. Hai khd nang eung cap xung clock eho hi? dinh thO'i eho i1 hinh 4.3. Bay giO' chung ta khao sat viec khdi dong, dang va dieu khie n cac hi? dinh thrri.

Cach dan gian nhat M khdi dong va dang cac hi? dinh thci la Stl' dung hit dieu khien hoat dong' TRx trong thanh ghi TCON. TRx dircc x6a khi .thiet lap la i h~ thong ; nghia la cac hi? dinh tho i ngung hoat dong. ( xem hinh 4.4 ).

Do thanh ghi TCON la thanh ghi diroc dinh dia chi titng hit, ta de dang khoi dcng' hoac dang cac hi? dinh thO'i bang chuang trinh.

~74

H<;> vi dieu khierr 8051

'I'imer

clock ---+- ....

__ -+ __ .. Timer regist.ers

o = Up (timer stopped)

1 = Down (time!' started)

Htnh 4.4 : Bdt dfiu va dang ca c bi? dinh thai Timer clock: xung clock dinh thai

Timer registers: cac thanh ghi dinh thai

o = Up ( timer stopped) : 0 = vi tri trsn ( hi? dinh thai dang)

1 = Down ( timer started) : 1 = V! tri dLJ6"i ( bi? dinh thai dLJQ'e khdi di?ng )

Thi du bi? dinh thai 0 diroc khdi di?ng b!ing lenh :

SETB TRO

va diroc dieu khien dang bang' lenh :

CLR TRO

Trinh dich hop ngtr sethuc hien viec bien d6i ky hieu " TRO " thanh dia chi bit. Lenh SETB TROd6ng nghia vrri lenh SETB 8CH.

Mi?t phmrng phap khac de di~u khien cac bi? dinh thai la sa dl,mg bit GATE trong thanh ghi TMOD va ngo vao INTx. Bling each set bit

i"' .,----- .. -----------

GATE len 1 ta cho phep bi? dinh thai du<;>,c di~u khien bdi INTx. Phuong

pha p nay thinrng diroc dung de d~ di? rqng xung nhu du<:>,c trinh bay sau day.

GiS. sa INTO a rmrc th§p r6i chuyen sang mire cao trong mQt khoa ng thai gian va ta do khoang thai gian nay. Ta khdi dQng bi? dinh thai 0 d che dQ 2, che' dQ dinh thai 16-bit vdi TLOfI'HO = OOOOH, GATE = 1 va TRO = 1. Khi INTO chuyen len mire cao, bQ dinh thai du<;>,c md c6'ng va nhan xung clock co tan s6 IMHz. Khi INTO chuyen xu6ng mire thap, bi? dinh thai bi khria c6'ng khong nhan xung clock nira va di? rong xung tinh bang us la s6 de'm trong TLO / THO ( INTO co th~ du<;>,c l~p trinh d~ tao ra 1 ngl'it khi chan nay chuyen trd v~ rmrc th§p ).

Hinh 4.5 minh hoa bi? dinh thai 1 hoat di?ng d che' di? 1 ( bi? dinh thai 16-bit ). Cling vdi cac thanh ghi TLlfI'Hl va co' tran TFl, so' d6 d hinh 4.5 trinh bay cac kha nang cap nguon xung clock, khdi di?ng, dang va di~u khieri bi? dinh thai 1.

Chuong 4 : Hoa t di?ng dinh thai

75 ~

8051

r-T~r--.--l--+-· ... _!--., TL1 I TH1 ~'TF11

(16 bits)

0= Up

1 = Down

(P3.3)

Hmh 4.5 : Hoat dQng ache' dQ 1 cua bQ dinh thai 1 On-chip osc : mach dao dQng ben trong chip

4.7 KH<11 D<)NG vA TRUY XUAT cAc THANH GHI DINH THCH

Cac hi? dinh thai thuO'ng diroc khdi dQng mot Ian 6' thai digm hilt ddu chuong trinh dg thiet l~p che dQ hoat dQng theo yeu cau. Trong than cua chuong trinh, cac hQ dinh thai diroc dieu khien hoat dQng, dang, kiem tra cac hit cava x6a, cac thanh ghi dinh thai dinrc d9C hoac cap nhat va V.v ... tuy theo yeu cdu ciia ang dung,

TMOD la thanh ghi diroc khdi dQng trudc tien vi day la thanh ghi thiet lap che dQ hoat dQng. Lay thi du lenh sau day khdi dQng hi? dinh thai 1 hoat dQng 6' che di? 16-hit ( che dQ 1 ), xung clock dU<;1C cap tit mach dao dQng tren chip ( dinh thai mi?t khoang thai gian ) :

MOV TMOD, #OOOlOOOOB

Ket qua cua lenh nay la thiet l~p M1 = 0 va MO = 1 M an dinh che' di?l, CIT = 0 va GATE = 0 dgs!1 dung xung clock tren chip, x6a cac hit chon che dQ cua hi? dinh thai 0 ( xem hang 4.2 ). DI nhien tren thuc te hQ dinh thai khong hiitddu cong viec dinh thai cho den khi hit dieu khien hoat dQng TR1 dU<;1c set bang 1.

,

Trong truO'ng hop cdn den s6 dem han ddu, cac thanh ghi dinh thai TL1!TH1 cling phai dU<;1c khdi d~ng. Can nhd Ia cac hi? dinh thdi dem len va thiet 14p cd tran bang 1 khi xay ra tran s6 dem ta FFFFH xu6ng OOOOH,v~y thi mot khoan thai ian 100 IlS c6 thg dU<;1c dinh thdi hling. cach khdi dQng T l!TH1 chaa so:. dem n ° hO'n OOOOH mot lUo'n~.1a 100, 'nghia la -100 hay FF9CH. Caclenh sau thuc hien di~u nay:

~76

HC? vi di~u khien 8051

'j'MOV TLl,#9CH

. . MOV THl, #OFFH

Ke den' bi? dinh thO'i b~t d§u hoat dong bang each thiet ll%p bit dieu khi~l heat di?ng bang' 1 nhu sau :

, SETB TRI

Cd tran duqc tv d<;mg thi€t ll%p sau khoang thai gian 100 !J.S. Phan mern. c6 thg chua mot vong lap tri hoan thO'i' gian 100 !J.S bang' each sit dung mot lenh re nhanh va lap la i chinh lenh nay trong khi cd tran chua duoc set biing 1 :

WAIT: JNB TFl, WAIT

Khi hq dinh thai tran, ta can dung bq dinh thai va xO'a cd tran h~ng

Mn~ •

CLR TRI

CLR TFI

; dung hi? dinh thai 1

; x6a co' t.ra n

Dc;>c be} djnh thai dang Iroa t de}ng

Trong mot so ling dung ta can phai doc gia tr] ( nqi dung) chua trong cac thanh ghi dinh thO'i dang hoa t dqng.

Do ta phai doc 2 thanh ghi dinh thO'i bang 2 dong lenh lien tiep ( do khong co lenh doc dang thai ca hai thanhghi dinh thO'i nay), mot sai pha ( phase error) co thg xUllt hi~n neu co tn~n 'te than chuy~n sa~iii'a 2 1 . n doc va 0 vay ta khong thg doc dung duqc gia tri Cdn doc. Gia i phap dua ra la trudc tien ta pha i doc byte cao,ke den doc byte thap va r6i doc byte cao Ian nira. Neu byte cao thay d6i gia trj, ta lap lai cac thao tac doc vita neu. Cac lenh sau day doc nci dung cua cac thanhghi dinh thO'i TL1 I TH1, dira vao cac thanh ghi li6 / R7 va gia i quyet va'n d~ vita neu :

AGAIN:

MOV A, THI

MOV R6, TLI

CJNE A, THl, AGAIN

MOV R7, A

4.8 KHOANG THOI GIAN NGAN vA KHOANG THOI GIAN nAI

Tam nao cila cac khoa ng thai gian co thg dinh thO'i duqc ? Va'n <:1~ nay duqc khdo sat b~ng each gia sit 8051 hoat dong v6'i thach anh noi vci mach dao dqng nqi co t§n so hoat dqng 1a 12 MHz. Mach dao dqng tren chip duqc chia cho 12 va cac xung cl;ck cap cho mach dinh thO'i cD

Chuong 4 : Heat dQng dinh tho-i

77~

I

!

tI~n s6 1 MHz. Khoang thei gianngan nhat co thg dinh tho-i dttqc hi gidi han khOng pha i hai t§n s6 cua xung clock dinh tho-i rna bdi phan mern nghia la tho-i gian thuc thi cac l~nh tao ra giai han d6i vdi cac khoang tho-i gian dinh tho-i ra't ngan. Lenh nglln nha't clla 8051 t.huc hien trong

~

me?t chu ky may hay ~Bang 4.5 tom tilt cac ky thuat dttqc dung dg

~o ra cac khoang thdi gian dinh thdi khac nhau.

Khodng thiJi gian

'" 10 256 65536

khoug gi6"i han

KY thutit

£)i~u ehinh phd n m~m V

BQ dinh thai 8-bit tv dQng nap lai. BQ dinh thai 16-bit ../

BQ dinh thai 16 bit + cac vong lap

pi

Bang 4.5 : Khoang thai gian dinh thai eve dai ( us ) Thi du 4.1 : Tao dang xung

Vitt 1 chuang trinh. tao dang xung twin hoan. tren. chan Pl.O c6 tan 86 cao c6 thtc6 duoc. Tan 86 va chu ky nhiem uu ctia dqng xung la bao nhieu ?

Cac khoang thei gian ra't ngdn ( co nghla la t§n s6 cao ) co thg ducc l~p trinh rna kh6ng can sil dung den cac he? -dinh tho i. Thi du chuong trinh sau:

ORG 8100H

LOOP: SETB P1.0

CLR P1.0·

SJMP LOOp· END

; 1 ehu ky may ; 1 ehu ky may ; 2 ehu ky may

Chuong trinh tren tao ra dang xung tren chan P1.0, xung co chu ky la 4 us : thci gian mac cao la 1 us va thci gian mac tha'p la 3 us trong met chu ky. T§n so cua xung la 250 KHz va chu ky nhiern VI,I la 25% (xem hinh 4.6)

ISETB pl.OI CLR Pl.O I

1-----------4~s-----------

SJMP loop

ISETB pl.OI etc.

Pl.O

I_One machine cycle (Lus)

Hinh 4.6 : Dang xung cua thi du 4.1

j

~78

H9 vi dieu khi~n 8051

SJMP loop: vong lap' SJMP

One machine cycle ( 1 us ) : mot chu ky may ( Ius )

Lenh SETB P1.0 thuc te' kh6ng set bit 0 ella port 1 len 1 eho de'n khi ke't thuc le nh nay, trong thoi gian S6P2, va cac lenh tie'p theo cling tirong ttf. Chu ky ella tin hieu ngo ra co th~ keo dai them mot it bang erich chen cac lenh NOP ( khong toan hang ) vao trong vong lap . ..MQL lenh NOP du~e chen them lam eho u ky ella tin hi~u nora uae con

t em 1 IlS. Thi du neu ta chen 2 lerih NOP sau l~nh SETB P1.0, chuang t:iliih se tao (1 ngo ra mot xung vuong co ehu ky 6 IlS va t~n so la 166.7 KHz. Tuy vay ta cdn tha'y rang viec dieu ehinh -ph§n mern nhu tren se tro nen e6ng kenh va vung v~, each lira chon tot nhat de tri hoan van la sti dung bi? dinh tho i.

Cac khoang thci gian dai vira phai d~ dang nhan duoc biing each sii dung ehe' di? ttf nap Iai 8-bit, che di? 2. Do cac khoang thci gian can du~c dinh tho.i dinrc thie't lap bo-i mot so dern 8-bit, khoang tho i gian dai nhat co th~ co trtrdc khi tra n la 28 = 256 ~LS.

'I'hi du 4.2 : Song vuorig 10 KHz

Viet 1 chuang trinh. tao song uu6ng 10 KHz tren chan P1.0 bang each Sit dung bf) dinh. thai O.

Song vuong 10 KHz yeu cdu ehu ky 100 IlS vdi thoi gian mire eao la 50 IlS va thdi gian rmrc tha'p la 50 IlS. Do khoa ng thdi gian nay nho han 256 us ne n che di? 2 diroc sii dung. Mi?t tran xay ra sau m8i 50 IlS yeu du mot gia tri so dern nhO han OOH mi?t lucng' la +50 pha i ducc nap va nap l~i eho TLO, nghia la gia tri nap eho THO la -50. DUc1i day la chuong trinh theo yeu e§u :

ORG 8100H
MOV TMOD, #02H ; che' d(J tv nap la i 8-bit
MOV THO, #-50H ; THO clnra gia tri -50
SETB TRO ; bi? dinh thai hoat di?ng
LOOP: JNB TFO, LOOP ; cho tra n 1:1
CLR TFO ; x6a Co' tra n
CPL P1.0 ; d5i tra ng thai bit P1.0
SJMP LOOP ; I*Ji la i
END Chuang trinh tren sii dung lerih lay bu bit CPL thay vi la l~nh S~!3 l . . va CLR nhu trong thi du 4.1. Giira hai thao tac la'y bu e6 mot tri hoan

~-

L .

Chirong 4 : Hoa t di?ng dinh thci

83 ~

1

Bit KY hifU tu« chi bit M6 tii

T2CON.7 TF2 CFH CO' tra n cua be? dinh thdi 2. (khong dl1q"c set khi TCLK hoac RCLK = 1)

T2CON.6 EXF2 CEH CO' ngoa i cua bi? dinh thdi 2. CO' dl1q"c set khi c6 su thu nha n hoac na p la i xay ra do bdi su chuyen trang thai 1 ~ 0 (j chan T2EX va EXEN2 = 1 ; khi ca e ngl1t do bi? dinh the i duoc phe p, EXF2 = 1 lam cho CPU tro Mi trlnh phuc vu ngdt. EXF2dltq"c x6a bdi phd n m~m.

T2CON.5 RCLK CDH Clock thucua bi? dinh the i 2. Khi duoc set, bi? dinh tho i 2 cung dip t6c di? baud (khi thu) eho port n6i tie' p; bi? dinh thoi 1 eung clip toc di? baud (khi pha t).

Clock pha t cua be? di nh tho i 2. Khi duoc set, bi? dinh tho i 2 cung clip toc di? baud phat; bi? dinh tho'i 1 cung clip toe do baud thu.

T2CON.4 TCLK

CCH

T2CON.3 EXEN2

CBH

Cho phep W ben ngoa i, Khi duoc set, viec thu nhan va na p lai xua t hie n khi c6 st! chuyen tra ng thai 1 ~ 0 (j chan T2EX.

T2CON.2 TR2

CAH

Bit di~u khi~.n hoa t do ng' bi? diuh tho i 2. Bit Hay duoc set hay x6a b(ji pha n m~m M di~u kh ien bi? diuh thoi 2 hoa t di?ng hoa c ngung, _

Bit cho n chuc na ng dern hoa c di nh tho-i cua bi? di nh thci 2. 1 = dern su kie n; 0 = dinh tho-i mot, khod ng thO'i gian.

Cd thu nha n/na p la i cua bi? di nh thoi 2. Khi duoc set, viec thu nha n xulit hien khi e6 su chuyan tra ng thai 1 ~ 0 (j T2EX neu EXEN2 = 1; khi dl1q"c xoa , tv di?ng nl.lp la i xua't hien khi c6 tra n bi? dinh thoi hoac su chuye n tra ng thai 0' T2EX neu EXEN2 = 1 ; ne'u RCLK hoac TCLK = 1, bit nayducc bO qua.

T2CON.l CIT 2 C9H

T2CON.O CPIRL2C C8H

Bang 4.6 : Thanh ghi dj~u khien di nh thoi T2CON

..-.- .• --~. ------------------

~84

H<,> vi dieu khien 8051 .

Viec nap lai xay ra khi co trim s6 dern tU FFFFH xuong OOOOH i!J TL2ITH2va thie't lap cd TF2 bang 1. Di~u kien nay dugc xac dinh boi phan mern hoac dinrc l~p trinh M tao ra mot ngat, Vdi ca 2 each vira neu, TF2 phai dugc xoa bai phan mern trudc khi cd nay dinrc set Mn ntra.

Bang each set bit EXEN2 trong thanh ghi T2CON bang 1, viec nap lai ding xwlt hien khi co sV chuyen trang thai 1 -)- 0 cua tin hieu d*t vao chan T2EX ( chan Pl.l ). Su chuyen trang thai 1 -)- 0 i!J T2EX ding thie't lap bang 1 cho mot bit cd mdi trong bi? dinh thdi 2 : bit EXF2. Cling vdi TF2, bit EXF2 cling duoc kiem tra bdi phan mern hoac tao ra mot ngat. EXF2 phai dinrc xoa bi!Ji phan mern, Che di? tv nap lai cua bi? dinh thoi 2 duoc trmh bay a hmh 4.9.

4.9.2 Che' de} thu nhan

Khi CP/RL2 = 1, che' di? thu nhan dinrc chon. Bi? dinh thai 2 hoat di?ng nhu mi?t bi? djnh thoi 16-bit va thie't lap cd TF2 bang' 1 khi xay ra tran s6 dern tli FFFFH xu6ng OOOOH i!J TL2ITH2. Trang thai cua TF2 dinrc kiern tra bdi phan mern hoac tao ra 1 ngat.

D~ cho phep che' di? nay hoat di?ng, bit EXEN2 trong T2CON phai dugc set bang' .1. Neu EXEN2 = 1, su chuyen trang thai 1 -)- 0 i!J T2EX ( Pl.l ) se dua gia trj trong cac thanh ghi dinh thdi TL2 I TH2 vao trong cac thanh ghi RCAP2L va RCAP2H. Viec nap gia tri nay dugc dieu khien bi!Ji xung clock. Cd EXF2 trong T2CON cling dime set va nhu da d~ cap i!J phan tren, du<!c kigm tra bdi phan mem hoac tao ra mot ngat,

Che' de? thu nhan cua bi? dinh tho i 2 duoc trmh bay a hmh 4.10. 4.10 T~O TOC DQ BAUD

Mi?t ling dung khac cua bi? dinh tho i la cung cap xung clock t6c di? baud cho port n6i tiep cua chip vi dieu khien. Bi? dinh thdi 1 i!J 8051 llQt,Lc bQ dinh thdj 1, bQ dinh tht¥i-2 a 8052 thuc hi~n dudC cong viec nay. TIilO t6e di? baud se d~ cap den trong chuong 5.

Trong chuong nay ta da khao sat cac bi? dinh thoi cua cac bi? vi dieu khien 8051 va 8052. Cac gia i phap phan mern cho cac thi du lam n6i bat net chung nhirng bi gidi han. Cac giai phap nay lam mat nhieu thai gian cua CPU. Cac chuong trmh thuc thi cac vong lap de cho bi? dinh thci tran. Di~u nay t6t cho Cac muc dich nghien cuu hoc hoi nhirng d6i vci cac ling dung hudng dieu khien thuc te' sa dung cac bi? vi dieu khien, CPU phai thuc hien cac nhiern vu khac cling nhu phai dap ling vdi cac sV kien ben ngoai ( nhu la ngtnri di~u khien dira vao met tham s6 tU ban phim chiing, han ). Trong chuong khao sa t hoa t ,di?ng nga: t, ta se minh

..

Chinrng 4 : Hoat di?ng dinh thai

85 l!J

hoa viec sit dung cac hi? dinh thai trong mo i trtiang diroc dieu khi~n ngat.

,...---

Ta khong can phai kiern tra co' trim cua hi? dinh thai bang' met vong

lap rna se 40 ra mo t ngdt khi co tran hi? dinh thai. .

8052

0= Up

1 = Down

r-------------~

0= Up

1 = Down

t------- ....

Hinh 4.9 : Che'di? tv nQ.p IQ.i 16-bit cua bi? dinh tho·i 2

8052

0= Up

1 = Down

0= Up

1 = Down

T2EX--~--------------~~·,~-+--------~--------------·I~XF21 (P1.l)

0= Up h--------------' 1 = Dowri

Hmh 4.10 : Che' d(l thu nha n 16-bit cua bi? dinh thai 2

,

I

I

\.

~86

HI? vi di~u khi~n 8051

On-chip osc ; bQ dao dQng ben trong chip Reload ; na p Il;I i

Capture; thu nha n

o = Up ; n'e'u bit di~u khi~n 1ft 0, chuyen mach fJ vitri tren

1 = Down; ne'u bit di~u khi~n 1ft 1, chuyen mach fJ V! tri dll6'i

1

t t I f

~

17 I!l

5

Ben trong chip '.51 co mot port n6i tiep heat dQng d mo t vai che' dQ tren mot tilm Mn so' rong. Chuc nang co ban cua port n6i tiep l~__tlu[c hien vi~c chuygn dOi di1 lieu song song thanh n6i tiep khi phat· vQ. chuyen d6i di1 lieu n6i tiep thanh song song khi thu.

Cac mach phan cirng ben ngoai truy xua t port n6i tiep thong qua cac chan TxD ( pha t di1 li~p ) va RxD ( thu di1 lieu ) da diroc gidi thieu d chuong 2. Cac chan nay da h9'P vdi hai chan cua port 3 : ~ P3.0 (RxD).

£)?c trirng cua port n6i tiep la hoat dQng song cong ( full duplex ), nghia la co k!Ia nang thu va phat d6ng thO'i. Ngoai ra port n6i tiep con co mot dac trimg khac, viec dern di1 lieu khi thu cua port nay cho phep mot ky tv dll9'c nht%n va htu gii1 trong bQ d~m thll trong khi ky tv tiep theo dll9'c nht%n vao. Neu CPU doc ky tv thu nhat truce khi ky tv thu hai dll9'c nhan day du, dir Iieu se khong bi melt.

Philn mern sli dung hai thanh ghi chirc nang dac bi~t SBUF va

. -

SCON d~ truy xuat port n6i tiep. BQ d~m cua port noi tiep ~ co dia

chi byte la_~, tren thuc te bao g6m hai bQ d~m. Viec ghi len SBUF se nap du lieu M phat va viec doc SBUF se truy xWlt dir lieu da nhan diroc. £)i~u nay co nghta la ta co hai thanh ghi rieng re va phan bi~t :. thanh ghi phat ( chi ghi ) hay bQ d~m phat va thanh ghi thu ( chi doc ) hay bo dern thu ( xem hinh 5.1 ).

Thanh ghi dieu khi~n port n6i tiep SCON ( co dia chi byte la 98H ) la thanh ghi duoc dinh dja chi tirng' bit, thanh ghi nay chua cac bit tra ng thai va cac bit di~u khien, Cac bi.i_ di~u khi~n se tbiet lap che do hoa.! dong cho po~ tjP'p COIl dc bit tr~Dg thai chi ra sll ket thuc viec thu_

.

~88

H9 vi dieu khi~n 8051

hO~£_l)hIH mot ky tv Cac bit trang thai dune kiern tra bo i phan mern

---- '"

hoa c duoc l~ p trinh de ta 0 ra nga t.

Tall so hoat dong cua port noi tiep, hay eon go i la toe dQ baud ( baud rate ) eo the eo' dinh hoac thay d6i. Khi toe dQ baud thay d6i ducc sit dung, bi? dinh thai 1 eung cap xung clock to'e di? baud va ta pha i lap trmh sao cho phil hcp. T're n 8032/8052, bi? dinh thai 2 ding eo the ducc la p t.rinh M cung dip xung clock toe di? baud.

TXD RXD

IP3.1) (I':J.O I

L
D
CLK 8BUF Q I--- 8h1l1 regrster
I (write-only)
I CLK
A ~
Baud rate Baue! rut.e
clock clock
(transmit) (receive)
8BUF
(read-only I
l
{ 8051 In t.ernul bus 1 Hinh 5.1 : SCi db kh6i cua port 116i tie'p

Write only: chi ghi Read only: chi do e

Shift register: thanh ghi dich bit

Baud rate clock ( transmit) : xung clock t6c di? baud ( phat ) Baud rat~ clock ( receive) : xung clock t6c di? baud ( thu ) 8051 internal bus : bus nqi cua 8051

5.2 THANH GHI DI~U KH[EN PORT NOI TIEP

Che di? hoat di?ng cua port noi tiep duoc thiat lap bang each ghi tli dieu khian len thanh ghi chon ehe di? SCON cua port noi tiep (j dia chi byte 99H ( xem bang 5.1 va 5.2 ).

Trude khi sit dung port noi tiep, thanh ghi SCON pha i diroc kho'i dong dung che' di? yeu duo Thf du Ienh sau :

MOV SCON, #01010010B

Chuong 5 : Hoat dQng cua port ndi tie'p

89 ~

khdi dQIlg port noi tiep d che dQ 1 ( SMOISM1 = Oil ), cho phep thu ( REN = 1 ) va set Cd ngat phat bang' 1 ( TI = 1 ) dg chi ra rling port noi tie'p sdn sang phat dli lieu.

5.3 cAc CHE DQ HO~ T DQNG

Port ndi tie'p cua 8051 co 4.£bi dQ hoat dong, cac che' dQ dU<;1c chon bang each ghi 1 hoac 0 cho cac bit SMO va SM1 trong thanh ghi SCON. Ba trong so cac che dQ hoat dQng cho phep tru:¥:~n khong d6ng bQ ( asynchronous ), trong do m6i mQt ky tu dudC thuhOac dudC phat se cung vdi mQt bit start va mQt bit' stop t~ thanh mot khung ( frame ) .

. ~ ----

Neu dll lam quen vdi hoat dong cua port ndi tiep theo chuan RS- 232C treri mo t may vi tinh, ta se tim thay dU<;1c su tirong ttl d cac che dQ nay. V6i che dQ thO' tu, port noi tiep hoat dQng nhu mot thanh ghi dich bit dan ginn. M6i mot che' dQ se dU<;1c d~ cap tom bit sau day.

Bit Ky hi~u Dja chi

SCON.7 SMO 9FH

SCON.6 SM1 9EH

SCON.5 SM2 9DH

SCON.4 REN

9CH

SCON.3 TB8

9BH

SCON.2 RB8

SCON.1 TI

9AH 99H

SCON.O RI

98H

Me') hi

bit 0 chon che' dq cua port nbi tie'p

bit 1 chon che'dq cua port nbi tie'p

bit 2 chon che' docua port nbi tie'p. Bit nay cho phep truyen thOng da xi'! Ij dcac che' dQ 2 va 3; bit RI se kho ng duQ'c tich eire neu bit th(( 9 nha n duoc la O.

cho phep thu. Bit nay pha i duoc set M nhan cac kj tv.

bit phat 8. Bit th(( 9 duQ'c phat d

ca c che' dQ 2 va 3; duQ'c set va x6a bdi phan mern

bit thu 8. Bit th(( 9 nhan duoc.

cd ngdt pha t, Cd nay duoc set ngay khi ke't thuc vi~c phat mQt ky tv ; duQ'c x6a bdi pha n mem

I Cd ngdt thu. ce nay duoc set ngay khi ke't thuc viec thu mQt ky tv ; duQ'c x6a Mi pha n mern

Bang 5.1: T6m tdt thanh ghi SCON ( di€u khi€n port nbi tiep )

~90

H<.> vi dieu khien 8051

5.3.1 Thanh ghi djch 8-bit ( che' de} 0 )

Che. di? 0, duoc chon bang each ghi gia tri 0 vao cac bit SMQ__va SMl trong thanh ghi SCON, dat port n6i tiep vao"'che di? thanh ghi dich 8-bit. Dl1 lieu noi tiep <\l{,O'c 1:!!..u va P.E.at thong qua chan RxD, chan TxD xuat xung clock dich bit. Khi phat va thu dtr Iieu 8-bit, bit co y nghia ( gia trj vi tri ) nho nhat ( bit LSB ) diroc thu hoac phat trudc tien. T6c do baud c6 dinh va bang 1112 t§n s6 cua mach dao dQng tren chip. Cac thua t ngu " RxD " va " TxD " bi sai lech y nghra trong che di? nay. Chan !UP duO'c stl' dl:lng cho cli thu 3-a phat_dl1 li~u con c~ d!tQ'c dung lam chan i"ua't xung clock dich bit.

8MO 8Ml Che'de'? Mo ta To'c de'? baud
0 0 0 Thanh ghi dich C6 di nh ( tan 56 dao do ng' / 12 )
0 1 1 UART 8·bit Thay d6i ( thie't Iij. p b6"i bo di nh
thoi )
1 0 2 UART 9-bit C6 di nh ( tfin 56 dao do ng / 12
hoa c / 64 )
1 1 3 UART 9-bit Thay deli ( thie't lap boi bq dinh
thoi )
Bang 5.2 : Cac che' dq cua port 116i tie'p Viec phat du lieu dU<;1c khdi dong Mng mQt l~nh ghi dil lieu vao

-

SBUF. Du lieu duoc dich ra ngoai tren chan RxD ( P3.0 ) vo i cac xung

flock dfch bit dU<;1c go-i ra tren chan TxD ( P3.1 ). Moi mi?t bit h<;1p l~ duoc truy~n di tren duo-ng R_2ill trong mot chu ky may.

(

Trong mdi mot chu ky may, xung clock dich bit d6i thanh mire thap 0-

S3Pl va trd Iai mire cao 0- S6P!.

Gian d6 tho i gian phat du Iieu duoc trinh bay C1 hinh 5.2.

Viec thu dil lieu duoc khci dong khi bit cho phep thu REN C1 logic 1 va cO- ngat thu RIO-logic O. Qui lua t t6ng quat la ta pha i set bit REN bang 1 C1 tho-i di~m biH dau chuong trinh M khdi dong port n6i tiep va

~

sau do xoa bit RI dti Mt. d§u cong viec thu du lieu,

-~ -~~---"

Khi bit RI duoc xoa, cac xung clock dich bit dircc xuat ra tren chan TxD, ta bilt d~u chu ky may tiep theo va d111i~u dU<;1c dich vao chan RxD bo i xung clock dich bit ( hi~n nhien 13 cac mach ghep noi M cung cap du Iieu tren duo-ng RxD duoc d6~g bi? bo i xung clock dich.Lit tren--dufulg TxD ( xem hinh 5.3 ).

Chuong 5 : Hoa t dong ella port n6i tiep

91 I!l

1+------- One maclune cyele --------.j

81 82 S3 84 85 86

i PI I P21 PI I P21 PI I P21 PI I P21 PI I P21 PI I P2i

r

ALE

Valid data bit

Osc.

Data alit 1

1--83Pl

SGPl--'1

·1 1 1 1

-----

WjUTE

Shift CLOCK 1 1 1 1

;;;-

;-

,/

Expanded view

------

to SBUF

;;

-----

ALE

Data alit

1

IRXOI 1 1

Slli~~l__tl__t~rL_rl __ rL_r-

CLOCK

(TXO

Hinh 5.2 : Gia n db tho'i gian phat da Iieu i1 che dQ 0 One.machine cycle: mot chu ky may

Osc : xung clock cua mach dao dQng

ALE : xung ALE

Data out: da Iieu xua't

Valid data bit: bit da Iieu hcp l~

Shift CLOCK: xung clock dich bit

WRITE to SBUF : ghi va o SBUF Expanded view: qua n sat duoc ph6ng da i

-I

I-Onp machine cycle

ALE

Data out IRX])

Shift CLOCK (TXD)

• Hlnh 5.3 : Gia n db thai gian thu da Iieu & che dQ 0

~92

H<.> vi dieu khien 8051

One machine cycle: mQt chu ky may ALE: xung ALE

Data out: di1 Iieu xua't

Shift CLOCK: xung clock dich bit

Vi~c dich di1 Iieu vao port n6i tiep xay ra i1 canh dliang ( canh len) cua TxD.

~

MQt ung dung kha thi cua che' dQ 0 ( che dQ thanh ghi dich bit) la

me rong them cac ngo ra cho 8051. M9t vi mach thanh ghi dich nci tiepsong song co the dliqc n6i vdi cac chan TxD va RxD cua 8051 de cung cap them 8 dli<'1ng xuat ( xern hinh 5.4 ). Cac thanh ghi dich bit khac co the ghep cascade vdi thanh ghi dich bit dau tien de me rQng them mra.

8 Extra outputs

8051
TXD (P311 CLOCK
RXD (P30) Data Shift register
Hinh 5.4 : Che'dQ thanh ghi dich bit cua port n6i tie'p

Shift register: thanh ghi dich bit 8 extra outputs: 8 ngB ra roO- rQng

5.3.2 UART 8·bit co t6c dQ baud thay d6i ( che'dQ 1 )

Trong che' dQ 1, port n6i tiep cua 8051 hoat dQng nhu mot bQ thu phat khong d6ng bi? ( universal asynchronous receiver transmitter ) DART 8-bit co t6c di? baud thay d6i. DART hi mot bQ thu va phat di1 lieu noi tiep vdi m6i ' tli di1 li~u dliqc dan trlidc b6'i mot bit start ( logic 0 ) va dliqc dang sau bi1i m9t bit stop ( logic 1 ). 'I'hinh thoang mot bit chan Ie diroc chen gifra bit di1 Iieu sau cung va bit stop. Hoat di?ng c~eu cua mot DART la bien d6i dCf lieu phat tli song song thanh n6i tiep va bie'n d6i di1 li~u thu tli n6i tiep thanh song song.

Nhu vl%y i1 che dQ 1 ta co 10 bit dliqc thu tre~han RxD va 10 bit diroc phat tren chan TxD cho m&i mot ky tv di1 lieu, chung bao go m 1 hit startTTti6il1uon In ~it dli Iu;m ( bit LSB trlidc tien ) va 1 bit stop ( luon lucn hi 1 ). Khi heat dQng thu, bit stop dira den bit RB8 cua SCON. Vdi 8Q.5.1, '~Q baud d1f,?,c tbie'.t.J~ bi1i t6c dQ trim ( overflow rate) cua bQ "dinh t~con i1 8052, t6c dQ baud dli<_1c thiet ll%p b6'i t6c

r __

I

I

t i

! f

I

L

Chucng 5 : Hoa t di?ng cua port noi tie'p

93 ~

di? tran cua bi? dinh thoi ~c bi? dinh thni 2 hoac t6 hcp cua ca hai (

mot cho phat va mot cho thu ). _,----.

Viec cap xung clock dich bit va d6ng bi) cac thanh ghi dich bit cua port noi tiep d cac che' di? 1, 2 va 3 duoc thiet lap bdi mot bi) de'm 16, ngc ra cua bi? dern la xung clock toc di? baud ( xem hmh 5.5 ). Ngo va o cua bodem vita neu duoc cho n ba ng phan me m va SEl duoc trinh bay sau.

lG x baud rate --+~

Baud rate clock Senal port shift register

Hmh 5.5 : Ca'p xung clock cho port nO'i tie'p 16 x baud rate: 16 x to'c dQ baud

Baud rate clock: xung clock to'c dQ baud

Serial port shift register: thanh ghi dich bit cua port nO'i tie'p

Viec phat duoc khdi dong bang each ghi vao SBUF nhung viec phat kh6ng thl!C sl! biit d§.u cho de'n Hn tran ke clla b6aFm 16, bi? dern cung ca'ptoc di? baud cho port noi t.iep. Dil lieu duoc dich bit. d~ duoc xua t ra tren dinrng TxD SEl bat d§.u bang bit. st.art., t.iep theo la 8 bit dir Iieu ro i

---

de'n bit. st.op. 'I'hci gian cua mo i mot bit. la gia trj nghich dao cua toe di?

baud, ~i? baud co dli<,1c b~ng cach l~p t.rinh cho bi? dinh thO'i, CO' ngdt phat TI dli<,1c set b~ng 1 ngay khi bit. st.op xuat hien tren dinrng TxD ( xem hmh 5.6 ).

-I

1 1

----

Baud rate

TXD

Start bit

TI (SCON.l)

I

Trun smit interrupt (ready for more dat.u )

Hlnh 5.6 : Set ca TI cua port nO'i tie'p Baud rate: to'c dq baud

Start bit: bit start

Stop bit: bit stop

Transmit interrupt: ngilt pha t

~94

HI;> vi dieu khien 8051

Viec nhan duqc khdi dong bdi mot chuyen trang thai tlJ 1 xu6ng 0 tre n dinrng RxD ( bat dau bit. start ). Bi? dern 16 ngay lap tlic-duqc xo a

" ~,

de grin cae so dem cho dong bit den chan RxD { bit ke tiep den khi bi?

dern tran l~n nua va v .v.. ). Dong bit den duqc lay mau d giua 16 so"

dern. '

Bi? thu bao gom viec pha t hie n bit start sai bang each yeu cau 8 so" dem (j t.ra ng thai 0 sau khi co su chuyen tra ng thai tlJ 1 xuo'ng 0 Ian dau tie n. Neu dieu nay khong xay ra, bo thu duoc gia sv ding dd duqc nhan duc_1c nhieu thay vi 1:'1 nhan mo t bit. ho p le. Bi? thu se duoc thiet lap lai , quay v~ trang thai nghi va chrr su chuyen trang thai tir 1 xuo'ng 0 ke. Gi:::i sV mot bit start. hap le diroc phrit hien, viec nha n ky W se tiep tuc. Bit start duoc bo qua va 8 bit dli lieu duoc nhan tuan W va o thanh ghi d~.it. cuaPQIt no i tiep. Khi ca 8 bit da dinrc nhau, cac dieu sau ~-

----.

ra :

V 1. Bit t.ht! 9 ( bit. stop SCON.

duoc dua den bit RB8 trong thanh ghi

,

/'2, 8 bit dli lieu duoc nap vao S~ 0. Co ngat thu RI duoc set.

Tuy nhien cac dieu tran chi xay ra neu cac dieu kie n sau to n ta i :

1 . ..El:=_0

2. SM2 = 1 va bit. stop nha n duoc la bit 1, hoac SM2 = O .

..._

Yeu cdu RI = 0 dam bao r5ng phan me m da doc ky tv trudc ( va xoa RI ), Di~u kie n thu hai co ve phirc tap nhung chi a p dung trong che. dQ truye n thong da xV Iy. Yeu cau co nghia la khorig set RI bang 1 trong chedo truyen thong da xvlj khi bit dli lieu thu 9 la O.

5.3.3 UART 9-bit co t6c dQ baud co' djnh ( che' dQ 2 )

Khi SMI = 1 va SMO = 0, port no i t.iep hoa t dong d che di? 2, che' dQ UART 9-bit co ~i? baud co dinh. 11 bit duoc thu hoac phat cho viec thu phd t mot. ky W dir Iieu : bit start, 8 bit. duli¢u, bit dli lii~u tht!~p t~ va bit stop. Khi phri t, bit tht! 9 Y. bit bat k'·-'"i. dat vao . - TB8 trong thanh ghi SCON ( co the la bit chan le ). Khi thu, bit thit 9

" nhan duoc se da t vao bit RB8. T6c dQ baud d che dQ 2 bang 1132 hoac bang 1164 tan so cua mach dao dong tre n chip ( xem muc 5.6 ).

5.3.4 UART 9-bit co t6c dQ baud thay dc:ij ( che'dQ 3 )

Che' di? 3, UART 9-bit co toc dQ thay d6i, tirong W nhu che di? 2 ngoai tTl! toe di? baud diroc lap trinh va ducc cung cap bdi bi? dinh tho i. Th~ t

Chuong 5 : Hoa t dQng cua port noi tiep

95 ~

ra cac che dQ 1, 2 va 3 d~u tuong W nhau. Chung khac nhau & toe dQ baud ( co dinh i1 che dQ 2 va thay d6'i i1 ca c che dQ 1 va 3 ) va d so bit dv lieu ( 8 bit i1 che dQ 1 va 9 bit & cac che dQ 2 va 3 ).

5.4 KHdI DQNG VATRUY XUAT cAc THANH GHI 5.4.1 Cho phe p thu

Bit cho phep thu REN trong thanh ghi SCON pha i diroc set bang 1 bdi phan mern M cho phep nhan cac ky W. Di~u nay thinrng diroc thuc hien i1 dau chuong trinh khi port noi tiep, cac bQ dinh tho i V.v .. duoc khdi dQng va c6 th~ duoc thuc hien theo 2 each. Lenh :

. SETB REN

set bit REN bang 1 hoac le nh :

MOV SCON, #xxxlxxxxB

set bit. REN bang 1 va xoa hoac set eric bit khac trong SCON neu can

5.4.2 Bit dii' li~u thli' 9

Bit dv lieu t.hti 9 duoc pha t d cac che di? 2 va 3 p!!.rii dtlO'c nllI> cho bit.

TB8 ba ng' phan me m. Bit dv lieu thu 9 thu diroc pha i dat va o bit. RB8

----.,--:='--'=-:--:-, -~, - ." ,"'---'-'

cua SCON. Pha n me m co the YE!u cau hoac khong ye u cau bit dv lieu thir

9· .. ·i~;y~~ cac dac t.inh cua thiet bi no i tiep rna vo i thiet bi 1H1Y viec truyen dv lieu ducc thiet l~p ( bit thu 9 con dong vai tro quan trong

----

trong truy~n thong da xtily ).

5.4.3 Them vao bit chlln Ie

Bit thu 9 thinrng duoc dung lam bit chan Ie cho mo t ky til. Nhir da d~ cap i1 chuong 2, bit P trong tU tra ng thai chuong trinh PSW duoc set hoac xda i1 m6i met chu ky may M thiet lap viec kiern tra chan cho 8

~ bit chua trong thanh chira A.

Thi du neu viec truyen thong yeu cau 8 bit du lieu corig vo i mot bit. kiern tra chan, cac le nh sau duoc dung M phat di 8 bit. trong thanh clura vdi bit. ki~m tra chan ducc dua vao bit thu 9 :

\ MOV C, P ; dua bit k ie m tra chd n VaG TB8

MOV TB8, C ; bit nay tro' tha nh bit thu 9

l.

MOV SBUF, A

; di chuye n 8 bit du Iieu tli ACC dfin~

Neu kiern tra le duoc ye u cau, cac lerih t.re n pha i diroc sira d6'i nhu

sau:

MOV C, P CPL C

; dtra bit ki~III tra chziu VaG TB8 ; bien d6i tha nh kism tra Ie

J

~96

H<,> vi dieu khidn 8051

MOV TB8, C MOV SBUF, A

; bit nay trO- tha nh bit thrr 9

; di chuye n 8 bit di1 lieu W ACC de'n SBUF

DI nhien viec sli dung bit chan Ie khong bi gidi ha n i1 cac che di? 2 va che di? 3. Trong che' di? 1, 8 bit du li~u diroc phat di bao g6m 7 bit di1 lieu cong vdi bit chan le. De pha t di mot rna ASCII 7 -bit cung vdi bit kiem tra chan ( bit thu 8 ), ta co the stt dung cac dong lenh sau :

CLR ACC.7

; dam ba o bit MSB dutrc x6a

; ki€m tra chan

MOV C, P MOV ACC.7, C MOV SBUF, A

; sao che p bit P vao C

; d~t bit ki€m tra chiin vao bit MSB

; phat ky W

; 7 bit di1 Iieu cQug vrii bit ki€m tra chan

5.4.4 Cae eo ng~t

Cac cd ngl'it thu RI va ngl'it phat TI trong thanh ghi SCON dong mot vai tro quan trong trong viec truye n di1 Iieu n6i tiep cua 8051. Cil 2 bit deu dircc set bang' 1 bang pha n cling nhirng pha i duoc x6a bang phan merri,

Bien hinh la RI duoc set bang 1 khi ket thuc viec nhan mot ky W va

chi ra rang' bi? d¢m thu day. Di~u kie n nay diroc kiern tra bang phd n

me m hoac dtt<,>"c 14p trinh de tao ra met ngdt ( cac ngl'it dinrc d~ cap trong chucng 6 ).

Neu phan me m muon nha p mo t ky W tit mo t thiet bi ghep vrii port no i tiep, pha n mern phai cho' cho den khi RI diroc setbllng 1, ke den pha n mern x6a RI va doc ky t1;i tit SBUF. Dieu nay diroc the hien nhu sau :

WAIT:

JNB RI, WAIT

CLR RI

MOV A, SBUF

Cd TI duoc set bang 1 khi ket thiic viec phat mot ky W va chi ra ding bi? de m pha t r6ng. Neu phan me m muon pha t mo t ky W den mot thiet bt gbep vcri portIioi tiep, phan mern trirdc tien pha i kiem tra M biet port noi ti~p da slln sang. N6i each khac, neu mot ky W trinrc do da dtt<,>"c pha t, phan me m phai chc viec phat ket thuc trttdc khi gi1i tiep ky ttt ke.

Cac lenh sau day phat mot ky t1;i chua trong thanh chua :

Chuong 5 : Hoat di?ng cua port noi tii{p

97 ~

WAIT:

TI, WAIT

; ki~lIl tra TI cho de'n khi bli.ng 1

JNB

CLR TI"

MOV SBUF, A

; x6a TI

; phat ky tv

Cac chuci lenh thu va pha t d tren la met phan cua cac chuong trinh con xuat nhap ky t'!. Chung se dl1Q'c mo ta chi tiet trong thi du 5.2 va thi du 5.3.

5.5 TRUYEN THONG DA xU' LY

Cac che di? 2 va 3 Iii. cac che di? du' phong cho viec truyen thong da xli IY. Trong cac che d(> nay, 9 bit di1 li~u ducc thu va bit thu 9 ducc dua den~. Port co the dl1Q'C lap trmh sao cho khi b)t stop dircc nhan, ~ <!2.._port noi tiep dl1dC tich qtcchi neu RB8 = 1. Dac trung nay co duoc

, r--. ,

bang each Sf!! bit SM2 trong thanh ghi SCON bang). Mot ling dung cho

dieu nay la mot mo i trinrng mang sli dung nhieu 8051 ducc sap xep theo mo hinh chu I to' ( master I slave) nhu trlnh bay trong hlnh 5.7.

32 I/O Jines

32 I/O Jines

DDUD DDDD

PO PI P2 pa PO PI P2 P3
Muster 8051 8051 Slave # 1 8051 Sluvo #2
TXD RXD RXD
I r r Hinh 5.7 : Truyen thong da xli Iy Master 8051 : 8051 chu

8051 slave # 1, # 2 : 8051 t6' 1,2 32 I/O lines: 32 duO'ng xua'tlnh4 p

Khi bi? xli ly chu muon truye n mo t khoi dir Iieu den mo t trong nhieu bi? xli ly td, trudc tien bi? xli ly chu phat di mot byte dia chi nhan dang bi? xli ly td dich. Mi?t byte dia chi khac vdi mot byte dir Iieu d cho b~ 9 la 1 trong byte dia chi va la 0 trong byte di1li~u. Mo t byte dia chi ngat ta't ca cac bi? xli ly to' M cho mo i mot bQ xii' ly to' co the' khao sat byte nhan dl1Q'c de kiern tra xem co pha i la bi? xii'ly td dang dl1Q'c dinh dia chi khong. Bi? xli ly td ducc dinh dia chi se xca bit SM2 c? lnh va an

~98

H<;> vi di~u khien 8051

hi nh3.n cac byte du li~u theo sau. Cac b6 xU' 1:>' td khOng dU<;1C d~nh dia chi cti cac bit SM2 clla chung du<1c set biing I_va thuc thi cac ccng vi~c cua rierig chung, bi) qua kho ng nhan cac byte du Iieu. Cac bi? xU' If nay se diroc ng8:t Jan nira khi bi? xU'lf chu phat tie'p byte dia chi ke'. Cac so d6 cu tIfi co th~ ducc neu ra sao cho mot khi lien ke't chu td da duoc thie't l~p, bi? xU' If td cling co th~ phat deri bi? xU' If chu, Muu meo d day Iii khong sU' dung bit du Iieu thti' 9 sau khi lien ke't vua diroc thie't l~p ( ngucc lai cac bi? xU' If td khac co th~ ducc chon mot each khong c6 f ).

SM2 kho ng anh hucng de'n che di? 0, va trong che' di? 1 bit nay co the diroc dung M kiem tra su hop l~ cua bit stop. o .che' di? 1 thu, neu SM2 = 1, ng8:t thu se khong dL(<;1C tich cue trir phi bit stop thu ducc Ia hop l~.

5.6 TOe DQ BAUD ellA PORT NOI TIEP

Nhu da tha'y trong bang 5.2, t6c di? baud se c6 dinh trong cac che di? o va 2. Trong che di? 0, t6c di? baud lucri luon bang Mn s6 clla mach dao do trong chip chia cho 12. Thong thtnrng nguc i ta sU' dung mi?t thach auh ben ngoai chip cho mach dao di?ng nay. Gia sU' t§.n s6 cua mach dao dong' la 12 MHz, t6c di? baud cua che di? 0 la 1 MHz ( xem hinh 5.8a ).

Sau khi h~ thong duoc reset, t6c di? baud cua che di? 2 bang tan s6 cua mach dao dong chia cho 64. T6c d<} baud cling bj anh hucng bdi mot bit trong thanh ghi dieu khien ngu6n PCON. Bit 7 cua PCON Iii bit SMOD va viec set bit nay bang 1 se lam tang t6c di? baud cua cac che di? 1, 2 va 3 len ga'p do i. d che d~ 2, t6c di? baud co th~ duoc nhan '2 tlt gia tri mac dinh la 1164 tan s6 cua mach dao di?ng ( Sl\1_OD = 0 ) trl1 thanh

-------

1132 tan s6 cua mach dao dQng ( ~_MOD = 1 ) ( xem hinh 5.8b ).

VI thanh ghi PCON khcng du<;1C dinh dia chi tirng bit, viec set bit SMOD len 1 rna khong lam thay d6i cac bit khac cua thanh ghi nay duoc thuc hien bang nhirng dong le nh sau :

MOV A, PCON SETB ACC.7 MOV PCON, A

; lily gin tri hie n ha nh cua PCON ; set bit 7 bhg 1 ( SMOD )

; ghi gin tri mci va o PCON

Cac t6c di? baud cua 8051 d che' di? 1 va che' di? 3 dU<;1C xac dinh bci toe di? tra n cua bi? dinh th<'1i 1. VI bi? dinh thci hoat di?ng d tan s6 tuong' d6i cao, ta can chia Mc di? tran cho 32 ( hoac 16 neu SMOD = 1 ) trirdc khi trd thanh xung clock t6c di? baud cung cap cho port n6i tiep. T6c di? baud cua 8052 11 cac che' di? 1 va 3 ducc xac dinh bdi t6c di? tran cua bi? dinh thci 1 hoac bi? dinh thC1i 2 hoac ca hai.

Chuong 5 : Hoa t d(mg cua port n6i tie'p

99 ~

On-chip ~ Baud rate

oscillator L.:.:J ---- clock

(a) Mode 0

[-;-32 SMOD=O

Tuner 1 Baud rate

overtlow -------+1. ~-::~---- clock

~~SMOD=l

Hlnh 5.8 : Cac ngudn xung clock cho port n6i tie'p (a) che' dq 0 (b) che' dq 2 (c)

che'dq 1 va 3

On chip oscillator: bq dao dQng trong chip Baud rate clock: xung clock t6c dQ baud

Timer 1 overflow : tran bq dinh thai 1

5.6.1 S11' du ng bQ djnh thoi1lam xung clock to'c dQ baud

Ky thuat thinrng dung M tao xung clock t6c dQ baud Ia kQQ:_i dong thanh ghi TMOD ij che' do tu' n~p Iai 8-bit ( che di? dinh thai 2 ) va da t gia tr] nap lai thich hop va o thanh ghi TH1 de co t6c di? tran dung, tit do tao ra t6c di? baud.

Thanh ghi TMOD duoc khdi dorig' nhir sau :

MOV TMOD, #OOlOxxxxB xxxx danh cho bi? dinh tho-i o.

Day khong pha i la kha nang duy nhat. Cac t6c do baud rilt cham c6 th~ nhan du'<?,c bang' each sU' dung che' di? 16-bit, che' dQ dinh tho-i 2 v6j (TMOD) = 000lXXXJ(I:3: Tuy nhien c6 mot 16i phan mern ij day do cac thanh ghi.,._TH1!l'L1 phiii dl.&c khiji dQng I~i sau m6i llin trim. Di~u nay can du'<?,c thuc hien trong mot trinh ph\lc V\l ngiit_ Mot Iva chon khac la cung cap xung clock ben ngoai cho bi? dinh thoi 1 bang each sU' dung ng6 vao T1 ( P3.5 ). Du Ia Iva chon nao, t6c dQ baud cung b~ng t6c di? t.ran cua bQ dinh tho-i 1 chia cho 32 ( hoac chia cho 16 neu SMOD = 1 ).

_-

j

~100

HI? vi dieu khien 8051

Do vay, cong thirc dung de xac dinh toe di? baud d de che' di? 1 va 3 lD.:

BAUD RATE = TIMER 1 OVERFLOW RATE -r- 32

Tfic d(l baud = tfic d(l tra n b(l dinh thai 1 chia cho 32

Thi du neu can toe di? baud la 1200, toe di? tra n cua bi? dinh thai 1 phai la 38.4 KHz ( = 1200 x 32 ).

Neu tfin so cua mach dao dong ben trong chip la 12 MHz, bo dinh thai 1 duoc cap xung clock la 1 MHz hay 1000 KHz. Do bi? dinh thai pha i tra n d toe di? la 38.4 KHz, viec tra n can xay ra sau m8i 26.04 xung clock ( 1000 + 38.4 ) va dU<;fe lam tro n la 26. Vi ba dinh thai dem len va tra n khi co so dern tit FFH ehllyen thanh OOH, 26 so dern nho hen 01a gurtrin~p la i can co M nap eho thanh ghi TIp. Gia tr] nay la -26. Cach de dang nhat M dat gia tr~ nap la i vao TH1 la :

MOV THl, #-26

Trinh dich hop ngu se tlurc hien viec bien d6i dn thie t. Trong trinrng hop nay -26 dinrc bien d6i tha nh OE6H, vay thi l~nh tre n trri thanh :

MOV THl, #OE6H

Do ta lam tron so xung derrr nen se co mot sai so nho trong ket qua t.inh toe di? baud. Trong truorig hcp t6ng quat, mot sai so ~ la sai so chap nha n dmrc trong truyen du Iieu khorig d6ng bo. Cac toe di? baud ehiuh xac co th~ nhan duoc bang each sti dung mo t thach anh 11.059 MHz eho mach dao dong trong chip. Bang 5.3 tom tih cac gia trj nap la i can dira vao thanh ghi TH1 eho cac toe dQ baud thinrng dung nha t ( sti dung thach anh 12 MHz hoac 11.059 MHz ).

Tc5c de:> T~nsc5 SMOD Gia trj Tc5c de:> Sai
baud tha ch anh n~p cho baud A'
so
THI th1!c te'
9600 12.000 MHz 1 -7 (F9H) 8923 7%
2400 12.000 MHz 0 -13 (F3H) 2404 0.16%
1200 12.000 MHz 0 -26 (E6H) 1202 0.16%
19200 11.059 MHz 0 -3 (FDH) 19200 0
9600 11.059 MHz 0 -3 (FDH) 9600 0
2400 11.059 MHz 0 -12 (F4H) 2400 0
1200 11.059 MHz 0 -24 (E8H) 1200 0
Bang 5.3 : T6m ti1t tfic d(l baud Chuongf : Hoat di?ng cua port noi tie'p

101 ~

Thi du 5.1 : Khdi di?ng port noi tie'p

Viet mot chuoi lrnh di khai d¢ng port noi tiep sao cho port nay hoat d¢ng nhu mot UART 8-bit vai toe do 2400 baud_ Sd dung b¢ dinli thai 1 dl cung clip xung clock t6c d¢ baud.

V6i thi du nay ta phai khdi do ng' 4 thanh ghi: SMOD, TMOD, TCON va THl. Cac gia tr] yeu cau dli<!c tom hit dli6i day:

SMO SM1 SM2 REN TB8 RB8 TI RI
SCON: 0 1 0 1 0 0 1 0
GTE CIT M1 MO GTE CIT M1 MO
TMOD: 0 0 1 0 0 0 0 0
TF1 TR1 TFO TRO IE1 IT1 lEO ITO
TCON: 0 1 0 0 0 0 0 0
TH1: 1 1 1 1 0 0 1 1 Viec thie't lap SMO/SMl = Oil nharn d:)t port noi tie'p d che' di? DART 8-bit. REN = 1 cho phep port noi tie'p thu cac ky tV. Viec set TI bang 1 cho phep phat ky tv dilu tien bang each chi ra rang bi? dern phat ding. V6i thanh ghi TMOD, viec thie't lap M1/M0 = 110 da t bi? dinh thdi 1 vao che'di? tv nap lai 8-bit. Viec set TR1 trong TCON bang' 1 se khdi dong bi? dinh thdi 1 hoa t dong.

Cac bit khac diroc cho bang 0 do chung dieu khien cac dac trung hoac cac che' di? khorig dli<!c stl dung trong thi du nay.

Gi::i tri can nap cho TH1 la gia tri cung cap toc di? tran : 2400 x 32 = 76.8 KHz. Gia Stl tiln so ella mach dao dong' trong chip la 12 MHz, ~o dinh thci 1 diroc cung cap xung clock co tiln so 1 MHz hay 1000 KHz va (so xun~ho moi liln tra n la 1000' + 76.8 = 13.02 ( lam tron lit 13 ). Gia tri nap lai la -13 hoac OF3H.

Chuci l~nh khrri dorig port noi tie'p nhu sau :

ORG 8100H
INIT: MOV SCON, #52H ; port nO'i tie'p, che' di? 1
MOV TMOD, #20H ; bi? d~nh thai 1, che' di?2
MOV TH1, #-13 ; gia try nap Iai M c6 2400 baud
SETB TR1 ; bi? dinh thai 1 heat di?ng
END j

~102

HQ vi dieu khicin 8051

Thi du 5.2 : Chuong trinh con xua t ky W

Vie't mot chuang trtnli can gQi la OUTCHR di phiit nui ASCII 7-bit chua trong thanh chua A ra port n6i tiep ctla 8051 uai bit kiim tra le la bit thit 8. Vi¢c tro u~ til chuang trinh. can kh6ng lam thay d6'i ll(ii dung thanh chua ( nghia la thanh chua co n(ii dung gi6ng nhu n(ii dung truce hhi chuang trinh. con duoc gQi ).

Thi du filly va thi du ke' minh hoa hai trong nhieu chuong trinh con thong dung nhat trim cac h~ may vi tinh co thiet b] d:h cutii ghep qua chuan RS-232 : xuat ky W ( OUTCHR ) va nhap ky W ( INCHAR ).

OUTCHR:

AGAIN:

ORG 8100H
MOV C,P ; dat bit chdn Ie VaG efJ C
CPL C ; d6i thanh Ie
MOV ACC.7, C ; dua VaG bit 7 cua ACC
JNB TI, AGAIN ; b(l dern phat rang?
; sai, ki~m tra la i
CLR TI ; dung, x6a efJ TI va
MOV SBUF, A ; phat 1 ky W
CLR ACC.7
RET
END Ba lenh d§u W~n da t bit kicim tra Ie vao bit 7 cua thanh chua. Do bit P trong tli tra ng' thai chuong trinh PSW thie't I~p kiern tra ch:'in cho thanh chua, bit nay phai duO'c lay bu tnft1c khi dat vao ACg,7, Lenh JNB tao ra m(>t vong lap cho de kiern tra cO- ngiit phat TI cho den khi cO- nay diroc set bang' 1. Khi TI =1 ( do viec phat ky W trirdc do vira ke't thuc ), bit nay duo c xca va sau do ky W trong thanh chua diroc ghi vao bo d~m cua port noi tie'p SBUF va viec phat ky tv bih dau is lan tra n ke cua bi? dern 16 tao xung clock cho port. no i tiep. Sau cung bit. ACC,7 dircc xoa de gia trj tra v~ giong nhu khi rna 7 -bit dUQ"c chuyen den chuong trinh con,

Chucng trinh con OUTCHR thuong dircc goi bo i mo t chirong trlnh go i de pha t mot ky tv hoac mot chuo i ky tu. Thi du cac lerih sail pha t rna ASCII cua ky W 2 den thief bi noi t.ie'p ghep vdi port n6i tiep cua 8051 :

MOV A, # '2' CALL OUTCHR

Chuong 5 : Hoa t dQng cua port noi t.iep

103 ~

Ta cling co the' stt dung chuong trinh conOUTCHR nhu la mot khci dttgc thiet ke trong mo t chuong trinh con co ten la OUTSTR chang han, chuong trinh con nay pha t mot chuo i ky tI! ASCII ket thuc bdi byte

NULL ( OOH ) den Il1Qtthi~t : A"~ 'A' 'vfJi port noi tiep cll

Thi du 5.3 : Chuong trinh con thu met ky tI!

Vict mOt chuang trinh. con co ten la INCHAR dc' thu mot k)' t~( til port no'i tiep ctla 8051 va trti vel mii ASCII 7-bit trong thanh chiia. Sti dung kic'm tra If? trong bit thic 8 th.u duoc va set co nho Mng 1 nCll co 16i chan u.

ORG 8100H
INCHAR: JNB RI, $ ; chc ky tv
CLR RI ; xo a cO'
MOV A, SdUF ; doc ky tv va o thauh chua
MOV C,P ; v6'i ki€>m trn Ie trong A
; P crln duoc set bi1ng 1
CPL C ; vie c lilY bu chi ra co Ioi hay
; kh6ng?
CLR ACC,7 ; x6a
RET
END Chuong trinh con nay bat dau bang viec cho cd ngdt thu RI diroc set bang 1 de' chi ra ding ky t.V dfl stin sang t.rong bi? dern thu SBUF ( de diroc d9C ), Khi RI = 1, Ie nh JNB chuyen dieu khien de'n l~nh tiep theo lenh nay, Co Rldttgc xoa va rna trong SBUF duoc doc van thanh chirn. Bit P trong PSW thiet lap kiern tra chd n cho thanh chua, do vay bit. nay can ducc set bang 1 neu ban than thanh chua chua bit. kiern tra le 6' bit thli 7 cua thanh ghi nay, Viec di chuyen bit P van cd nho lam cho CY = o neu kh6ng co 16i. Mat khac, neu thanh chua chua mo t 16i chan le, Cd CY se ba ng l. Cuo] cung bit. ACC.7 diroc xoa de dam bao rilng chi co rna 7 -bit du<?,c tra v~ cho chuong trinh go i.

Cac nQi dung trong cac chuong 4 va 5 trinh bay cac chi tiet chinh can den khi ta l~p trinh dinh t.hO'i va thu phat nbi tiep. Cac chuong trinh thi du trong 2 chuong nay hoa n toan kh6ng stt dung ngat rna stt dung cac vong lap, Viec stt dung eric vo ng lap trong lap trinh xuat nhap

j

~104

H9 vi dieu khi~n 8051

gap phai khuyet diern la b.Q_vi ,xv l~ kh6ng lam gi ca M cha cac di~u ki$n xmlt nh~p s::i"n sang.

Trim thuc te, nhieu irng dung lien quan den cac hi? dinh thai va port noi tiep cua 8051 yeu cau phai diroc d6ng hi? cling nhu M hi? vi xV ly khong phai cho, nguoi ta sv dung ng:lt. Day la chu d~ cua chuang tiep

theo, hoa t di?ng ng:l t. --

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->