P. 1
Info] - Cau Truc Va Lap Trinh Ho Vi Dieu Khien 8051

Info] - Cau Truc Va Lap Trinh Ho Vi Dieu Khien 8051

|Views: 206|Likes:
Published by Hoang Khuong

More info:

Published by: Hoang Khuong on Sep 25, 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

11/17/2013

pdf

text

original

· I;

---+----+--+ -t---t--+----+----t-'----+--+-+-+-+--+-+---+-+-+-+-----I---+-r- t + +

, ~ ~

t-----'----+-t--+---+-t----'-------t-+--t---+--+---+-+. ~ ~ ~ t ·--j-----j-----ir--T-t--t---+-t -+--+-t--l-t-+---t-I- ~ t

At .; "..A. "

CAU TRUC VA LAP TRINH

~ ~

HO VI OIEU KHIEN 8051

--+----t-----t---t- +- + -+------+- I-------t

t---;----t------t---.+--t---t--t + I + +

-.<----+----+---+ + t---t-----t--t___+_ t .. +

+------t-+-I t + :

.r u: +~r~:

+-~---t- +-----+ + .-+-.~.. -t-t-----t-c-+ + + r r

:-:'! t+

~ .. t+t- ....

t + ,. -I-------+----- +

t + +-'- + t

NGUytN TANG ClfONG, PHAN ouoc THANG

CAU TRUC vA LAp TRiNH

~ ~

HO VI f>IEU KHIEN 8051

NHA XUAT BAN KHOA HQC V A KY THUAT HA N(n - 2004

MVC LVC

Lm. noi dau ...................•...•...........................•....•.........•..........•••......•........•....•....•. 5

Chirong 1. BQ vi dieu khi~n 8051 7

IIDA 'd'''' kh':! 'h" h' 7

· uy VI leu len va ~ n ung ..

1.2 Tong quan v~ ho 8051 11

Chuang 2. Lap trinh hcp ngfi 8051 17

2.1 Ben trong 8051 17

2.2 Gioi thieu v~ lap trinh hop ngu cua 8051 20

2.3 Hop dich va chay chuang trinh 8051 22

2.4 B9 dern chuang trinh va khong gian ROM cua 8051 24

2 5 K·:! d- I'" a ch: d'" 28

· leu U I~U va c I an ..

2.6 Cac bit ca va thanh ghi dac bet PSW cua 8051 30

2.7 Bang thanh ghi va ngan xep cua 8051 33

Chuang 3. Vong lap, lenh nhay va lenh goi 39

3.1 Vong lap va lenh nhay 39

3.2 Lenh goi CALL .. , 45

3.3 Tao va tinh toan thai gian giG cham 51

Chuang 4. Cong vao/ra va lap trmh 55

4.1 M6 ta chan cua 8051 55

4.2 Lap trinh vao - ra, thao tac bit 62

Chuang 5. Che de} dinh dia chi 65

5.1 Che dQ dinh dia chi nrc thai va thanh ghi 65

5.2 Cac che dQ dinh dia chi truy cap bo nho 67

Chuong 6. Lenh s6 hoc 78

6.1 Cong va trir cac s6 khong dau 78

6.2 Nhan va chia cac s6 khong dau 85

6 3 so, , d"' , , he , h "" h 87

· co au va cac p ep tin so QC .

ChuOI1g 7. Lenh logic 92

7.1 Lenh logic va so sanh 92

7.2 Lenh quay va trao d6i 98

7.3 Chuang trinh irng dung rna BCD va ASCII 10 I

Chuang 8. Lenh xu Iy bit va lap trmh 104

8.1 Lap trinh voi cac lenh xtr 19 bit 104

8.2 Lenh thao tac ca nho CY 111

8 3 D ' ha d'" , a cho ;.> 113

. oc cae e an au vao va e ot eong .

Chuang 9. BQ dem/bQ dinh thoi va lap trlnh , 115

9.1 Lap trinh cac bo dinh thai cua 8051 115

9.2 Lap trinh eho bo dern 133

Chuang 10. Truyen tin noi tiep vOi 8051. 139

1 0 1 C.... ;.. <, t '" ti -r- ti '" 139

. v Sv eua ruyen In nOI lep .

10.2 Noi ghep 8051 voi RS232 147

10. 3 Lap trinh tru yen thong noi tiep eho 8051 149

Chuang 11. Ngat va lap trlnh 163

11.1 Ngttt cua 8051 163

11.2 Lap trmh ng<it bo dinh thai 167

11.3 Lap trinh ngat phan cirng ngoai '" 171

11.4 Lap trinh ngat truyen thong noi tiep 179

11.5 Mtrc tru tien ngiit cua 8051 184

Chuang 12. Noi ghep vOi the giOi thirc I -LCD, ADC va cam bien 188

12.1 N6i ghep LCD voi 8051 ; 188

12.2 Noi ghep 8051 voi ADC va cac bo cam bien 197

Chuang 13. Noi ghep voi the giOi thirc II -

dQng co biroc, ban phim va bQ bien d6i DAC 208

13.1 Noi ghep v6i dong co biroc 208

13.2 Noi ghep 8051 voi ban phim 214

13.3 Noi ghep DAC voi 8051 220

Chuang 14. Noi phep 8031151 vOi bQ nho ngoai 225

14.1 B¢ nho ban dan 225

14.2 Giai rna dia chi, 237

14.3 Giao tiep cua 8031/51 voi bo nho ROM ngoai 240

14.4 Khong gian bo nho dfr lieu cua 8051 245

Chuang 15. Noi ghep 8031151 voi 8255 255

15.1 Lap trinh 8255 ,. 255

15.2 Noi ghep voi 8255 263

15.3 Cac che d¢ khac cua 8255 269

Phu luc ..... go •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 274

Tai lieu tham khao .•.•....................•...• " .•.......•..•.......................................•....•... 285

LeJi NOI aAU

Ngay nay, cac bo vi dieu khien dang co ling dung ngay cang rong rai va tharn nhap ngay cang nhieu trong cac Iinh V\!C ky thuat va doi song xa hoi. Hau het cac thiet bi ky thuat tit phuc tap cho den dan gian nhu thier bi dieu khien ur dong, thiet bi van phong cho den cac thiet bi trong gia dlnh deu co dung cac bo vi dieu khien. Mac du co nhieu img dung thirc te nhu vay nhirng nhfrng tai lieu hien co chua dap ling diroc nhu cau ve h9C tap, giang day va nghien ciru ling dung cac bo vi dieu khien dang ngay cang tang hien nay.

Xuat phat tit thirc te do, muc dich chfnh cua tai lieu muon tap trung gioi thieu nhiing kien tlnrc co ban nhat ve cau tnic va lap trinh he vi dieu khien. Tai lieu d~c biet chu trong gioi thieu phan ling dung bao g6m t6 chirc cac h¢ thuc tien va phirong phap lap trinh cho cac he do.

Tai lieu nay la nam trong loat cac tai lieu da diroc B¢ mon TV dong va Ky thuat tfnh, Khoa Ky thuat Dieu khien an hanh, bao g6m cau true may tfnh, cau tnic va lap trtnh cac he xu ly tfn hieu so, nay la cau tnic va l~p trlnh cac h¢ vi dieu khi~n.

Tai lieu diroc chia thanh 15 chuang va diroc slip xep nhir sau:

Chuang 1 gioi thieu lich sir phat trien, tong quan chung ve ho vi dieu khien 8051 va cac thanh vien nhu 8751, 89C51, DS5000 va 8031.

Chuang 2 trinh bay ve kien true cua ho 8051 va nhtrng khai niern CO ban v~ l~p trinh hop ngir voi 8051.

Chuang 3 co chu de v~ t6 chirc vong lap, lenh nhay va lenh re nhanh cung m¢t loat cac chuang trinh vf du,

Chuang 4 danh cho t6 chirc cong vao ra cua 8051 va each lap trinh, Chuang nay cho phep cac sinh vien thirc hien cac thir nghiem dau tien ve t6 chtrc giao dien vao/ra cua 8051.

Chuang 5 d~ cap toi cac che d¢ dinh dia chi cua 8051, each truy cap du lieu va rna lenh trong khong gian nho cua 8051.

Chuang 6, chuang 7 va chuang 8 ban ve cac lenh so h9C, lenh logic, lenh XU ly bit va each lap trmh ling dung,

Chuang 9 cung cap cac thong tin ve bo dem/ bo dinh thai va phirong phap lap

trlnh ling dung.

Chuang 10 nghien ciru truyen tin noi tiep tren 8051, t6 chirc giao dien vci RS232 va truyen thong qua cong COM cua may tinh IBM PC.

Chuang 11 ban ve mot chu de rat quan trong trong trien khai cac ling dung cua 8051, do la t6 chirc ngat va each lap trinh voi cac ngat.

Chuang 12 va 13 bao gom cac ling dung thtrong g~p trong thirc te la t6 chirc noi ghep voi cac thiet bi ngoai vi nhir voi den LED, bo bien d6i ADC, bo cam bien, ban phfrn, dong CC1 biroc va cac bo bien d6i DAC.

Hai chuang cuoi cung la 14 va 15 di sau van hai chu de quan trong la t6 chirc noi ghep 8031/51 voi bo nho ngoai va voi vi mach phuc vu cho van ra 8255.

Cuon sach diroc dung lam giao trinh giang day bac dai h9C va sau dai h9C chuyen nganh dien, dien tir hoac lam tai lieu tham khao cho cac nghien ciru sinh va cho nhirng ai quan tam den cau tnic va lap trmh cho cac he vi dieu khien.

Cuon sach diroc bien soan bci PGS. TS. Nguyen Tang CUOng va TS. Phan Quoc Thang, do PGS. TS. Nguyen Tang CUOng chu bien va duoc phan cong nhu sau: PGS. TS. Nguyen Tang CUOng viet ttl' chuang 1 den chuang 7, cac ph an con lai do TS. Phan Quoc Thang dam nhiem,

Nhan dip nay, tap the tac gia xin bay to lei earn an chan thanh nhat den nhirng ngiroi di'i co nhieu dong gop trong qua trinh hoan thanh tai lieu, den cac anh chi em B¢ mon TI! dong va Ky thuat tfnh thuoc Khoa Ky thuat Dieu khien, H9C vien Ky thuat Quan sir, dac bier phai ke den su h6 tro hieu qua cua TS. Tran Van Hqp va ky thuat vien te Van Minh.

Do kinh nghiern va thai gian han che, tai lieu nay chac chan khong the tranh khoi nhfrng thieu sot. Rat mong nhan dtroc cac y kien dong gop va xay dung cua

ban doc gan xa. Y kien dong gop xin gi'ri ve dia chi: BQ mon TI! dong va Ky thuat tinh, Khoa Ky thuat Dieu khien, H9C vien Ky thuat Quan sir, 100 Hoang Quoc Vi~t, Ha N¢i; Di~n thoai (04)7542281, email: tcuong@hn.vnn.vn.

Ha NQi, thang 8 nam 2003 Tap th~ tac gia

Chlldng 1

Bt? VI DIEU KHIEN 8051

... ,," ""?,,,... ,

1.1 8Q VI DIEU KHIEN VA H~ NHUNG

BQ vi dieu khi~n va bQ vi xii Iy da nang

Su khac nhau gitra bo vi dieu khien va bo vi xir Iy 13. gl? B¢ vi xu ly b day muon noi den cac bQ vi xu 19 da nang nhu ho Intel x86 (8086, 80286, 80386, 80486 va Pentium) hoac ho Motorola 680xO(68000, 68010, 68020, 68030, 68040 v.v.). Cac b¢ vi xu 19 nay khong co RAM, ROM va khong co cac ding vao fa tren chip. Voi 19 do do rna cluing diroc goi chung la cac bo vi xir 19 da nang.

Mot nha thiet ke he thong sir dung b¢ vi xu 15' da nang, chang han nhtr Pentium hay 68040 can b6 sung them RAM, ROM, cong vao fa va bo dinh thai ngoai thi he thong moi hoat dong diroc. Di nhien, b6 sung RAM, ROM va cong VaG fa ben ngoai se lam cho he thong cong kenh va dat han, song co uu diem quan trong la het sue linh hoat. Vi du, nha thiet ke co th~ tuy theo yeu cau cua tUng Ung dl;lng rna quyet d~nh soluqng RAM, ROM va cae c6ng vao fa phil hqp.

(a) H$ vi XlI 19 da nang

(b) B¢ vi di~u khi~n

CPU
I I I l J
C6ng
B(> RAM ROM I/O Timer COM
vi XlI 19 Port n6i
da nang tiep
I I I l J
Bus d\a chi Bus dCt lieu

CPU RAM ROM
C6ng
I/O Timer COM
n6i
liep Hlnh 1.1. H~ vi xu Iy va bQ vi dieu khien

Doi voi cac bo vi dieu khien thi van de nay lai khac. B¢ vi dieu khien co tren chip bo vi xu 19 (CPU), bo nho RAM, ROM, cong vao fa va bo dinh thai. N6i each khac, bo xu 19, RAM, ROM, cong vao fa va bo dinh thai, tat cit cung diroc nhung tren chip vi dieu khien. Do vay, nguoi thiet ke' khong can b6 sung them bo nho ngoai, cong vao fa hoac bo dinh thai de cho he thong hoar dong, B¢ vi dieu

8

Cau true va It;ip trinh h» vi die'u khiln 8051

khien voi mot dung luong RAM, ROM tren chip va cong vao - ra da tra nen rat thfch hop trong nhieu irng dung yeu cau gia thanh ha va khong gian su dung han che. Nhirng irng dung, vi du nhir bo dieu khien TV tit xa, thl khong can cong suat tfnh roan 100 nhu cua bo vi xuly 486, tham chi khong can den mac 8086. Mot so img dung lai doi hoi khong gian nho, cong suat tieu tthu va gia thanh thap. Mot so irng dung khac chi can cac thao tac vao - ra d~ d9C tin hieu va t:it - rna nhtrng bit nhat dinh. Tit nhirng yeu cau da dang d6, mot so nha san xuat da di xa hem la tfch hop vao trong b¢ vi dieu khien ca bo chuyen d6i ADC va mot so thiet bi ngoai vi khac.

Bang 1.1. MQt s6 san pharn nhung sir dung bQ vi dieu khien

80 di~n trong nha May tinh gia dinh Nhac cu di~n tlt
Thiet b] May dam thoai Tivi May khau
gia dinh May di~n thoai Truyen hinh cap 8ieu khi€n anh sang
H~ thong an toan VCR May nhan tin
B(> ma cira nha xe May quay camera May choi Pool ball
May tra loi Dieu khi€n tlt xa 80 choi
May Fax Tro choi di~n tlt Dunq cu tap th€ hinh
Thiet b] 8i~n thoai May Fax May in lazer
van May Hnh La vi s6ng May in rnau
ph6ng H~ thong an toan May sao chup May nhan tin
Thiet b] May Hnh hanh trinh Do ILfong 8ieu hoa nhi~t d(>
tl!dQng 8ieu khi€n d(>ng co H~ thong bao mat 8i~n thoai t6 ong
Tui dem khf Dieu khi€n truyen tin Ma clta khong ~,
can
1 Thiet b] ABS Giai tri chia khoa Be;, vi dieu khi~n Cs cac h~ thong nhung

o cac tai lieu ve bo vi xir ly, cluing ta thirong g~p khai niern hf th/ing nhung (Embeded system). Thuc te, bo vi xir Iy va vi dieu khien diroc su dung rong rai trong nhieu san pham nhung, San pham nhung dung bo vi xu ly (hoac bo vi dieu khien) d~ thirc hien mot nhiem vu va chi mot rna thoi. May in la vi du ve mot he nhiing, VI bo xir ly chi dtroc dung cho cong viec nhan va in dir lieu. D6i voi may tinh PC Pentium (hoac PC urong thich IBM x86 bat ky) thl khac. May tinh PC c6 th~ diroc dung cho nhieu c6ng viec khac nhau, nhir tram dich vu in, may tro choi

B(J vi diciu khif!11 805!

9

dien tiI, tram dich vu mang hoac tram dilu cuoi Internet. May tlnh c6 th6 chay nhieu chuang trlnh phan mem khac nhau. 01 nhien, may tfnh PC thuc hien diroc nhieu cong viec nhir vay la do co bo nho RAM du Ian va mot he dieu hanh, May tfnh PC x86 thuong c6 hoac diroc noi toi cac san phdm nhung khac nhau, chang han nhir ban phfm, may in, modem, b¢ dieu khien ilia, the am, b¢ dieu khien COROM, chuot v.v. M6i thiet bi ngoai vi deu co mot bo vi dieu khien d~ thirc hien chi mot cong viec. Vi du, ben trong chuot co bo vi dieu khien d6 thuc thi cong viec xac dinh vi trf chuot va giri thong tin den may tfnh. Bang 1.1 liet ke mot so san pharn nhung,

Ung dl)ng nhung cua PCx86

Thuc te, cac bo vi dieu khien la giai phap thfch hop cho nhieu he thong nhung, Tuy nhien, trong nhieu tnrong hop, bo vi dieu khien khong du kha nang dap irng nhiem vu d~t ra. VI 19 do d6 rna nlumg nam gan day, nhieu nha san xuat b¢ vi xir Iy da nang, chang han nhtr Intel, Motorola, AMO (Advanced Micro Devices, Inc ... ) va Cyrix (hien nay la mot b¢ phan cua National Seniconductor, Inc) da huang toi bo vi xiI Iy cao dip cho thi tnrong nhung. Intel, AMO va Cyrix san xuat cac bo xir 19 da nang cho ca thi tnrong nhung va th] tnrong may tfnh PC M ban, con Motorola vftn duy trl ho vi xir Iy 68000 chu yeu cho cac he thong nhiing cao cap va Apple hien khong con dung 680x cho may tfnh Macintosh nfra, Trang nhirng nam d&u thap ky 90 cua the ky XX, may tfnh Apple bat dau siI dung cac b¢ vi xir Iy Power PC (nhir 603, 604, 620 v.v.) thay cho 680xO. B¢ vi xiI Iy Power PC la ket qua lien ket giifa IBM va Motorola, va n6 diroc huang cho thi tnrong nhung cao cap cilng nhu cho ca thi tnrong may tfnh Pc. Can luu y rang, khi mot bo vi xiI Iy da nang duoc huang cho th] tnrong nhung thl bo vi xfr 19 d6 can toi iru hoa cho cac img dung he nhung. VI Iy do d6 rna cac b¢ vi xir Iy nay thirong diroc goi la cac bo xiI Iy nhung cao cap. Do vay, thuat ngu bo xir ly nhung va bo vi dieu khien thtrong diroc dung Ian.

Mot trong nhirng yeu cau khat khe cua he nluing la giarn thieu cong suat tieu thu va giam khong gian sir dung. B¢ xu ly nhung dua tren x86 va 680xO, k~ ca khi b6 sung them cong vao/ra, cong COM va b¢ nho ROM tren chip thl deu c6 cong suat tieu thu thap, Cac bo xiI ly nhung cao cap co xu huang tfch hop them chirc nang tren chip CPU va cho phep nguoi thiet ke chon nhtrng dac tfnh nao ho muon sir dung, Thier ke' he thong PC hien cting dang theo xu huang nay. Thong

10

Cdu true va lap trinh ho vi dieu khien 8051

thuong, khi xay dung bang mach chu Pc. ta can c6 CPU, b¢ chip-set chua c6ng vao/ra, bo dieu khien cache, bo nho Flash ROM c6 BIOS va cuoi cung la bo nho cache thu cap. Nhirng thiet ke moi, vf du, Cyrix tuyen bo rang ho dang ItuTI viec tren mot chip chua toan bo mot may tinh PC ngoai trir DRAM, hien cung dang khan tnrong di vao san xuat cong nghiep. C6 Ie trong mot wang lai gan, cluing ta se dtroc nhin thay may tinh PC narn gon tren mot chip.

Hien nay, MS-DOS va Windows dang la chuan cho nhieu he may tinh va ca cac he nhung PC x86 . Trong nhieu tnrong hop, dung may tinh PC x86 cho cac ung dung nhung cao cap la khong tiet kiern, song lai cho phep rut ngan dang ke thai gian phat trien do c6 san thtr vien phan mem phong phu viet tren nen DOS va Windows. Day la mot van de quan trong can luu y khi xay dung he nhung trong thirc teo

U!a chon be? vi dU~u khi~n

C6 bon ho vi dieu khien 8 bit chinh, d6 la: 6811 cua Motorola, 8051 cua Intel Z8 cua Zilog va PIC 16x cua Microchip Technology. M6i loai tren deu c6 mot tap lenh va thanh ghi rieng nen cluing khong W011g thfch Hln nhau. Cung con c6 cac bo vi dieu khien 16 bit va 32 bit. V ~y tieu chuan d~ 11!a chon bo vi dieu khien khi thiet ke la gi? Co ba tieu chuan chfnh la: I) Dap irng yeu cau tfnh toan mot each hieu qua va kinh teo 2) C6 san cac cong ClJ phat trien phan mern, chang han nhir cac trinh bien dich, trinh hop dich va go roi, 3) Ngu6n cung cap be vi dieu khien co nhieu va tin cay.

Chung ta 'Se nghien cuu ro han ve cac tieu chuan nay.

1. Tieu chuan dau tien khi lira chon mot bo vi dieu khien, d6 la phai dap trng yeu cau ve tfnh toan mot each hieu 'qua va kinh teo Do vay, tnroc het can xem xet bo vi dieu khien 8 bit, 16 bit hay 32 bit la thich hop. Mot so tham so ky thuat can diroc can nhac khi lua chon 1'3.:

a. Toe d¢: Toe d¢ Ian nhat rna bo vi dieu khien h6 tro 13 bao nhieu.

b. Ki~u dong vo: La kieu 40 chan DIP, kieu QFP hay 13 kieu dong vo khac (DIP - vo dang 2 hang chan. QFP 1'3. vo vuong det)? Kieu d6ng vo quan trong khi co yeu cau ve khong gian, kieu lap rap va tao mau thir cho san pham cu6i cung,

C. Cong suat tieu thu: La mot tieu chuan can d<}e biet ltru y neu san pharn dung pin hoac ae quy.

II

d. Dung hrong bo nho RAM va ROM tren chip.

e. So chan vao/ra va bo dinh thai tren chip.

f. Kha nang de dang nang cao hieu suat cao hoac giam cong suar tieu thy.

g. Gia thanh tren mot dan vi khi mua so luong Ian: Day la van de c6 anh huang den gia thanh cuoi cung cua san pham. Vi du, c6 be vi dieu khien gia 50 cent khi mua 100.000 bo.

2. Tieu chuan thu hai khi lira chon b¢ vi dieu khien la kha nang phat trien cac san pharn nhu the nao? Vi du, kha nang co san cac trinh hop dich, go roi, bien dich ngon ngir C, mo phong, dieu kien h6 tro ky thuat cung nhir kha nang sv dung trong nha va ben ngoai moi tnrong. Trong nhieu twang hop, str h6 tro cua nha cung cap thir ba cung her sire quan trong.

3. Tieu chuan thtr ba la kha nang san sang dap trng ve so luong a hien tai cung nhu a tuong lai. Doi voi mot so nha thier ke, van de nay tharn chi can quan trong han ca hai tieu chuan dfiu tien, Hien nay, trong cac ho vi dieu khi~n 8 bit hang dau thi 8051 co so luong Ian nhat va c6 nhieu hang cung cap. Nha cung dip la nha san xuat ben canh nha sang che b¢ vi dieu khien. Doi vci 8051 thl nha sang che la Intel, nhung hien nay c6 rat nhicu hang khac cung san xuat. Cac hang nay gam: Intel, Armel, Philips/Signetics, AMD, Siemens, Marra va Dallas, Serniconductior,

Ciing nen luu y rang, Motorola, Zilog va Microchip Technology da danh mot luong dtr trtr Ian d~ (tam bao ve mat thai gian cho cac san pham. Trong nhtrng nam gfin day, cac hang tren cung dil bat dau ban tlur vien vi dieu khien cho ASIC.

1.2 TONG QUAN VE HQ 8051 Tom tat llch Slt phat tri~n 8051

Nam 1981, hang Intel cho ra mat mot bo vi dieu khien diroc goi la 8051. B¢ vi dieu khien nay c6 128 byte RAM, 4K byte ROM, hai bo dinh thai, mot cong noi tiep va bon cong 8 bit. Tflt d deu diroc tlch hop tren mot chip. Luc bay gio; b¢ vi dieu khien nhir vay diroc coi la mot "h¢ thong tren chip ". 8051 la bo XV Iy 8 bit, nrc la CPU chi co th~ lam viec voi 8 bit dir lieu, Dir lieu Ian han 8 bit diroc chia thanh cac dfr lieu 8 bit d~ XV IY. 8051 c6 tat ca bon cong vao/ra, moi c6ng rong 8 bit, xem hinh 1.2.8051 c6 th~ c6 mot ROM tren chip eire dai la 64 K byte. Tuy nhien, hie d6 cac nha san xuar da cho xuat xirong chi 4K byte ROM tren chip. Chi tiet ve van de nay se duoc de cap a phan sau.

12

Cdu true va lap trinh ho vi dieu khiin 8051

8051 da tro nen pho bien sau khi Intel cho phep cac nha san xuar khac san xuat va ban bat ky dang bien the nao cua 8051 rna ho mu6n voi dieu kien ho phai de rna chuang trinh tuong thich voi 8051. Tir do din den su ra doi nhieu phien ban cua 8051 voi cac t6c d¢ khac nhau va dung luong ROM tren chip khac nhau. Tuy nhien, dieu quan trong la mac du co nhieu

bien the cua 8051, nhir khac nhau ve toe d¢ va Bang 1.2. Thong so cua 8051

dung luong nho ROM tren chip, nhung tat cit cac lenh deu wang thfch voi 8051 ban dau, Dieu nay co nghia la, neu chuang trinh diroc viet cho mot phien ban 8051 nao do thl cling se chay diroc voi moi phien ban khac khong phu thuoc van hang san xuat,

B9 vi dieu khi~n 8051

B¢ vi dieu khien 8051 la thanh vien dau tien cua ho 8051. Hang Intel ky hieu la MCS51. Bang 1.2 gioi thieu mot s6 thong s6 ky thu~t cua 8051.

D~c tinh S61lt<;lng
ROM 4K byte
RAM 128 byte
89 dinh thoi 2
Chan vao/ra 32
C6ng noi tisp 1
Ngu6n ngat 6 ROM I+---i On-Chip 1+---1 chuang trinh

CPU

T T

Htnh 1.2. SO' do khol bQ vi dit!u khi~n 8051

ETC <

0).

I--T_im_e_r _o ---i:--} .~

Timer 1 ~ _

CD',

3

P3 TXD

Cae thanh viim khae ella he 8051

Co hai bo vi dieu khien thanh vien khac cua ho 8051 la 8052 va 8031.

13

89 vi die·u. khien 8052

8052 Iii mot thanh vien cua ho 8051. 8052 co tat ca cac thong so ky thuat cua 8051, ngoai ra con co them 128 byte RAM, 4K ROM va mot bo dinh thai nira. Nhu vay, 8052 co tong cong 256 byte RAM, 8K byte ROM (8051 co 4K byte ROM) va ba bo dinh thai. Xern bang 1.3.

Bang 1.3. Mc)t so thong so chinh cac thanh vien ho 8051

£)~c tinh 8051 8052 8031
ROM tren chip (byte) 4K 8K OK
RAM (byte) 128 256 128
B9 d!nh thai 2 3 2
Chan v8o/ra 32 32 32
C6ng noi tiep 1 1 1
Nguon ngat 6 8 6 Nhir thay tir bang 1.3, 8051 la mot twang hop rieng cua 8052. MQi chuang trinh viet cho 805 I deu chay duoc tren 8052. nlurng dieu ngiroc lai la khong dung.

89 vi dieu khien 8031

8031 IA mot thanh vien khac ella ho 8051. Chip nay thuong diroc coi IA 8051 khong co ROM tren chip. D€ co th~ dung duoc chip nay can phai b6 sung ROM ngoai chua chuang trinh can thiet cho 8031. 8051 co chuang trinh a ROM tren chip bi giot han den 4K byte, con ROM ngoai cua 8031 thi co the len den 64 kbyte, Tuy nhien, d~ co th~ truy cap her bo nho ROM ngoai can dung them hai cong, do vay chi con lai hai cong d~ sit dung, Nham khac phuc van de nay, cluing ta co th~ b6 sung them cong vao/ra cho 8031. Chi tiet han Ban co th~ tham khao

chuang 14. '

Cae phlen ban ella 8051

8051 la thanh vien pho bien nhat ella ho 8051, tuy nhien chung ta se khong thay nguyen ph an ky hieu s6 "8051" tren chip. Sa di nhir vay IA do 8051 co nhieu phien ban. VI du voi cac kieu bo nho khac nhau nhir UV-PROM, Flash va NVRAM va cluing deu diroc the hien a ky hieu linh kien, VI du, 8051 voi bo nho UV-PROM diroc ky hieu IA 8751. Phien ban Flash ROM cung diroc nhieu hang san xuat, chang han, cua Armel Corp co ten goi IA AT89C51. Con phien ban NVRAM cua Dalas Semi-Conductor thl goi la DS5000. Ngoai ra con eo phien ban

14

Cdu true vd lap trinh ho vi die'u khien 8051

OTP (kha trinh mot Ian) cling diroc nhieu hang san xuat.

B9 vi dieu khien 8751

Chip 8751 chi co 4K byte bo nho UV -EPROM tren chip. D~ str dung chip nay can co bo dot PROM va b<) xoa UV-EPROM. Do ROM tren chip cua 8751 la UV-EPROM, nen can phai mat 20 phut d~ xoa 8751 tnroc khi diroc lap trinh. VI day la qua trinh mat nhieu thai gian nen nhieu nha san xuat da cho ra mat phien ban Flash Rom va UV -RAM. Ngoai ra con co nhieu phien ban voi cac toe d¢ khac nhau.

B9 vi dieu khiffn A T8951 cue Atmel Corporation

AT8951 la phien ban 8051 co ROM tren chip la b¢ nhc Flash. Phien ban nay rat thich hop cho cac irng dung nhanh VI bo nho Flash co th~ diroc xoa trong vai giay (chtr khong phai 20 phut nhir 8751). Di nhien la d~ dung AT895 I can phai co mot bo dot ROM ho tro bo nho Flash, song lai khong can bo xoa ROM VI bo nho Flash diroc xoa bang bo dot PROM. D~ tien sir dung, hien nay Hang Atmel dang nghien ciru mot phien ban cua AT 89C51 co th~ diroc lap trinh qua cong COM cua may tinh IBM PC va nhir vay se khong can bo dot PROM

Bang 1.4. Cac phien ban ella 8051 ella Atmel (Flash ROM)

Ky hi~m' ROM RAM Chan 1/0 Timer Ngat Vee D6ng vo
AT89C51 4K 128 32 2 6 5V 40
AT89LV51 4K 128 32 2 6 3V 40
AT89C1051 1K 64 15 1 3 3V 20
AT89C2051 2K 128 15 2 6 3V 20
AT89C52 8K 128 32 3 8 5V 40
AT89LV52 8K 128 32 3 8 3V 40 Ghi chu: * Chir C trong ky hieu A T89C51 la CMOS.

Thong s6 ve kieu dong vo va toe d¢ cua bo vi dieu khien cling diroc th~ hien a ky hieu. Vi du, tir bang 1.5, chi! "C" dung tnroc so 51 a ky hieu AT 89C51 - 12PC la M chi cong nghe CMOS (tieu thu nang hrong thap), "12" M chi toe d¢ 12 MHZ va "P" la kieu dong vo DIP, va chi! "C" cuoi cung la ky hieu cho thuong mai (ngiroc voi chi! "M" la quan sir ). AT89C51 - 12PC rat thich hop cho cac thir nghiern cua h9C sinh, sinh vien,

B() vi die-u khie;1 805 J

15

Bang 1.5. Cac phien ban 8051 vOi toe dQ khac nhau cua Atmel

Ky hi~u ToedQ So chan Dong vo Ml:le dieh
AT89C51-12PC 42MHz 40 OTP Tturonq mai Bt) vi dieu khien DS5000 cue Hang Dallas Semiconductor

Mot phien ban pho bien khac nira cua 8051 la DS5000 cua Hang Dallas Semiconductor. B¢ nho ROM tren chip cua DS5000 la NV -RAM. DS5000 co kha nang nap chuang trinh vao ROM tren chip trong khi no van a trong he thong rna khong can phai I<ry ra. Cach thuc hien la dung qua cong COM cua may tinh IBM Pc. Day la mot di~m rnanh nit diroc ua chuong. Ngoai ra, NV -RAM con co uu viet la cho phep thay d6i noi dung ROM theo tirng byte. Nhac lai la, bo nho Flash va EPROM phai dtroc xoa het truoc khi lap trinh lai.

Bang 1.6. Cac phien ban 8051 cua Hang Dallas Semiconductor

Ma linh ki~n ROM RAM Chan 110 Timer Ng~t Vee Dong vo
OS5000-8 8K 128 32 2 6 5V 40
OS5000-32 32K 128 32 2 6 5V 40
OS5000T-8' 8K 128 32 2 6 5V 40
OS5000T-8' 32K 128 32 2 6 5V 40 Ghi chu: * Chit "T' sau k)" hieu "5000" Hi co dong ho thai gian thuc.

Liru y dong h6 thai gian thuc RTC khac voi bo dinh thai Timer. RTC tao va hru gilt thai gian cua ngay (gio; phiit, giuy) va ngay thang (ngay, thang, narn) k~ ca khi Ult ngu6n.

Con co nhieu pl.ien ban DS5000 voi nhirng toe dQ va kieu dong goi khac nhau nhu trinh bay a bang 1.7. Vi du, DS5000-8-8 co 8K NV-RAM va toe do SMHZ. Thong tlurong DS5000-8-12 hoac DS5000T-8-12 la thich hop cho cac nghien CI1U, thir nghiern cua sinh vien,

Bang 1.7. Cac phien ban cua DS5000 vo; cac toe dQ khac nhau

Ma linh ki~n NV- RAM ToedQ
OS5000-8-8 8K 8M Hz
OS5000-8-12 8K 12MHz
OS5000-32-8 32K 8M Hz
OS5000T-32-12 32K 8M Hz
OS5000-32-12 32K 12MHz
DS5000-8-12 8K 12MHz (co RTC) 16

Cdu true va lap trinh hi! vi dieu khie'n 8051

Phien ban OTP cua 8051

Phien ban OTP (One Time Programmable) cua 8051 la cac chip 8051 co th~ lap trlnh diroc mot hin va diroc nhieu hang san xuat khac nhau eung cap. Cac phien ban Flash va NY -RAM thtrong duoc dung de phat trien san pharn mau, Khi san pham mllu diroc ,ho~m Hit th) phien ban OTP cua 8051 duoc dung d~ san xuat hang ioat vl gia tnanu tren mot don V! san ph~m se re hem nnieu.

H9 8051 cua hang Philips

Mot nha san xuat quan trong khac cua ho 8051 la Philips Corporation. Qua thuc, hang nay co mot dai lira chon cac bo vi dieu khien ho 8051 rat rong. Nhieu san pham cua hang da g¢p luon mot so chirc nang nhu bo chuyen d6i ADC, DAC, cong I/O rna rong, ca cac phien ban OTP va Flash.

2.1 BEN TRONG 8051

Chltdng 2

L~P TRINH HQP NGlI 8051

o phan nay cluing ta nghieu ciru cac thanh ghi co ban cua 8051 va each sir dung cluing voi mot so lenh dan gian nhu MOV va ADD.

Cae thanh ghi

Thanh ghi ella 8051 diroc dung M hru tarn thai du lieu hoac dia chi. Cac thanh ghi nay chu yeu co kfch thiroc 8 bit va 8051 cling chi co mot kidu kieu du lieu 8 bit. 8 bit cua thanh ghi diroc sap xep nlur a hlnh diroi, trong do D7 la bit co trong so cao nhat (MSB), con DO la bit eo trong so thap nhat (LSB).

VI chi co mot k ieu du lieu 8 bit, nen moi du lieu Ian hem 8 bit tnroc khi xu ly deu diroc chia thanh cac phan 8 bit. 8051 co nhieu thanh ghi, nen a day cluing ta chi xern xet chu yeu cac thanh ghi da nang. Ban co th~ tham khao them phan phu luc d~ co thong tin day du han v~ cac thanh ghi cua 8051.

A

B

RO

R1
R2
R3
R4
R5
R6
R7 I 07 II 07 II 05 II 04 II 03 II 02 II 01

00

a)

Hlnh 2.1. MQt so thanh ghi ella 8051

a) dang 8 bit; b) dang 16 bit

PC

PC (program counter)

b)

Thanh ghi thirong dtroc Slr dung nhat la A (thanh ghi tfch luy hay noi gon han la thanh chua), B, RO -T R7, DPTR (con tro du lieu) va PC (bo dern chuang trlnh). Trir DPTR va PC la 16 bit, cac thanh ghi con lai deu 8 bit. Thanh ghi tfch luy A

18

Cau true va lap trinh ho vi die'u khien 8051

duoc sir dung a cac phep roan so h9C va logic. De minh hoa, cluing ta se tim hieu thong qua mot so vi du voi lenh ADD va MOV.

L~nh chuy~n dflli~u MOV

Lenh MOV thuc hien sao dii lieu tit vi trf nay den mot VI trf khac. Cu phap cua lenh nhu sau:

MOV dich, nguon

;Sao dv lieu nguon VaG dich

Lenh yeu cau CPU chuyen (trong thirc te Iii sao chep) roan hang nguon vao roan hang dich. Vi du, "MOV A, RO" sao n¢i dung thanh ghi RO vao thanh ghi A. Sau khi lenh nay diroc thirc hien thl thanh ghi A se co gia tri giong nhu thanh ghi

RO. Lenh MOV khong anh hirong den roan hang nguon. 6 doan clurong trinh dtroi day, dllu tien Iii nap thanh ghi A gia tri 55H, sau do chuyen gia tri nay vao cac thanh ghi cua CPU. Luu y dau "#" Iii de thong bao rang do la mot gia trio

MOVA, #55H; ;Nap gia tri 55H vaG thanh ghi A (A = 55H)
MOVRO,A ;Sao nQi dung A VaG RO (bay gia RO=A)
MOVR1, A ;Sao nQi dung A vaG R1 (bay gia R1=RO=A)
MOVR2,A ;Sao nQi dung A VaG R2 (bay gia R2=R1=RO=A)
MOVR3, #95H ;Nap 95H VaG thanh ghi R3 (R3=95H)
MOVA, P' ;Sao nQi dung R3 VaG A (bay gia A=95H) Khi lap trinh bo vi dieu khien 8051 can hru y cac diem sau:

I. Cac gia tri co the diroc nap tnrc tiep vao bat ky thanh ghi nao A, B, RO - R7. Tuy nhien, de thong bao do Iii gia tri tire thai thi can dat ky hieu "#" a tnroc. Xem vi du sau:

MOV A, #23H MOV RO,#12H MOV R1,#lFH MOV R2,#2BH MOV B,#3CH MOV R7, #9DH MOV R5, #OF9H MOV R6, #12

;Nap gia tri 23H VaG thanh ghi A (A=23H) ;Nap gia tri 12H VaG RO (RO 12H)

;N~p gia tri 1FH VaG R1 (R1 1FH)

;N~p gia tri 2BH VaG R2 (R2 2BH)

;N~p gia tri 3CH VaG B (B = 3CH)

;Nap gia tri 9DH VaG R7 (R7 ;N~p gia tri F9H VaG R5 (R5

9DH) F9H)

;Nap tri 12d = OCH VaG R6(R6= OCH)

Liru y a lenh "MOV R5, #OF9H" thi can co so 0 dirng truce F vii sau dau # de bao ding F Ia mot so Hexa chir khong phai Iii mot ky til. Noi each khac, lenh "MOV RS, #F9H" se gay 16i.

L(lp trinh hap ngii 805 J

19

2. Neu cac gia tri tir 0 den F diroc chuyen van mot thanh ghi 8 bit, thl cac bit con lai diroc coi la O. VI du, lenh "MOV, A #5" cho ket qua A=05, nrc la A = 0000 a 101 a dang nhi phan.

3. Chuyen mot gia tri Ian han kha nang chira cua thanh ghi thl se gay ra leli, VI du:

MOV A, #7F2H MOV R2,456

iKhong h~p le vi 7F2H > FFH iKhong h~p le vi 456 > 255 (FFH)

4. D~ nap mot gia tri van mot thanh ghi thi can phai gan dau "#" truce gia tri do. Neu khong co dau "#" tht duoc hi~u la nap gia tri luu tai 6 nho. Vf du, "MOV A, 17H" co nghia la nap gia tri a ngan nho co dia chi 17H vao thanh ghi A va tai dia chi do, dii lieu co th~ co bat ky gia tr] nao tv 0 den FFH. Con d~ nap gia tri 17H VaG thanh ghi A thi can phai co dau "#" tnroc 17H, nrc "MOV A, # 17H". Va cung can luu y la neu thieu dau "#" thl leuh se khong gay leli vi hop ngii cho do la m¢t lenh hop I? Tuy nhien, ket qua se khong dung nhu y muon cua nguoi lap trinh,

L~nh cQng ADD

Lenh cong ADD co cu phap nlur sau:

ADD A,ngu6n iCong to~n hang ngu6n vAG thanh ghi A.

Lenh cong ADD yeu cau CPU cong toan hang nguon voi thanh ghi A va luu

ket qua a thanh ghi A. Vf du, cong hai so 25H va 34H thi can thirc hien theo trinh tv sau:

MOV A,#2SH iNap gi~ trt 2SH VaG A

MOV R2, #34H iN(l.P q i a t.r i 34H vAG R2

ADD A,R2 ;CQng R2 vAG A vA ket qua A=A+R2

Th,!C hien chuong trlnh tren ta duoc A = 59H (25H + 34H = 59H) va R2 = 34H, va noi dung R2 khong thay d6i. Chuong trtnh tren c6 th~ viet thea nhieu each khac nhau phu thuoc van thanh ghi diroc sir dung. Dtroi day la mot trong cac

each viet:
MOV RS,#2SH ;N(l.p gi~ trt 2SH vAG thanh ghi RS
MOV R7,#34H ;Nap gia trt 34H vAo thanh ghi R7
MOV A,#O ;Xo~ thanh ghi A (A=O)
ADD A,RS ;CQng nQi dung RS vAG A (A=A+RS)
ADD A,R7 ;CQng R7 VaG A (A=A+R7=2SH+34H) 20

Cdu true va l~p trinh hi! vi die'u khien 8051

Chuang trlnh nay cho ket qua a A La 59H. Tuy nhien, qua doan chuang trinh tren co the d~t cau hoi la co can chuyen cii hai du lieu vao hai thanh ghi tnroc khi cong cluing voi nhau khong? Cau tra lai la khong can. Hay tim hieu doan chuang trlnh sau:

MOV A,#25H ;N~p tri thu nh§t VaG thanh ghi A (A=25H) ADD A,#34H ;CQng gia tri thu hai la 34H VaG A (A=59H)

Trong vf du tren, 25H va 34H la cac toan hang nrc thai. 6 lenh ADD trong vf

du nay, toan hang nguon co th~ la thanh ghi hoac la dir lieu nrc thai, nhirng roan hang dfch thl luon luon la thanh ghi tfch luy A. Nhir vay, lenh "ADD R2, #12H" la khong hop I~. VI toan hang dich khong phai la thanh ghi A. Tuong nr, lenh "ADD R4, A" cling khong hop I~, VI thanh ghi A phai la toan hang dfch. Tom lai, moi phep toan so h9C cua 8051 luon dung thanh ghi A lam toan hang dich.

Co hai thanh ghi 16 bit cua 8051 duoc dung nun bo dern chuang trlnh PC (Program Counter) va con tro du lieu DPTR (Data Pointer). Muc 2.3 gioi thieu each sir dung chung. Thanh ghi DPTR duoc dung d~ truy cap du lieu va se diroc trinh bay a chuang 5 khi de cap tci cac che d¢ dinh dia chi.

2.2 GIOI THI~U VE LAp TRINH HQP NGa CUA 8051

CPU chi co th~ tfnh toan dUQ'C tren cac so nhi phan va voi tee d¢ rat cao. Tuy nhien, doi vci con ngiroi, neu phai lap trlnh voi cac so nhi phan thl that nham chan va cham chap. Chuang trmh chi gam cac so 0 va 1 la trinh ngon ngjr may.

Thai ky cHlu cua may tinh. cac I~p trinh vien phai viet chuang trtnh diroi dang ngon ngu may. Mac du so thap luc phan (so Hexa) da bieu dien kha hieu qua so nhi phan, song lam viec tren rna may van con la cong viec nang nhoc doi voi con ngiroi. Cuoi cung, hop ngu di'i duoc xay dung, trong do co str dung cac tir goi nho va cung voi nhtrng dac tinh khac ntra, hop ngtr da giiip cho cong viec lap trinh diroc de dang va ft 16i han. Thuat ngfr fll gpi nho hay ttl g<li (mnemonic) thuong diroc sii dung trong cac tai lieu ky thuat may tfnh d~ chi cac rna lenh va tir viet tilt nrong dei de nho. Cac chuang trlnh hop ngu can diroc dich ra dang rna may nho mot chuang trinh diroc goi la trtnh hop dich. Hop ngtt diroc coi la mot ngon ngif b~c thap VI no co quan he tnrc tiep voi cau true ben cua CPU. D~ lap trinh hop nglr, lap trlnh vien can nam virng tat ca cac thanh ghi cua CPU, kfch rhuoc cua chung cung nhu cac chi tiet lien quan khac,

L~p trinh hap ngii' 805 J

21

Ngay nay, Ban co th~ sir dung cac ngon ngir lap trinh khac nhau, nhu Basic, Pascal, C, C++, Java va nhieu ngon ngu khac, Cac ngon ngu nay la nhirng ngon ngfr bac cao vi lap trmh vi en khong can phai tuong tac voi cac chi tiet ben trong CPU. Trinh hop dich diroc dung d~ dich chuang trinh hop ngir ra rna may (doi khi cGng con diroc goi la rna d6i nrong (Object Code) hay rna lenh (Opcode)), con cac ngon ngir bac cao diroc dich thanh rna may bang mot chuang trlnh goi la trinh bien dich. VI du, khi viet mot chuang trlnh bang C, chung ta can sir dung mot trinh bien dich C de dich chuang trlnh ve dang rna may. Tiep theo, chung ta se tim hi~u dang thirc hop ngfr cua 8051 va dung trinh hop dich d~ tao ra mot chuang trlnh chay diroc.

Cau true ella hep nget

Mot chuang trlnh hop ngu bao gom mot chuoi cac dong lenh hop ngtr. Mot lenh hop nglr co mot tir goi nho (mnemonic), va tuy thea tung lenh rna sau do co mot hoac hai roan hang. Tir goi nhc la lenh yeu cau CPU thuc hien con cac toan hang la cac dtr lieu can thao taco

ORG OH
MOV R5,#25H
MOV R7,#34H
MOV A, #0
ADD A,R5
ADD A,R7
ADD A,#12H
HERE: SJMP HERE
END ;B&t d~u (origin) t~i ngan nh6 0 ;N~p 25H vao R5

;N~p 34H vao R7

;N~p 0 vao thanh ghi A

;CQng nQi dung R5 vao A (A=A+R5) ;CQng nQi dung R7 vao A (A=A+R7) ;CQng gia tr~ 12H vao A (A=A+12H) ;0 l~i trong vong l~p

iKet thuc t$p nguon hqp ngu

ChU011g trlnh 2.1. Vi du ve m(>t chU011g trlnh hop ngii

Chuang trinh 2.1 tren day la mot chuoi cac cau lenh hay dong lenh, trong do co cac lenh hop ngu nlur ADD va MOV va cac chi dan. Lenh hop ngtr luon yeu cau CPU phai thirc thi mot nhiem vu, con chi dAn co nhiern vu huang dan cho hop

ngfr chrr khong yeu cau CPU thirc hien hanh dong nao, 0 VI du tren thl ADD va MOV la lenh, con ORG va END la chi dan hop ngff. ORG huang dan hop ngu b6 trf rna lenh bat dau tir ngan nho 0, con END la thong bao cho hop ngfr bier chuang trlnh diroc ket thiic. Nhtr vay, mot chuang trlnh hop ngu luon diroc bat dau va ket thiic bang cac chi dan.

22

eli" true va /iJ,p trinh hq vi die" khiin 8051

Noi chung, lenh hop ngii co cau true gorn 4 tnrong nhir sau:

[ nh an c ] [tv qo i, nh o] [cae t o an h ariq] [; chu q i.a i.]

Dau ngoac vuong la tuy chon, do vay khong phai dong lenh nao cling co du 4 twang nhu tren. Co may diem can hru y nhir sau:

I. Twang nhan cho phep chuang trinh tham chieu den mot dong lenh bang ten nhan. Ten nhan co dQ dai t6i da diroc qui dinh, Can kiem tra do dai toi da cho phep khi viet chuang trinh hop ngir.

2. Tir goi nho (lenh) va cac toan hang la cac twang ket hop voi nhau d~ thirc hien cac nhiem VI,I cua chuang trinh. o cac lenh:

ADD A,B MOV A, #67

thi ADD va MOV la lenh (tu goi nho), can "A, B" va "A, #67" la nhirng toan hang. o hai twang "tir goi nho" va "roan hang" co the la cac gia lenh hoac chi dan hop ngir. Nen nho la chi dan khong tao ra rna lenh nao (rna may) va cluing chi can cho trinh hop dich, Ngiroc lai, cac lenh thl diroc dich ra rna may (rna lenh)

d~ CPU thirc hien. o chuang trinh 2.1, ORO va END la cac chi dan (d6i voi 8051 co the diroc viet diroi dang .ORO va .END). M6i hop ngii co th~ co qui dinh each viet rieng. Van de nay cluing ta se xem xet ky han b muc 2.5.

3. Twang chu giai luon diroc bat dau bang dau cham phay ";". Chu giai co the bat dau b dau dong hoac giira dong. Chu giai diroc trinh hop dich bo qua, nhirng lai can thiet cho cac lap trinh vien. Chu giai la phan tuy chon, tuy vay, I(\p trinh vien nen dung d~ rno ta chuang trinh nham giup cho ngiroi khac dQC va hieu chuang trinh de dang han.

4. ve nhan HERE cua chuang trinh 2.1: sau rnoi nhan tham chieu den mot

lenh can co dau hai cham ":", o lenh nhay ngan SJMP, thi 8051 se lap vo han vong lap nay. Neu he thong co chuang trinh giam sat thi se khong can den vong lap nay va co the xoa khoi chuang trinh,

2.3 HQP D!CH vA CHf\Y CHUaNG TRINH 8051

Cau hoi dat ra, Ja chuang trlnh diroc viet, hop dich va chay nhir the nao?

Trinh tt! thuc hien co the nhir sau:

I. Tnroc het, can sir dung mot trinh soan thao d~ viet diroc chuang trinh, VI du nhir chuang trinh 2.1. Co nhieu trinh soan thao tuyet voi, trong s6 do co the ke

L(Jp trinh hap ngii' 8051

23

nhu EDIT cua MS-DOS hoac Notepad cua Windows. Luu y la, trtnh soan thao phai tao ra tep rna ASCII. Doi voi nhieu trinh hop dich, ten tep can tuan thea guy iroc cua DOS, con phan rna rong cua tep nguon phai la "asm" hay "src" tuy theo trinh hop dich diroc sir dung,

2. Tep nguon co phan rna rong "asm" diroc tao ra a biroc 1 se diroc chirong trinh dich hop ngfr cua 8051 hop dich. Trinh hop dich se dich cac lenh cua tep tin nguon ra rna may va tao ra tep doi nrong, tep liet ke voi phan rna rong urong irng la "obj" va "Ist".

3. Btroc thtr ba goi la lien ket. Trinh lien ket se lien ket mot hoac nhieu tep doi nrong dtS tao ra tep doi urong tuyet doi co phan rna rong "abs",

4. Tiep theo, tep "abs" diroc cap eho chirong trinh chuyen tep d6i urong v~ dang so Hexa goi la "OH" (Object to Hexa Converter) d~ tao ra tep moi co phan rna rong la "Hexa" de co tM nap van ROM. Chuang trlnh nay co a moi bo hop dich cua 8051. Hien nay, cac trinh hop dich dira tren nen Windows da ket hop cac biroc 2 den 4 van mot biroc.

T~p ".asm" va ".object"

Tep "vasm" con diroc goi la tep nguon, va VI Iy do do rna mot so trinh hop dich dung phan rna rong la "src" tir chtr "source" la nguon thay cho "iasm". Hay kiern tra trlnh hop dich 8051 dang sir dung xern dn phan rna rong nao? Nhu da noi truce day, tir tep clurong trinh nguon, trinh trinh hop dich cua 8051 tao ra cimg voi tep dci tuong .objeet, con co rep liet ke "lst" (List file).

T~p li~t ke ".1st"

Tep liet ke la tuy chon, Tep nay htru Ich cho I~p trinh vien vi no liet ke tat ca cac rna lenh, dia chi cling nhir cac 16i rna trinh trinh hop dich phat hien diroc. Nhieu trinh hop dich d<:tt ngam dinh la khong tao

myfile.lst

myfile.obj

myfile.abs •

Trinh chuyen doi OH

myfile.hex

Hlnh 2.2. Cac biroc xay dung chuang trlnh hop ngfr

24

Cdu true va It)p trinh ho vi die'u khie'n 8051

ra tep tin liet ke, Lap trinh vien sir dung tep liet ke de xac dinh cac 16i cu phap, Chi sau khi da sua het cac 16i diroc danh dau trong tep li¢t ke, thl tep d6i nrong moi san sang lam d<iu vao cho chuang trinh lien keto

1 0000 ORG OH ;B~t dau (; dia chI 0
2 0000 7D25 MOV R5,#25H ;N<;l.p 25H vao R5
3 0002 7F34 MOV R7,#34H ;N<;l.p 34H vao R7
4 0004 7400 MOV A, #0 ;N<;l.p 0 vao A (xoa A)
5 0006 2D ADD A,R5 ;CQng R5 vao A (A=A+R5)
6 0007 2F ADD A,R7 ;CQng R7 vao A (A=A+R7)
7 0008 2412 ADD A,#12H ;CQng 12H vao A (A=A+12H)
8 OOA BCEF ? vong
HERE: SJMP HERE ;0 l<;l.i lap
9 OOOC END ;Ke't thuc tep .asm
Chuang trlnh 2.2. Tep liet ke ,. ,.' ......,.. ?

2.4 SQ 8EM CHUaNG TRINH VA KHONG GIAN ROM CUA 8051

Be;, dem chU'dng trinh

Mot thanh ghi quan trong khac cua 8051 la bo dern chuang trlnh. B(> dern chuang trtnh co nhiern vu tro den dia chi cua lenh ke tiep din diroc thirc hien. M6i khi CPU nhan rna lenh tir bo nho ROM, thl bo dern chuang trinh tang len de tro den lenh ket tiep, B(> dern chuang trinh cua 8051 rong 16 bit, dieu do co nghia la, 8051 co the truy cap diroc dia chi chuang trlnh tir 0000 den FFFFH, tong cong la 64k byte. Tuy nhien, khong phai Hit ca moi thanh vien cua 8051 deu co du 64k byte ROM tren chip. Van M tiep theo la, khi 805 \ duoc bat nguon thl dia chi khoi dau diroc bat dAu tir dau?

aja chi bAt d~u khi 8051 dU'<;1C cap nguon

M6i ho vi dieu khien khi bat nguon deu diroc bat dAu tir nhirng dia chi khac nhau. D6i voi ho 8051 thi dia chi bat dAu deu tir 0000. Bat nguon co nghia la cap dien ap Vee den chan RESET nhu se trinh bay a chuang 4. Noi each khac, khi 8051 diroc cap nguon, thl bo dern chuang trlnh co gia tri 0000. Dieu nay co nghia la no se thirc hien rna lenh dau tien diroc liru a dia chi ROM OOOOH. Tai sao lai a vi trf OOOOH cua b(> nho ROM? Do la nho chi dfin ORO a chuang trinh nguon nhtr da trlnh bay tnroc day. Diroi day la trlnh nr hoat dong cua bo dern chuang trlnh trong qua trinh nhan va tlurc thi mot chuang trinh mau.

Lgp trinh hap ngii 805 J

25

Ma chLtdng trinh a ROM

D~ hieu r5 hon v~ bo dern chuang trinh, cluing ta se xem xet hoar dong cua b¢ dern chuang trinh moi khi nhan va thuc hien lenh, Tnroc het, cluing ta khao sat mot Ian nira tep liet ke cua chuong trinh mau hru a ROM. Nhu co th~ thay, rna lenh va toan hang cua tung lenh diroc liet ke a ben trai cua tep liet ke.

0000 ORG OH
0000 7D25 MOV R5,#25H
0002 7F34 MOV R7,#34H
0004 7400 MOV A,#O
0006 2F ADD A,R5
0007 2F ADD A,R7
0008 2412 ADD A,#12H
OOOA 80FE HERE: SJMP HERE
oooe END
Chuong trinh 2.3. Tep liet ke Bang 2.1. NQi dung tep liet ke cua chuong trinh 2.3

D!a chi ROM Ngon ngfr may HQ'p ngfr
0000 7D25 MOV R5,#25H
0002 7F34 MOV R7,#34H
0004 7400 MOV A,#O
0006 2D ADD A,R5
0007 2F ADD A,R7
0008 2412 ADD A,#12H
OOOA 80EF HERE: SJMP HERE Sau khi chuang trinh diroc dot vao ROM cua ho 8051, nhtr 8751, AT895 I hoac OS5000 thl rna lenh va toan hang diroc dat luon bat d<iu til dia chi 0000.

Noi dung ROM cua chuang trlnh 2.1 diroc gioi thieu a bang 2.2. Tai dia chi 0000 co rna 70 la rna lenh chuyen mot gia tri vao thanh ghi R5, con dia chi 0001 chua toan hang (gia tri 25H) can diroc chuyen vao R5. Do vay, lenh "MaY R5, #25H" c6 rna Ja "70 25" trong do 70 la rna lenh, con 25 la toan hang. Tuong tu, rna may "7F 34" diroc ghi a dja chi 0002 va 0003 bieu dien rna lenh va toan hang cua lenh "MaY R7, #34H". Cung nlur vay, rna may "74 00" tai dia chi 0004 va

26

Cdu true va lap trinh ho vi die'u khien 8051

0005 la bieu dien lenh "MOY A, #0". 6 nho 0006 Bang 2.2. N6i dung ROM c6 rna 20 la rna lenh cua "ADD A, R5", con 6 nho 0007 co noi dung 2F la rna lenh cua "ADD A, R7". Ma lenh cua lenh "ADD A, #12H" diroc d~t 6 6 nho 0008 va toan hang 12H diroc dat 6 6 nho 0009. 6 nho OOOA co rna lenh cua lenh SJMP va dia chi dfch cua no duoc d~t 6 6 nho OOOB. Ly do VI sao dia chi dfch la FE se duoc giai thfch 6 chuang 3.

Trinh tl/ thirc hi~m chuang trinh

Gia Slr chuang trinh tren da duoc ghi vao ROM cua 8051 (hoac 8751, AT 8951 hay DS5000) thi trinh nr cac biroc hoat dong cua 8051 khi diroc cap nguon nhir sau:

I. Khi 8051 duoc b~t nguon, b¢ dem chuang trinh PC co noi dung 0000 va bat duu nap rna lenh dau tien tir vi tri nho 0000 cua ROM chuang trmh. Doi voi chuang trinh neu tren, d6 la rna 70 (chuyen mot roan hang vao R5). Khi thirc hien ma lenh, CPU nhan gia tri 25 va chuyen vao R5. Den day viec thuc hien mot lenh diroc ker thiic. Sau do, bo dern chuang trinh duoc tang len d~ tro den 6 nho 0002 (PC = 0002), tai day co chua rna 7F la rna cua lenh chuyen mot toan hang vao R7 "MOY R7, ... ".

2. Khi thirc hien rna lenh 7F thi gia tri 34H diroc chuyen vao R 7 sau d6 PC diroc tang len 0004.

3. 6 nho 0004 chua rna lenh cua lenh "MOY A, #0". Lenh nay diroc thirc hien va sau do PC tang len 2 dan vi: PC= 0006. Liru y la tat ca cac lenh tren deu la nhirng lenh 2 byte, nrc la moi lenh chiern hai 6 nho.

4. Voi PC = 0006, bo dern chuang trinh tro den lenh ke tiep la "ADD A, R5".

Day la lenh mot byte, sau khi thirc hien lenh PC = 0007.

5. Ngan nho 0007 chua rna 2F la rna lenh cua "ADD A, R7". Day ding la lenh mot byte, sau khi thirc hien lenh, PC diroc tang len 0008. Qua trinh nay cu tiep tuc cho den khi Ult ca moi lenh deu diroc nhan va thirc hien.

Do b¢ dern chuang trinh co y nghia va each thirc lam viec nhir vay, nen 6 mot so bo vi xu ly, dac bier la dong Intel x86, bo dern chuang trinh con diroc goi la con tro lenh IP (Instruction Pointer).

Dja chi Ma I~nh
0000 70
--
0001 25
0002 7F
0003 34
0004 74
0005 00
0006 20
0007 2F
0008 24
0009 12
~-
OOOA 80
0008 FE L(Jp trinh IWp ngif 8051

Ban do nhd ROM cua hQ 8051

Nhu ta da thay 6 chuang tnroc, mot so thanh vien ho 8051 chi co 4k byte b¢ nho ROM tren chip, VI du 8751, AT 8951, va mot so khac nhu AT895 I co 8 k byte ROM, DS5000-32 cua Dallas Semiconductor co 32k byte ROM. Dallas Semiconductor cling co mot 119 8051 voi ROM tren chip IA 64k byte. Diem can luu y IA khong co thanh vien nao cua ho 8051 co tht truy C~lP diroc tren 64k byte rna lenh, VI bo dern chuang trmh cua 8051 la 16 bit (dai dia chi tir 0000 den FFFFH). Va diem luu y thtr hai do la, lenh dau tien ella ROM chuang trmh deu d~t (1 nooo, con Ienh cuoi cling phu thuoc vao dung hrong ROM tren chip cua moi thanh vien ho 8051. Nhu vay, 8751 va AT 8951 vai 4k byte ROM thi dai dia chi se tir 0000 den OFFFH. Do do, ngan nho dau tien co dia chi 0000 va ngan nho cuoi cling co dia chi OFFFH. Hay xet VI du sau:

Vi d1,l2.1

TIm dia chi bo nho ROM cua nhfrng thanh vien ho 8051 sau day.

a) AT 8951 (hoac 8751) voi 4k byte.

b) DS 5000.32 voi 32k byte.

Ghli:

a) V6i 4k byte khong gian nho ROM tren chip, ta co 4096 byte bang 1000H 6 dang Hcxa (4x 1024 = 40960 = 1000 H). B¢ nho nay diroc sap xep trong cac ngan nho tir 0000 den OFFFFH. Luu y 0 luon la ngan nho dau tien.

b) V6i 32k byte nho ta co 32.768 byte, (32x 1024). Chuyen 32.768 v~ so Hexa ta nhan diroc gia tri 8000H. Do vay, khong gian nho la dai tir 0000 den 7FFFH.

0( Byte.

0( Byte. 00000 OFFF

0000 Do( Byte • 1FFF

8752 AT89C52

0000

8751 AT89C51

7FFF '-- __ ----'

OS5000-32

Hinh 2.3. Dai dia chi cua ROM tren chip cua mot s6 thanh vien ho 8051

27

28

Cdn true va l~p trinh hi! vi dUu khiln 8051

2.5 KIEU DU LI~U vA CHI DAN Ki~u dii li~u va chi d~n cua 8051

B¢ vi dieu khien chi co mot kieu dii' lieu, do la kieu 8 bit, va d¢ dai cua thanh ghi cling la 8 bit. Nhiern VI;! cua lap trinh vien doi v6'i dii' lieu tren 8 bit la phan chia dtr lieu thanh cac phan 8 bit (tir 00 den FFH hay tir 0 den 255) de CPU xu IY. VI du ve xu Iy dii' lieu 16'0 han 8 bit diroc trinh bay a chuang 6. Dtr lieu cua 8051 co the la so am hoac so duong.

Chi d~n dlnh nghia byte DB

DB la chi dan dinh nghia dii' lieu 8 bit va diroc su dung het sire rong rai khi lap trinh hop ngir, 8 bit dii' lieu duoc dinh nghia co the a dang thap phan, nhi phan, Hexa hay ASCII. Doi v6'i dii' lieu thap phan thi co chir "0" sau so thap phan, so nhi phan thl co chfr "B" va dii' lieu dang Hexa thl co chir "H". Va du so diroc dinh nghia a dang nao, thi hop ngfr luon chuyen ve dang Hexa. Truong hop rna sir dung la ASCII thi dan gian la d~t ky tv vao 'ddu nhay dan'. Trinh hop dich se gan rna ASCII so hoac ky nr mot each tv dong. DB la chi dan co the su dung de dinh nghia chuoi ky nr ASCII co nhieu han 2 ky nr, Do vay, DB co the diroc dung de dinh nghia cac dii' lieu ASCII. Diroi day la mot so VI du dung dinh nghia DB:

ORG 500H

DATAl: DB 28 ;s6 th~p phan (=lCH)
DATA2: DB OOl10101B ;s6 nht phan (=35H)
DATA3: DB 39H ;s6 dariq Hexa
ORG 510H
DATA4 : DB " 2591" ;Cac '" ASCII
so
ORG 518H
DATA5: DB "My name is Binh" ;Ky tv ASCII Doi voi chuoi ASCII co the su dung dau nhay dan hoac nhay kep. Neu dau nhay dan cling diroc dung de chi ky tv hay sa hiru each cua rn¢t so ngon nglr (nhir a tieng Anh chang han), thl dinh nghia ky ttf ASCII hie do nen su dung dau nhay kep, VI du, "Nha 0' Leary". Chi dan DB cling con diroc dung de cap phat bo nho thea kfch thiroc byte.

MQt so chi d~n thlldng g~p cua trlnh hop dlch ORG (origin)

Chi dan ORG diroc dung de bao dia chi bat dau cua chuang trinh. So sau ORG co the a dang Hexa hoac thap phan, Neu co chtr H dang sau so thi do la a

Lap trinh hap ngic 8051

29

dang Hexa, con neu khong co thl a dang thap ph an va trinh hop dich se chuyen thanh so Hexa. Mot so trinh hop dich sir dung dau cham dimg tnroc ".ORO" thay cho "ORO".

EQU (Equate)

Duoc dung de dinh nghia mot hang so. ChI dan EQU khong Slr dung 0 nho de cat dfr lieu, rna thirc hien gan mot hang so cho nhan sao cho khi nhan xuat hien trong chuang trinh thi gia tri hang so se dtroc thay the cho nhan, 6 vf du sau dung chi dan EQU d~ gan gia tri cho bo dern va sau do nap gia tri hang cho thanh ghi RS.

COUNT EQU 25

MOV R3, #count

Sau khi thuc hien lenh "MOY R3, #COUNT" thi thanh ghi R3 se co gia tri 25 (chu y den dau #). Vay uu diem cua viec sir dung chi dan EQU la gi? Gia Slr co mot hang so (mot gia tri co dinh) diroc dung a nhieu cho khac nhau trong chuang trlnh va lap trinh vien muon thay d6i gia tri do. Nho Slr dung chi dan EQU, I~p trlnh vien chi can thay d6i gia tri mot lan va trinh hop dich se tv dong thay d6i gia tri tai moi vi trf rna hang xuat hien,

END

Mot chi dan quan trong khac do la END. Chi dan nay bao cho trinh hop dich bier ket thuc tep nguon (asm). Chi dan END la dong cuoi cung cua chirong trinh 8051, co nghia la 0 ma nguon thi moi cau lenh sau chi dan END deu bi trinh hop dich bo qua. Mot so trlnh hop dich sir dung .END co dau cham dung tnroc thay cho END.

Quy dinh tim nhan

Voi each chon ten nhan co nghia, I~p trinh vien co the lam cho clnrong trlnh tra nen sang sua va de bao trt sau nay. Co mot so quy dinh rna cac ten nhan can tuan theo. Thir nhat la, ten nhan phai la duy nhar, Cac ten diroc sir dung lam nhan a hop ngir gam cac chfr cai viet hoa, viet thuong, cac so tir 0 den 9 va cac dau dac bier nhir: dau hoi (?), dau gach diroi (_), dau do la ($) va dau cham (.), dau a cong (@). Ky tv d&u tien cua nhan phai la mot chir cai va khong the la chir so. M6i trinh hop dich co mot so tir du trfr la cac tir goi nho cho cac lenh va khong diroc dung lam nhan trong chuang trmh, YI du nhir "MOV" va "ADD". Ben canh cac

30

Cdu true va lijp trinh ho vi die'u khien 8051

tv gci nho can co mot so tv du trii khac, Nen kiem tra ban li~t ke cac tv du phong cua hop ngii dang sir dung,

2.6 cAe BIT co vA THANH GHI BAc B~T PSW COA 8051

Cling nhir cac bo vi xir 19 khac, 8051 co mot thanh ghi co de bao cac dieu kien so hoc, Thanh ghi co trong 8051 duoc goi la thanh ghi tv trang thai chirong trinh PSW.

Thanh ghi tll tranq thai chtronq trinh PSW

Thanh ghi tv trang thai chuong trinh PSW (Program Status Word), hay can goi la thanh ghi co la thanh ghi 8 bit. Thanh ghi PSW co 8 bit, nhirng chi 6 bit diroc 8051 sir dung. Hai bit chua dung la cac co rna nguoi dung co th~ dinh nghia diroc. Bon trong so cac co la co dieu kien, nghia la, chung bao mot so dieu kien diroc thiet lap do ket qua thirc hien mot lenh, Bon co nay la: co nho CY (carry), co phu AC (auxiliary cary), co chan Ie P (parity) va co tran OV (overflow).

Nhir giai thieu 6 hinh 2.4, cac bit PSW3 va PSW4 la RSO va RSI va diroc dung d~ thay d6i cac thanh ghi bang. PSW5 va PSW I la cac bit co trang thai da nang va lap trinh vien co the sir dung cho muc dich cua minh.

Diroi day la giai thfch SO liroc ve 4 bit co cua thanh ghi PSW. CO' nhci CY

Co nay diroc thiet lap moi khi co nho tir bit 07 va la ket qua cua lenh cong hoac trir 8 bit. Co th~ thiet lap tnrc tiep co CY len I hoac xoa ve 0 bang lenh "SETB C" va "CLR C", hay noi each khac la "thiet lap co nho" va "xoa CO nho".

CO' nhci phv AC

Co nay bao co nho tv bit 03 sang 04 6 phep cong ADD hoac trir SUB. Co nay diroc cac lenh so hoc rna BCD sir dung,

CO'b;jc P

Co bac (co chan le) phan anh so bit I trong thanh ghi A la chan hay le, Neu thanh ghi A chua mot so chan cac bit 1 thl P = 0 can chua mot so le bit 1 thl P= 1.

CO' trim OV

Co duoc thiet lap moi khi ker qua cua phep tfnh so co dau qua Ian lam cho bit cao bi tran vao bit dau, Nhin chung, co nho diroc dung d~ phat hien 16i trong cac phep tfnh so hoc khong dau, can co tran duoc dung d~ phat hien 16i trong cac phep tinh so hoc co dau va diroc ban chi tier 6 chuang 6.

CY AC FO RS1 RSO OV P
CY PSW.7 ; Co nho
AC PSW.6 ; Co phu
PSW.5 ; Danh cho ngLfoi dung Sll' dunq muc dfch chung
RS1 PSW.4 ; Bit = 1 chon bang thanh ghi
RSO PSW.3 ; Bit = 0 chon bang thanh ghi
OV PSW.2 ; Co tran
PSW.1 ; Bit danh cho ngLfoi dung dinh nghia
P PSW.O ; Co chan, 1E'!l. Thiet lap/xoa bang phan cCmg
RS1 RSO Bang thanh ghi f)ia chi'
0 0 0 OOH - 07H
0 1 1 08H - OFH
1 0 2 10H-17H
1 1 3 18H - 1FH H1nh 2.4. Thanh ghi tir trang thai PSW L~nh ADD va PSW

Bay gio ta xern xet Hie dong cua lenh ADD len cac bit CY, AC va P cua thanh ghi PSW. Di nhien, lenh ADD khong chi tac dong len ba cO- nay rna ca co ov. Rieng co OV se duoc n6i den 6 chuang 6 VI c6 lien quan den phep tinh so hoc cua cac so c6 dau.

Cac VI du tir 2.2 den 2.4 minh hoa tac dong cua lenh ADD len cac bit ca.

Bang 2.3. Cac lenh anh hUOng tm bit co

L~nh CY OV AC L~nh CY OV AC
ADD X X X SETBC 1
ADDC X X X CLRC 0
SUBB X X X CPLC X
MUL. 0 X ANL C,bit X
DIV 0 X ANL C,/bit X
DA X ORL C,bit X
RRC X ORL C,/bit X
RLC X MOV C,bit X
SETB C 1 CJNE X 31

32

Cdu true va lap trinh ho vi die'u khien 8051

Vi d1,l2.2

Hay xac dinh trang thai cac bit co CY, AC va P sau lenh cong 38H voi 2FH:

MOY A,#38H

ADD A, #2FH

Giai:

; Sau khi cong A = 67H, CY = 0

38 00111000

+ 2F

00101111

67 01100111

Co CY = 0 VI khong co nho tir 07

Co AC = 1 VI co nho tir 03 sang 04 Co P = 1 VI thanh ghi A co 5 bit 1 (le)

§U2.3

I Hay xac dinh trang thai cac co CY, AC va P sau phep cong 9CH voi 64H.

Giai:

9C + 64

10011100 01100100

100 00000000

Co CY = 1 VI co nho qua bit 07

Co AC = 1 VI co nho tir D3 sang D4

Co P = 0 VI thanh ghi A kh6ng co bit I nao (chan)

--------------------------~

Vi d1,l2.4

Hay xac dinh trang thai cac CO CY, AC va P sau phep cong 88H voi 93H.

Giai:

88 10001000

+ 93 10010011

liB 000 I 1 0 1 1

Co CY = 1 VI co nho tir bit 07

Co AC = 0 VI khong co nho tir D3 sang 04 Co P = 0 VI sO' bit 1 trong A la 4 (chan)

Lap trinh hop Ilgi18051

33

2.7 SANG THANH GHI vA NGAN XEP CUA 8051

8051 c6 tift ca 128 byte RAM. Trong muc nay chung ta xem xet phan be cua 128 byte RAM nay va khao sat cong dung ella chung khi sir dung lam thanh ghi va ngan xep.

T6 ehlre khong gian bC) nhe RAM cua 8051

8051 c6 128 byte RAM (mot so thanh vien, VI du 8052 c6 256 byte RAM). 128 byte RAM ben trong 8051 diroc gan dia chi tir 00 den 7FH. Nhir ta se thay 6 chuang 5, cluing c6 th~ diroc truy cap tnrc tiep nhu cac ngan nho. 128 byte RAM nay duoc phan chia thanh tung nh6m nhu sau:

I. Tu ngan nho 00 den 1 FH, tong cong 32 byte, duoc danh lam cac bang thanh ghi va ngan xep,

2. re ngan nho 20H den 2FH, tong cong 16 byte, diroc danh lam bo nho doc/ghi dinh dia chi duoc thea

bit. Chuang 8 se ban chi tier v~ bo Hinh 2.5. T6 chirc RAM ella 8051

nho va cac lenh dinh dia chi bit.

3. Tir ngan nho 30H den 7FH, tong cong 80 byte, diroc dung de hru thong tin khi d9C va ghi, hay nhir vitn thirong goi la bang nhap. 80 byte RAM nay thirong duoc cac l~p trlnh vien sir dung d~ hru dCi' lieu va tham so. Con 6 cac chuang sau, cluing ta se slr dung de liru dCi' lieu qua cac cong vao/ra vao CPU.

Bang thanh ghi cua 8051

Nhu da noi 6 tren, tong cong 32 byte RAM diroc danh rieng cho cac bang thanh ghi va ngan xep, 32 byte nay diroc chia thanh 4 bang thanh ghi, m6i bang c6 8 thanh ghi tu RO den R7. Cac ngan nho tir 0 den 7 la bang 0 gorn 8 thanh ghi RO-R, trong d6 RO 6 ngan nho 0, R 1 6 ngan nhc 1, R2 6 ngan nh« 2 v.v. Bang thanh ghi thu hai (cflng RO den R7) bat dilu tlr ngan nho 8 cho den ngan nho OFH. Bang thir ba bat dau tir ngan nho 10H den 17H va cuoi cung tir ngan nho 18H den 1FH la dung cho bang thli tu'.

RAM bang nhap

7F
30
2F
20
1F
18
17
10
OF
08
07
00 RAM dinh dja chi bit

Bang thanh ghi 3

Bang thanh ghi 2

Bang thanh ghi 1 (ngan xep)

Bang thanh ghi 0

34 Cau true va It;ip trtnh hl} vi die'u khiin 8051

Bang 0 Bang 1 Bang 2 Bang 3
7 1 R7 F 1 R7 I 178Ej 1F R7
6 1 R6 Ern 16 R6 1E R6
5 1 R5 o R5 15§§ 10 R5
41 R4 ci R4 1 14 R4 1C R4
3m BI R3 1 13 R3 1B R3
2 R2 A§§ 121 R2 I 1A R2
1 1 R1 1 9 R1 111 R1 1 19 R1
o I RO 1 8 RO 101 RO 1 18 RO

Hinh 2.6. Cac bang thanh ghi va dla chi RAM Nhu c6 th~ thay a hlnh 2.5, bang I dung chfnh khong gian RAM lam ngan xep, Day la mot van quan trong trong lap trinh 8051. Nhu V?y d~ b6 trf ngan xep, chung ta hoac la sir dung bang 1 hoac phai dung mot khong gian RAM khac,

Vi d1,l2.5

Xac dinh noi dung cua cac ngan nho RAM sau khi thuc hien doan chuang

trinh sau:
MOVRO, #99H N.;:tp RO gia trt 99H
MOVRl, #8SH N.;:tp Rl gia trt 8SH
MOVR2, #3FH N.;:tp R2 gia trt 3FH
MOVR7, #63H N.;:tp R7gia trt 63H
MOVRS, #12H N.;:tp RS gia trp2H
Giai: Sau khi thuc hien chuang trlnh tren ta c6:

Ngan nho 0 cua RAM c6 gia tri 99H Ngan nho I cua RAM c6 gia tri 85H Ngan nho 2 cua RAM c6 gia tri 3FH Ngan nho 7 cua RAM c6 gia tri 63H Ngan nho 5 cua RAM c6 gia tri 12H

Ldp trinh hop ngil8051

35

BAng thanh ghi mac dlnh

Nhu da biet, cac ngan nho tir 00 den IF duoc danh cho bon bang thanh ghi, vay bang thanh ghi nao can duoc truy cap moi khi 8051 duoc cap ngu6n? Cau tra loi la bang thanh ghi O. D6 la cac ngan nho RAM s6 0, I, 2, 3, 4, 5,6 va 7 diroc truy cap voi ten lap trinh la RO, R I, R2, R3, R4, R5, R6 va R7. 01 nhien str dung ten d~ truy cap ngan nho thuan Iqi hon nhieu so vci sir dung tnrc tiep cac ngan nho d6. Hay xem vi du sau.

VI dl;l2.6

Hay viet lai chuang trinh a vi du 2.5 str dung dia chi RAM (rna khong dung ten cac thanh ghi).

Giai:

Day diroc goi la che d¢ dinh dia chi true tiep va str dung dia chi ngan nho RAM lam dia chi dfch. Xem chi tier a chuang 5 ve che d¢ dinh dia chi.

MOV OO,#99H ;Nc;tp thanh ghi RO gia trt 99H
MOV Ol,#85H ;Nap thanh ghi Rl gia tri 85H
MOV 02,#3FH iNap thanh ghi R2 gia trt 3FH
MOV 07,#63H ;Nap thanh ghi R7gia trt 63H
MOV 05,#12H ;Nc;tp thanh ghi R5 gia tri 12H Chuye;n bang thanh ghi

Nhu dii noi a tren, bang thanh ghi 0 la mac dinh khi 8051 diroc cap nguon.

Chung ta co th~ chuyen sang cac bang thanh ghi khac bang each su dung bit 03 va 04 cua thanh ghi PSW nhu gioi thieu a bang 2.4.

Bit 03 va 04 cua thanh ghi PSW con duoc viet diroi dang PSW.3 va PSW.4

va chung c6 the diroc truy cap bang cac lenh djnh dia chi theo bit nhu SETB va CLR. VI du, "SETB PSW.3" se thiet lap PSW.3 va chon bang thanh ghi 1. Chung ta xern xet VI du 2.7 diroi day.

Bang 2.4. Bit chQn bang thanh ghi RSO va RSI

RS1 (PSW.4) RSO (PSW.3)

Bang a

a

a

Bang 1

a

1

Bang 2

1

a

Bang 3

1

1

36

Cdu true va lap trinh ho vi die'u khiln 8051

Vi dl;12.7

Hay xac dinh noi dung cac ngan nho RAM cua doan chirong trinh sau:

SETB PSW.4 ;ChQn bang thanh ghi 4

MOVR O,#99H ;N~p thanh ghi RO gia tr~ 99H ;N~p thanh ghi Rl gia tr~ 85H ;N~p thanh ghi R2 gia trt 3FH ;N~p thanh ghi R7gia trt 63H ;N~p thanh ghi R5 gia trt12H

MOVR 1,#85H
MOVR 2,#3FH
MOVR 7,#63H
MOVR 5,#12H
Giai: Theo mac dinh PSW.3 = 0 va PSWA = O. Do vay, lenh "SETB PSWA" se bat bit RS I = I va RSO = 0, khi do bang thanh ghi RO den R 7 s6 2 diroc chon, Bang 2 sir dung cac ngan nho tir 10H den 17H. Nen sau khi thirc hien doan chirong trinh tren ta co noi dung cac ngan nho nhu sau:

Ngan nho vi trf 10H co gia tri 99H Ngan nho vi trf IIH co gia tri 85H Ngan nho vi trf 12H co gia tri 3FH Ngan nhc vi trf 17H co gia tri 63H Ngan nho vi trf ISH co gia tri 12H

Ngan xep cua 8051

Ngan xep la mot vung bo nho RAM diroc CPU sir dung d~ hru thong tin tarn thai. Thong tin nay co th~ la dO' lieu hoac dia chi. CPU can khong gian hru tru nay VI s6 cac thanh ghi bi han che,

Truy c~p ngan xep

Neu ngan xep la mot vung cua bo nho RAM thi phai co cac thanh ghi trong CPU tro den. Thanh ghi diroc dung d~ tro den ngan xep diroc goi la thanh ghi con tro ngan xep SP (Stack Pointer). Con tro ngan xep cua 8051 rong 8 bit, nrc la chi co th~ tro diroc cac dia chi tir 00 den FFH.

Khi 8051 diroc cap nguon thl SP chua gia tri 07, co nghia la ngan nho 08 cua RAM la ngan nho dau tien diroc dung lam ngan xep. M6i Ian liru thanh ghi vao ngan xep diroc goi la cat va lenh nrong irng la PUSH, con ngiroc lai, m6i Ian nap noi dung ngan xep tra lai thanh ghi diroc goi lay ra va lenh nrong irng la POP. D~ hi~u them v~ qua trinh lam viec cua ngan xep cluing ta se xem xet cac lenh PUSH va POP diroi day.

Ldp trinh }UlP ngti 8051

Cat thanh ghi V80 ngsn xep

a 8051 con tro ngan xep luon tro den ngan nho sir dung cu6i cung (goi la dinh ngan xep). Khi cat dtr lieu vao ngan xep thl con tro ngan xep SP diroc tang len I. LUll 9 ding van de nay d6i vci cac bo vi xiI 19 co the khac, vi du, d6i voi cac be) vi xir 19 x86, SP giam xu6ng khi cat dii lieu vao ngan xep, Xet VI du 2.8 diroi day, ta thay rang moi khi lenh PUSH diroc thuc hien thl noi dung cua thanh ghi diroc cat vao ngan xep va SP diroc tang len 1. Di nhien dieu do la cho tnrong hop dil' lieu I byte. De cat gia tri thanh ghi vao ngan xep, I~p trlnh vien cGng co the siI dung dia chi RAM cua chung , Vi du, lenh "PUSH 1" la cat thanh ghi R 1 vao ngan xep,

Vi dl;l2.8

Hay xac dinh ngan xep va con tro ngan xep a dean chuang trinh sau. Gia thiet vung ngan xep la mac dinh,

MOV R6, #25H

MOV R1, #12H
MOV R4, #OF3H
PUSH 6
PUSH 1
PUSH 4
Gial:
Sau PUSH 6 PUSH 1 PUSH 4
OB OB OB OB
OA OA OA OA F3
09 09 09 12 09 12
08 08 25 08 25 08 25
B~t dau SP=07 SP=08 SP=09 SP=OA
Lay ngi dung tit ngsn xep Lay noi dung ra tir ngan xep tro lai thanh ghi la qua trinh ngiroc voi cat noi dung thanh ghi vao ngan xep. M6i Ian lay ra thi byte tren dinh ngan xep diroc sao vao thanh ghi diroc xac dinh trong lenh va con tro ngan xep giam xu6ng 1. Vi du 2.9 minh hoa lenh lay noi dung ra khoi ngan xep.

37

38

Cdll true va 14p trinh hi! vi die'u khiin 8051

Vi d1;l2.9

Khao sat ngan xep va xac dinh noi dung cua cac thanh ghi va SP sau khi thirc hien doan chuang trinh sau day:

POP3 ;L§y ngan xep tra l?i R3

POP5 ;L§y ngan " tra l?i R5
xep
POP2 ;L§y ngan " tra lai R2
xep
Giai:
Sau: pOP 3 POP 5 POP 2
08 54 08 08 08
OA F9 OA F9 OA OA
09 76 09 76 09 76 09
08 6C 08 6C 08 6C 08 6C
Bat dau SP=08 SP=OA SP=09 SP=08
Gi6'i hQn tren cue ngan xep Nhtr da,noi b tren, cac ngan nho RAM tir 08 den IFH co th~ diroc dung lam ngan xep, Va khi do, cac ngan nho RAM tir 20H den 2FH danh cho bo nho dinh dia chi bit va khong th~ dung lam ngan xep. Neu trong mot chuang trinh can co ngan xep 100 han 24 byte (08 den I FH = 24 byte) thl ta co th~ d6i SP d~ tro den cac ngan nhc 30 den 7FH. Dieu nay duoc thirc hien boi lenh "MOY SP, #XX".

L~nh gt)i CALL va ngan xep

Ngan xep, ngoai viec dung d~ hru cat noi dung cac thanh ghi thi con diroc CPU sir dung d~ luu cat tam thai dia chi cua lenh dung ngay sau lenh CALL. Nho vay rna CPU biet diroc dia chi quay v~ sau khi thirc hien xong chuang trinh con. Chi tiet v~ lenh goi CALL dUQ'C trinh bay b chuang 3.

,

ti-

t

ChLfdng 3

VONG LAp, LENH NHAvvA LENH GOI

.. ..

3.1 V6NG LAp v); L~NH NHAv Tao yang I~p d 8051

Qua trlnh lap lai chuoi lenh voi mot s6 llin nhat dinh diroc goi la Yang lap.

Vong lap thuong hay diroc cac b9 vi xir ly sir dung. D6i vci 8051, thuc hien vong I~p la lenh "DJNZ thanh ghi, nhan", trong do thanh ghi giam di mot dan V! va neu khac 0 thi lenh nhay den dia chi dfch xac dinh boi nhan. Tnroc khi vong I~p bar dau, s6 Ian I~p se diroc nap vao thanh ghi bo demo Chung ta xem mot s6 VI du sau:

Vi dl;l3.1

Viet chuang trinh d{ a) xoa ACC va sau do b) cong 3 vao ACC 10 bin.

Giai:

A,#O R2,#10

iXoa ACC, A = 0 iNap bQ dgrn R2 = 10

MOV MOV

BACK:ADD A,#10 ;CQng 03 vao ACC

DJNZ R2,AGAIN ;L~p eho dgn khi R2 = 0 (10 lan)

MOV R5,A ;c&t A vao thanh ghi R5

6 chuang trinh tren, thanh ghi R2 diroc sir dung lam bo demo B9 dern hie dau duoc dat bang 10. Sau moi Ian lap, lenh DJNZ giarn R2, neu R2 khac 0 thl no nhay den dia chi dloo co nhan "AGAIN". Qua trinh I~p lai nay tiep tuc cho den khi R2 co gia tri bang O. Sau khi R2 = 0, chuang trinh thoat khoi Yang lap va thirc hien lenh dung ngay sau vong lap do la "MOV R5, A".

Liru y rang d6i voi lenh DJNZ thl 0 VI trf thanh ghi co th~ la bat ky thanh ghi nao trong s6 RO - R 7. BQ dern cling co th~ la mot ngan nho 0 RAM nhu se thay 0 chuang 5.

Vi dl;l3.2

So lan cue dai rna Yang lap 0 VI du 3.1 co th~ lap lai la bao nhieu?

Gial:

Vi thanh ghi R2 chua s6 dern va do la thanh ghi 8 bit nen chi co th~ chua

diroc gia tri eire dai la FFH hay 255. Do vay s6 Ian lap lai eire dai rna vong lap 0 VI dl;l 3.1 co th~ thuc hien 1ft 256.

40

Can true va lap trinh ho vi dUu khie'n 8051

Yang I~p long nhau

Nhir trlnh bay a VI du 3.2, s6 dern eire dai la 256. V~y dieu gt xay ra neu chung ta mu6n l~p lai nhieu han 256 IAn? D~ giai quyet van d~ nay, cluing ta co the sir dung mot yang lap long ben trong mot yang lap khac, goi la yang lap long nhau. Nhu vay, d~ t6 chirc yang lap long nhau can sir dung 2 thanh ghi d~ hru s6 demo Xem VI du 3.3 sau day.

Vi d1,l3.3

Hay viet mot chuang trlnh: a) nap thanh ghi ACC voi gia tri 55H va b) H1Y bu ACC 700 Ian.

Giai:

VI 700 Ian han 256 (la s6 eire dai rna mot thanh ghi co the chua duoc) nen ta phai dung hai thanh ghi d~ chua s6 demo Doan chuang trinh diroi day trinh bay each sir dung hai thanh ghi R2 va R3 d~ chua s6 demo

A,#55H R3, #10 R2,#70

;N9.p A = 55H
;N9.P R3 10 -! dem vong lap ngoai
= so
;N9.p R2 70 -! dem vong l~p trong
= so MOV MOV NEXT: MOV

AGAIN: CPL A ; so thanh ghi A

DJNZ R2,AGAIN ;L~p 19.i 70 Ian (vong l~p trong) DJNZ R3, NEXT

Trong chuang trinh nay. thanh ghi R2 duoc dung de chua s6 dern yang lap trong. 0 lenh "DJNZ R2, AGAIN", moi khi R2 = 0 chuang trinh thuc hien luon lenh "JNZ R3, NEXT'. Lenh nay buoc CPU nap lai R2 gia tri s6 dern 70 va yang lap trong bat dau lap lai qua trinh nay va tiep tuc cho den khi R3 tra v~ 0 va yang l~p ngoai ket thuc,

L~nh nhay c6 dieu ki~n

Cac Ienh nhay co dieu kien cua 8051 diroc t6ng hop a bang 3.1. can chi tier cua tirng lenh diroc gioi thieu a phu luc A. Co the thay, a bang 3.1 mot s6 lenh nhir JZ (nhay neu A = 0) va JC (nhay neu co nho) chi thirc hien nhay neu co mot dieu kien dtroc thoa man. Tiep theo, chung ta se xem xet cu th~ han mot s6 Ienh nhay co dieu kien thuong gap.

L~nh JZ (nhay nell A = 0)

o lenh nay noi dung cua thanh ghi A diroc kiem tra. Neu A=O thl chuang

V()ng /gp, /~l1h nhd» va /¢nh gQi

41

trlnh nhay den dia chi dich. Vi du xet doan chuang trinh sau:

MOV JZ MOV JZ OVER

a doan chuang trinh tren, neu RO hoac R I c6 gia tri bang 0 thl chuang trinh nhay den dia chi c6 nhan OVER. Liru y la lenh JZ chi c6 the sir dung cho doi voi thanh ghi A. Chuang trinh chi tien hanh kiem tra xem thanh ghi A c6 bang 0 hay khong, do d6 lenh nay khong ap dung cho bat ky thanh ghi nao khac, C6 the thay sir dung lenh nay dan gian hoa chuang trinh dang k~ boi VI lap trlnh vien khong can phai thirc hien bat ky lenh so h9C nao khac.

A,RO OVER A,Rl OVER

iN9P gia tri cua RO vao A ;Nhay den OVER neu A = 0 iN9P gia tri cua Rl vao A iNhay den OVER neu A = 0

31 C'

h

ang_ .. ac len nhay co leu kien
L~nh Y nghia
JZ Nhay neu A = 0
JNZ Nhay neu A-:/:. 0
Giam va nhay neu A = 0 --
DJNZ
CJNE A, byte Nhay neu A -:/:. byte
CJNE re, # data Nhay neu ~yte -:/:. data
JC Nhay neu CY = 1
JNC Nhay neu CY = 0
Nh8y neu bit = 1 --
JB
JNB Nhay neu bit = 0
JBC Nhay neu bit = 1 va xoa no Vi d1;l3.4

A,R5 NEXT

iSao nQi dung R5 vao A iNhay den NEXT neu A-:/:. 0

Viet chuang trinh xac dinh xem R5 c6 chua gia tri 0 khong? Neu khong thl nap vao R5 gia tri 55H.

Giai:

MOV JNZ

NEXT:

MOV R5, #55H

L~nh JNC (nhay neu ca nno CY = 0)

6 lenh nay, bit co nho cua thanh ghi co psw diroc dung lam dieu kien nhay.

Khi thirc hien lenh "JNC nhan", bo xu Iy kiern tra co nho xem c6 diroc bat khong (CY = I). Neu khong diroc bat thi CPU se thuc hien lenh ttr dia chi cua nhan, Neu co CY = I thl chuang trlnh khong thirc hien lenh nhay rna thirc hien lenh k€ sau lenh JNC.

42

Cdu true va l~p trinh hi! vi die'll khiln 8051

Can hru y la cling co lenh "JC nhan", 6 lenh JC thl neu CY = I chuang trinh nhay den dia chi dfch la nhan,

Ngoai ra, trong nh6m lenh nhay co dieu kien con co lenh JB (nhay neu bit co mire cao) va JNB (nhay neu bit co rmrc thap), Cac lenh nay duoc trinh bay a chuang 4 va 8 khi noi ve thao tac bit.

VI d1;l3.5

Tim t6ng cua 79H, F5H va E2H. Ghi byte thap ella tong van RO va byte cao van R5.

Giai:

N-l:

N-2:

OVER:

MOV A,#O ;Xoa thanh ghi A 0
MOV R5, A ;Xoa R5
ADD A,#79H ;A = 0+79H=79H
JNC N-l ;N.§u kh6ng co nha cQng k.§ ti.§p
INC R5 ;N.§u CY = 1, tang R5
ADD A,#OF5H ;A = 79H+F5H = 6EH va CY = 1
JNC N-2 ;Nhay '" CY 0
neu =
INC R5 ;N.§u CY = 1 tang R5 (R5 1 )
ADD A,#OE2H ;A = GE + E2 = 50 va CY 1
JNC OVER ;Nhay A' CY 0
neu =
INC R5 ;Neu CY = 1 tang R5
MOV RO,A ;Bay gia RO = SOH va R5 = 02 Can hru y ding tat ca cac lenh nhay co dieu kien deu Ia cac lenh nhay ngan, co nghia la dia chi cua dfch deu phai nam trong khoang -127 den + 127 byte, nrc chua du trong bo dern chuang trinh Pc.

L~nh nhay khong dieu ki~n

Lenh nhay khong dieu kien la lenh nhay trong do dieu khien diroc truyen

khong co dieu kien den dia chi dich. 6 8051 co hai lenh nhay khong dieu kien, d6 la: nhay dai LJMP - va nhay ngan SJMP.

L~nh nhay dil; LJMP (Long Jump)

Nhay dai LJMP la lenh 3 byte, trong d6 byte dau tien la rna lenh, hai byte con lai la dia chi 16 bit cua dfch. Dia chi dfch 02 byte cho phep lenh c6 the nhay den bat ky vi trf nho nao trong khong gian nho 0000 - FFFFH.

CAn hru y ding, bo dern chuang trmh cua 8051 la 16 bit - nghia la irng voi

V img lap, I¢nh nhdy va lenh g(Ji

43

khong gian dia chi 64k byte, tuy nhien, khong phai moi thanh vien ho 8051 deu co bo nho chirong trinh ROM tren chip chiern het ca khong gian nho nay. 8051 diiu tien chi co 4k byte ROM tren chip, do vay m6i byte bo nho deu rat quy gia. VI Iy do do rna co cit lenh nhay ngan SJMP chi chiern 2 byte va co ca lenh nhay dai LJMP dai 3 byte. Day cling la each tiet kiern bo nho Ci nhieu irng dung rna

khong gian bo nho han hep, -

L~nh nhay ngsn SJMP (Short Jump)

Trong 2 byte cua lenh nhay ngan thl byte dilu tien la rna lenh va byte tlur hai la dia chi tuong doi cua dia chi dich. Dia chi urong doi voi pham vi 00 - FFH diroc chia thanh dia chi nhay tien va dia chi nhay lui, nghia la, phan ra nr -128 den + 127 byte cua bo nho urong doi so voi dia chi hien thai cua b¢ dern chuong trinh. Neu la lenh nhay tien thi dia chi dfch co th~ narn trong khoang 127 byte tl1 gia tri hien thai cua bo dem chirong trinh. Neu dia chi dfch Ci phia sau (nhay lui) thl co th~ nam trong khoang -128 byte tir gia tri hien hanh cua Pc.

Xac djnh dja chi nhay ngan

SJMP Ut I¢nh nhay ngan. Ngoai ra tat ca cac lenh nhay co dieu kien nhu JNC, JZ va DJNZ cling la nhirng lenh nhay ngan va co kfch thuoc 2 byte. 6 nhung lenh nay, byte thtr nhat la rna lenh, con byte thir hai la dia chi c1fch urong d6i. Dia chi dich urong dol co nghia la so voi gia tri cua bo dern chuong trinh. D~ xac dinh dia chi dich, byte tlur hai diroc cong vao thanh ghi PC cua lenh dung ngay sau lenh nhay. D~ hieu r6 hon, cluing ta xem vi du sau:

Vi dl;l3.6

SiI dung tep tin li¢t ke duoi day hay kiem tra viec xac dinh dia chi nhay ve
tnroc.
Dong PC Ma l~nh L~nh Toan h~ng
01 0000 ORG 0000
02 0000 7800 MOV RO, #0
03 0002 7455 MOV A, #55H
04 0004 6003 JZ NEXT
05 0006 08 INC RO
06 0007 04 AGAIN: INC A
07 0008 04 INC A
08 0009 2477 NEXT: ADD A, #77h 44 Cdu true va It,ip trinh ho vi die'u khiin 8051

09 OOOB 5005 JNC OVER
10 0000 E4 CLR A
11 OOOE F8 MOV RO, A
12 OOOF F9 MOV R1, A
13 0010 FA MOV R2, A
14 0011 FB MOV R3, A
15 0012 2B OVER: ADD A, R3
16 0013 50F2 JNC AGAIN
17 0015 80FE HERE: SJMP HERE
18 0017 END Ghii:

Tnroc het nen hru 9 rang cac lenh JZ va JNC deu la lenh nhay ve tnroc. Dia chi dfch cua lenh nhay ve tnroc diroc xac dinh bang each cong gia tri PC cua

lenh di ngay sau lenh nhay vao byte thtr hai cua lenh nhay ngan. 6 dong 4 lenh "JZ NEXT" co rna lenh 60 va roan hang 03 tai dia chi 0004 va 0005. 6 day 03 la dia chi nrong d6i - wang d6i nghia la so voi dia chi cua lenh ke tiep la: "INC RO" va do la 0006. Cong 0006 vao 3 se diroc dia chi dich cua nhan NEXT la 0009. Bang each nrong tu, a dong 9 lenh "JNC OVER" co rna lenh va roan hang la 50 va os, trong do SOla rna lenh con 05 la dia chi wang d6i. Do vay, 05 diroc cong voi OOOD la dia chi cua lenh "CLR A" dung ngay sau lenh "JNC OVER" va cho gia tri 12H chinh la dia chi cua nhan OVER.

Vi d1,l3.7

Hay xac dinh dia chi ella cac nhay lui a vi du 3.6.

Glai:

6 danh sach liet ke chuang trinh, lenh "JNC AGAIN" co rna lenh la 50 va dia chi nrong doi la F2H. Lay dia chl wang doi F2H cong voi dia chi lenh dirng sau lenh nhay la ISH, chung ta co ISH + F2H = 07 (phan nho diroc bo di). D~ 9 rang 07 la dia chi nhan AGAIN. Tuong ur, lenh "SJMP HERE" co rna lenh 80 va dia chi wang d6i FEH. U1Y dia chi wang doi FEH cong voi gia tri PC cua lenh ke tiep la 00I7H cho dia chi 00 ISH - chfnh la dia chi nhan HERE (17H + FEH = ISH phan nho diroc bo di). Liru 9 rang FEH la -2 va 17h + (-2) = ISH. ve phep cong so am se diroc ban a chuang 6.

V ong l(ip, lenh nhdy va /¢nh g{Ji

45

Xac djnh dja chi dich I~nh nhay lui

a twang hop nhay tien, gia tri do doi la mot s6 dirong trong khoang ttr 0 den 127 (00 den 7F a dang Hexa), con a twang hop nhay lui, gia tri do doi la mot s6 Am nam trong khoang ur 0 den -128 nhtr se gidi thfch a vi du 3.7.

Can ltru y rang, du nhay tai hay nhay lui thi d6i voi rnoi lenh nhay ngan bai ky, dia chi dich khong bao gio co the ngoai vung -128 den + 127 byte so voi dia chi cua lenh dirng ngay sau lenh SJMP. Neu thirc hien lenh nhay ngoai pham vi neu tren thi trinh hop dich cho ra thong bao 16i.

3.2 L~NH GQI CALL

Mot lenh chuyen dieu khien khac la lenh CALL dung M goi chuang trinh can. Chuang trinh con thirong diroc st'r dung de thuc thi cac cong viec diroc lap lai thirong xuyen. Nho each t6 chirc chuang trinh can rna chuang trlnh vua tiet kiern

khong gian nho vira rro nen co cau true. a 8051 co hai lenh goi la: goi dai LCALL va goi tuyet d6i ACALL. Yeu t6 quyet dinh sir dung lenh goi nao phu thuoc vao dia chi dfch.

L~nh gQi dili LCALL (Long Call)

Day la lenh 3 byte. Byte dau tien la rna lenh, con hai byte sau la dia chi cua chuang trinh con dich. Nhir vay LCALL co th~ diroc dung de goi chuang trinh can a bat ky vi trf nao trong pham vi 64k byte khong gian dia chi cua 8051. Sau khi thirc hien xong mot chuang trinh con, d~ 8051 biet diroc cho quay tra v~ thl dia chi cua lenh dirng ngay sau lenh goi LCALL se duoc tl! dong cat vao ngan xep. Khi mot chuang trinh con dtroc goi, dieu khien diroc chuyen den chuang trinh con va bo xir ly cat bo dern chuang trinh PC vao ngan xep va bat dau nap lenh va thirc hien lenh cua chuang trinh con. Sau khi thirc hien xong chuang trinh can, lenh RET se chuyen dieu khien v~ chuang trinh vira goi no. Chuang trinh con luon co lenh cu6i cung la lenh RET, xern vi du 3.8.

Vi du 3.8

Hay viet chuang trinh lien tuc d6i tat ca cac bit cua ccng PI bang each luan phien gt'ri den cong cac gia tri 55H va AAH. Thier lap mot d¢ tr~ thai gian giira cac Ian xuat du lieu cua cong Pl. Chuang trinh nay se diroc sir dung de kiem tra cac cong cua 8051 a chuang tiep theo.

46

Glal:

Cdu true va ltJ,p trinh ho vi die'u khie'n 8051

ORG 0000

BACK: MOVA, #55H MOV Pl, A LCALL DELAY MOV A, #OAAH MOV Pl,A LCALL DELAY SJMP BACK

;N~p A vai gia trt 55H ;Gvi 55H d§n c6ng Pl ;Tao tr~ thai gian

;N~p A vai gia trt AAH ;Gvi AAH den c6ng Pl ;Giv ch~m

;L~p lai va t~n

;--8Ay la chuang trinh con t~o dO tr~ thai gian

ORG 300H ;8~t trinh t~o tr~ & dta chi 300H

DELAY:MOV R5,#00H ;N~p bO dem R5 = 255H (hay FFH) AGAIN: DJNZ R5,AGAIN ;Tiep t~c cho den khi R5 ve 0

RET END

;Tra dieu khi~n ve nguon gQi ;K§t thuc t$p tin

Tv VI du 3.8 can luu y cac di~m sau day:

l. Khi thuc hien lenh "LCALL DELAY" dtiu tien thl dia chi cua lenh ngay sau n6 la "MOV A, #OAAH" dtroc day van ngan xep va 8051 bM dau thirc hien cac lenh a dia chi 300H.

2. o chuong trlnh con DELAY, 11k dau bo dern R5 diroc nap gia tri 255 (R5 = FFH). Do vay, vong lap duoc l~p lai 256 Ian, Khi R5 tra v~ 0, lenh thuc hien tiep thea la RET, khi d6 dia chi tv ngan xep duoc tai van van bo dern chuang trinh va lenh sau lenh CALL se tiep tuc diroc thirc hien.

Khoang thai gian tre trong vi du 8.3 phu thuoc vao tan so cua 8051. Cach tinh chinh xac thai gian se diroc giai thfch a clnrong 4. Tuy nhien, ta c6 th~ tang thai gian tre bang each sir dung vong lap long nhau nhir a vi du sau.

;Vong l~p t~o tr~

;N~p R4=255 (FFH d~ng hexa) ;N~p R5=255 (FFH d~ng hexa) ;L~p l~i cho den khi RT=O ;Giam R4

;Nap tiep R5 cho den khi R4=0 ;Tr& ve (khi R4=O)

DELAY:
MOV R4,#255
NEXT: MOV R5,#255
AGAIN: DJNZ R5,AGAIN
DJNZ R4,NEXT
RET V(lng lap, lenh nhdy va lenh gQi

47

L~nh CALL va vai tre cua ngan xep

Ngan xep va con tro ngan xep se diroc nghien CUu a chuang cu6i. De hieu ducc vai tro cua ngan xep trong cac bo vi dieu khien, cluing ta se khao sat noi dung cua ngan xep va con tro ngan xep qua VI du 3.9 diroi day.

Vi dl,l3.9
Phan tfch noi dung cua ngan xep sau khi thuc hien lenh LCALL dau tien a
VI du sau:
001 0000 ORG 0
002 0000 7455 BACK: MOV A, #55H iN<;l.p 55H VaG A
003 0002 F590 MOV P1,A iGui 55H tai ,< P1
cong
004 0004 120300 LCALL DELAY iT<;l.o tr§
005 0007 74AA MOVA, #OAAH ;N<;l.p AAH VaG A
006 0009 F590 MOV P1,A ;Gui AAH tai ,< P1
cong
007 OOOB 120300 LCALL DELAY ;T<;l.o tr§
008 OOOE 80FO SJMP BACK iTie'p tvc
009 0010
010 0010 ;-- Day 1a trinh t<;l.O tr§
Oll 0300 ORG 300H
012 0300 DELAY:
013 0300 7DFF MOV R5,#FFH ;N<;l.p 255 VaG R5
014 0302 DDFE AGAIN:DJNZ R5,AGAIN iDung a day
015 0304 22 RET ;Tra ve " gQi
nguon
016 0305 END ;Ke't th6c Giiii:

Khi lenh LCALL dilu tien diroc thirc hien thl dia chi cua lenh "MOV A,

. . ... .

#OAAH" dii diroc cat vao ngan xep, Byte thap cat tnroc va

byte cao cAt sau. Lenh cuoi cung cua chuang trinh con la lenh tra ve RET, khi d6 CPU lay lai (POP) cac byte tren dinh cua ngan xep vao bo dern chuang trinh PC va tiep tuc thirc hien lenh tai dia chi 07. Liroc d6 ben canh chi ra trinh nr noi dung cua ngan xep sau IAn goi LCALL dau tien.

OA

09 00

08 07

SP=09

sli dl;Jng I~nh PUSH va POP d ehltdng trinh eon

Khi goi mot chuang trinh con thl ngan xep phai hru diroc vi trf tra ve cua

48

Cdu true va l~p trinh ho vi die'u khii'n 8051

CPU sau khi ket thtic chuong trinh con. Vi vay, can het sue cAn than moi khi co thao tic voi ngan xep, Co mot nguyen tac khi goi bat ky chuong trinh con nao la so ldn cat vao (PUSH) luon bang s6 Ian lay ra (POP). Noi each khac, moi khi co met lenh PUSH thl cling can phai co mot lenh POP, xem VI du 3.10.

Vi d1;l3.10

ORG

o

Phan tfch noi dung ngan xep khi thirc hien lenh LCALL dfiu tien a doan chuang trlnh sau.

01 0000

Giai:

Tnroc het nen hru y la d6i voi lenh PUSH va POP cluing ta can xac dinh dia chi tnrc tiep cua thanh ghi diroc cat vao va lay ra tir ngan xep. Diroi day la bieu dien noi dung cua ngan xep,

02 0000 7455 03 0002 F590 04 0004 7C99 05 0006 7067 06 0008 120300 07 OOOB 74AA. 08 0000 F590 09 OOOF 120300 10 0012 80EC 11 0014

12 0300

13 0300 C004 14 0302 C005 15 0304 7CFF 16 0306 7DFF 17 0308 DDFE 18 030A DCFA 19 030C 0005 20 030E 0004 21 0310 22 22 0311

BACK: MOV

A,#55H

;N9.p 55H vao A

MOV P1, A ; Gvi 55H ra c6ng P1

MOV R4, #99H

MOV R5, #67H

LCALL DELAY

;T9.o tr~

;N9.p AH vao A

MOV A, #OAAH MOV P1, A LCALL DELAY

SJMP BACK ; Tie'p t uc t hirc h i en

;-- Day 1a trinh con t9.0 tr~ DELAY

ORG 300H

DELAY: PUSH 4 PUSH 5

;Gvi AAH ra c6ng P1

;D~y R4 vao ngan xe'p I ;D~y R5 vao ngan xe'p

MOV R4, OOFH ; GEm R4 FFH NEXT: MOV R5,#00FH ;Gan R5 = 255 AGAIN: DJNZ R5,AGAIN

DJNZ R4 , NEXT

POP 5 ;Keo dlnh ngan xe'p vao R5

POP 4 ;Keo dlnh ngan xe'p vao R4

RET ;Tra ve nguon gQi

END ;Ke't thuc t$p tin hqp ngu

V ong lap, lenn nhdy va lenh gC;i 49

Sau LCALL dau tien Sau PUSH 4 Sau PUSH 5
OB OB OB 67 R5
OA OA 99 R4 OA 99 R4
09 00 PCH 09 00 PCH 09 00 PCH
08 OB PCL 08 OB PCL 08 OB PCL GQi chuang trinh con .

Cach t6 chirc cua trinh hop ngir thuong co mot chuang trlnh chinh va nhieu chuang trlnh con diroc goi tir chuang trlnh chinh do. Dieu nay cho phep ta xay dung chuang trinh con dirci dang module rieng biet, M6i module co th~ dtroc kiem tra doc l~p va sau d6 diroc ghep cung voi chuang trlnh chfnh. Cach t6 chirc nhu v~y cfing nit c6 y nghia khi xay dung chuang trlnh 100 VI cac module c6 th~ diroc giao cho nhieu l~p trinh vien khac nhau nham rut ngan thai gian phat trien.

Can nhac lai rang, tnroc khi Slr dung lenh LCALL, dia chi dich cua chuang trinh con co the (j mot vi trf nao do trong khong gian 64k byte cua bo nho 8051. Tuy nhien, doi voi cac lenh goi khac tht co the khac, vl du, trong tnrong hop lenh ACALL nhir gici thieu (j dtroi day.

RET

K~t thuc chuang trinh con 2 SUBR_3 .. ,

;Chuang trinh chinh MAIN 99i chuang trinh con

ORG 0

MAIN: LCALL SUBR-I LCALL SUBR-2 LCALL SUBR-3

HERE: SJMP MAIN

;--K~t thuc chuang trinh chinh MAIN

SUBR_I:

RET

K~t thuc chuang trinh con 1 SUBR_2 .. ,

RET

;--K~t thuc chuang trinh con 3

END ;K~t thuc trinh hqp ngu

Hlnh 3.1. Chuong trmh chinh hop ngfr cua 8051 goi cac trmh con

50

Cdu true va lrJ.p trinh ho vi die'u khie'n 8051

L~nh 9Qi tuy~t doi ACALL (Absolute call)

Lenh ACALL la lenh 2 byte, khac voi lenh LCALL dai 3 byte. Do ACALL chi co 2 byte nen dia chi dfch cua chuang trlnh con phai nam trong khoang 2 Kbyte dia chi VI chi co II bit cua 2 byte diroc dung d~ xac dinh dia chi. Neu xet theo khfa canh cat bo dem chuang trinh van ngan xep hay sir dung lenh RET ket thtic chuang trlnh con thl giifa ACALL va LCALL khong co su khac biet nao. Chi co mot diem khac biet do la dia chi dfch cua lenh LCALL co th~ nam bat cu dau trong pharn vi 64k byte khong gian dia chi cua 8051, trong khi do dia chi cua lenh ACALL phai nam trong khoang 2 Kbyte. Thuc te, mot s6 bien the 8051 chi co 1 Kbyte ROM tren chip. Trong nhfrng tnrong hop do, sir dung lenh ACALL co th~ tiet kiern diroc mot s6 byte bo nho cua khong gian ROM chuang trlnh so voi lenh LCALL.

Vi d1:l3.1l

Mot ky sir thiet ke phat trien irng dung cua minh dua tren chip vi dieu khien Atmel AT89CI051 voi 1 Kbyte ROM Flash tren chip. V~y trong tnrong hop nay nen sii dung lenh LCALL hay lenh ACALL.

Giai:

Trong tnrong hop nay neu Slr dung lenh ACALL thl m6i lfin goi lenh se tiet kiern bo nho han VI do la lenh 2 byte.

01 nhien, d~ viet chuang trlnh co hieu qua can hieu r6 tirng lenh dtroc sii dung. Sau day la mot vf du,

Vi d1:l3.12

Viet lai chuang trlnh a vf du 3.8 sir dung lenh lay bu.

Giai:
ORG 0
MOV A,#55H ;Ni;l.p q i a t r i. 55H vao A
MOV Pl,A ;Xu&t A :? Pl
BACK: ra cong
ACALL DELAY ;Ti;l.o tr€!
CPL A ;L&y bu thanh ghi A
SJMP BACK ;Ti~p t\lC tht!c h i.eri va hi;l.n Vong /gp,/fllil nhdy va lenh goi

51

; -- Day la trinh con tao tr~ DELAY

DELAY:MOV R5,#OFFH ;Nap R5=255 (FFH) cho bQ dem AGAIN: DJNZ R5, AGAIN ; Dung cho den khi R5=O

RET ;Tra ve

END ;Ket thuc

Luu y fling, a chuang trmh nay thanh ghi A diroc nap gia tri 55H. Lay bu 55H diroc AAH, va lay bu AAH lai cho 55H. Sa di nhu vay VI diroi dang ma nh] phan "0101010 I" (tire 55H) va "10101010" nrc AAH la btl cua nhau.

3.3 TI:\O vA TiNH ToAN THai GIAN GIQ CHAM Chu ky may

D~ thirc hien mot lenh tht can mot so chu ky dong h6. 0 ho 8051, cac chu ky dong h6 nay diroc goi la cac chu ky may. Phu luc A-2 cung cap danh sach lenh 8051 va cac chu ky may cua cluing. D¢ dai cua chu ky may cua ho 8051 phu thuoc vao Uin so cua bo dao dong thach anh noi voi h~ thong 8051. B¢ dao dong thach anh cung voi mach dien tren chip cung cap xung d6ng h6 cho 8051. Tan so tinh th~ thach anh thuong trong khoang 4MHz den 30 MHz phu thuoc vao toe d¢ chip va nha san xuat. Thuong duoc Slr dung nhat la bo dao dong thach anh tan so I 0.0592MHz nharn nrong thich voi cong noi tiep cua IB~ PC, xem chuang 10. D6i voi 8051, mot chu ky may g6m 12 chu ky dao dong thach anh. Do vay, M tfnh chu ky may dan gian la xac dinh 12 chu ky dao dong tinh the thach anh. Xem VI du 3.13.

Hay tim chu ky may trong 3 tnrong hop tAn so dao dong thach anh sau day:

a) 11.0592MHz; b) 16MHz; c) 20MHz.

Vi dI,l3.13

Giai:

a) 11.0592/12 = 921.6kHz; Chu ky may la 1 /921.6kHz = 1.085 J.lS .

b) 16MHz/12 = 1.333MHz; Chu ky may MC = 1/1.333MHz = 0.75~ls.

c) 20MHz/12 = 1.66MHz =:> MC = 1/1.66MHz = 0.60J.ls.

52

Cdu true va It,ip trinb ho vi die'u khiln 8051

H~ th6ng 8051 co tan s6 dao dong thach anh la 11.0592MHz. Hay tim thai gian can thiet de thuc hien cac lenh sau day.

a) MOV R3, #55; b) DEC R3;

d) LJMP; e) SJMP;

c) DJNZ R2,dich f) NOP

Vi d1;l3.14

g) MUL AB. Gial:

Chu ky may cua he 8051 co tan s6 dong h6 11.0592MHz la 1.085~s nhir

da tfnh 0 vi du 3.13. Bang A-I 0 phu luc A gioi thieu s6 chu ky may d6i voi cac
lenh tren. V~y ta co:
Lenh' S6' chu ky may Thai gian thuc hien
a) MOV R3,#55 1 1x1.085 f..ls 1.085 f..ls
b) DEC R3 1 1x1.085 f..ls 1.085 f..ls
c) DJNZ R2,target 2 2x1.085 f..ls 2.17 f..ls
d) LJMP 2 2x1.085 f..ls 2.17 f..ls
e) SJMP 2 2x1.085 f..ls 2.17 f..ls
f) NOP 1 1x1.085 f..ls 1.085 f..ls
g) MUL AB 4 4x1.085 f..ls 4.34 f..ls Tinh thdi gian tri

Nhir da trlnh bay 0 tren, mot chuang trlnh con thuc hien giU cham g6m co hai phan: (1) thiet lap bo dern va (2) mot vong lap, Hau het thai gian giU cham diroc thirc hien boi than vong lap nhu trmh bay 0 VI du 3.15.

Hay xac dinh thoi gian giii' cham 0 chuang trinh sau, neu tan s6 dao dong thach anh Iii 11.0592MHz.

Vi d1;l3.15

MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPLA
SJMP AGAIN; --Thai gian giil ch~m
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE Yang liJp,l¢nh nhdy va lenh goi

53

DELAY: MOV

R3,#200

1 2

RET

Gild:

Tit bang A-I cua phu luc A, chung ta co diroc s6 chu ky may irng v6'i cac lenh cua chirong trlnh con gift cham nhtr sau:

Chu ky may

HERE: DJNZ R3, HERE

RET

Nhu vay, tong thoi

436. 17J.ls.

1

gian giil' cham la [(200 x 2) + 1 + 1] x 1.085 =

Thong thirong, thoi gian giil' cham duoc xac dinh dua vao thoi gian thuc hien cac lenh ben trong yang lap va be, qua cac lenh 0 ben ngoai yang l~p.

o VI du 3.15 gia tri 16'0 nhat rna R3 co th~ hru diroc Hi 255, do vay mot each dan gian de tao thoi gian tr~ la sir dung lenh NOP (khong thirc hien gl) d~t 0 trong Yang lap. Chung ta xem VI d1:l3.16 dtroi day.

56 p)lu ky may
DELAY: MOV R3,#250 f
HERE: NOP 1
NOP 1
NOP 1
NOP 1
DJNZ R3,HERE 2
RET 1 Vi dt;t3.16

Hay xac dinh thoi gian tr~ cua chirong trtnh con sau. Gia thiet tan s6 dao dong thach anh la 11.0592MHz.

Gild:

Thoi gian tr~ ben trong Yang l~p HERE la [250 (1 + 1 + 1 + 1 + 1 + 2)] x 1.0851 J..lS = 1627.5 us. Cong them hai lenh ngoai yang lap ta co 1627.5 J..lS x 1.085J..ls = 1629.67J..ls.

54

Cdu true va lap trinh ho vi die'u khief, 8051

TCilO tr~ dung yang I~p 16ng nhau

Mot each khac de tao diroc d¢ tn~ 1611 la sir dung yang lap long nhau, nrc la dung yang lap nay long ben trong yang lap khac, Xem vf du 3.17 duci day.

Vidl,13.17

Cho chu ky may la 1.085Ils, hay tfnh thai gian tn~ a chuang trlnh con sau:

DELAY:

Chu ky may
1
1
1
1
2
2
1 MOV AGAIN: MOV

R2,#200 R3,#250

HERE: NOP
NOP
DJNZ
DJNZ
RET
Gial: R3,HERE R2,AGAIN

Rieng yang lap HERE ta co (4 x 250) x 1.0851ls = 10851ls. Yang I~p AGAIN thirc hien l~p lai yang lap HERE 200 Ian, do vay thai gian tre do yang lap HERE la 200 x 10851ls = 2170001ls. Tuy nhien, a day can them thai gian tn~ cua lenh "MOY R3, #250" va "DJNZ R2, AGAIN" a dau va cu6i yang lap AGAIN tao nen la (3 x 200 x 1.0851ls) = 6511ls. Do vay, tong thai gian tn~ cua ca chuang trlnh DELAY la 217000 + 651 = 2176511ls = 217,651 ms. Cling nen hru y la, a tnrong hop yang lap long nhau cling nhir a cac tnrong hqp yang lap gilt cham khac, each tfnh thai gian tre nhir tren chi la gan dung VI chua tlnh den cac lenh dau va cu6i cua chuang trinh con.

Chltdng 4

~ " "A "-

CONG VAO/RA VA U~P TRINH

4.1 MO rA CHAN COA 8051

Ole thanh vien cua ho 8051, vf du 8751, 89C51, DS5000 deu co kieu dong YO khac nhau, chang han, dang hai hang chan DIP (Dual In - Line Pakage), dang YO det vuong QFP (Quad Flat Pakage) va dang khong co chan dO'LLC (Leadless Chip Carrier), song chung deu co 40 chan voi cac chirc nang, nhir vito ra I/O, doc

RD, ghi WR, dia chi, dCi' lieu va ngat. can hru y la mot so hang san xuat phien ban 8051 co 20 chan vci so cong vao/ra it han cho cac irng dung yeu diu thap han. Tuy nhien, VI hau het cac nha phat trien chfnh deu Slr dung chip dong vo 40 chan kieu hai hang chan DIP nen cluing ta chi tap trung mo ta phien ban nay.

PDIP/Cerdip
P1.0 1 40 Vee
P1.1 2 39 PO.O (ADO)
P1.2 3 38 PO.1 (AD1)
P1.3 4 3? PO.2 (AD2)
P1.4 5 36 PO.3 (AD3)
P1.5 6 35 PO.4 (AD4)
P1.6 t 34 PO.5 (AD5)
P1.7 8 33 PO.B (ADB)
RST 9 8051 32 PO.? (AD?)
(RXD) P3.0
(TXD) P3.1 10 (8031) 31 EA/CPP
11 30 ALE/PROG
(NTO) P3.2 12 29 PSEN
(NTI) P3.3 13 28 P2.7 (A15)
(TO) P3.4 14 27 P2.B (A14)
(T1) P3.5 15 26 P2.5 (A13)
(WR) P3.B 16 25 P2.4 (A12)
(RD) P3.7 17 24 P2.3 (A11)
XTAL2 18 23 P2.2 (A10)
XTAL1 19 22 P2.1 (A9)
GND 20 21 P2.0 (AS) Hlnh 4.1. 86 trt chan cua 8051

56

Cd" true va l~p trinh ho vi die'u khie'n 8051

Hinh 4.1 trinh bay bo' trf chan cua 8051. Trong so 40 chan co 32 chan danh cho bon ccng PO, PI, P2 va P3, rn6i cong co 8 chan. Cac chan con lai danh cho nguon Vee, dlit GND, cac chan dao dong XTAL 1 va XT AL2, khoi dong l<;li RST, cho phep chot dia chi ALE, truy cap diroc

dao dong tren chip nhtrng viin can co mot

"lnh 4.2. b) Noi dong ho ngoai bo dong ho ben ngoai M kich hoat. B¢ dao

dong thach anh ngoai thirong duoc noi toi cac Bang 4.1. Gla tr] mot so

chan VaG XTALI (chan 19) va XTAL2 (chan thanh ghi sau RESET 18). Khi mac dao dong thach anh, phai co hai tu dien 30pF, mot dfiu rn6i tu noi t6i XTALI va XTAL2, con dfiu kia nol dAt, nhu trinh bay b hlnh 4.2a.

Gin hru y la ho 8051 co nhieu phien ban tee d¢ khac nhau. Toe d¢ duoc hieu la tan so eire dai cua bo dao dong noi toi chan XT AL, vi du, chip 20 MHz, 12MHz hoac thap han. Giao dong dong h6 ngoai khong nhat thiet la bo dao dong thach anh rna ciing co th~ dung

b¢ dao dong TTL. Khi do bo dao dong duoc noi toi chan XT AL 1, con chan

dia chi ngoai EA, cho phep cat chuang trmh PSEN . Trang 8 chan nay thi 6 chan Vee, GND, XTAL 1, XTAL2, RST va EA diroc cac ho 8031 va 8051 siX dung. Noi each khac, cac chan nay can duoc noi M cho he thong lam viec rna khong phu thuoc VaG b¢ vi dieu khien Ia 8051 hay

8031. Hai chan con lai Ia PSEN va ALE duoc siX dung chu yeu trong cac h¢ thong dira tren 8031.

Nh6m chan nguon, dao dQng va dieu khi~n

vee - Chan so 40, cung cap dien ap nguon +5V cho chip.

GND - Chan so 20 Ia chan dat.

XTAL 1 va XTAL2: 8051 co mot b¢

XTAL2

Hlnh 4.2. a) N6i dong ho thach anh

NC

XTAL2

Tin hi~u dao d¢ng ngoai

XTAL 1

GND

1

Register Reset Value
PC 0000
ACC 0000
B 0000
PSW 0000
SP 0000
DPTR 0007 Cling vaolra va hlP trinh

57

XTAL2 M ho, nhir hinh 4.2b.

RST· Khai dong lai (RESET).

D6 la chan vao, so 9, mire tich eire cao, blnh thirong a rmrc thap. Khi co xung cao d~t toi chan nay thl bo vi dieu khien se ket thuc moi hoar dong hien tai va tien hanh khoi dong lai. Qua trlnh xay ra hoan toan urong tu nhu khi bat nguon. Khi Reset, rnoi gia tri tren cac thanh ghi se bi xoa. Bang 4.1 gioi thieu gia tr] cac thanh ghi cua 8051 khi thirc hien Reset.

Liru y ding, khi Reset, gia tri cua bo dern chuang trlnh PC bang 0, va nhir vay CPU nhan ma Ienh dAu tien tai dia chi 0000 cua bo nho ROM. Do do, tai dja chi nay phai co lenh c_lau tien chuang trlnh nguon cua ROM. Hinh 4.3 trlnh bay hai each noi chan RST voi mach Reset.

D~ Reset co hieu qua, chan Hinh 4.3. b) Mach Reset v6i phim khei RST din duy tri trang thai tfch eire

dong lai

(rrurc cao) toi thieu 2 chu ky may.

o 8051 mot chu ky may bang 12 chu ky dao dong dong ho, nhir diroc de cap

Vee

i1fF~ 31
EANpp
19 X1
D
• 8.2K 30l!F T 18
/1.0592 MHz X2
,
9
RST Hinh 4.3. a) Mach noi chan RST

Vee

'------c3~1-1 EANpp

X1

X2

a chuang 3.

EA - Truy cap bo nho ngoai (External Access) la chan vao, so 31 tren vo ki~u DIP. Doi voi cac thanh vien ho 8051 co ROM chuang trinh tren chip nhir

8751, 98C51 hoac DS5000 thl chan EA diroc nol toi nguon V cc- Truong hop khong co ROM tren chip nlur 8031 va 8032 thl rna chuang trlnh diroc hru cat 6 bo

nho ROM ngoai, khi do chan EA diroc noi dat. Nhu vay chan EA hoac dtroc noi voi nguon V cc hoac voi dar GND chir khong bao gia de ho.

~-+-------j RST 9

8.2K

58

Cdu true va ltJ.p trinh ho vi die'u khiin 8051

Chuang 14 se gioi thieu phtrong phap 8031 dung chan nay ket hop voi PSEN M truy cap cac chuang trlnh cat tren bo nho ROM a ngoai,

Vi d1,l4.1

Xac dinh chu ky may voi a) XTAL = 11.0592MHz b) XTAL = 16MHz.

Gla]:

a. 11.0592MHz/12 = 921.6kHz.

Chu ky may = 1/921.6kHz = 1.085Ils. b. 16MHz/12 = 1.333MHz

Chu ky may = 1/1.333MHz = 0.75Ils.

Tren day la cac chan rna moi thanh vien cua ho 8051 deu co va deu diroc noi giong nhau. Diroi day la mot so chan Slr dung chu yeu a he thong 8031 va se diroc trtnh bay chi tiet han a chuang 11.

PSEN - La chan ra co chirc nang cho phep cat chuang trlnh (Program Store Enable). 0 he thong 8031, khi chuang trinh cat a bo nho ROM ngoai thl chan nay diroc noi toi chan OE cua ROM. Chi tiet dtroc ban a chuang 14.

ALE - Cho phep chot dia chi (Address Latch Enable) la chan ra co mire tich eire cao. Khi noi 8031 toi bo nho ngoai thl cong 0 cling diroc cap dia chi va dfr lieu. Hay noi each khac, 8031 don dia chi va dfr lieu qua cong 0 de tier kiern so chan. Chan ALE diroc sir dung de phan kenh dia chi va dfr lieu bang each noi toi chan G cua chip 74LS373.

Nh6m chan c6ng Vao/Ra

Bon c6ng PO, PI, P2 va P3 deu co 8 chan va tao thanh cong 8 bit. Tat ca cac cong khi Reset deu diroc cau hlnh lam cong ra. D~ lam dau vao thl can duoc lap trlnh.

Cong PO

C6ng PO co 8 chan (tit chan 32 den 39). Blnh thirong day la cong ra.

Vee t ~ t t t t t ~
10K
PO.O

055000 PO.1
8751 PO.2 "U
0
8951 PO.3 ::I-
0
POA
PO.5
PO.6
PO.?
Hmh 4.4. M~c di~n tra keo cong PO

Cdng vaolra va lap trtnb

59

Di co the vira lam delu ra, vira lam dau VaG thi m6i chan phai duoc n6i toi mot dien tra keo 10 kn ben ngoai. Sa di nhu vay la VI cong PO co dang eire mang ho; day la diem khac voi cac cong PI, P2 va P3. Khai niern eire mang ha cling tuong W nhu colector he; tuy nhien a day ap dung cho cac chip dang MOS. 0 moi he thong 8751, 89C5I hoac DS5000 thl PO luon diroc n6i voi cac dien tro keo, xem hinh 4.4, va nhu vay ta co diroc cong VaG ra PO. V6'i cac dien trc keo ngoai, khi khoi dong lai, cdng PO diroc cau hlnh lam cong ra. Vi du, dean chuang trinh sau se lien tuc gui ra cong PO cac gia tri 55H va AAH.

MOV A, #554

BACK: MOV PO,A

ACALL DELAY

CPL A

SJMP BACK

PO lam cong vso

Khi co cac dien tro n6i toi cdng PO, de tao thanh c6ng VaG thl can phai l~p trinh bang each ghi 1 toi tat ca cac bit cua ding. DOC;ln chuang trinh sau se cau hlnh PO lam cong VaG thea each vira neu, sau do dtr lieu nhan diroc a cong nay se gi'ri d~n Pl.

MOV A,#FFH iGan A = FF d9ng Hexa

MOV PO, A ;D~t PO lam c6ng vao b~ng cach iGhi 1 vao t&t ca cac bit

BACK: MOV A, PO ;Nh~n dir li$u tv pO

MOV Pl, A ;Gui no den c6ng 1

SJMP BACK ;L~p 19i

Chuyen dCt Ii~u va dia chi qua cong PO

Nhu trinh bay a hlnh 4.1, PO ngoai chirc nang chuyen du lieu con diroc dung M chuyen 8 bit dia chi ADO - AD7. Khi n6i 8051/31 toi bo nho ngoai, thl cong PO cung cap ca dia chi va du lieu bang each don kenh de tiet kiern s6 chan. Chan ALE se bao PO co dia chi hay du lieu, Neu ALE = 0 thl PO cap du lieu DO - D7, con neu ALE= 1 thi la dia chi.

Cong P1

Cong PI cling co 8 chan, tir chan 1 den chan 8, va co the sit dung lam delu

60

Cdu true va l~p trtnh ho vi die'u khiln 8051

vao hoac ra. Khac voi cong PO, cong P1 khong can den dien tra keo VI no da co cac dien tro keo ben trong. Khi Reset, cong P1 diroc cau hlnh lam ccng ra. Vf du, doan chuang trlnh sau se giri lien tuc cac gia tri 55H va AAH ra cong PI,

MOV BACK: MOV ACAL SJMP

A, #55H Pl, A DELAY BACK

Cong P1 lam ds'u vao

De chuyen cong PI thanh dAu vao can l~p trinh bang each ghi 1 den tat ca cac bit cua cong. Chi tier ve van de nay diroc neu 6' phu luc C.2. Trong doan chuang trinh sau, cong PI diroc cau hinh lam cong vao bang each ghi 1 vao cac bit cua cong, sau do dfr lieu nhan diroc tir cong nay duoc cat vao R7, R6 va R5.

MOV A,#OFFH iN<;lp A FF ? dang hexa
= CJ
MOV Pl,A iT<;lo ;{ Pl thanh ;{ ct&u VaG bang
cong cong
icach ghi 1 VaG cac bit cua ;{
cong
MOV A,Pl iNh€ln du li$u tv Pl
MOV R7,A iCgt n6 VaG thanh ghi R7
ACALL DELAY iCher
MOV A,Pl iNh€ln du lieu khac tv Pl
MOV R6,A ;cgt n6 VaG thanh ghi R6
ACALL DELAY iCher
MOV A,Pl ; Nh€ln du li$u khac tv ;{ Pl
cong
MOV R5,A iCgt du li$u VaG thanh ghi R5
Cong P2 Cong P2 ding co 8 chan, tir chan 21 den 28, va co the diroc sir dung lam dau vao hoac dau ra. Cling gi6ng nhir cong PI, cong P2 khong can dien tro keo VI ben trong da co cac dien tr6' keo, Khi Reset, thi cong P2 duoc cau hlnh lam dau ra. Vf du doan chuang trinh sau se giii lien tuc ra cdng P2 cac gia tri 55H va AAH.

MOV A, #55H

BACK: MOV P 2 , A ACALL DELAY CPL A SJMP BACK

Cdng VCIO/ra va I¢p trinh

61

Cong P2 lam ds'u vao

D~ P2 lam cong vao thi can diroc lap trlnh bang each ghi cac s6 1 toi tat d. cac chan cua cong. Doan chuang trlnh sau day dau tien dinh cau hinh P2 lam c6ng vao bang each ghi I den tat d. cac chan cua cong, sau do dfr lieu nhan diroc til' P2 diroc giri lien tuc den PI.

MOV A,OFFH ; GElD A gia tr:j. FF d<;l.ng Hexa
MOV P2,A ;£)~t P2 lam -? vao b~ng cach
cong
;ghi 1 den tEit ? cac ChElD ? -?
ca cua cong
BACK: MOV A,2 ; Nh€tn dv lieu tv P2 MOV Pi, A ;Gui du li~u den Pi

SJMP BACK ;Lap l<;l.i

Hai chuc nang cue cong P2: Chuyen dja chi va dft Ii~u

a cac he thong 8751, 89C51 va DS5000, P2 diroc dung lam ccng vsolt»: Tuy nhien, a he thong 8031 thl cong P2 con duoc sir dung cung voi PO d~ tao ra dia chi 16 bit cho bo nho ngoai, Vi 8031 co kha nang truy cap 64k byte bo nho ngoai, nen can co bus dia chi 16 bit. PO cung cap 8 bit dia chi thap qua AO - A 7, con lai 8 bit dia chi cao A8 - A IS do P2 cung cap. Nhtr vay, khi 8031 diroc n6i tai bQ nh6 ngoai thl 8 bit P2 diroc dung cho dia chi 16 bit va khong th~ dung cho vao ra duoc.

Tir nhirng trinh bay tren day, co the thay rang, trong cac he thong 8751, 89C51 hoac DS5000 thl chung ta co

ba cong PO, PI va P2 cho cac thao Bang 4.2. Cac clnrc nang khac ella cong

tac vao ra va nhu the Ia co th~ du P3

eho hau het cac irng dung. Con lai ccng P3 diroc dung cho ngat va ta se cung tim hieu duoi day.

Cong P3 Cong P3 chiern 8 chan, tir chan 10 den chan 17. C6ng nay co th~ diroc sir dung lam dAu vao hoac dau ra. Cling nhir PI va P2, cong P3 khong can dien trc keo. Khi Reset, ccng P3 diroc cau hlnh lam mot

Bit P3 Chlrc n~ng ChilO
P3.0 Nhan dLt li$u (RXO) 10
P3.1 Phat dLt li$u (TXO) 11
P3.2 Ngat O(INTO) 12
P3.3 Ngat 1(INT1) 13
P3.4 B9 dlnh thai 0 (TO) 14
P3.5 1 B9 d!nh thoi 1 (T1) 15
P3.6 Ghi (WR) 16
P3.7 eoc (RO) 17 62

Cdu true va l~p trinh ho vi die'u khiln 8051

(

cong ra, tuy nhien day khong phai la trng dung chu yeu, Cong P3 co them mot I

chirc nang quan trong khac la cung dip mot s6 tin hieu dac biet, chang han nhir ,

ngat. Bang 4.2 gioi thieu cac chirc nang khac cua cong P3. Bang nay duoc sir

dung cho ca 8051 va 8031. t

Bit P3.0 va P3.1 diroc dung d~ thu va phat dfr lieu trong truyen thong noi tiep. 1

Bit P3.2 va P3.3 diroc danh cho ngat ngoai va se diroc trinh bay chi tiet a chuang

11. Bit P3.4 va P3.5 diroc dung cho cac bo dinh thai 0 va I va chi tiet diroc trlnh

bay a chuang 9. Cu6i cung bit P3.6 va P3.7 dung d~ ghi, doc cac bo nho ngoai,

Trong cac he thong 8751, 89C51 hoac D35000, cac chan P3.6 va P3.7 diroc dung

cho vao/ra, cac chan con lai cua P3 duoc dung voi cac chirc nang khac nhau.

4.2 L~P TRINH vAo - RA, THAO TAe BIT PhU'dng phap truy c~p toan be? 8 bit

DO<;1n chuang trinh sau gioi thieu each truy cap toan bo 8 bit cua cong PI .

BACK: MOV A,#55H
MOV Pl,A
ACALL DELAY
MOV A, #OAAH
MOV Pl,A
ACALL DELAY
SJMP BACK DO<;1n chuang trinh tren lien tuc thay d6i gia tri bit cua Pl. Chuang trinh dang nay ding da diroc biet a phan tren. Bay gio cluing ta viet lai doan chuang trinh tren gon han bang each truy cap tnrc tiep cong rna khongcan thong qua thanh ghi

tich Iuy nhir sau:
BACK: MOV Pl,#55H
ACALL DELAY
MOV Pl,#OOH
CALL DELAY
SJMP BACK Cling co th~ trinh bay doan chuang trinh tren bang ky thuat doc-sira d6i- ghi nhu a phan trinh bay sau day.

C6ng vaolra va ldp trinn

63

Cac cong cua 8051 co the diroc

truy cap bang ky thuat diroc goi la: PO.1 P1.1 P2.1 P3.1 01

D9c-Sua d6i-Ghi. Phuong phap nay PO.2 P1.2 P2.2 P3.2 02

giam thieu diroc nhieu dong lenh PO.3 P1.3 P2.3 P3.3 03

nho ket hop tff"t cii ba thao tac: 1) PO.4 P1.4 P2.4 P3.4 04

doc cong, 2) sua d6i va 3) ghi ra PO.S P1.S P2.S P3.S os

cdng vao mot lenh don, Doan PO.6 P1.6 P2.6 P3.6 06

cmrong trlnh sau dau tien dl[tt PO.7 P1.7 P2.7 P3.7 07

01010101 (nhi phan) vao cong I, sau do lenh "XLR PI, #OFFH" thuc hien phep logic XOR (OR loai trir) tren cong PI voi 1111 1111 (nhi phan) va cuoi cung ghi ket qua trc lai cong Pl.

MOV P1, #55H

Bang 4.3. Cae c6ng d!nh d!a chi bit

PO P1 P2 P3 Bit

PO.O P1.0 P2.0 P3.0 DO

OQc-Slta d6i -Ghi (Read - Modify - Write)

;P1 = 01010101

AGAIN: XLR

P1,#OFFH

;P1 XOR 1111 1111

ACALL DELAY SJMP AGAIN

Liru y rang lenh XOR 55H vai FFH cho ket qua la AAH. Tuong tl! nhir vay, lenh XOR AAH voi FFH lai cho gia tri ket qua la 55H. Cac lenh logic diroc trinh bay a clurong 7.

Ojnh dja chi bit cua cac c6ng

Nhieu irng dung cluing ta chi can truy cap I hoac 2 bit cua cong thay VI truy cap ca 8 bit. Mot di~m manh cua cac cong 8051 la co kha nang truy cap tirng bit mot rna khong lam thay d6i cac bit con lai cua cong. Doan chuong trinh a vi du sau lien tuc dao gia tri cua bit Pl.2.

BACK: CPL r-i . 2 ; LElY bu bit r-i , 2 ACALL DELAY

SJMP BACK

Mot bien the khac cua doan chuang trlnh tren nhu sau:

AGACN: SETB Plo2 ;Di;l.t bit Plo2 len cao

ACALL DELAY CLR r i . 2

;Xoa bit P1.2 xu6ng thElp

ACALL DELAY SJMP AGAIN

64

Cdu true va ItJ,p trinh hi! vi die'u khiln 8051_-

Bang 4.3 trlnh bay cac bit cac cong VaG ra cua 8051. Xem vi du 4.2 ve thao tac bit tren cac bit vao/ra. Nen hru y la a vi du 4.2, nhirng bit khong diroc dung thi khong bi thay d6i. Day Hi kha nang dinh dia chi bit cua cac cong vao/ra va la m¢t trang nhfing di~m rat m~nh ella b¢ vi dieu khi~n 8051.

Vi dl,l4.2

Hay viet chuang trinh thuc hien cac cong viec sau:

a) Ouy tri kiem tra bit PI.2 cho den khi bit nay len cao.

b) Khi P1.2 len cao, hay ghi gia tri 45H VaG cong PO.

c) Giri mot xung Cao-Xuong-Thap (Hvto-L) toi P2.3.

Giai:
SET PI. 2 iTi;l.o bit PI. 2 La dau VaG
MOV A,#45H iGfm A = 45H
AGAIN: JNB Pl.2,AGAIN iThoat khi PI.2 - 1
MOV PO,A iXU&t A tai c6ng PO
SETB P2.3 i8ua P2.3 len cao
CLR P2.3 iXoa P2.3 d~ tc;J.o xung H-to-L Trang chuang trinh nay lenh "JNB Pl.2, AGAIN" (y lai vong lap cho den kh! P1.2 chU~ l~~ cao. K,hi Pl.2 len cao, chtron~ trlnhth~at khoi vb~g lap, ghi I gm tq 45H Wl cong PO va t~o ra xung H-to-L nha chu6l lynh SETB va CLR.

ChLldng 5

CHE ao alNH alA CHI

.. .

CPU co the truy cap du lieu theo nhieu each khac nhau. Dir lieu co th~ b trong mot thanh ghi, b trong bo nho hoac diroc cho dtrci dang mot gia tri nrc thai. each CPU truy cap du lieu diroc goi la che d¢ dinh dia chi. Chuang nay cluing ta tim hi~u cac che d¢ dinh dia chi cua 8051.

Cac che d¢ djnh dia chi diroc xac dinh ttr khi thiet ke bo vi xi! ly va khong the

bi thay d6i do lap trlnh vien, 8051 co tat ca 5 che d¢ dinh dja chi nhu sau:

I. Tuc thai

2. Thanh ghi

3. Trt!c tiep

4. Gian tiep qua thanh ghi

5. Chi s6

5.1 CHE DO D!NH D!A CHI TOc THai vA THANH GHI Che d9 djnh dja chi tuc thbi

ache d¢ dinh dja chi nay, toan hang nguon la mot hang s6 va nhir ten goi, toan hang co ngay sau rna lenh. LI1U y rang, tnroc dil lieu nrc thai can co dau "#". Ch€ do dinh dia chi nay co the diroc dung de nap dfr lieu vao moi thanh ghi, ke ca thanh ghi con tro du lieu DPTR. Vf du:

MOVA, #25H MOVR4, #62

;N~p gia tri 25H VaG thanh ghi A ;N~p gia tri 62 th~p phan VaG R4

MOVB,#40H ;N~p gia tri 40 H VaG thanh ghi B

MOVDPTR,#4521H ;N~p 4512H VaG con tro du li~u DPTR

DPTR la thanh ghi 16 bit, tuy vay no cling co the duoc truy cap nhu hai thanh

ghi 8 bit DPH va DPL, trong do DPH la byte cao va DPL la byte thap, Xet dO<;t11 chuang trinh sau:

MOVD PTR,#2550H MOV A, #50H

MOV DPH, #25H

Cling nen hru y Ut lenh diroi day co th~ tao ra 16i VI gia tr] nap vao DPTR Ian

66

Cau true va It;ip trinh hi! vi dUu khiln 8051

hon 16 bit:

MOV DPTR,#68975 iGia trt kh6ng hqp l~ ( >65535=FFFFH)

Co tM dung chi dan EQU de truy cap dii lieu tac thai nhu sau:

COUNT EQU 30

MOV MOV ORG

R4,#COUNT DPTR,#MYDATA 200H

iR4=lE (30=lEH) ; DPTR=200H

MYDATA: DB "Nha Trang"

Tat nhien cluing ta cling co th~ sir dung che dQ dinh dia chi nrc thai d~ giIi du lieu den cac cong cua 8051.

VI d ... , \\ MOV Pl, # 55H" la mot lenh hop I¢.

Che dQ dinh dia chi thanh ghi

Ch€ dQ dinh dia chi thanh ghi la sir dung cac thanh ghi d~ IlIU dii lieu can thao taco Chung ta xem cac vf du sau:

iSao nQi dung thanh ghi RO vao thanh ghi A iSao nQi dung thanh ghi A vao thanh ghi R2 iCQng nQi dung thanh ghi R5 vao thanh ghi A ;CQng nQi dung thanh ghi R7 vao thanh ghi A iSao nQi dung thanh ghi A vao thanh ghi R6

Nen hru y la thanh ghi nguon va dich phai phu hop ve kfch thuoc. Noi each khac, lenh " MOY DPTR, A" se gay loi VI ngu6n la thanh ghi 8 bit va dlch lai la thanh ghi 16 bit. Xet doan chuong trinh sau:

MOV DPTR,#25F5H MOV R7, DPL

MOV R6, DPH

De y rang, ta co the chuyen dii lieu giiia thanh ghi tfch luy A va thanh ghi Rn (n co gia tr] tir 0 den 7) nhung chuyen dii lieu giiia cac thanh ghi Rn th! khong diroc phep, Vf du, lenh "MOY R4, R7" la khong hop I~.

Nhu vay, b hai che dQ dinh dia chi vira xet b tren, toan hang hoac b trong thanh ghi hoac la mot gia tri tnrc tiep trong lenh. Tuy nhien, trong nhieu tnrong hop, dii lieu lai nam 6 bo nhc RAM hoac ROM. Sau day, cluing ta xem xet each truy cap du lieu b nhiing tnrong hop nay.

MOV A,RO
MOV R2,A
ADD AiR5
ADD A,R7
MOV R6,A CMd(j dinh dia chi

67

5.2 cAc CHE D9 D!NH D!A CHi TRUY cAp BO NHO Che de? dinh dia chi trl/c tiep

Nhir da noi a chuang 2, trong 8051 co 128 byte bo nho RAM. B¢ nho RAM dircc gan dia chi tir 00 den FFH va diroc phan chia nlur sau:

- Cac ngan nho tll' 00 den 1 FH duoc gan cho cac bang thanh ghi va ngan xep.

- Cac ngan nho tlr 20H den 2FH diroc danh cho khong gian dinh dia chi bit

de hru du lieu thea tirng bit.

- Cac ngan nho tir 30H den 7FH la khong gian d~ liru dtr lieu co kfch thiroc 1 byte.

Che d¢ dinh dia chi true tiep co the truy cap roan bo khong gian cua bo nho RAM. Tuy nhien, che do nay thirong diroc dung de truy cap cac ngan nho RAM ttt 30H den 7FH, VI thuc te d6i voi khong gian nho danh cho bang thanh ghi thl dii diroc truy cap bang ten thanh ghi nhir RO - R7. d che' d¢ dinh dia chi tnrc tiep, dia chi ngan nho RAM chua du lieu la roan hang cua lenh. De thay su khac nhau vai che d¢ dinh dia chi nrc thl, a do dil lieu (chir khong phai la dia chi) nr no la toan hang cua lenh, Dau "#" la dau hieu phan bier giii'a hai che do dinh dia chi. Xet cac VI du diroi day va hru y ding cac lenh khong co dau (#):

MOV RO,40 iSao nQi dung ngan nha 40H cua RAM vao RO

MOV 56H,A iSao thanh ghi A vao ngan nha RAM 56H

MOV R4,7FH iChuy~n nQi dung ngan nha 7FH vao R4

Nhu da noi a phan tren, cac ngan nho tir 0 den 7 cua RAM duoc cap cho bang 0 cua cac thanh ghi RO - R7. Cac thanh ghi nay co th~ diroc truy cap theo

hai each nhu sau:
MOV A,4 iL~nh nay va l~nh (MOV A,R4)la nhv nhau
MOV A,R4 iSao nQi dung thanh ghi R4 vao A
MOV A,7 iL~nh nay va l~nh (MOV A,R7)la nhv nhau
MOV A,R7 iSao nQi dung thanh ghi R7 vao A D~ r5 han v~ vai tro cua dau (#) trong cac lenh cua 8051, chung ta se xem xet

cac doan chuang trinh sau day:

MOV R2, #05 iGan R2=05

MOV A,2 iSao nQi dung thanh ghi R2 vao A (A=R2=5)

MOV B,2 iSao nQi dung thanh ghi R2 vao B (B=R2=5)

r

\

68 Ca" true va lr,ip trillh h9 vi dUu khiill 8051

MOV 7,2 ;Sao R2 VaG R7

;vi Le nh \\MOV R7,R2" La kh6ng h op leo

D~ thay sir dung ten RO - R 7 d~ truy cap b¢ nho thi r6 rang han la dung dia chi b¢ nho. Tuy nhien, doi vci cac ngan nho tir 30H den 7FH thi khong th~ truy cap thea each nhu vay dUQ'C rna phai dung dia chi cua cluing, VI dan gian (} vung nay cluing khong co ten.

Nh6m thanh ghi SFR va dla chi cua chung

Trong so cac thanh ghi chung ta tim hieu tren day, vi du RO - R 7, deu la mot phan trong 128 byte cua bo nho RAM. The con cac thanh ghi nhir A, B, PSW va DPTR lieu co dia chi khong? Cau tra lai Ia co. Cac thanh ghi A, B, PSW va DPTR thuoc nhom thirong diroc goi la nhom thanh ghi dac biet SFR (Special Function Registers). Co nhieu thanh ghi voi chirc nang dac biet va chung diroc sir dung rong dli rna chung ta se co dip de cap den (} cac chuang sau. Cac thanh ghi SFR co the truy cap thea ten (va nhu the d~ dang han nhieu) hoac thea dia chi. VI du, dia chi cua thanh ghi A la EOH va thanh ghi B la FOH nhir gioi thieu (} bang 5.1.

Sau day la VI du cac cap lenh co cung chtrc nang:
MOV OEOH,#55H ;Nc;J.p 55H VaG thanh ghi A (A=55H)
MOV A,#55H
MOV OFOH,#25H ;Nc;J.p 2SH VaG thanh ghi B(B=25)
MOV 3,#25H
MOV OEOH,R2 ;Sao nQi dung thanh ghi R2 VaG A
MOV A,R2
MOV OFOH,RO ;Sao nQi dung thanh ghi RO VaG B
MOV B,RO Bang 5.1 diroi day liet ke cac thanh ghi dac biet SFR cua 8051 va dia chi cua chung. Can phai luu y den hai diem sau ve dia chi cua SFR:

1. Cac thanh ghi SFR co dia chi nam trong khoang tir 80H den FFH, VI cac dia chi tir 00 den 7FH la dia chi bo nho RAM trong cua 8051.

. ..

2. Khong phai tat ca moi dia chi ttr 80H den FFH deu danh cho SFH sir dung.

Nhirng ngan nho trong khoang tir 80H den FFH chua dung la dir trfr nlurng lap trinh vien 8051 khong diroc sir dung.

eM d(J dinh dia chi

69

Bang 5.1. I)~a chi cua thanh ghi cmrc nang d~c bi~t SFR

L~nh Ten Dja chi
ACC* Thanh ghi tieh luy (thanh ghi t6ng ) A OEOH
B* Thanh ghi B OFOH
PSW· Tu tranq thai chironq trinh OOOH
SP Con tro ngan xep 81H
DPTR Con tro dii Ii~u hai byte
DPL Byte thap ella OPTR 82H
DPH Byte eao ella OPTR 83H
PO* C6ng 0 80H
P1* C6ng 1 90H
P2* C6ng 2 OAOH
P3* C6ng 3 OBOH
IP* Dieu khi~n uu tien ngat OB8H
IE* Dieu khi~n eho phep ngat OA8H
TMOO Dieu khi~n ehe d9 b9 dem/b9 dinh thoi 89H
TCON* Dieu khi~n b9 dem/b9 dinh thoi 88H
T2CON* Dieu khi~n b9 dem/b9 dlnh thoi 2 OC8H
T2MOO Dieu khi~n ehe d9 b9 dem/b9 djnh thol 2 OC9H
THO Byte eao ella b9 dem/b9 d!nh thol 0 8CH
TLO Byte thap ella b9 dem/b9 dlnh thol 0 8AH
TH1 Byte eao cua b9 dem/b9 dlnh thol 1 80H
TL 1 Byte thap ella b9 dem/b9 d!nh thoi 1 8BH
TH2 Byte eao cua b9 dem/b9 dinh thoi 2 OCOH
TL2 Byte thap ella b9 dem/b9 dinh thol 2 OCCH
RCAP2H Byte eao ella thanh ghi b9 dem/b9 dinh thoi 2 OCBH
RCAP2L Byte thap ella thanh ghi b9 dem/b9 dinh thoi 2 OCAH
SCON* Dieu khi~n noi tiep 98H
SBUF B9 d~m dii li~u noi tiep 99H
PCON Dieu khi~n eong suat 87H Ghi chit: * Cac thanh ghi co the dinh dia chi bit.

Nen hru y ache d¢ dinh dia chi tnrc tiep la dia chi truy cap duoc den tirng byte, trong khoang 00 - FFH. Nhir vay, neu str dung che d¢ dinh dia chi nay thi Ban e6 th~ truy cap diroc cac ngan nho RAM va cac thanh ghi ben trong 8051.

70

Cdu true va ltJ.p trinh hi! vi diiu khiln 8051

Vi d1:l5.1

Viet chirong trinh de gjri 55H den cong PI va P2 Slr dung hoac

a) Ten cac cong

b) Hoac dia chi cac cong

Giai:

A,#SSH PI,A

a) MOV MOV

;A=SSH ;PI=SSH

MOV P2, A ; P2=SSH

Tir bang 5.1 ta c6 dia chi cong PI la 80H va P2 la AOH

A,#SSH 80H,A OAOH A

;A=SSH ;PI=SSH ;P2=SSH

MOV MOV MOV

NgAn xep va che dQ djnh dja chi tnrc tiep

Mot irng dung quan trong cua che d¢ dinh dia chi true tiep la ngan xep, Trong ho 8051, chi c6 che d¢ dinh dia chi true tiep la duoc phep cat va lay du lieu tir ngan xep, Nhir vay, lenh "PUSH A" la khong hop I~. Lenh tren can viet diroi dang sau "PUSH OEOH", trong d6 OEOH la dia chi cua thanh ghi A. Tirong tu nhir vay, de cat thanh ghi R3 bang 0 vao ngan xep, ta can viet la "PUSH 03". Tirong tu, d6i voi lenh POP cling can sir dung che d¢ dinh dia chi true tiep.

Vi d1:l5.2

Viet doan chirong trlnh cat thanh ghi R5, R6 va A vao ngan xep va sau d6 lay ra va cho vao cac thanh ghi R2, R3 va B urong irng,

Giai:
PUSH 05 ;£)gy RS vao ngan --'
xep
PUSH 06 ;£)gy R6 vao ngan x~p
PUSH OEOH ;£)gy thanhghi A vao ngan --'
xep
POP OFOH ;Keo dinh ngan x~p eho vao thanh ghi B
;Bay gi6' B=A
POP 02 ;Keo dinh ngan --' eho vao thanh ghi R2
xep
;Bay gi6' R2=R6
POP 03 ;Keo dinh nqan ~, eho vao thanh ghi
xep
;Bay gi6' R3=RS Chl dl) dinl: dia chi

Che dQ dinh dia chi gilm tiep thanh ghi

o che d¢ nay, thanh ghi diroc dung d~ tro den dCt lieu co trong b¢ nho, Neu dii lieu co tren chip CPU thi chi cac thanh ghi RO va R 1 moi diroc str dung, va nhu vay cling co nghia khong th~ dung cac thanh ghi R2 - R 7 dt tro den dia chi cua toan hang b che eo dinh dia chi nay. Neu RO va RI dugc dung lam con tro, nghia la chung hru dia chi cua cac ngan nho RAM thl tnroc cac thanh ghi can d~t dau "@" nhu vi du sau:

MOV A, @RO MOV @Rl,B

;Chuygn ng~n nha RAM c6 dia chI a RO vao A ;Chuygn B vao ng~n nha RAM c6 dta chI a Rl

Luu y rang, b day RO cling nhu RI deu co dau "@" dirng tnroc. Neu khong eo dau "@" thi do la lenh chuyen noi dung thanh ghi RO va RI chu khong phai dii lieu ngan nho co dia chi trong RO va RI.

Vi d ... 5.3

Viet chuang trlnh d~ sao gia tri 55H vao cac ngan nho RAM tai dia chi tit

40H den 44H sir dung:

a) Che d¢ dinh dia chi tnrc tiep.

b) Che d¢ dinh dia chi gian tiep thanh ghi khong dung vong lap.

c) Che d¢ b co dung vong lap.

Giai:
a)
MOV A,#55H ;N<;l.p A q i.a trt 55H
MOV 40H,A ;Sao A vao ng~n nha RAM 40H
MOV 41H,A ;Sao A vao ng~n nha RAM 41H
MOV 42H,A ;Sao A vao ngan nha RAM 42H
MOV 43H,A ;Sao A vao ngan nha RAM 43H
MOV 44H,A ;Sao A vao ng~n nha RAM 44H
b)
MOV A,# 55H ;N<;l.p vao A gL3. trt 55H
MOV RO,#40H ;N<;l.p con tro RO=40H
MOV @RO,A ;Sao A vao ngan nha RAM do RO tro d~n
INC RO ;Tang con tro. Bay gia RO=41H
MOV @RO,A ;Sao A vao ngan nha RAM do RO tro d~n 71

72 ecfu true va 14P trinh ho vi dUu khiin 8051

INC RO ;Tang con tro. Bay gia RO=42H
MOV @RO,A ;Sao A vao ngan nha RAM do RO tro den
INC RO ;Tang con tro. Bay gio RO=43H
MOV @RO,A ;Sao A vao ngan nha RAM do RO tro den
INC RO ;Tang con tro. Bay go RO=44H
MOV @RO,A
c)
MOV A,#55H ;N<;l.p vao A q i a tr:t, 55H
MOV RO,#40H ;N<;l.p con tro. Elia chI RAM RO=40H
MOV R2,#05 ;N<;l.p bQ dem R2=5
AGAIN:
MOV @RO,A ;Sao A vao ngan nh c RAM do RO chI den
INC ;Tang con tro RO
DJNZ R2,AGAIN ;L.;i.p l<;l.i cho den khi b9 dem=O. lIu di~m cua che dQ djnh dja chi gian tiep thanh ghi

Mot trong nhtrng iru diem cua ch€ d¢ dinh dia chi gian tiep thanh ghi la cho phep truy cap dtr lieu linh hoat han so voi che d¢ dinh dia chi true tiep. Vi du 5.3 gioi thieu doan chuang trlnh sao gia tri 55H vao cac vi trf ngan nho cua RAM tir 40H den 44H . Liru Y ding loi giai b) co hai lenh diroc lap lai mot so lan. Nhir vay co th~ tao ra mot vong lap hai lenh nay nhir alai giai c). Loi giai c) r5 rang hieu qua han ca va chi co th~ sir dung ache d¢ dinh dia chi gian tiep qua thanh ghi. Vong l~p khong dung diroc ache d¢ dinh dia chi true tiep. Day la sir khac nhau chu yeu giCi'a dinh dia chi tnrc tiep va gian tiep,

Vi dl;l 5.4 .~

Hay viet chuang trlnh xoa 16 ngan nho RAM bat dau tir dia chi 60;-' Gial:

CLR A ;Xoa A=O
MOV Rl,#60H ;N<;l.p con tro.Rl=60H
MOV R7,#16H ;N<;l.p bQ dem,R7=16(lO H d<;l.ng hexa) I
AGAIN:
MOV @Rl,A ;Xoa ngan nha RAM do Rl tro den I
INC Rl ;TC'tng Rl dem=o__j
DJNZ R7,AGAIN ;Lap La i, eho den khi be Chi cit) dinh dia chi

73

Vf du 5.5 rninh hoa each sir dung RO va R I ache d¢ dinh dia chi gian tiep thanh ghi khi truyen kh6i.

Hay viet chuang trlnh d~ sao mot kh6i 10 byte dir lieu tir vi trf ngan nho RAM bAt dciu tir 35H vao cac vi trf ngan nho RAM bat dciu tir 60H

Vi du 5.5

Gicii:
MOV RO,#35H ;Con tro '"
nguon
MOV R1,#60H ;Con tro dich
MOV R3,#10 ;BQ d~m
BACK: MOV A, @RO ;La:y 1 byte tv '"
nguon
MOV @R1,A ;Sao chep d~n dich
INC RO ;Tang con tro .e
nguon
INC R1 ;Tang con tro dich
DJNZ R3,BACK ;L~p cho d~n khi h~t 10 byte Hill" che cua che de? djnh dja chi giim tiep thanh ghi d 8051

Nhu da noi a phan tnroc, RO va R I la cac thanh ghi duy nhat diroc dung de lam con tro ache dQ dinh dia chi gian tiep thanh ghi. RO va R 1 la cac thanh ghi 8 bit, nen cluing chi cho phep truy cap den cac ngan nho RAM trong, tir dia chi 30H den 7FH va cac thanh ghi SFR. Trong thirc te, co nhieu tnrong hop can truy cap dii' lieu diroc cat a RAM ngoai hoac a khong gian ROM tren chip. Trong nhfrng tnrong hop do chung ta can sil dung thanh ghi 16 bit la DPTR.

Che de? djnh dja chi chi so va truy c~p be? nho ROM trim chip

Che do dinh dia chi chi s6 diroc sil dung rong rai khi truy cap cac phan til dii' lieu cua bang trong khong gian ROM chuang trmh cua 8051. Lenh diroc dung cho muc dfch nay la "MOVC A, @ A + DPTR". Thanh ghi 16 bit DPTR va thanh ghi A diroc dung d~ tao ra dia chi cua phan til dfr lieu diroc hru trong ROM tren chip. Do cac phan til dii' lieu diroc cat trong khong gian ROM chuang trlnh tren chip 8051, nen can dung lenh MOVC thay cho lenh MOV (chir "C" a cuoi lenh

co nghia la rna lenh "Code"). 0 lenh nay, noi dung cua A diroc cong vai noi dung thanh ghi 16 bit DPTR d~ tao ra dia chi 16 bit. Xet vi du sau.

74

Cdu true va ltJp trinn ho vi dieu khiin 8051

r

f

!.

Vi d1,l5.6

Gia sir chir "NAM" duoc luu trong ROM co dia chi bilt dau tai 200H va chuang trinh duoc ghi vao ROM bat dau tir dia chi O. Hay phan tfch each chuang trinh hoat dong va xac dinh xern chir "NAM" sau khi chay chuang trinh nay diroc cat vao dau?

Giai:

MYDATA:

DB OOOOH

o chuang trinh tren, cac ngan nho ROM chuang trinh 200H - 2002H co cac noi dung sau:

200 = ('N'); 201= ('A') va 202 = ('M')

Chung ta bat dau voi DPTR = 200H va A = O. Lenh "MOVC A, @ A + DPTR chuyen noi dung cua 6 nho 200H trong ROM (200H + 0 = 200H) vao A.

Thanh ghi A chua gia tri 4EH la rna ASC cua ky tv "N". Ky tv nay diroc cat vao RO. Ke do, DPTR duoc tang len tao thanh DPTR = 201H, A lai diroc xoa v~ 0 d~ lay noi dung cua vi trf nho ke' tiep trong ROM la 201 H chua ky nr "A", Sau khi chuang trlnh nay chay ta co RO = 4EH, R I = 41 H va R2 = 4FH la cac rna ASCII cua cac ky nr "N", "A" va "M",

MOV DPTR,#200H
CLA A
MOVC A,@A+DPTR
MOV RO,A
INC DPTR
CLR A
MOVC A,@A+DPTR
MOV Rl,A
INC DPTR
CLR A
MOVC A@A+DPTR
MOV R2,A
HERE: SJMP HERE
;Di1 lieu duqc dot
ORG 200H
END ;B~t dau dot ROM tai dia chi OOH ;Dia chi bang tharn chigu DPTR=200H iXoa thanh ghi A (A=O)

iL~y ky tv khong gian nh& chu0ng trinh iC~t VaG trong RO

;DPTR=201, tro dgn ky tv kg tigp

iXoa thanh ghi A iL~y ky tv ke tiep iC~t VaG Rl

;DPTR=202 con tro tro dgn ky tv sau do iXoa thanh ghi A

;Nh~n ky tv kg tigp

iC~t VaG R2

; Dung La i. 21 day.

21 khong gian rna l$nh t~i dia chi 200H

;Kgt thuc chu0ng trinh

Chi drJ dinh dia chi

Vi d1,l5.7

Gia sir khong gian ROM bat dau tit dia chi 250H c6 tit "Viet Nam", hay viet chuang trinh de chuyen cac byte vao cac vi trf ngan nho RAM bat dau tit dia chi 40H.

Giai:

; (a) Phuong phap su dung rnQt bo dern ORG 000

MOV DPTR,#MYDATA ;N.;J.p can tro ROM
MOV RO,#40H ;Nap can tro RAM
MOV R2,#7 ;N.;J.p bo dern
BACK: CLR A ;Xoa thanh ghi A
MOVC A, @A+DPTR ;Chuy€;n du lieu tu khong gian rna
MOV RO,A ;C&t vao ngan nho- RAM
INC DPTR ;Tang can tro ROM
INC RO ;Tang can tro RAM
DJNZ R2,BACK ;Lap lai cho denkhi bo dern=O
HERE: SJM HERE ;khong gian rna cua ROM tren chip dung d€; c&t du lieu ORG 250H

MYDATA: DB "Viet Narn" END

; (b) phuong phap su d~ng ky t~ null d€; ket thuc chu6i ORG 000

;N.;J.p can tro ROM
;Nap can tro RAM
;Xoa thanh ghi A(A=O) MOV DPTR,#MYDATA
MOV RO,#40
BACK: CLR A S
MOVC A,@A+DPTR
JZ HERE
MOV DPTR,#MYDATA
INC @RO,A
INC RO
SJM BACK ;Chuy€;n du li~u tu khong gian rna ;Thoat ra neu co ky t~ Null

;C&t vao ngan nho- cua RAM

;Tang can tro ROM

;Tang can tro RAM

;Lc';l.p La i

75

76 Ca" true va l~p trinh hfJ vi die'" khiln 8051

HERE: SJMP HERE

;Kh6ng gian rna cua ROM tren chip dung dg ca't du li~u ORG 250H

MYADTA:DB "Viet Narn",O;K~ tv Null dg k~t th6c chu3i END END

Liru 9 den each ta sii dung lenh JZ de phat hien k9 nr trong, 0 va ket thuc chuoi.

Bang tham chleu va Slt dl:lng che dQ djnh dja chi chi so

Khai niern bang tham chieu diroc dung rong rai trong lap trinh cac bo vi xii 19. Bang cho phep truy cap cac phan tir cua mot bang tlurong xuyen diroc sir dung voi so thao tac it nhat. Vi du, gia sii co mot irng dung can x2 gia tri trong pham vi o den 9. Ta co th~ sir dung bang tham chieu thay cho viec tinh toano Hay xem vi du sau.

Vi dl;l5.8

Hay viet mot chuang trinh de lay x gia tri cong PI va lien tuc giri gia tri x2 toi cong P2.

Giai:
ORG 000
MOV DPTR,#300 H
MOV A,#OFFH
MOV P1,A
BACK: MOVA, P1
MOVC A,@A+DPTR
MOV P2, A ;N9-p d:j..a chI bang xc'ip '"
xep
;N9-p A gia tr:j.. FFH
;f)~t ;? P1 la dau van
cong
;La'y gia tr:j.. X tu P1
;La'y gia tr:j.. X tu bang ;Xua't n6 ra c6ng P2 ;L~p 19-i

SJMP BACK ORG 300H

XSQR-TABLE:

DB 0,1,4,9,16,25,36,49,64,81

END

LUll 9 lenh dAu tien co th~ thay bang lenh "MOV DPTR, #XSQR-TABLE" .

Che' d¢ dinh dia chi

Vi d1;l5.9

Til hinh 4.8 hay tra lai cac elm hoi sau:

a) Xac dinh noi dung cac V! trf 300 - 309H cua ROM.

b) V] trt nao cua ROM chua 62 va gia tri d6 Ia bao nhieu?

c) Gia sir PI e6 gia tri la 9 thi gia tri P2 la bao nhieu (0 dang nhi phan)?

Giai:

a) Gia tri 0 cac ngan nho 300H-309H cua ROM la:

300=(00) 301=(01) 302=(04) 303=(09)

304=(10) 4x4=16=10 in hexa
305=(19) 5x5=25=19 in hexa
30 =(24) 6x6=36=24H
307=(31) 308=(40) 309=(51) b) 306H va gia tri la 24H.

c) 01010001 B la gia tr] nhi phan cua 51 H va 81 D (92=81).

Ngoai viec sir dung DPTR de truy cap khong gian bo nho ROM chirong trinh thl no con e6 th~ diroc dung de truy cap bo nho ngoai cua 8051, xem chirong 14.

MQt thanh ghi khac mra diroc Slr dung 0 che dQ dinh dia chi chi so la bQ dem chuong trmh, xem phu luc A.

Trong nhieu vi du neu tren day, cluing ta thuong dung lenh kha quen thuoc la lenh MOV, tuy nhien cling c6 the sir dung bat ky lenh nao khac neu van h6 tro cho che dQ dinh dia chi. Vi du, lenh "ADD A, @ " se cong noi dung ngan nho do RO tro 00 den vao noi dung cua thanh ghi A.

77

ChLtdng 6 LeNH SOHQC

6.1 CQNG vA TRl1 cAc so KHONG DAU

S6 khong dau duoc dinh nghia la dii lieu rna tat ca moi bit deu duoc dung dti bieu dien gia tri va khong co bit danh cho dau am hoac dirong. Nhu vay, doi voi dir lieu 8 bit, roan hang chi co the nam giira 00 va FFH (0 den 255 he thap phan),

CQng cac so khong dau

d 8051, de cong hai toan hang can sir dung thanh ghi A. CU phap lenh cong ADD co dang sau:

ADD A,nguon

;A = A + nguon

Lenh ADD diroc dung de cong hai toan hang. Toan hang dfch luon la thanh ghi A, con toan hang nguon co the la thanh ghi, dfr lieu tire thai hoac bo nho. Can nho ding, d6i voi hop ngfr thi phep toan s6 hoc gitra hai dfr lieu bo nho voi nhau la khong hop l¢. Lenh cong co the lam thay deli cac bit AF, CF hoac PF cua thanh ghi ca. Tac dong cua lenh ADD len ca tran se duoc tim hieu a muc 6.3. Chung ta xem xet VI du 6.1 diroi day:

Vi du ti.I

Hay lam r6 cac lenh sau anh huang len ca nao?

MOV A, #OF5H A=F5H

ADD A,#OBH ; A=F5+0B=00

Gia]:

1111 0000

0101 1011

F5H OBH 100H

+

. +

0000

0000

Sau phep cong, thanh ghi A chua gia tr] 00 va trang thai cac co se nhtr sau:

CY = 1 VI co phep nho tir 07.

PF = 1 VI s6 cac s6 1 la 0 (mot s6 chan) ca PF diroc thiet lap len 1. AC = 1 vi co nho tir bit 03 sang 04.

Lenh sf)' hoc

79

CQng cac byte

6 chuang 2 da trinh bay phep cong 5 byte dfr lieu, Gia tri tong diroc giii lai nho han FFH nrc nho han gia tri eire dai 8 bit. De tfnh tong cac toan hang thi ca nho phai diroc kiem tra sau moi Ian cong mot toan hang. Vi du sau dung R7 de tfch luy gia tri nho moi khi toan hang diroc cong VaG A.

Vi d1,l6.2

Gia sir cac ngan nho 40 - 44 cua RAM co gia tri a dang Hexa nhir sau: 40 = (70); 41 = (EB); 42 = (C5); 43 = (5B) va 44 = (30). Hay viet chuang trinh tfnh tong cac gia tri tren. Ket qua byte thap cat a thanh ghi A va byte cao cat a R7 (cac gia tri tren deu duoc cho a dang Hexa).

Glai:
MOV RO,#40H
MOV R2,#5
CLR A
MOV R7,A
AGAIN: ADD A, @RO
JNC NEXT
INC R7
NEXT: INC RO
DJNZ R2,AGAIN ;N.;J.p con tro
;N.;J.p bQ d$rn
;Xoa thanh ghi A
iXOa thanh ghi R7 iCQng byte con tro d~n theo RO iN~u CY=O kh6ng tich luy co nha iBarn theo s6 ian nha

;Tang con tro

iLap l.;J.i cho d~n khi RO=O

Phan ticn vi dl! 6.2

Chung ta se ban ky han ba Ian lap lai cua vong lap. Phan con lai danh cho ngiroi doc tt! kiem tra.

I. a Hin lap diiu tien, 70B diroc cong VaG A voi CY = 0 va R7 = 00 va bo dem R2 = 04.

2. a Hin lap lai thu hai, EBH duoc cong vao A va ket qua trong A la 68B can CY = 1. VI cis nho xuat hien, R7 diroc tang len. Luc nay bo dern R2 = 03.

3. a IAn lap lai thir ba, C5H diroc cong VaG A nen A = 2DB va Cel nho lai tiep tuc nho, do vay R7 lai diroc tang len va b¢ dem R2 = 02.

a phan cuoi. khi vong lap ket thuc, gia tri tong diroc liru o thanh ghi A va R7, trong do A chua byte thap va R7 chua byte cao.

80

Call true va l~p trinh hi! vi die'u khiln 8051

CQng c6 nhe. va cQng cac so 16 bit

Khi cong hai toan hang 16 bit thl phai 11m y den gia tri nho tir byte thap den byte cao. Trong nhirng twang hop do can dung lenh ADDC (cong co nho). Vi du, cong hai s6 sau: 3CE7H + 3B8DH.

3C E7

+ 3B 80

78 74

Khi cong byte th(r nhat (E7 + 8D = 74, CY = 1), co nho diroc truyen len byte cao tao ra ket qua 3C + 3B + 1 = 78. Diroi day la chuang trlnh thirc hien cac biroc neu tren.

Vi du 6.3

Hay viet chuang trinh cong hai s6 16 bit la 3CE7H va 3B8DH. Cat ket qua vao R7 (byte cao) va R6 (byte thap).

Giai:
CLR
MOV A,#OE7H
ADD A,#8DH
MOV R6,A
MOV A,#3CH
ADDC A,#3BG
MOV R7,A
SoeCD ;Xoa co CY=O

;N~p byte th&p VaG A ~ A=E7H

;CQng byte th&p VaG A ~ a=74H va CY=l ;Luu byte th&p cua t6ng VaG R6

;N~p byte cao VaG A ~ A=3CH

;CQng byte cao co nha VaG A ~ A=78H ;Luu byte cao cua t6ng VaG R7

56 BCD la s6 thap phan diroc rna hoa thea nhi phan (Binary Coded Decimal). Day la dang s6 rat quen thuoc boi VI trong thirc te chung ta thtrong sir dung cac s6 tir 0 den 9 clur khong phai s6 nhi phan hay s6 Hexa. Bieu dien nhi phan cua cac s6 tir 0 den 9 diroc goi la BCD, xem hlnh 6.1. Trong cac tai lieu v~ may tinh thirong co hai khai niern v~ cac s6 BCD la: BCD dang nen va khong nen.

s6 BCD dflng khong nen

56 BCD khong nen co 4 bit thap bi~u dien s6 BCD con 4 bit cao being O. Vi du "00001001" va "0000

s6 BCD
1 0000
2 0001
3 0010
4 0011
5 0100
6 0101
7 0111
8 1000
9 1001 Hmh 6.1. Ma BCD

Lfnh S(Y hoc

81

0101" Ia nhtrng s6 BCD khong nen cua s6 9 va s6 5. D~ bieu dien s6 BCD khong nen can dung mot byte bq nho hay mt)t thanh ghi 8 bit.

se scn min

D6i voi s6 BCD nen thi mot byte chua diroc 2 s6 BCD, mot s6 a 4 bit thap va s6 tlur 2 nam a 4 bit cao. Vf du "0 lO 1 1001" la s6 BCD nen cua s6 59H. Nhir vay chi can 1 byte bo nho la co th~ luu duoc hai toan hang BCD. Day Ia 19 do tai sao sil dung s6 BCD nen thl hieu qua hru dCi' lieu tang gap doi.

Co mot van d6 khi cong cac s6 BCD can diroc khac phuc, Do la sau khi cong

cac s6 BCD nen thi ket qua khong con la s6 BCD nira. Vf du:

MOV A, #l7H

ADD A, #2 8H

Cong hai s6 nay cho ket qua la 0011 1111B (3FH) khong con la s6 BCD. Mot s6 BCD chi nam trong giiH 0000 den 100 I (tir s6 a d~n s6 9), noi each khac, cong hai s6 BCD phiii cho ket qua la s6 BCD. Ket qua tren dang le phai la 17 + 28 = 45 (0 I 00 a 1 01). De giai quyet van de nay, I~p trlnh vien phai cong 6 (0 110) vao s6 thap 3F + 06 = 45H. Truong hop nrong nr cling co th~ xay ra a s6 cao, VI du, khi cong hai s6 52H + 87H = 094. Ttrong nr, de kh~c phuc van de nay can cong them 6 VaG s6 cao (D9H + 60H = 139). Ttnh trang nay r{\t hay xiy ra 'Va rnoi be.> xu ly deu co mot lenh chuyen de xu 19 van d~ nay. o 8051, do la lenh "DAA".

L,nh DA

Lenh dieu chinh thap phan cua phep cong DA (Decimal Adjust for Addition) a 8051 dung de hieu chinh sl! sai lech nhir trinh bay tren day khi cong cac s6 BCD. Lenh DA se cong 6 VaG 4 bit thap hoac 4 bit cao khi can. Con blnh thuong lenh d~ nguyen ket qua tim duoc, Hay xem vi du sau.

MOV MOV

A, #4 7H B,#25H

;A=47H la toan h~ng BCD dau tien iB=25H la toan h~ng BCD thu hai

ADD A,B ;CQng cae 56 hexa(nhi phan)A=6CH

DA A iDieu ch.i nh phep cQng BCD (A=72H)

Sau khi chuang trlnh dtroc thuc hien, thanh ghi A se chua 72h (47 + 25 = 72).

Lenh "DA" chi lam viec voi thanh ghi A. Cling can nhan manh rang, lenh DA se duoc su dung sau phep cong cac s6 BCD va cac s6 BCD khong the Ian han 9. Nhir vay, a s6 BCD cac s6 A - F la khong diroc phep. Con mot hru 9 nira la DA diroc dung sau phep cong ADD, rna khong bao gio dung sau Ienh tang INC.

82

Ctiu true va l{ip trinh hi! vi die'u khiin 8051

Tom tat ve' hOl1t d9n9 cua I~nh DA Sau lenh ADD hoac ADDC,

I. Neu 4 bit thap Ian han 9 hoac co AC = I thl lenh se cong 0 I 10 van 4 bit thap.

2. Neu 4 bit cao Ian han 9 hoac co CY = I thi lenh se cong 0 II 0 vao 4 bit cao.

Trong thirc te, co AC chi dung de phuc vu cho phep cong va hieu chinh cac so BCD. Vi du, cong 29H voi 18H cho ket qua 41 H la sai. De sua lai, lenh DA se cong 6 van 4 bit thap (vi AC = I).

Hexa BCD
29H 0010 1001
+ 18H + 0001 1000
41H 0100 0001 AC=l
+ 6 + 0110
47H 0100 0111
Vi d1;l6.4 Gia sir 5 dir lieu BCD diroc liru trong RAM tai dia chi bat dau tir 40H nhu sau: 40 = (71), 41 = (II), 42 = (65), 43 = (59) va 44 = (37). Hay viet chuang trinh tinh t6ng cua tat ca 5 so tren va ket qua ph iii la dang BCD.

Giai:

AGAIN:

MOV RO,#40H ;Nc;l.p can tro
MOV R2,#5 ;Nc;l.p bQ de'm
CLR A ;Xoa thanh ghi A
MOV R7,A ;Xoa thanhg ghi R7
ADD A,@RO ;CQng byte can tro chI b0i RO
DA A ;8ieu chInh ;, dc;l.ng BCD dung
ve
JNC NEXT ;Ne'u CY=O kh6ng tich luy co nha
JNC R7 ;Tang R7 bam thea '" lan nha
so
INC RO ;Dtch can tro len 6 nha ke' tie'p
DJNZ R2,AGAIN ;L~p lc;l.i cho de'n khi R2=0 NEXT:

Trli cac so khong dau eLi ph lip

SUBB A,nguon

;A=A-nguon-CY

a cac bo xu ly thong thirong co hai lenh thirc hien phep tinh trir, do la phep trir SUB va trir co rmron SUBB (Substract With Borrow). D6i voi 8051 thl chi co

r

!

Lenh so' hoc

mot lenh SUBB duy nhat. D~ co th~ thirc hien phep tfnh trir SUB can phan biet hai tnrong hop cua lenh SUBB la CY = 0 va CY = 1. btu y a day ta dung ea CY d~ rmron,

L~nh SUSS khi CY=O

De thirc hien phep trir, tat cac bo vi xir ly deu dung s6 bu 2. B¢ 8051 diroc thiet ke mot mach cong d~ thirc hien lenh trir, Gia sir 8051 Slr dung mach cong de thuc hien lenh trir va CY = 0 tnroc khi thirc hien lenh thi tom tcit cac biroc CPU tien hanh lenh SUBB voi tnrong hop cac s6 khong dau nlur sau:

I. Lay bu 2 cua s6 trir (toan hang nguon)

2. Cong s6 vira tim duoc voi s6 b] trir (A)

3. Dao co nho

Ket thuc ba biroc se eho ket qua cua phep trir va cac ea cling duoc thiet lap.

Chung ta xem vi du minh hoa sau:

Vi du ti.S

Lam r5 cac buoc thirc hien trong chuang trmh sau:

CLR C ; Tc\O Cy=Q
MOV A,lf3FH ;Ni?-p 3FH VaG A (A=3FH)
MOV R3,#23H ;Nap 23H VaG R3 (R3=23H)
;Trlr cho R3 d~t k~t ? VaG A
SUBB A,R3 A qua Giai:
A= 3F 0011 1111 0011 1111
R3= 23 0010 0011 + 1101 1101 bu2 (bU0C 1 )
1C 1 0001 1100 - 1C (bU0C 2) o CF=O (bU0C 3)

Cac ea se diroc thiet lap nhir sau: CY = 0, AC = 0 va lap trinh vien phai bier diroc trang thai cac ea nho de xac dinh ket qua Hi am hay duong.

Sau khi thirc hien lenh SUBB, neu CY = 0 thl ket qua la s6 duong, con neu CY = 1 thl ket qua Hi s6 am va dieh co gia tri bu 2 cua ket qua. Ket qua thirong diroc d~ a dang bu 2, song cling co the sir dung lenh lay bu CPL va tang INC d~ chuyen ket qua ve dang thong thuong, Lenh CPL tien hanh lay bu I, sau do ket qua diroc tang len 1 (INC) va trc thanh dang bu 2. Xem vi du 6.6.

83

84

Cau true va lap trinh ho vi die'u khiln 8051

Vi du 6.6

Phan tlch chirong trlnh sau:

CLR C
MOV A,#4CH
SUBB A,#6EH
JNC NEXT
CPL A
INC A
NEXT: MOV R1,A
Glai: ;N9P A gia trt 4CH(A=4CH) ;Trl1 A eho 6EH

;N§u CY=O nhay d§n dieh NEXT ;N§u CY=l th~e hi$n bu 1 ;Tang 1 d~ e6 bu 2

;Ll1u A vao R1

0100 1100

Cac buoc thuc hien lenh "SUBB A, 6EH" nhir sau:

0100 1100

4C

- 6E 0110 1110 ~ la:y bu 2 1001 0010 (biroc 1)

- 22 0 1101 1110 (bircc 2)

Co CY=l,k§t qua la s6 am 6 d9ng bu 2.

L~nh SUSS khi CY = 1

Lenh nay diroc dung khi trir cac so nhieu byte va co muon. Neu CY = 1 tnroc khi thuc hien lenh SUBB thi ket qua cling se diroc trir di I. Xem vi du sau day.

Phan tich chuang trlnh sau:

CLR C ;Cy=o

MOV A, #62 ;A=62H

SUB BA,#96H ;62H-96H=CCH with CY=l
MOV R7,A ;Save the result
MOV A,#27H ;A=27H
SUB BA,#12H ;27H-12H-1=14H
MOV R6,A ;Save the result Giai:

Sau khi SUBB, A = 62H - 96H = CCH va co nho diroc lap bao fling co rmron, VI CY = 1 nen khi SUBB diroc thirc hien Hin thu 2 thi a = 27H - 12H - 1= 14H. Do vay, ta co ket qua 2762H - 1296H = 14CCH.

Lenh sohoc

85

6.2 NHAN vA CHIA cAc s6 KHONG DAU

Khi nhan va chia hai s6 b 8051 can phai sir dung hai thanh ghi A va B VI cac lenh nhan va chia chi lam viec voi nhfrng thanh ghi nay.

Nhan hai so khong diu

B9 vi dieu khien chi h6 tro phep nhan byte voi byte. Cac byte diroc gia thiet . la du lieu khong dau, Cau tnic lenh nhu sau:

MUL AB iAxB va k€t qua 16 bit duqc d~t trong A va B Khi nhan byte voi byte thi mot toan hang b thanh ghi A con toan hang kia b thanh ghi B. Ket qua cua phep nhan duoc hru b A va B, trong d6 byte thap b A, con byte cao b B. Vi du duai day trlnh bay phep nhan 25H vai 65H. Ket qua 16 bit duoc d~t b A va B.

MOV MOV MUL

A,#25H B,65H AB

iN~p vao A gia trt 25H

;N~p vao B gia trt 65H i25H*65H=E99 v&i B=OEH va A=99H

Bang 6.1. Tom tat phep nhan hai s6 kh6ng dau (MULAB)

Nhan Toan h~ng 1 Toan h~ng 2 Ket qua
8yte*8yte A 8 A = byte thap, 8 = byte cao Chia hai so khong diu

8051 cling chi h6 tro phep chia hai s6 khong dau byte cho byte. Cii phap nhu

sau:

DIV AB iChia A cho B

Khi chia mot byte cho mot byte thl tir s6 (s6 bi chia) phai b trong thanh ghi A con rnau s6 (s6 chia) phai b trong thanh ghi B. Ket qua cua phep chi a DIV diroc d~t trong A, con s6 dtr diroc d~t trong B. Xetvf du dum day:

MOV

A,#95

iN~p so bt chia vao A=95

MOV B, #10 iN~p so chia va o B=10

DIV AB iA=09(thudng SO)iB=05(sc) dir)

D6i voi lenh "DIV AB" can hru y cac di~m sau:

1. Lenh nay luon xoa cac co CY = 0 va OV = 0 neu mAu s6 khac O.

2. Neu miiu s6 bang 0 (B = 0) thi OV =1 bao 16i va CY = o.

86

Cdu true va UJ,p trinb hi! vi die'u khiln 8051

ThI,lC te, b tat ca moi bo vi xir ly khi chia mot s6 cho 0 thl b¢ vi xir ly se bao ket qua khong xac dinh. D6i voi 8051 do la co OV diroc thiet lap len 1.

Bang 6.2. Tom tat phep chia kh6ng dau (DIV AB)

Phep chia Tiiso Mau so Thlldng so So dtr
Byte cho Byte A B A B VI dl! Ling dl!ng I~nh chia

Trong thuc te, chung ta thirong gap mot he do, trong do bo chuyen d6i tin hieu urong ur sang tfn hieu s6 AOC diroc noi toi mot cong cua 8051. Dau van cua AOC Iii gia tri nhiet d¢ hay ap suat, con dau ra la dir lieu 8 bit b dang Hexa trong dai 00 - FFH. Oft lieu Hexa nay can diroc chuyen d6i v~ dang thap phan, Dti thirc hien chuyen d6i, dit lieu diroc chia lap nhieu Ian cho 10 va luu lai s6 dir, nlur trlnh bay b VI du sau.

b) D~ chuyen d6i s6 nhi ph an hay Hexa ve s6 thap phan ta thirc hien chi a l~p cho 10 lien tuc cho den khi thirong s6 nho han to. Sau m6i Ian chia, can hru lai s6 du, Truong hop s6 nhi phan 8 bit FOH (2530) trmh W tien hanh nhu sau:

Vi d1;l6.8

a) Viet chuang trinh d~ nhan dft lieu dang Hexa trong pham vi 00 - FFH tir cong 1 va chuyen d6i v~ dang thap phan, Liru cac s6 van cac thanh ghi R 7, R6 va R5 trong do s6 co nghia nho nhat Iii R7.

b) Phan tich chuang trinh voi gia thiet PI co gia tri dft lieu FOH.

Giai:
a)
MOV A,#OFFH
MOV P1,A iTc;:to P1 la :1 dau vao
cong
MOV A,P1 if)QC dii li$u tv P1
MOV B,#10 iB=OA Hexa(O th~p phan)
DIV AB iChia cho 10
MOV R7,B icgt A' thgp
so
MOV B, #10
DIV AB iChia 10 lan niia
MOV R6, B iCgt '" ti§p theo
so
MOV R5,A iCgt '" ",. cung
so CUOl Lenh s()' hoc 87

Thl1ang -' sO' dl1
so
FD/OA 19 3 s6 tha:p
19/0A 2 5 s6 giua
2 s6 cao Do vay, ta co FDH = 253D. D~ hien thi dG' lieu nay thl can phai chuyen v~ rna ASCII va se diroc de cap den (j chuang sau.

6.3 s6 CO DAU vA cAc PHEP TiNH so HQC

Tat ca nhirng dfr lieu diroc xet cho den bay gio deu Hi s6 khong dau, co nghia la roan bo 8 bit deu diroc dung bieu dien d¢ Ian cua dfr lieu. Co nhieu irng dung

yeu diu dfr lieu co dau. o phan nay chung ta se tim hieu cac lenh lien quan den cac s6 co dau.

Khai ni~m so c6 dau trong may tinh

Cac s6 diroc dung trong thirc te co the la am hoac dirong. Vi du 5 d¢ diroi OoC duoc bieu dien la -5°C va 20 d¢ tren OoC diroc bieu dien la +20oC. May tfnh cling can dap irng diroc yeu cau nay. Cach bieu dien s6 am va dtrong co dau trong may tinh nhu sau: Bit cao nhat MSB duoc dung cho bit dau (+) hoac (-). cac bit con lai duoc dung bieu dien d¢ 160. Bit dau co gia tri 0 d6i voi cac s6 dirong va 1 d6i voi cac s6 am. Mot byte co dau diroc bieu dien nhir tren hlnh 6.2.

Toan hllng 8 bit co dau

o toan hang dang byte co dau thi bit cao nhat MSB la D7 diroc dung M bieu dien dau, 7 bit con lai tir D6 - DO

dung de bieu dien d¢ Ian

ella s6 do. Neu D7 = 0 thi Hmh 6.2. Toan hang 8 bit co daD

do la toan hang dirong va neu D7 = 1 thl do la toan hang am.

So dutmg co dau

Dai gia tri cua so duong duoc bieu dien nhir tren hlnh 6.2 la tir 0 den + 127.

Neu muon bieu dien s6 160 hon thi can sir dung toan hang 16 bit. VI 8051 khong hCi tro dfr lieu 16 bit nen ta khong ban den a day.

I D7 I D6 I D5 I D4 I D3 I D2 D 1 I DO I

Dau

Gia tri

88

Cdu true va ltJ,p trinh ho vi dUu khie"n 8051

Scj' am co dau

D6i voi s6 am thl D7 = 1, eon do Ian diroc bieu dien (j dang so bu 2. Nhiem vu chuyen deli sang s6 bu 2 do trlnh hop dich dam nhiem, song d6i voi chung ta dieu quan trong Iii phai hieu diroc viec chuyen deli d6 dien ra nhir the nao. De chuyen deli ve dang s6 am (s6 bu 2), cac biroc tien hanh nhir sau:

1. Viet d¢ Ian cua s6 (j dang nhi phan 8 bit (khong dau).

2. Dao ngiroc tat ca cac bit.

3. Cong s6 vira xac dinh vci l.

Vi d1,l6.9

Trinh bay each 8051 bieu dien so - 5. Giai:

Cac biroc thuc hien nhir sau:

1. 0000 0101 Bi~u di@;n 86 5 a da nq 8 bit nh i. phan

2. 1111 1010 Bao c ac bit

3. 1111 1011 CQng 1 (t hanh 86 FB a d a nq Hexa)

Do vay, s6 FBH Iii bieu dien s6 e6 dau dang bu 2 cua s6 - 5.

Vi d1,l6.10

Trinh bay each 8051 bieu dien s6 - 34H. Giai:

Hay quan sat cac biroc sau:

1. 0011 0200 se 34 dircc cho a da nq nh i, pha n

2. 1100 1011 Bao ca c bit

3. 1100 1100 CQng 1 (t.hanh 86 CC a d ariq Hexa)

V~y s6 CCH la bieu dien dang bu 2 e6 dau cua s6 - 34H.

Trinh bay each 8051 bieu dien s6 - 128:

Gia]:

Quan sat cac biroc sau:

1. 1000 0000 s6 128 a dc;l.ng nhi phan 8 bit

2. 0111 1111 Bao bit

3. 1000 0000 CQng 1 (tra t.hanh 86 80 da nq Hexa)

V~y - 128 = 80H Iii bieu dien s6 e6 dau dang bu 2 cua - 128.

L¢nh .1'(/ hoc

89

Tir cac vi du tren day c6 th~ & dang thay rang, dai cua cac so am c6 dau 8 bit Hl tir - 1 den - 128. Diroi day la liet ke cac so c6 dau 8 bit:

s6 th~p phan ss nb i: phan s6 Hexa
-128 1000 0000 80
-127 1000 0001 81
-126 1000 0010 82
-2 1111 1110 FE
-1 1111 1111 FF
0 0000 0000 00
+1 0000 0001 01
+2 0000 0010 02
-127 0111 1111 FE
Trim d cac phep toan so co dau Khi si! dung cac so c6 dau xuat hien mot van d~ bU9C phai xi! Iy, do I~l tran.

Khi tran, 8051 chi bao co l6i bang each thiet lap co tran OV, nhirng d~ tranh nham lan, lap trinh vien can hieu r6 han v~ truong hop nay. V~y tran la gl? Neu ket qua cua mot phep toan tren cac so co dau rna Ian qua kich thtroc thanh ghi thl xuat hien tran va lap trinh vien can duoc canh bao. Xet VI du 6.12 duoi day.

~------------------------------------------------------------

Khao sat doan chuang trlnh sau va phan tfch ket qua.

MOV A,#+96 ;A=0110 0000(A=60H)

Vi du 6.12

MOV R1,#+70 ;R1=0100 0110 (R1=46H)
ADD A, Rl ;A=lOlO OllO=A6H=-90D Sai I I I
Gial:
+96 0110 0000
+ +70 0100 0110
+ 166 1010 0110 va OV=l CPU cho ket qua la -90 va do la ket qua sai. CPU bat CO OV = 1 d~ bao tran so.

6 VI du 6.12 thi +96 duoc cong voi +70 va ket qua CPU tfnh diroc la -90. Tai sao nlur v~y? Ly do la ket qua cua (+96) + (+70) = 172 virot qua kfch thiroc thanh

90

Cdu true va lap trinh ho vi die'u khien 8051

ghi A. Thanh ghi A co 8 bit va chi chua diroc so lon nhat la + 127, VI the, ca tran OV diroc thier ke nharn muc dfch bao cho lap trinh vien biet ket qua cua phep toan so co dau vira tinh la sai,

Thiet I~p cd trim OV

6 cac phep toan voi so co dau 8 bit thl ca OV diroc bat len I khi xuat hien mot trong hai dieu kien sau:

I, Co nho tir D6 sang D7 nhung khong co nho ra tir D7 (co CY = 0).

2. Co nho tir 07 (co CY = 1) nhung khong co nho tir 06 sang 07.

Noi each khac, co tran OV duoc b~t len 1 neu co nho ttr 06 sang 07 hoac ttr 07 nhtrng khong dong thai xay ra ca hai. Nhtr vay, neu co nho ca tir 06 sang 07

va tir 07 ra thl ca OV = 0.6 vi du 6.12 VI chi co nho tir 07 ra nen ca ov = I. Vi du 6.13, vi du 6.14 va 6.15 minh hoa them ve sir dung ca tran trong cac phep tinh so hoc vci so co dau,

Vi du 6.13

Hay quan sat doan chirong trinh sau va d~ y den vai tro cua ca av.

MOV A, #-128 ; A=1000 0000 (A=80H)

MOV R4,#-2 ;R4=1111 (R4=FEH)
ADD A, R4 ;A=0111 1110(A=7EH=+126,invalid)
Giai:
-128 1000 0000
+ - 2 1111 1110 -l30 0111 1110 va OV = 1

CPU cho ker qua + 126 la sai (co av = I).

Vi du 6.14

Hay quan sat doan chuong trlnh sau va hru y ca av.

MOV A,#-2 ;A=1111 1110 (A=FEH)

MOV R1, #-5 ;R1=1111 1011 (R1=FBH)

ADD Giai:

A,R1

;A=llll 1001 (A=F9H=-7,orrect,OV=0)

-2 1111 1110

+ -5 1111 1011

-7 1111 1001 va OV=O

CPU cho ket qua - 7 la dung (co OV = 0).

91

Xac dinh vai tro cua ca OV qua doan chuang trlnh sau.

0001

1001 v a Ov=o

Vi du 6.15

MOV MOV ADD

A, #+7 F.1,#+18 A,R1

;A= 0000 0111 (A=07H) ;R1=0001 0010 (R1=12H)

;A=l111 1001 (A=19H=-25,dung,OV=0)

Giai:

CPU cho ket qua - 25 la dung (co OV = 0).

7 + 18 25

0000 0111

0001 0010

Tir cac VI du tren day cluing ta co th~ ket luan rang, trong moi phep cong s6 c6 dau, ca OV luon luon bao ket qua la dung hoac sai. Neu ca OV = 1 thi ket qua lit sai, con neu OV = 0 thl ket qua la dung. Cling can nhan manh lai rang, a phep cong cac s6 khong dau, l~p trinh vien din thea doi trang thai ca nho CY, con a phep cong cac s6 co dau thl do la ca tran OV. D6i voi 8051, cac lenh nhtr INC va JC cho phep chuang trinh re nhanh ngay sau phep cong cac s6 khong dau, nhir trinh bay a muc 6.1, song a tnrong hop ca tran OV thi khong nhir vay. Tuy nhien, van co the thirc hicn diroc dieu nay bang lenh "IB PSW.2" hoac "INB PSW.2" VI PSW la thanh ghi co co th~ dinh dia chi bit.

Lenh thirc hien phep AND logic tren hai toan hang dich va nguon va d~t ket qua vao dich. Dich thirong la thanh ghi tfch luy.

Toan hang nguon c6 the la thanh ghi, bo nho hoac gia tri cho san. Xem phu luc A 1 d~ biet them ve cac che d¢ dinh dia chi cua lenh nay. ANL la Ienh voi toan hang dang byte va khong c6 tac dong len cac ca. Lenh nay thirong diroc dung d~ che (xoa ve 0) mot so bit cua toan hang. Xem VI du 7.1.

ChU'dng 7 LeNH LOGIC

7.1 L~NH LOGIC vA so SANH L~nh AND (VA)

Cuphap

ANL dich,nguon

;dich=dich AND nguon

x

Y

XANDY

o

o

o

o

1

1

o

1

1

o

o

1

Vi du 7.1

Xac dinh ket qua cua cac lenh sau:

MOV A, #35H ; Ca n A=35H

ANL A,#OFH ;Th~c hien AND logic A vai OFH(A=05)

Giai:

35H AND OFH=05H

35HO OFHO 05HO

o 1 1 0 1 0 1 000 1 111 000 0 101

L~nh OR (Ho~c) Cuphap

ORL diCh,nguon ;Dich=dich OR nguon Lenh thirc hien OR logic toan hang dlch v(ji

toan hang nguon, ket qua cat vao dich, Phep OR c6 th~ diroc dung d~ thiet lap nhirng bit nhat dinh cua mot toan hang. Toan hang dich thirong lei thanh ghi

x

Y

X ORY

o

o

o

o

1

1

1

o

1

1

1

1

Lenh logic

93

tich luy, roan hang nguon co th~ la thanh ghi, bo nho hoac gia tri cho san. Tham khao phu luc A d6 bier them ve che do dinh dia chi cua lenh nay. ORL la lenh dung cho cac toan hang dang byte va se khong co tac dong den bat ky co nao. Xem VI du sau:

MOV MOV

Giai:

A,#04 A,#68H

;A=04 ;A=6C

Vi du 7.2

Xac dinh ket qua cua doan chirong trinh sau:

04HOOOO 0100

68H0110 1000

6CH0110

1100

04 OR 68=6CH

L~nh XOR (OR loai trCt) eli phap

XRL dich,nguon ;dich=dich OR lo~i tru nguon

Lenh thirc hien phep XOR tren hai toan hang va d~t ket qua vao dich. Dfch thirong la thanh ghi rich IUY. Toan hang nguon co th~ la thanh ghi, bo nho hoac gia tri cho san. Xem phu luc A.I d~ bier them ve che d¢ dinh dia chi cua lenh nay. XRL la lenh dung voi toan hang dang byte va se khong co tac dong den bat ky cis nao. Xet VI du

x

Y

X XORY

o

o

o

o

1

1

1

o

1

cac VI du sau.

1

1

o

XRL A,#78H
Giai:
54HO 1 0 1 0 1 0 0
78HO 1 1 1 1 0 0 0
2CHO 0 1 0 1 1 0 1 54H XOR 78H=2CH

Vi d1;l7.3

Trinh bay ket qua cua doan chuang trinh sau:

MOV A, #54H

94

Cdu true va lap trinh ho vi die'u khien 8051

Vi du 7.4

Lenh XRL co th~ dung M xoa noi dung cua mot thanh ghi bang each XOR vci chinh no, Lam r6 lenh "XRL A, A" xoa noi dung cua thanh ghi A nhu the n11O? gia thiet AH = 45H.

Gia]:

45H 01000101

01000101 00000000

45H 00

54H XOR 78H=2CH

Lenh XRL cling co th~ diroc dung d~ kiern tra hai thanh ghi co gia tri giong nhau hay khong? Lenh "XRL A, R I" thuc hien OR loai trir thanh ghi A vci thanh ghi R I va d;rtt ket qua van A. Neu ca hai thanh ghi co cung gia tri thl cho ket qua o A la O. Sau do co th~ dung lenh nhay JZ d~ re nhanh thirc hien chuang trlnh dira vao ket qua vira tinh. Xem vi du sau.

Vi du 7.5

D9C va kiern tra cong PI xem co chua gia tri 45H khong? Neu co thi gjri 99H den cong P2, neu khong co thoat khoi doan chuang trinh.

Giai:
MOV P2,#00 ;Xoa P2
MOV P1,#OFFH ; U1Y P1 L€J. c6ng dau VaG
MOV R3,#45H ;R3=45H
MOV A,P1 ;f)Qc P1
XRL A,R3
JNZ EXIT ;Nhay " A co gia tri khac 0
neu
MOV P2,#99H
EXIT: Mot irng dung thuong gap khac cua lenh XRL la dao mot bit nao do. Vi du, dao bit 2 va giG' nguyen cac bit con lai cua thanh ghi A, cluing ta co th~ sir dung lenh sau.

XRLA, #04H ; A XOR 0000 0100 L~nh CPL A (lay bu thanh ghi tich luy)

Lenh thirc hien lay bu noi dung cua thanh ghi tich luy A. Phep bu la phep dao cac bit 0 thanh I va 1 thanh O. Phep dao nay con diroc goi la phep bu I.

L¢nh logic

95

MOV A, #SSH

CPL A ;Bay gio A=AAH

;Vi dao 0101 0101(SSH)d~Qc 1010 1010(AAH)

D~ HlY bu 2, dan gian la cong them I vao ket qua bu I. 6 80S 1 khong co lenh bu 2. Liru y rang khi lay bu mot byte thi dir lieu phai a trong thanh ghi A. Lenh CPL khong h6 tro che d¢ dinh dia chi nao. Xem vi du sau.

ADD A,#l

;CQng 1 vao A d~Qc bu 2 ;A=Olll 1011(7BH)

Vi du 7.6

TIm gia tri bu 2 cua 8SH. Giai:

MOV A, #8SH MOV A

;N~p 8SH vao A(8SH=1000 0101)

;Lay bu 1 cua A(ket qua=Olll 1010)

L~nh so sanh

80S 1 co mot lenh cho phep so sanh hai toan hang voi nhau. Cu phap cua lenh nhu sau:

CJNE Dich,ngu6n,d~a chi tuong d6i

d 80S I lenh so sanh diroc ket hop voi lenh nhay thanh mot lenh do la CJNE (Compare and Jump if Not Equal). Lenh CJNE so sanh hai roan hang ngu6n va dfch va nhay den dia chi nrong d6i neu hai toan hang khong bang nhau. Ngoai ra, lenh con thay dbi co nho CY d~ bao neu roan hang dich Ian han hay nho han. Va dieu quan trong la cac co khac van gifr nguyen khong thay d6i. Vi du, sau khi thuc hien lenh "CJNE A, #67H, NEXT" thi thanh ghi A van co gia tri ban dllu (nrc gia tri tnroc lenh CJNE). Lenh nay so sanh noi dung thanh ghi A voi gia tri 67H va nhay den dia chi dfch NEXT neu thanh ghi A co gia tri khac 67H.

Vi du 7.7

Xet doan chuang trlnh diroi day va xac dinh:

a) Chuang trlnh co nhay den NEXT khong?

b) A co gia tri bao nhieu sau lenh CJNE?

96

Cdu true va lap trinh ho vi die'u khiin 8051

MOV CJNE

A,#55H A,#99H,NEXT

NEXT:

Giai:

a) C6, VI 55H va 99H khong bang nhau.

b) A = 55H la gia tri tnroc khi thirc hien ONE.

o lenh CJNE thi toan hang dich c6 th~ la thanh ghi tich luy hoac trong mot cac thanh ghi Rn. Toan hang nguon c6 th~ la thanh ghi, bo nho hoac gia tri cho san. Hay xem phu luc A de biet

them chi tiet v~ cac che d¢ dinh dia chi cua lenh. Lenh nay chi tac dong tci co nho CY va gia tri thay d6i duoc gioi thieu a bang 7.1. Diroi day la vi du minh hoa sir dung lenh

Bang 7.1. Thiel Il~p CO' CY iJ I~nh CJNE

So sanh ce nhd CY

£)fch > Nquon CY = 0

£)fch < Nquon CY = 1

so sanh.

CJNE R5,#80,NOT-EQUAL ;Ki~m tra R5=80? ;Ngu R5=80

NOT-EQUAL: JNC NEXT

NEXT:

Cung nen luu y la co nho CY luon diroc kiem tra song chi sau khi da xac dinh diroc cac toan hang khong bang nhau. Xem cac vi du sau.

Vi du 7.8

Hay viet doan chuang trinh xac dinh xem thanh ghi A c6 chua gia tri 99H khong? Neu c6 thi nap R 1 = FFH con neu khong thl R I = O.

Giai:

MOV Rl, #0

;Xoa Rl

CJNE A,#99H ;Ngu A khong bang 99H thi nhay dgn NEXT MOV Rl,#OFFH ;Ngu bang nhau thi gan Rl=OFFH

NEXT:

OVER:

;Ngu khong bang nhau thi gan Rl=O

Lenh logic

Vi d ... 7.9

Gia sir c6 mot cam bien nhiet diroc n6i tm cdng ddu vao Pl. Hay viet chirong trlnh doc nhiet d¢ va so sanh voi gia tri 75. Dira vao ket qua kiem tra de d~t gia tri nhiet d¢ vao cac thanh ghi nhu sau:

Neu T = 75 thl A = 75

NeuT<75 thl RI = T
NeuT>75 thl R2 =T
Giai:
MOV Pl,OFFH
MOV A,Pl
CJNE A,#75,OVER
SJMP EXIT
OVER: JNCNEXT
MOV Rl,A
SJMP EXIT
NEXT: MOV R2,A
EXIT: ;T~o Pl lam c6ng dau vao ;BQc c6ng Pl,nhi~t dQ ;Nhay d~n OVER n~u A*75 ;A=75 thoat

;N~u CY=O thi A>75 nhay d~n ;N~u CY=lthi A<75 luu vao Rl ;va thoat

;A>75 luu no vao R2

Lenh so sanh ve ban chat la mot phep tfnh trir song khong lam thay d6i gia tri cua cac toan hang. Cac co diroc thay d6i tuy thuoc vao ket qua thirc hien lenh tri'r SUBB. Cling can nhac lai rang, ket qua so sanh b lenh CJNE khong c6 tac dong len cac toan hang rna chi tac dong len co CY.

Vi d ... 7.10

Viet chuang trlnh lien tuc kiem tra c6ng PI khi c6 gia tri khac vei 63H.

Neu PI = 63H thl khong kiem tra PI nil'a.

Giai:
MOV Pl,#OFFH ;ChQn Pl lam c6ng dau
HERE: MOV A,Pl ;L&.y nQi dung ? Pl
cua
CJNE A,#63,HERE ;Duy tri ki~m tra tru vao

khi P=63H

Vi d ... 7.11

Gia sir cac ngan nho cua RAM b dia chi 40H - 44H chua nhiet d¢ cua 5 ngay nhu duqc chi ra duai day. Hay tim xem c6 gia tri nao bAng 65 khOng?

97

98

Cdu true va l(ip trinh ho vi die'u khien 8051

Neu co gia tri 65 hay dat dia chi ngan nho vao R4, neu khong tht dat R4 = 0, 40H=(76); 41H=(79); 42H=(69); 43H=(65); 44H=(64)

Ghii:

BACK:

MOV R4,#0 ;Xoa R4 = 0
MOV RO,#40H ;Nap con tro
MOV R2,#05 ;Ni;l.p bQ de'm
MOV A,#65 ;Gan gia tr.t _, tim VaG A
can
CJNE A,@RO,NEXT ;So sanh du 1i$u RAM vai 65
MOV R4,RO ;Ne'u la 65,111U d.ta chI VaG R4
SJMP EXIT ;Thoat
INC RO ;Ne'u kh6ng tang bQ de'm
DJNZ R2,BACK ;Ki~m tra cho de'n khi bQ de'm 0 NEXT:

EXIT:

.... ", ",,?

7,2 LI~NH QUAY VAO TRAO £)01

Trong rat nhieu irng dung can phai thuc hien phep quay bit cua mot toan

. hang, a 8051 co cac lenh chuyen cho muc dich nay la R 1, RR, RLC va RRC Chung cho phep quay cac bit cua thanh ghi tich iuy sang trai hoac phai. D6i voi 8051, d~ quay mot byte thl toan hang ph iii a trong thanh ghi A. Co hai kieu quay la: quay don gian cac bit cua thanh ghi A va quay co nho,

Quay cac bit cua thanh ghi A sang trai hoac phai

Quayphai

RR A ;Quay cac bit thanh ghi A sang phai a phep quay phai, 8 bit cua thanh ghi

tich iuy diroc quay sang phai 1 bit va bit DO I" ~ I roi tir V! trf bit thap nhat va chuyen sang bit ~ MSB ~ LSB ~

cao nhat D7. Xem doan chuong trinh duoi

L- ~

day:

MOV A,#36H ;A=OOl1 0110
RR A ;A=OOOl 1011
RR A ;A=1000 1101
RR A ;A=1100 0110
RR A ;A=0110 0011 Lfnh logic

99

Quay trai

RL A ;Quay trai cae bit cua thanh ghi A

d phep quay trai, 8 bit ella thanh ghi A

duqc quay sang trai 1 bit va bit 07 rai khoi vi tri bit cao nhat chuyen sang vi trf bit thap nhat DO. Xem doan rna clurong trinh sau:

MOV A, #72H ;A=Olll 0010

RL A ; A = 111 0 0100

RL

;A=1100 1001

YL-_M_S_B_+- __ L_SB___.J~

A

Liru <; ding cac lenh RR va RL khong tac dong len ea nao.

Quay co nhe,

8051 con co 2 lenh quay nira la quay phai co nho va quay trai co nho. RRCA va RLC A

Quay phsi co nho

RRC A

Khi quay phai co nho thi cac bit ella thanh ghi A dtroc quay tit trai sang phai 1 bit, bit thap nhat diroc dtra vao CO nho CY va sau do ea CY diroc dtra vao vi trf bit cao nhat. Noi each khac, b lenh "RRC An

thi LSB diroc chuyen vao CY va CY diroc chuyen vao MSB. Thirc te, ca nho CY lam viec nhir la mot bit ella thanh ghi A, do vay thanh ghi A dirong nhir co 9 bit.

~L-_M_SB_~ __ LS_B__.J~ CY J

;make Cy=o

;A=0010 0110 ;A=OOOl 0011 CY=O ;A=OOOO 1001 CY=l ;A=1000 0100 CY=l

CLR C
MOV A#26H
RRC A
RRC A
RCC A
Quay trai co nheY
RLC A Khi thtrc hien lenh RLC A, cac bit diroc dich phai mot bit va dAy bit MSB vao ea nho CY, sau do ea CY diroc

L cv +1L-_M_SB_+- __ LS_B___i~

100

Cd" true .. a ~p trinh hfJ .. i dii'u khiin 8051

chuyen vao bit LSB. Noi each khac, a lenh RLC thl bit MSB diroc chuyen vao CY va CY dircc chuyen vao LSB. Hay xem doan chtrong trinh sau.

SETB C iB~t CY=l

MOV A#15H iA=OOOl 0101

RRC RRC RCC RCC

iA=0101 1011 CY=O iA=0101 0110 CY=O iA=1010 1100 CY=O iA=1000 1000 CY=l

A A A A

L,nh trao d6i thanh ghi A: SWAP A

M¢t lenh hil'u Ich khac nita la lenh trao dOi SW AP. Lenh nay chi lam viec a thanh ghi A thea each: mra phan eao ella byte va mra phan thap ella byte dircc trao dOi cho nhau. Nhir vay, 4 bit eao duoc chuyen thanh 4 bit thap va 4 bit thap tra thanh 4 bit eao.

Tnr6'c I 07 - 04 II 03 - DO I Sau I 03 - 00 II 03 - DO I

Truce

0111 II 0010

Sau

0010 II 0111

Vi du 7.12

a) Hay tim noi dung ella thanh ghi A a doan chirong trlnh sau.

b) Truong hop khong co lenh SWAP thl can phai lam the' nao de trao dOi nhtrng bit nay? Hay vi~t doan chuang trmh thirc hien qua trmh do.

Ghii:
a)
MOV A,#72H iA=72H
SWAP A iA=27H
b)
MOV A,#72H i A=Ol11 0010
RL A ; A=1110 0100
RL A i A=l1 00 1001
RL A iA=0010 0111

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)//-->