bộ điều khiển trung gian
1. Chương trình viết cho bộ điều khiển trung tâm. org 00h
Ljmp main
;P3.4 CUA MASTER NOI VOI CHAN P3.5 CUA CA 2 SLAVE ;P3.5 CUA MASTER NOI VOI CHAN P3.6 CUA CA 2 SLAVE ;MO TA CAC CHUONG TRINH CON.
;1. chuong trinh con TRANS :nhiem vu lay du lieu tu may tinh gui xuong.
;2. chuong trinh con EXPUTRAM1 :nhiem vu lay 16 bo du lieu xung truyen cho slave1 ;3. chuong trinh con EXPUTRAM2 :nhiem vu lay 16 bo thong so chieu quay truyen cho slave2.
;4. chuong trinh con NGATNT :nhiem vu xu ly co TI,RI khi co ngat noi tiep. ;5. chuong trinn con delay :dung mot khoang thoi gian khi can thiet
;6. chuong trinh con direczero :lam cau noi truyen tin hieu chieu quay tu slave1 sang slave2 org 03h
Ljmp exputram1 ;dia chi ngat ngoai 0 org 13h
Ljmp exputram2 ;dia chi ngat ngoai 1 org 23h ; dia chi ngat noi tiep RI,TI ljmp ngatnt ; nhay den chuong trinh ngat org 30h
main: ; bat dau chuong trinh chinh mov a,pcon ; hieu chinh toc do truyen setb acc.7 ; nhan doi toc do truyen
mov pcon,a ; bat dau nhan doi (baud=19200) mov tmod,#21h ; khoi tao hai bo dinh thoi 0,1 mov th1,#-3 ; luu gia tri truoc vao bo dinh thoi 1 mov scon,#50h ; dinh khung du lieu 8 bit 1start,1stop
mov ie,#95h ; cho phep ngat noi tiep,ngat ngoai0,ngat ngoai1 setb IT0 ; ngat ngoai0 tich cuc muc
setb IT1 ; ngat ngoai1 tich cuc muc setb PS ; ngat noi tiep uu tien
setb tr1 ; bat dau bo dinh thoi noi tiep
setb P3.4 ; reset de truyen nhan tin hieu bat tay setb P3.5 ; reset de truyen nhan tin hieu bat tay
clr 00h ;bit nay =1 thi PC ra lenh truyen du lieu toi master
setb 01h ;bit nay =1 thi du lieu moi duoc lay tu master chuyen sang cac slave clr 02h ;bit nay chi cho chuong trinh trans thuc hien mot lan, neu chua cho phep mov P0,#0ffh ;Reset P0 noi voi slave1
Start:
trannext0:Jnb 00h,trannext1 ;neu matlab chua truyen du lieu thi chua cho slave1,2 chay Lcall trans ;neu matlab truyen tin hieu thi nhan ngay,va truyen cho slave1,2
trannext1: cpl P1.1 ;bao hieu da tu do Lcall delay Jmp Start trans: JB 02h,exitrans
push 00H ;cat R0 vao stack
push 2AH ;cat dia chi 2AH vao stack trans1:JNB 00H,trans1
mov @R0,B ;lay du lieu o ngat noi tiep dua vao bo dem clr 00h
DJNZ 2Ah,steptrans1 JMP exitrans1
steptrans1:
mov sbuf,#1 ;MAT MA TRUYEN DU LIEU XUONG MASTER inc R0 JMP trans1 exitrans1: pop 2AH setb P3.7 setb 02h
clr P3.7 ;goi slave 2 lam viec truoc nop setb P3.7 exitrans: ret exputram1: JNB 01h,link10
clr P3.4 ;chua cho slave1 nhan du lieu
setb P3.5 ;dieu kien thu nhat cho viec truyen du lieu (cua minh) push 7Ah ;cat noi dung 7Ah vao stack
push 7Bh ;cat dia chi luu tru so byte truyen push 00h ;cat R0 vao stack
inc 7Bh ;tang noi dung 7Bh len 1 don vi
inc 7Bh ;tang noi dung 7Bh len 1 don vi (2 don vi) mov 7Ah,7Bh ;dua noi dung 7Bh da tang len 2 vao 7Ah
mov R0,#7Ah ;BAT DAU TRUYEN TU DIA CHI 7AH (DU 2 DIA CHI) exput1:JNB P3.5,exput1 ;cho dieu kien thu 2 moi truyen du lieu
setb P3.4 ;Ra lenh cho slave1 nhan du lieu nop
clr P3.4 ;ngat dieu kien nhat thu nhat Djnz 7Ah,exput1
JNB P3.5,$
setb P3.4 ;tra chan nay ve muc ban dau pop 00h ;tra lai noi dung cho R0 pop 7Bh ;tra lai noi dung cho 32h pop 7Ah ;lay lai noi dung cua 33h
clr 01h ;xoa bit nay de master tien be xu ly cac cong viec khac o ngat(thuc hien sau cung)
mov P0,#0ffh ;Reset P0 sau moi lan truyen Jmp linkend1
link10: push Acc
mov P0,#0ffh ;REset P0 mov A,P0
link11: Cjne A,#1,link12 mov P2,#1
Lcall linkslave2 ;ngat slave2 va cho bit P3.5=0 Lcall repslave1 ;Bao ve cho slave1 va cho P3.4=0 clr P1.4
Jmp linkexit link12:Cjne A,#2,link13
Lcall repslave1 ;Bao ve cho slave1(ngat slave1 cho bit P3.4=0) mov A,P0
Lcall linkslave2 ;ngatslave2 va cho bit P3.5=0 mov P2,A ;dua tin hieu chieu quay vao P2 cua master nop
nop nop
Lcall waitlink2 ;giai phong wait cho slave2 (clr P3.5) JB P3.5,$
Lcall repslave1 ;Bao ve cho slave1 va cho P3.4=0 Jmp linkexit
link13:Cjne A,#3,link14 mov P2,#3
Lcall linkslave2 ;ngat slave2 va cho bit P3.5=0 mov P2,#0ffh ;Reset P2 de nhan du lieu
push 00h ;lay R0 lam con tro dem du lieu gui qua mov R0,#6
mov P0,P2 ;mov sbuf,P2
;JNB 00h,$ ;clr 00h
Lcall repslave1 ;ngat slave1 va cho slave 1 thong qua bit P3.4=0 Djnz R0,cotivelo
jmp exitvelo cotivelo:
clr P3.5 ;ra hieu cho slave2 truyen tin hieu van toc nop nop setb P3.5 JB P3.5,$ jmp move21 exitvelo: clr P1.3 jmp linkexit link14: linkexit:pop Acc linkend1: reti
;CHUONG TRINH NAY SE BAO VE CHO SLAVE1 repslave1:
setb P3.6
clr P3.6 ;bao ve cho slave 1 la master da giup xong nop
setb P3.6 JB P3.4,$ ret
;CHUONG TRINH NAY SE LINK VOI SLAVE2 linkslave2:
setb P3.7
clr P3.7 ;bao ve cho slave 2 la master da giup xong nop
JB P3.5,$ ret
;CHUONG TRINH NAY SE CHO TIN HIEU PHAN HOI TU SLAVE2 waitlink2:
clr P3.5 ;xoa bit nay de bao cho slave2 la master da nhan duoc thong bao nop
setb P3.5
mov P2,#0ffh ;Reset P2 ret
exputram2:
setb P3.5 ;dieu kien thu nhat cho viec truyen du lieu push 00h ;cat R0 vao stack
mov R0,#2Bh ;BAT DAU TRUYEN TU DIA CHI 2BH
exput2:JNB P3.5,exput2 ;cho dieu kien thu 2 moi truyen du lieu mov p2,@R0
setb P3.4 ;Ra lenh cho slave2 nhan du lieu nop
clr P3.4 ;ngat dieu kien nhat thu nhat inc R0
cpl P1.0 JNB P3.5,$
setb P3.4 ;tra chan nay ve muc ban dau pop 00h ;tra lai noi dung cho R0
mov P2,#0ffh ;Reset P2 sau moi lan truyen setb P3.6
nop
clr P3.6 ;goi slave 1 lam viec sau khi slave2 da lam viec nop setb P3.6 reti ngatnt:jb TI,exit mov B,sbuf clr RI setb 00h reti exit: clr TI reti delay: mov R7,#10 loop: mov TH0,#3Ch mov TL0,#0B0h setb TR0 waitdt:JB 00h, exitdt JNB TF0,waitdt exitdt: clr TF0 clr TR0 Djnz R7,loop ret end
2. Chương trình viết cho Slave1
org 00h Ljmp main
;1.KICHXUNG :nhiem vu kich xung cho cac dong co voi toi da 16 bo du lieu.
;2.SHARKHAND :nhiem vu bat tay voi slave2(goi slave2 voi mat ma(1,0) goi slave2. ;3.VELOCITY :nhiem vu xet dieu kien van toc cho cac dong co
;4.TEXTXUNG ;nhiem vu kiem tra su ket thuc cua mot buoc trong 16 bo xung. ;5.INSTEP :lay bo du lieu ke tiep trong 16 bo du lieu neu con.
;6.INPUTRAM ;lay du lieu 16 bo thong so tu master. ;7.NGATN0 ;dua bit 04h=1, de khoi dong chuong trinh ;8.TEXTZERO ;to chuc viec ra goc khi moi khoi dong. ;9.PLUSE ;kich xung khi ra goc.
;10.TEXTLIMIT :kiem tra su qua cong tac hanh trinh khi ra goc
;11.SENDDIREC ;gui cau noi thong qua master den slave2 du lieu chieu quay ;mat ma goi master la 0,1
MASTER VOI SLAVE---*****
;P1=01, SALVE1 YEU CAU MASTER BAO VOI SLAVE 2 GUI CHIEU QUAY CHO NO ;P1=02, SLAVE 1 YEU CAU MASTER TRUYEN BYTE CHIEU QUAY KHI DO GOC ;P1=03, org 03h Ljmp ngatn0 org 13h Ljmp ngatn1 org 30h main:
mov Tmod,#01h ;khoi dong bo dinh thoi 0
clr 00h ;khi bit nay =0 thi du lieu xung se duoc chuyen sang diem ke tiep clr 01h ;khi bit nay=0 thi tin hieu chieu quay chua duoc gui
clr 02h
clr 03h ;buoc vung dem phai trong
clr 04h ;bit quy dinh bat dau chuong trinh,master cho phep clr 05H ;khi bit nay=1 thi chuong trinh kich xung hoat dong clr 06h ;bit dung cho master thuc hien mot lenh
clr 07h ;bit lam dau hieu chi rang slave1 dang nho master lam mot viec gi do ;ma khong phai truyen du lieu so xung
mov 7AH,#7Ch ;nap dia chi dau tien cho con tro truy xuat bo dem du lieu(bo dem du lieu xung)
clr 7Eh ;hai bit tu do cua byte kich xung chuong trinh clr 7Fh ;nhu tren
setb 66H ;hai bit nay dam bao cho 2 bit tu do tren setb 67H ;nhu tren
setb IT0 ;ngat ngoai0 tich cuc muc setb IT1 ;ngat ngoai1 tich cuc muc mov 2EH,#0ffh ;byte chieu quay ra goc mov 2DH,#0ffh ;byte kich xung ra goc
mov 2Bh,#00h ;nap gia tri ban dau cho trang thai xung day clr P0.0 ;************thu
stand:JNB 04H,stand ;CHO TIN HIEU TU MASTER MOI HOAT DONG setb P0.0 ;***********thu
;Lcall textzero ;goi chuong trinh do goc toa do mov 2EH,#0ffh ;tra ve gia tri ban dau
mov 2DH,#0ffh ;tra ve gia tri ban dau
Start:JNB 04H,go1 ;khi nao bit 04H=1 thi chuong trinh inputram moi duoc goi mot lan Lcall inputram ;khi no thuc hien xong thi bit 04H duoc xoa tro ve 0
go1: JB 00H,go2 ;khi bit 00H=0 thi chuong trinh incstep se duoc goi mot lan Lcall sendvelo ;goi chuong trinh gui van toc
Lcall incstep ;khi no thuc hien xong thi bit 00H=1,dong thoi 05H=1,va bit ;01H=0, BAN DAU 01H=0,05H=0
Lcall delay Lcall delay
go2: JNB 05h,go4 ;khi bit 05H=1, thi chuong trinh kich xung moi lan
JNB 01h,go3 ;khi tin hieu chieu quay chua gui thi phai goi chuong trinh sharkhand Lcall kichxung ;mot lan goi chi kich duoc mot xung,
jmp go4 go3:Lcall sharkhand go4:clr P2.0 cpl P2.1 ;Lcall delay JMP Start
;CHUONG TRINH CON KICH XUNG CHO TAT CA CAC DONG CO ;DIA CHI 2FH(78-->7F) LAM BIT KICH XUNG
kichxung: Lcall velocity Lcall textxung ANL 2FH,#11000000b push 01h mov R1,2Bh cjne R1,#3Fh,exitkxung
cjne R2,#0,exitkxung ;kiem tra 6 ma Ri(Byte thap) cjne R3,#0,exitkxung
cjne R4,#0,exitkxung cjne R5,#0,exitkxung cjne R6,#0,exitkxung
cjne R7,#0,exitkxung
clr 00h ;buoc chuong trinh phai nap gia tri cho diem ke tiep clr 05h ;khong cho chuong trinh kich xung hoat dong exitkxung:pop 01h
ret
sharkhand:mov P1,#1 ;mat ma goi cho master yeu cau slave 2 gui chieu quay Lcall linkmaster ;goi master(ngat va cho tin hieu bit 06h).
Lcall waitlink1 ;giai phong WAIT cho master clr 07h ;ket thuc nho master setb 01h ;bao hieu tin hieu chieu quay da duoc gui ret
CHUONG TRINH NAY SE GOI DE LINK VOI MASTER linkmaster:
setb 07h ;nho master ma khong phai truyen tin hieu xung setb P3.7
clr P3.7 ;nho master bao hieu cho slave2 truyen du lieu chieu quay nop
setb P3.7
JNB 06h,$ ;cho tin hieu phan hoi tu master
clr 06h ;xoa bit dung cho master thuc hien mot lenh ret
;---o0o---
;CHUONG TRINH NAY SE CHO TIN HIEU PHAN HOI TU MASTER
waitlink1:clr P3.5 ;bit nay ra hieu cho master la slave1 da nhan duoc tin hieu phan hoi mov P1,#0ffh ;Reset P1
nop
setb P3.5 ;Reset P3.5 ret
;CHUONG TRINH NAY SE KIEM TRA XEM VOI DIEU KIEN VAN TOC DA GUI THI DONG CO NAO SE
;DUOC KICH XUNG velocity:
mov 2CH,#11000000b ;xoa tat ca cac dieu kien push Acc ;lay thanh chua lam con tro ;mov 2Ch,#0ffh
;jmp velo7
velo1: mov A,6Eh ;kiem tra byte van toc cua dong co 1 JZ velo2 ;phong khi so xung kich la 0
dec A
mov 6EH,A JNZ velo2 setb 60H
mov 6EH,74H velo2: mov A,6FH JZ velo3 dec A mov 6FH,A JNZ velo3 setb 61H mov 6FH,75H velo3: mov A,70H JZ velo4 dec A mov 70H,A JNZ velo4 setb 62H mov 70H,76H velo4: mov A,71H JZ velo5 dec A mov 71H,A JNZ velo5 setb 63H mov 71H,77H velo5: mov A,72H JZ velo6 dec A mov 72H,A JNZ velo6 setb 64H mov 72H,78H velo6: mov A,73H JZ velo7 dec A mov 73H,A JNZ velo7 setb 65H mov 73H,79H velo7:pop Acc ret
;CHUONG TRINH CON KIEM TRA SU KET THUC CHU TRINH, ;KHI CHU TRINH KET THUC THI BIT 00H=0,dong thoi bit 05H=0
;R2-->R7 LUU TRU BYTE THAP CUA SO XUNG TUNG DONG CO TU DC1 DEN DONG CO 6
;68H-->6DH LUU TRU BYTE CAO CUA SO XUNG NAY ;LAY DIA CHI 2FH(78-->7FH) LAM BIT KICH XUNG
;2BH(TU BIT 58H DEN 5FH), su dung de xac dinh trang thai bang 0 cua byte cao textxung:
push 01h
cpare11: JNB 60H,cpare21 ;dieu kien van toc chua cho phep cjne r2,#0,cpare12
mov R1,68H cjne R1,#0,reiput1
setb 58H ;noi dung byte cao 68H da bang Zero jmp cpare21
reiput1: mov r2,#255 dec 68H cpare12: setb 78h
dec R2 ; GIAM R2 DI MOT DON VI
cpare21: JNB 61H,cpare31 ;dieu kien van toc chua cho phep cjne r3,#0,cpare22
mov R1,69H cjne R1,#0,reiput2
setb 59H ;noi dung byte cao 69H da bang Zero jmp cpare31
reiput2: mov r3,#255 dec 69H
cpare22: setb 79h
dec r3 ; GIAM R3 DI MOT DON VI
cpare31: JNB 62H,cpare41 ;dieu kien van toc chua cho phep cjne r4,#0,cpare32
mov R1,6AH cjne R1,#0,reiput3
setb 5Ah ;noi dung byte cao 6AH da bang Zero jmp cpare41
reiput3: mov r4,#255 dec 6AH cpare32: setb 7Ah
dec r4 ; GIAM R4 DI MOT DON VI ;_________________________
cpare41: JNB 63H,cpare51 ;dieu kien van toc chua cho phep cjne r5,#0,cpare42
mov R1,6BH cjne R1,#0,reiput4
setb 5Bh ;noi dung byte cao 6BH da bang Zero jmp cpare51
reiput4: mov r5,#255 dec 6BH
cpare42: setb 7Bh
dec r5 ; GIAM R5 DI MOT DON VI
cpare51: JNB 64H,cpare61 ;dieu kien van toc chua cho phep cjne r6,#0,cpare52
mov R1,6CH cjne R1,#0,reiput5
setb 5Ch ;noi dung byte cao 6CH da bang Zero jmp cpare61
reiput5: mov r6,#255 dec 6CH
cpare52: setb 7Ch
dec r6 ; GIAM R6 DI MOT DON VI ;_________________________
cpare61: JNB 65H,cpare71 ;dieu kien van toc chua cho phep cjne r7,#0,cpare62
mov R1,6DH cjne R1,#0,reiput6
setb 5DH ;noi dung byte cao 6DH da bang Zero jmp cpare71
reiput6: mov r7,#255 dec 6DH cpare62: setb 7Dh
dec r7 ;GIAM R7 DI MOT DON VI cpare71: pop 01h ret incstep: push 00h push Acc mov R0,7AH
mov A,@R0 ; lay byte thap cho dong co 1 mov R2,a
inc R0
mov A,@R0 ;lay byte cao cho dong co 1 mov 68H,a
inc R0
mov a,@R0 ;lay byte thap cho dong co 2 mov R3,a
inc R0
mov 69H,a inc R0
mov a,@R0 ;lay byte thap co dong co 3 mov R4,a
inc R0
mov A,@R0 ;lay byte cao cho dong co 3 mov 6AH,a
inc R0
mov a,@R0 ;lay byte thap cho dong co 4 mov R5,a
inc R0
mov A,@R0 ;lay byte cao cho dong co 4 mov 6BH,a
inc R0
mov a,@R0 ;lay byte thap cho dong co 5 mov R6,a
inc R0
mov A,@R0 ;lay byte cao cho dong co 5 mov 6CH,a
inc R0
mov a,@R0 ;lay byte cao cho dong co 6 mov R7,a
inc R0
mov A,@R0 ;lay byte cao cho dong co 6 mov 6DH,a
mov A,7Bh ;dua tong so xung vao A
Add A,#7Bh ;cong noi dung con tro voi dia chi dau luu tru so xung subb A,R0
JNZ noreinput
mov R0,#7BH ; nap lai tu dau cho con tro di dong noreinput:
inc R0 mov 7AH,R0
mov 6EH,74H ;lay dieu kien van toc cho dong co 1 mov 6FH,75H ;lay dieu kien van toc cho dong co 2 mov 70H,76H ;lay dieu kien van toc cho dong co 3 mov 71H,77H ;lay dieu kien van toc cho dong co 4 mov 72H,78H ;lay dieu kien van toc cho dong co 5 mov 73H,79H ;lay dieu kien van toc cho dong co 6 mov 2BH,#00h ;lay tong so ton tai noi dung cua byte cao pop Acc
setb 00h ;khong cho goi chuong trinh incstep khi bit nay chua bang 0 setb 05h ;chuong trinh kich xung hoat dong(dieu kien1)
clr 01h ;chuong tirnh kich xung chi hoat dong khi sharkhand duoc goi(dieu kien 2) ret
inputram: setb p3.7 ;
clr p3.7 ;Goi master truyen du lieu xuong (NGAT O MASTER) nop
setb P3.5 ;dieu kien thu nhat cho viec nhan du lieu clr P3.6 ; chua cho master truyen du lieu
setb P3.7 ;xoa ngat (chi ngat 1 lan) push 7AH ;cat noi dung 7AH vao stack push 00h ;lay R0 lam con tro1
mov P1,#00h ;RESET PORT1 mov P1,#0ffh ;RESET PORT1
mov R0,#7Ah ;bat dau nhan tu dia chi 7Ah waitt0:
setb P3.6 ;ra hieu cho master truyen du lieu sang waitt1:JNB P3.5,waitt1 ;cho dieu kien thu 2 moi nhan du lieu clr P3.6 ;ngat dieu kien truyen thu nhat ben master mov @R0,p1
mov P0,@R0 acall delay inc R0
djnz 7Ah,waitt0
setb P3.6 ;tra chan nay ve muc ban dau(SLAVE2 SE TRA CHAN P3.5) dec 7Bh ;giam noi dung cua 7Bh di 1 don vi
dec 7Bh ;giam noi dung cua 7Bh di 1 don vi(7B bang dung gia tri) pop 00h
pop 7Ah
clr 04h ;bao hieu voi master la da nhan xong du lieu ret
sendvelo: mov P1,#3
Lcall linkmaster ;ngat master va cho bit 06h push 00h ;lay R0 lam con tro
mov R0,#74h ;lay noi dung van toc luu bat dau tu dia chi 74h mov P1,#0ffh ;Reset P1 de nhan du lieu
save:
mov @R0,P1 clr P3.5
nop nop setb P3.5 inc R0 Cjne R0,#7Ah,sendnext jmp exitvelo sendnext:
JNB 06h,$ ;cho master ngat ngoai 0 clr 06h jmp save exitvelo:pop 00h ret ngatn0: step0:JB 07h,step1
setb 04h ;bit khoi dau chuong trinh jmp step2
step1:setb 06h ;bit nho master step2: reti ;---o0o--- ;--- ngatn1: reti textzero:
push Acc ;cat noi dung thanh chua vao stack mov A,#00 ;xoa noi dung thanh chua