Thuc Hanh 89C51

Embed Size (px)

DESCRIPTION

code 89c51

Text of Thuc Hanh 89C51

ng dung ic lp trinh AT89C51: lam mach en quang bao dung led ma trn 8x8

T lam bang en Ledchay ch trnma trn 8x24 chi dung mt ic AT89C51Dn nhpDung ic lp trinh AT89C51, Ban co th kim soat c trang thai tt sang cua cac day Led trn cac bang en, t o co th tao ra c cac hinh anh hay cac dong con ch cho chay trn cac bang en nay, cng vic nay hin rt c nhiu Ban a thich, trong bai vit nay, ti trinh bay mt bai lam thc hanhkha n gian nhng rt cn c Ban hiu c nguyn lylam vic cua cac bang en quang bao. T cac hiu bit nay,Ban mi co th se m rngra nhiu kiu bang enphc tap hn. Mong Ban tmtim hiu.

Bai thc hanh nay se cho hin hinh va cac dong ch chay, nhp nhay, trn mt bang en ma trn 8x8. Vi ic mt lp trinh co 4 cang p0, p1, p2, p3, chung ta se dung 3 b ma trn 8x8 va nh vy se khng cn thm mt ic logic nao na. Mach rt n gian, nhng co c s ly thuyt rt c ban, se giup Ban hiu rt ro v cac loai en quang bao ln khac.

Phn tich s mach in:

Trt ht Ban tao bang endung 3 b 8x8. Cac hang, trn o la cac chn Anode cua cac Ledcho ni chung va dung mt transistor pnp dung bit0 xut trn cac chn cua cang p1 cp cho chn B cua transistor cho nidy chung ny ln ng ngun 5V. Do mach dung ky thut quet nn mi lnchi co mt hang cho ni vao ng ngun 5V.

Do cac chn cua Cathode cua Led cho ni chung trn mt ct, chung ta dung cac transistor npn cho ct thng masse ly dong cp cho cac Led, vy chn Cathode thng masse, chung ta se dung bit 1, cho xut trn cac chn cua cng p2, p0 va p3 cp cho chn B cua cac transistor. Vi mt cng co 8 bit, vy mi b ma 8 bit cho xut ra trn cac cng p2, p0, p3, chung ta co th iu khin mi nhip c 8 Led.

Tim hiu bang ma trn Led 8x8:

Hinh ve cho thy cach gn cac Led trn ma trn tao bi 8 hang va 8 ct.

S dung giai thich nguyn ly tt m cac Led trn bang ma trn 8x8.

Cach 1: Ma quet xut trn p2, p3, p0, ma hinh hay ch xut trn cang p1.S mach in trn, mt ln na, cho thy, chung ta cho ni 8 chn cua cac chn B cua 8 transistor 2SA1015 va cang p1. Vy khi co mt chn cua cang p1 xut ra bit 0, no se lam transistor bao hoa, va luc nay dy anode chung se c cho ni vao ng ngun 5V. y, Ban se vit oan chng trinh sao cho minhip chi co mt chn cua cng p1 xut ra bit 0.Ban thy khi xut 8 bit ra trn cang p2, 8 bit mc 1 ng vi mc volt cao dung cp cho chn B cua 8 transistor 2SC1815, luc nay cac transistor se bi lam cho bao hoa,no cho 8 ct trn o co cac chn Cathode ni masse, vy 8 Led nay se phat sang, trong mach chung ta han dong vi cac in tr 100 ohm.Tom lai, mun in hinh hay cac con ch trn bang Led ma trn nay, Ban phai lam c 2 vic sau:Vic 1:Vit oan chng trinh cp ma quet.Tc quet u nhanh dung hin tng lu anh cua mt ma gy ra "nhm ln" la trnbang en ang co rt nhiu Led angc cp dong phat sang.

Vit oan chng trinh xut ma quet bit0 trn cacchn B cua cac transistor pnp trn cac cang p1, cho ch tri xung.Vit oan chng trinh xut ma quet bit0 trn cac chn B cua cac transistor pnp trn cac cang p1, cho ch tri ln.

Vit chng trinh ma quet, cp bit 1 cho cacchn B cua cac transistor npn trn cac cang p2, p3, p0, tao hinh di ngang.

Vic 2: Vit cacday ma 8 bit cho xutma hinh trn cac cang p2, p3, p0 hay trn p1 lam sang cac Led trn cac ct, cac ma 8 bit nay vit theo kiuhinh hay dang con ch ma Ban mun no hinra trn bang en. Cac day ma 8 bit nay chung ta se cho ct trong cac bang, sau nay se dungcu lnh movc a, @a + dptr lycac day ma ra cho xut trn cac cang cua ic AT89C51.

Sau y la oan chng trinh vit choxut ma quet trn p2, p3, p0 va ma hinh xut trn cang p1.

h1b: ;t tn nhan dung cho lnh nhy mov r0, #11111110b ; t tri khi u dung cho lnh quay vong mov r1, #00h ; t tri khi u dung cho lnh ly ma hinh hay chll1b: mov a, r0 ; chuyn tri trong r0 vao thanh a rr a ; cho 8 bittrong a quay vong qua hng phai mov r0, a ; chuyn tritrong thanh a ra r0 mov p1, r0 ; cho xut tri trong thanh a ra cang p1 mov a, r1 ; chuyn tri trong thanh r1 vao thanh a movc a, @a + dptr ; truyly ma hinh trong bang cho ct vao thanh a mov p2, a ; choxut ma hinh trn cang p2

inc r1 ; tng tri trong thanh r1 ln +1 ly mahinh tip theo mov a, r1 ; chuyn tri r1 vao thanh a movc a, @a + dptr ; truy ly ma hinh trong bang cho ct vao thanh a mov p3, a; cho xut ma hinh trn cang p3

inc r1 ; tng tri trong thanh r1 ln +1 ly ma hinh tip theo mov a, r1 ; chuyn trir1 va thanh a movc a, @a + dptr ; truy ly ma hinh trong bang cho ct vao thanh a mov p0, a ; cho xut ma hinh trn cang p0

inc r1 ; lai tng tri trong r1 ln +1 call del1 ; goi chng trinh lam chm mov p1, #11111111b ; tt8 bit trn cang p1 mov p0, #00h ; tt 8 bit trn cang p0 mov p2, #00h ; tt 8 bit trn cang p2 mov p3, #00h ; tt8 bittrn cang p3 call del1 ; goi chng trinh lam chm cjne r0, #11111110b, ll1b ;so sanh ma quet chon hng nhy ret ; quay lai sau lnh calloan chng trinh nay vit ging nh oan trn, im khac bit la dung cu lnh rl a quay vong theo hng trai i hng chay hinh trn bang ma trn 8x24. Ma quet t trn cang p1, ma hinh ln lt cho ly ra xut trn cang p2, ri p3, ri p0.

h1c: ; t tn nhan dung cho lnh nhy mov r0, #01111111b ; t tri khi u dungcho lnh quay vong mov r1, #00h ll1c: mov a, r0 ;chuyn tri trong r0 vao thanh a rl a ; cho 8 bit trong thanh a quay vong theo hng trai mov r0, a ; chuyn tri trong thanh a vao thanh r0 mov p1, r0 ; cho xut tri trong thanh r0 ra cang p1; ly ma hinh xut trn cang p2 mov a, r1 movc a, @a + dptr mov p2, a; ly ma hinh xut trn cang p3 inc r1 mov a, r1 movc a, @a + dptr mov p3, a; ly ma hinh xut trn cang p0 inc r1 mov a, r1 movc a, @a + dptr mov p0, a; tranh hinh bi lem inc r1 call del1 mov p1, #11111111b mov p0, #00h mov p2, #00h mov p3, #00h call del1 cjne r0, #01111111b, ll1c ret ; quay lai sau lnh call

Mt minh hoa: Ma quet cho xut trn cac cang p2 ri p3 ri p0. Ma hinh xut trn cang p1.

Cho xut ma quet nhip nhanh trn cac cang p2, p3, p0 va cung luc cho xut ma hinh hay ch trn cang p1. Sau y la oan chng trinh vit cho trng hp 2 dung hin cac hinh va con ch trn bang ma trn 8x24.

Phn tich cu lnh:

; Cac cu lnh cho xut ma quet trn cang p2, ma hinh trn p1h1: ; t tn nhan dung cho lnh nhy mov r0, #00000001b ; t tri khi u cho thanh r0 mov r1, #00h ; tra thanh r1 v tri 0ll1: mov a, r0 ; cho chuyn tri trong r0 va thanh a rr a ; chocac 8 bit trong thanh a, quay vong qua theo hng bn phai mov r0, a ;chuyn tri cotrong thanh a vao thanh r0 mov p2, r0 ; cho xut tri trong r0 ra cang p2 mov a, r1 ;by gi chuyn tri trong r1 vao thanh a movc a, @a + dptr ; lyma 8 bit trong bang theo tri trong a, ri cho ct vao thanh a mov p1, a ; cho xut tri nay trn cang p1 inc r1 ; cho tng tri trong r1 ln +1 chun bi ly dong ma tip theo call del1 ; goi chng trinh lam chm tao mc sang cho cac led mov p2, #00h ; cho 8 chn cuacang p2 xung mc 0 tt cac Led. mov p1, #0ffh ; Cho 8 chn cua cang p1ln mc 1 cung tt Led call del1 ; cho goi tring lam chm xoa cac Led tranh lem cjne r0, #00000001b, ll1 ; so sanh tri trong r0, nu thy cha quay u 8 nhip thi tip tuc.

; Cac cu lnh choxut ma quettrncang p3, ma hinh trn cang p1ll2:; t tn nhan dung cho lnh nhy mov a, r0 ; lai chuyn tritrong r0 vao a rr a ; cho quay vong 8 bit trong a theo hng bn phai mov r0, a ; chuyn tri trong thanh a vao r0 mov p3, r0 ; cho xut tri trong r0 ra trn cang p3 mov a, r1; chuyn tri co trong r1 vao thanh a movc a, @a + dptr ; cho ly ma trong bang ct vao a mov p1, a ; cho tri trong a xut ra trn cang p1 inc r1 ; tng tri trong r1 ln +1 ly dong mahinh tip theo call del1 ; goi chng trinh lam chm mov p3, #00h; t8 chn cuacang p3 xung mc 0 mov p1, #0ffh ;t 8 chn cua cang p1ln mc1 call del1 ; cho goi chng trinh lam chm cjne r0, #00000001b, ll2 ; so sanh tri trong r0 inh hng nhy

; Cac cu lnh cho xut ma quettrncang p0, ma hinh trn cang p1ll3: ; t tn nhan dung cho lnh nhy mov a, r0 ; chuyn tri trong thanh a vao thanh r0 rr a ; cho 8 bittrong thanh a quay vong qua hng phai mov r0, a ; chuyn tri trong thanh a tr lair0 mov p0, r0 ; cho tri trong r0 xut ra trn cang p0 mov a, r1 ;chuyn tri trong r1 vao thanh a movc a, @a + dptr ; ly ma 8 bit trong bang ct vao thanh a mov p1, a ; cho tri trong a xut ra trn cang p1 inc r1 ;tng tri trong r1 ln +1 ly dong ma k tip call del1 ; goi chng trinh lam chm mov p0, #00h ; t 8 chncua cang p0 xung mc0 mov p1, #0ffh ; t 8 bit cua cang p1 ln mc 1 call del1 ; goichng trinh lam chm cjne r0, #00000001b, ll3; so sanh tri trongr0 inh hng nhy ret ; quay lai sau lnh call

Cach 2: Ma quet cho xut trn p1va ma hinh hay ch cho xut trn cang p2, p3, p0Mt minh hoa: Cho xut ma quet nhanh trn cang p1, cac ma hinh ln lt cho xut trn cang p2, cang p3, cang p0

Phn tich cu lnh:

h1b: ;t tn nhan cho lnh nhy mov r0, #11111110b ; t tri khi u cho nhip quet mov r1, #00h ; tra tri khi u cho r1 v 0ll1b: mov a, r0 ; chuyntri r0 vaothanh a rr a ; cho 8 bit trong a quay vong qua phai mov r0, a ; chuyn tri trong a ra r0 mov p1, r0 ; cho xut tri trong r0 ra cang p1 mov a, r1 ; chuyn tri r1 vao a movc a, @a + dptr ;ly ma ia chi trong bang cho ct vao thanh a mov p2, a ; cho xut 8 bit ma hinh ra trn cang p2 inc r1 ; tng tri trong r1 ln +1 mov a, r1 ; lai chuyn tri trong r1 vao thanh a movc a, @a + dptr ; lai ly tip 8 bit ma hinh ct vao thanh a mov p3, a ; cho xut 8 bit ma hinh ra trn cang p3 inc r1 ; mt lnnalai tng tri trong r1 ln +1 mov a, r1 ; lai chuyn tri cua r1 vao thanh a movc a, @a + dptr ; lai ly tip 8 bit ma hinh cho ct vao thanh a mov p0, a ; by gi cho xut 8 bit ma hinh ra trn cang p0 inc r1 ; lai tng tri r1 ln +1, chun bi ly tip cac ma hinh tip theo call del1 ;goi chng trinh lam chm mov p1, #11111111b ; xoaxung quet tranh lem hinh mov p0, #00h ; xoa ma hinh trn p0 mov p2, #00h ; xoa ma hinh trn p2 mov p3, #00h ; xoa ma hinh trn p3 call del1 ; goi chng trinh lam chm cjne r0, #11111110b, ll1b ; so sanh tri trong a vi ma quet khi u nhy. ret ; quay lai sau lnh call

Phn tich cach vit chng trinh ngun tng hp:

org 0000h ; khi u chng tring ngun slan equ 20h ; t tn slan "s ln" cho thanh 20hbd: call xoa ; goi chng trinh xoa mov r3, #0 ; t tri3 vao thanh r1 call h2 ; cho goi t