Mong mọi ngưởi chỉ giáo
em làm đồ án dùng 89c51 đo và hiển thị tốc độ động cơ bằng encoder có độ phân giải 100 xung/vòng
1 nút start
1 nút stop dừng đo và lưu lại kết quả cuối
hiển thị tốc độ bằng led 7 thanh có khoảng từ 0-2500 vòng/phút
em tính đi tính lại rồi vẫn sai ở T\C2
(T\C2 dùng ngắt tràn để sau mỗi 20ms lại cập nhật tốc độ mới 1 lần)
code viết bằng asm ạ Mong mọi người chỉ giáo với
;============================================
org 00h ;power on, reset vector
ljmp init
org 03h ;interrupt 0 vector
reti
org 0bh ;timer 0 interrupt vector
reti
org 13h ;interrupt 1 vector
reti
org 1bh ;timer 1 interrupt vector
reti
org 23H ;serial port interrupt vector
reti
org 2bh ;timer 2 interrupt vector
ljmp INT_T2 ;chuong trinh con phuc vu ngat
;================================================= ==========================================
;khai bao bien so
;================================================= ==============================================
D0 equ 50h
D1 equ 51h
D2 equ 52h
D3 equ 53h
D4 equ 54h
NxH equ 55h
NxL equ 56h
Tic equ 60h
digit equ 61h
Dnum equ 62h
;================================================= =============================================
;khai bao kieu bit
;================================================= ================================================== ====
ZOV equ 00h
;================================================= ================================================== ====
;cac bien cho truong trinh con
;================================================= =====================================
X0 equ 70h
X1 equ 71h
X2 equ 72h
X3 equ 73h

Y0 equ 74h
Y1 equ 75h
Y2 equ 76h
Y3 equ 77h

Z0 equ 78h
Z1 equ 79h
Z2 equ 7ah
Z3 equ 7bh

XL equ X0
XH equ X1
YL equ Y0
YH equ Y1
ZL equ Z0
ZH equ Z1
;================================================= ==================
;chuong trinh khoi tao
;================================================= ================================================== ===
org 100
init:
clr EA ;cam tat ca cac ngat
mov SP,#2h ;khoi tao ngan xep(bat dau 30h)
clr P3.0
mov TMOD,#00100101B; khoi tao timer 0 lam viec o che do la bo dem 16 bit, timer 1 lam viec o che do auto_reload
mov TH0,#0
mov TL0,TH0
mov T2CON,#02H ;T2 lam viec o che do autoreload
mov TH2,#0ECH
mov TL2,#77H
mov RCAP2H,TH2
mov RCAP2L,TL2
setb TR2
setb TR0 ;cho phep timer 0 chay
setb TR1
nop
setb IE.5 ;cho phep timer 2 ngat
;================================================= ================================================== ===========
mov Tic,#0h
mov digit,#0FFh
mov Dnum,#4
;================================================= ================================================== ===========
setb EA ;cho phep cac ngat
here:
sjmp here ;doi ngat
;================================================= =========================================
;Interrupt INT_T2
;FCT :do va hien thi toc do dong co
;Input none
;Output none
;================================================= ========================================
INT_T2:
clr TF2 ;xoa co tran
inc Tic
mov a,Tic
cjne a,#4,CDoc ;td=20ms?
mov Tic,#0
clr TR0
mov NxL,TL0 ;doc so lieu do
mov NxH,TH0
mov TL0,#0
mov TH0,#0 ;xoa bo dem
setb TR0
;================================================= ==============
mov XL,NxL ;tinh toan ket qua do
mov XH,NxH
mov YL,#0FAh
mov YH,#0
lcall mul16
mov XL,Z0
mov XH,Z1
mov YL,#3
mov YH,#0
lcall div16
mov XL,Z2
mov XH,Z3
;================================================= ==================================
lcall hextodec ;chuyen doi hex sang dec
CDoc:
lcall display ;hien thi toc do dong co(vong/phut)
reti
;================================================= ========================
;subrotine display
;FCT :hien thi toc do dong co len led 7 thanh
;Input: D3, D2,D1, D0
;Output: none
;================================================= ================================
display:
mov a,@r0
mov dptr,#bayth
movc a,@a+dptr ;tra ra 7 thanh
mov P0,a
inc r0
mov P2,digit
mov a,digit
rl a
mov digit,a
djnz Dnum,KT
mov r0,#D0
mov Dnum,#4
mov digit,#0FEh
KT:
ret
;================================================= ================================================== =================
;subrotine hextodec
;FCT bien doi tu do hex sang so BCD
;Input XH,XL
;Output D4,D3,D2,D1,D0
;================================================= ================================================== =================
hextodec:
mov YH,#00h
mov YL,#0ah
lcall div16 ;chia so can doi cho 10
mov D0,Z0
mov XH,Z3
mov XL,Z2
lcall div16 ;chia so can doi cho 10
mov D1,Z0
mov XH,Z3
mov XL,Z2
lcall div16 ;chia so can doi cho 10
mov D2,Z0
mov XH,Z3
mov XL,Z2
lcall div16 ;chia so can doi cho 10
mov D3,Z0
mov XH,Z3
mov XL,Z2
lcall div16 ;chia so can doi cho 10
mov D4,Z0
ret
;================================================= ================================================== ======================
;subrotine div16
; Z=X/Y
;Input X=(XH,XL); Y=(YH,YL)
;output Z3,Z2
; :quotient
;Z1,Z0
;ZOV Y=0, i,e., ket qua duoc suat ra
;range
;calls: sub16
;destroys: a, r0, r1, r2, r3, r4, r7, flags
;================================================= ================================================== ========================
div16:
push 0
mov a,YH ;gan byte cao
orl a,YL
jnz div_ok
setb ZOV
ret
div_ok:
mov r1,XH
mov r0,XL
mov XH,#0
mov XL,#0
mov r3,#0
mov r2,#0
mov r7,#16
div_loop:
clr c
mov a,r0
rlc a
mov r0,a
mov a,r1
rlc a
mov r1,a
mov a,XL
rlc a
mov XL,a
mov a,XH
rlc a
mov XH,a
lcall subb16
mov c,ZOV
cpl c
jnc div_1
mov XH,ZH
mov XL,ZL
div_1:
mov a,r2
rlc a
mov r2,a
mov a,r3
rlc a
mov r3,a
djnz r7,div_loop
mov Z3,r3
mov Z2,r2
mov Z1,XH
mov Z0,XL
clr ZOV
pop 0
ret
;================================================= ================================================== =====================
;subrotine subb16
; Z(ZH,ZL)= X(XH,XL) - Y(YH,YL)
; if (Y>X) then ZOV=1
;Input X(XH,XL), Y(YH,YL)
;Output Z(ZH,ZL)
;================================================= ================================================== ===
subb16:
push acc
mov a,XL ;load X low byte into

;accumulator
clr c ; clear carry flag
subb a,YL ;subract Y low byte
mov ZL,a ;put result in Z low byte
mov a,XH ;load X high byte into
;accumulator
subb a,YH ; subtract Y high byte
;with the borrow. ..
; ... from low byte
;operation
mov ZH,a ; save result in Z hgh
;byte
mov ZOV,c ;set ZOV id an external
;borrow is produced
pop acc
ret
;================================================= ================================================== ==============
; subroutine MUL8_16
; Z=X*Y
;byte multipled by 16 bit word
;input: X(XL); Y(YH,YL)
;output: Z2, Z1, Z0: Z2 is the most byte, Z0 is the least byte
;================================================= ================================================== =================
MUL8_16:
mov a,XL
mov b,YL
mul ab
mov Z0,a
push b
mov a,XL
mov b,YH
mul ab
pop 0
add a,r0
mov Z1,a
clr a
addc a,b
mov Z2,a
ret
;================================================= ================================================== =====================
;subrotine MUL16
;Multiply two 16_bit unsigned Word
;input :X(XH,XL); Y(YH,YL)
;output :Z3, Z2, Z1, Z0. Z3 is the most byte, Z0 is the least
;================================================= ================================================== =====================
MUL16:
lcall MUL8_16
mov a,Z2
push acc
mov a,Z1
push acc
mov c,Z0
push acc
mov XL,XH
lcall MUL8_16
mov b,Z0
pop Z0
pop acc
add a,b
mov b,Z1
mov Z1,a
pop acc
addc a,b
mov b,Z2
mov Z2,a
clr a
addc a,b
mov Z3,a
ret
;================================================= ================================================== ==========================
bayth: ;bang ma bay thanh
db 0C0h, 0F9h, 0A4h, 0B0h, 99h, 92h, 82h, 0F8h, 80h,90h
; 0 1 2 3 4 5 6 7 8 9
;================================================= ================================================== ==============

--------------------------------------------------------------------------------
Xem bài viết cùng chuyên mục: