利用8位數(shù)碼管顯示時(shí)間,原理與4位數(shù)碼管顯示的基本一樣
仿真圖:
程序源代碼:
本程序已經(jīng)通過(guò)在線(xiàn)軟件仿真和硬件制作.
org 0000h ;程序開(kāi)始入口地址
sjmp main ;跳轉(zhuǎn)至主程序
org 000bh ;T0中斷入口地址
ljmp inti0 ;跳轉(zhuǎn)至T0中斷程序
org 001bh ;T1中斷入口地址
ljmp inti1 ;跳轉(zhuǎn)至T1中斷程序
org 0030h
main:mov tmod,#11h ;設(shè)T0、T1為模式1
mov ie,#8ah ;開(kāi)T0、T1允許中斷
mov th0,#4ch ;賦T0 50ms初值
mov tl0,#00h
mov th1,#4ch ;賦T1 50ms初值
mov tl1,#00h
mov sp,#60h ;設(shè)置堆棧指針
mov 38h,#00 ;鬧鐘分初值
mov 39h,#00 ;鬧鐘時(shí)初值
mov 31h,#00 ;時(shí)間秒初值
mov 32h,#00 ;時(shí)間分初值
mov 33h,#00 ;時(shí)間時(shí)初值
setb tr0 ;啟動(dòng)定時(shí)器T0
setb tr1 ;啟動(dòng)定時(shí)器T1
loop:lcall display ;調(diào)用時(shí)間顯示程序
lcall keysan ;調(diào)用時(shí)間調(diào)節(jié)按鈕
lcall keynz ;調(diào)用鬧鐘控制按鈕
ajmp loop
inti0:push psw ;壓入堆棧指針,保護(hù)現(xiàn)場(chǎng)
push acc
clr ea ;關(guān)中斷www.dgzj.com
mov th0,#4ch ;重賦T0定時(shí)初值
mov tl0,#00h
inc 3ah ;3ah為50ms計(jì)數(shù)單元
mov a,3ah
cjne a,#20,out ;1秒計(jì)數(shù)是否到
mov 3ah,#00 ;清50ms計(jì)數(shù)初值
inc 31h ;秒加1
mov a,31h
cjne a,#60,out ;60秒計(jì)數(shù)是否到
mov 31h,#00
inc 32h ;分加1
mov a,32h
cjne a,#60,out ;60分計(jì)數(shù)是否到
mov 32h,#00
inc 33h ;時(shí)加1
mov a,33h
cjne a,#24,out ;24時(shí)計(jì)數(shù)是否到
mov 33h,#00
out: setb ea ;開(kāi)中斷
pop acc ;彈出堆棧指針,恢復(fù)現(xiàn)場(chǎng)
pop psw
reti ;中斷返回
inti1:push psw ;壓入堆棧指針,恢復(fù)現(xiàn)場(chǎng)
push acc
clr ea
mov th1,#4ch ;重賦T1定時(shí)初值
mov tl1,#00h
inc 71h ;71h為50ms計(jì)數(shù)單元
mov a,71h
cjne a,#20,out1
mov 71h,#00
mov a,32h ;鬧鐘判斷
cjne a,38h,out1 ;鬧鐘時(shí)間分判斷
mov a,33h
cjne a,39h,out1 ;鬧鐘時(shí)間時(shí)判斷
clr p1.0 ;到鬧鐘時(shí)間,清p1.0
out1:setb ea ;開(kāi)中斷
pop acc
pop psw
reti ;中斷返回
display:mov a,31h ;秒顯示
mov b,#10
p ab
mov 20h,a ;將十位放入20h單元
mov 21h,b ;將個(gè)位、余數(shù)放入21h單元
disp1: mov a,20h ;秒十位顯示
mov dptr,#table
movc a,@a+dptr ;將A的內(nèi)容+table內(nèi)容,結(jié)果回存給A
mov p0,a ;將A給P0口
clr p2.6 ;清p2.6,點(diǎn)亮第7位數(shù)碼管
lcall del1ms ;調(diào)用1ms延時(shí)子程序
setb p2.6 ;滅第7位數(shù)碼管
disp2: mov a,21h ;秒個(gè)位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.7
lcall del1ms
setb p2.7
disp4: mov a,32h ;分顯示
mov b,#10
p ab
mov 22h,a
mov 23h,b
disp5: mov a,22h ;分十位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.3
lcall del1ms
setb p2.3
disp6: mov a,23h ;分個(gè)位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.4
lcall del1ms
setb p2.4
disp7: mov a,33h ;時(shí)顯示
mov b,#10
p ab
mov 24h,a
mov 25h,b
disp8: mov a,24h ;時(shí)十位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.0
lcall del1ms
setb p2.0
disp9: mov a,25h ;時(shí)個(gè)位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.1
lcall del1ms
setb p2.1
disp10: mov 34h,#0bfh ;"-"顯示
mov a,34h
mov p0,a
clr p2.5
lcall del1ms
setb p2.5
disp11: mov 35h,#0bfh ;"-"顯示
mov a,35h
mov p0,a
clr p2.2
lcall del1ms
setb p2.2
ret
display1:mov a,38h ;鬧鐘分顯示
mov b,#10
p ab
mov 26h,a
mov 27h,b
d1:mov a,26h ;鬧鐘分十位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.3
lcall del1ms
setb p2.3
d2:mov a,27h ;鬧鐘分個(gè)位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.4
lcall del1ms
setb p2.4
dispnz: mov a,39h ;鬧鐘時(shí)顯示
mov b,#10
p ab
mov 28h,a
mov 29h,b
d5: mov a,28h ;鬧鐘時(shí)十位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.0
lcall del1ms
setb p2.0
d6: mov a,29h ;鬧鐘時(shí)個(gè)位顯示
mov dptr,#table
movc a,@a+dptr
mov p0,a
clr p2.1
lcall del1ms
setb p2.1
mov 35h,#0bfh ;"-"顯示
mov a,35h
mov p0,a
clr p2.2
lcall del1ms
setb p2.2
ret
table:db 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h ;數(shù)字0—9
del1ms:mov r7,#10 ;1ms延時(shí)子程序
del1: mov r6,#50
del2:djnz r6,del2
djnz r7,del1
ret
del20ms:mov r5,#40 ;20ms延時(shí)子程序
del3: mov r4,#250
del4: djnz r4,del4
djnz r5,del3
ret
keysan:jnb p3.1,key1 ;秒加1按鈕
jnb p3.2,key2 ;分加1按鈕
jnb p3.3,key3 ;時(shí)加1按鈕
sjmp keyout
key1: lcall del20ms
jb p3.1,keyout
jnb p3.1,$
inc 31h ;秒加1
mov a,31h
cjne a,#60,keyout
mov 31h,#00
sjmp keyout
key2:lcall del20ms
jb p3.2,keyout
jnb p3.2,$
inc 32h ;分加1
mov a,32h
cjne a,#60,keyout
mov 32h,#00
sjmp keyout
key3:lcall del20ms
jb p3.3,keyout
jnb p3.3,$
inc 33h ;時(shí)加1
mov a,33h
cjne a,#24,keyout
mov 33h,#00
sjmp keyout
keyout:ret
keynz:jnb p3.4,keynz4 ;顯示鬧鐘并可修改和顯示當(dāng)前時(shí)間
jnb p3.7,keynz5
jnb p3.5,nz3
jnb p3.6,nz4
sjmp nzout
nzout:ret
nz2:lcall display1
jnb p3.5,nz3
jnb p3.6,nz4
keynz4:jnb p3.4,nz2
sjmp nzout
nz3:lcall del20ms
jb p3.5,nzout
jnb p3.5,$
inc 38h
mov a,38h
cjne a,#60,nzout
mov 38h,#00
sjmp nzout
nz4:lcall del20ms
jb p3.6,nzout
jnb p3.6,$
inc 39h
mov a,39h
cjne a,#24,nzout
mov 39h,#00
sjmp keyout
keynz5:jb p3.7,nzout
jnb p3.7,$
setb p1.0
sjmp nzout
-
單片機(jī)
+關(guān)注
關(guān)注
6037文章
44558瀏覽量
635218 -
數(shù)碼管
+關(guān)注
關(guān)注
32文章
1882瀏覽量
91108
原文標(biāo)題:51單片機(jī)8位數(shù)碼管電子時(shí)鐘仿真圖及源代碼
文章出處:【微信號(hào):changxuemcu,微信公眾號(hào):暢學(xué)單片機(jī)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論