|
倒计时器在生活非常有用,用单片机自制,不但电路简单,而且功能可根据需要自行设计。由74系列、40系列小规模数字芯片设计的倒计时器非常多,但有显示不直观(无LED数码管);调整参数不灵活;计时精度低(采用RC振荡)等缺点。在此向大家介绍如何用89C2051单片机自制倒计时器的方法。
一、功能介绍
1. 独立的三路倒计时。均可独立设置启动、关闭。
2. 范围:0~20小时,可任意设定。关机后数据不丢失。计时精度高:<0.1S
3..独立的三路输出:
a:第1路 蜂鸣器输出: 15S
b:第2路 PNP晶体管输出:5S
c:第3路 PNP晶体管输出:直致关机
二、工作原理
电路见上图整机由89c2051、三位LED数码管、K1-K4、R、T等元器件组成。89C2051内部T1定时器完成100mS定时中断功能。为了减少硬件,由89C2051的P1口直接输出LED的段码,BCD转换由软件完成。位选码由P3.0,P3.1,P3.7输出。键盘扫描与LED位选码的脚共用。
由于89C2051的灌入电流:20mA。故直接驱动:蜂鸣器、PNP晶体管。LED数码管采用共阴高亮型,LED1数码管要旋转180度。因为要显示:“19:99”,而只有三位数码管,因此用第三位的数码管的小数点表示“1”。
三、软件编制
(软件流程见图三)
在T1中断程序中做一个软时钟,在定时处理程序不断查寻时间,并根据每路的启动状态,判别是否要减数。每路倒计时到"0"后,均要判别其它两路是否已启动。如果没有启动,则CPU进入掉电模式,电流<1.5uA,因此电路中无电源开关,再次启动,由K4(复位键)完成。每路倒计数的初值、启动状态(是否开始倒计数),均可单独设定并存在89c2015的RAM中,只要不断电,数据不会改变。
四、操作说明
电路中设有K1~K4四个开关:
1.功能如下:
K1:定时路数切换。上电:LED显示第1路定时时间,按下k1,LED显示第1路定时启动状态:
【F】表示:关 。【n】表示:开。再按下K1后,依此进入第2路、3路的设置,最后进入关机状态。 LED显示:【OFF】,K3按下则关机。
K2:小时位加1。当超过9小时后,LED1的小数位亮。表示“1”,因此LED显示最大的定时时间:19小时。LED1安装时要倒过来,而且最好用绿色的。K3:分位加1
、 启动状态切换【F-n-F】、关机确认。
1、 当LED数码管显示定时时间时,按下K3则相应路数的分位加1。范围:0~59分钟。
2、 当LED数码管显示【1.F】或【2.F】或【3F】时,按下K3则【F】变为【n】,再次按下K3则【n】变为【F】,一旦进入【n】则相应路数的倒计时就启动。
3、 当LED数码管显示【OFF】时,表示CPU已处于关机模式,只要按下K3,则CPU进入掉电模式。
K4:复位 复位89C2051,把MCU从掉电模式拉出重新启动。以上电路定时初值及启动状态均可时时改变。
2.定时初值及启动状态的设定:
三路定时初值及启动状态均可一次设置好,方法如下:
1、K1~K3键任意键按下不松约5s后,LED数码管显示开始闪烁,表示已进入初值和启动状态设定模式。
2、通过K1-K3的操作,可对三路的定时初值及启动状态进入设定。
四、注意事项
1、 当MCU用6MHz晶振时,工作电压可在2.6-5.5V之间。
2、 外接继电器,请注意工作电压,及晶体管的保护。
;***************
;timer_program 2000-12-3 倒计时器源程序
;***************
timers equ 08h
;**************************************************
al1_min equ timers+1h
al1_hour equ timers+2h
al2_min equ timers+3h
al2_hour equ timers+4h
al3_min equ timers+5h
al3_hour equ timers+6h
flash equ timers+7h
;****************************************************************
del1 equ flash+1h
del2 equ flash+2h
del3 equ flash+3h ;ram addres at 12h
;*****************************************************************
key_data equ del3+1h
sec equ del3+2h
timer_no equ sec+1h
del4 equ timer_no+1h
key_power equ del4+1h
;*************************************************
time_led1 equ 24h
time_led2 equ time_led1+1
time_led3 equ time_led2+1
led1 equ time_led3+1h
led2 equ led1+1
led3 equ led2+1
;*************************************************
al1_min_t equ led3+1
al1_hour_t equ al1_min_t+1
al2_min_t equ al1_hour_t+1h
al2_hour_t equ al2_min_t+1h
al3_min_t equ al2_hour_t+1h
al3_hour_t equ al3_min_t+1h
;***************************************************
beep_timer equ al3_hour_t+1h
;****************以上进行内存分配*******************
led_1 bit p3.0;P3.0控制最右边的数码管
led_2 bit p3.1;P3.1控制中间的数码管
led_3 bit p3.7;P3.7控制最左边的数码管
;(原理图标的好象有出入,以PCB为准,最左边的数码管是颠反安装的)
HA BIT P3.2;接蜂鸣器
key_f bit p3.3;接按钮公共端
pnp1 bit p3.4;T1路控制输出
pnp2 bit p3.5;T2路控制输出
timer_50ms bit 00h;定时50毫秒标志位
timer_1s bit 01h;定时1秒标志位
timer_3s bit 03h;定时3秒标志位
; timer_20s bit 04h;定时20秒标志位
timer_1min bit 05h;定时1分钟标志位
timer_10min bit 06h;定时10分钟标志位
;timer_am bit 07h
key_set_f bit 08h
sec_flash_f bit key_set_f+1h
;*************************************************************
al1_f bit sec_flash_f+1h
al2_f bit al1_f+1h
al3_f bit al2_f+1h
flash_f bit al3_f+1h
set_f bit flash_f+1h
;*****************************************************************
al_f bit set_f+1h
al_out_f bit al_f+1h
all_s bit al_f+2h
turn_f bit all_s+1h
aal1_f bit turn_f+1h
aal2_f bit turn_f+2h
aal3_f bit turn_f+3h
;*****************************************************************
tih equ 3ch ;9e58h 6MHz
til equ 0b0h ;3cb0h 12MHz
tis equ 10d ;20d 12MHz, 10d 6MHz
ti_no equ 3h
led_blk equ 0ah
led_c equ 0bh
led_e equ 0ch
;主程序开始
oRG 0000H; ;
sJMP start
ORG 0003H; ;
reti;
ORG 000BH ;
reti ;
ORG 0013H
reti ;
ORG 001BH ;
aJMP T1 ;
ORG 0023H ;
RETI; AJMP RS_485 ;
;**************************************
start:
MOV R7,#78h ;
MOV R0,#10H ;
CLR A ;
CR: INC R0 ;
MOV @R0,A ;
djnz r7,cr ;将内存清零
MOV SP,#40H ;
clr ea
clr set_f
clr turn_f
setb pnp2
setb pnp1
;********************************* j al1-3 data
mov r0,#al1_min
mov r1,#al1_min_t
mov r7,#07h
op_4:
mov a,@r0
cjne a,#19d ,op_1
op_2: mov a,#00h
mov @r0,a
sjmp op_3
op_1:
jnc op_2
op_3:
mov a,@r0
mov @r1,a
inc r0
inc r1
djnz r7 ,op_4
mov a,flash
mov c,acc.1 ;acc.1 al1 acc.2 al2 ....
mov al1_f,c
mov c,acc.2 ;acc.1 al1 acc.2 al2 ....
mov al2_f,c
mov c,acc.3 ;acc.1 al1 acc.2 al2 ....
mov al3_f,c
;************************************************
;************** INT_CTRL
; SETB ie0 ;
; SETB EX0 ;
; SETb ex1 ;
setB ET0 ;
SETB ET1 ;
; SETB IT0 ;
; setb IT1 ;
MOV TMOD,#00010001b ;
MOV TH1,#tih
mov tl1,#til
mov th0,#00h
mov tl0,#00h
;***********RS_CTRL
; MOV SCON,#00H ;
;*****************
; MINI_Pragram
;*****************
mov timers,#tis
mov sec,#00d
;mov al1_hour_t ,al1_hour
;mov al1_min_t ,al1_min
;mov al2_hour_t ,al2_hour
;mov al2_min_t ,al2_min
;mov al3_min_t ,al3_min
;mov al3_hour_t,al3_hour
mov timer_no,#ti_no
mov del4,#00h
;mov beep_timer,#8d
mov del1,#00d
mov del2,#00d
mov del3,#00d
mov del4,#00d
mov key_power,#01d
setb al_out_f
setb tr1
setb ea
setb timer_50ms
STAR_BI:
; setb flash_f
acall led_jmp_1;led_times
aCALL KEY_1
acall al_out_way
acall delay
sjmp star_bi
;;;;;;;;;;;;;;;;;
delay:
inc del3
mov a,del3
cjne a,#0ffh,del_1
mov del3,#00h
inc del4
mov a,del4
cjne a,#040h,del_1
mov del4,#00h
clr flash_f
clr set_f
del_1:
ret
;****************************
; key_pragm
;****************************
KEY_1:
mov p1,#00h
mov p3,#0ffh
clr key_f
mov a,p3
setb key_f
anl a ,#10000011b
cjne a,#10000011b,you_key_1
key_e:
mov p3,#0ffh
setb key_f
mov key_data,#00h
key_1_end:
ret
you_key_1:
mov del3,#0feh
mov del4,#0feh
setb key_f
mov b,#20h
lll_1:
push b
lcall led
pop b
djnz b ,lll_1
mov p1,#00h
mov p3,#0ffh
clr key_f
mov a,p3
setb key_f
anl a ,#10000011b
cjne a,#10000011b,you_key_2
sjmp key_e
you_key_2: jb acc.0 ,key_set
mov key_data,#01h
sjmp you_key_33
key_set: jb acc.1 ,you_key_3
mov key_data,#02h
sjmp you_key_33
you_key_3:
jb acc.7 ,you_key_33
mov key_data,#03h
sjmp you_key_33
you_key_33:
setb key_f
acall led
inc del1
mov a,del1
cjne a,#0ffh ,jhg_1
mov del1,#00h
inc del2
mov a,del2
cjne a,#08h ,jhg_1
mov del2,#00h
setb flash_f
setb set_f
; mov key_power,#01h
; mov key_data,#00h
jhg_1:
mov p1,#00h
mov p3,#0ffh
clr key_f
mov a,p3
anl a ,#10000011b
cjne a,#10000011b,you_key_33
setb key_f
mov p3,#0ffh
mov del1,#00h
mov del2,#00h
; acall beep
mov r6,#040h
bb_1:
;!!!!!!!!!!!!!!!!!!!!!!!!!!
clr ha
setb key_f
acall led
acall led
djnz r6 ,bb_1
setb ha
;******************************************
mov dptr,#key_ord_1
mov a,key_data
mov b,#03h;!!!!!!!!!!!!!!!
mul ab
jmp @a+dptr
key_ord_1: ljmp key_k0_0
ljmp key_k1_0
ljmp key_k2_0
ljmp key_k3_0
key_k0_0: ;no key
ajmp key_1_end
key_k1_0: ; key_3
acall key_3
ajmp key_1_end
key_k2_0: ; key_2
acall key_2
ajmp key_1_end
key_k3_0: ; key_1
inc key_power
mov a,key_power
cjne a,#08h ,io_1
mov key_power,#01h
io_1:
ajmp key_1_end
;**************************************
proc key_3
key_3:
mov dptr,#led_ord_3
mov a,key_power ;data
mov b,#02h
mul ab
jmp @a+dptr
led_ord_3: ajmp led_k0_2
ajmp led_k2_2
ajmp led_k1_2
ajmp led_k4_2
ajmp led_k3_2
ajmp led_k6_2
ajmp led_k5_2
ajmp led_k7_2
led_k0_2: ; nop
ajmp led_jmp_5_end
led_k1_2: ; al1 way off/on
cpl al1_f
jb flash_f ,tw_11
sjmp tw_21
tw_11:
mov c,al1_f
mov a,flash
mov acc.1 ,c
mov flash,a
tw_21:
ajmp led_jmp_5_end
led_k2_2: ;al1 timer
jb flash_f ,set_11
inc al1_min_t
mov a,al1_min_t
cjne a,#60 ,set_12
mov al1_min_t,#00h
set_12:
sjmp set_14
set_11:
inc al1_min
mov a,al1_min
cjne a,#60 ,set_14
mov al1_min,#00h
set_14:
ajmp led_jmp_5_end
led_k3_2: ;al2 way off/on
cpl al2_f
jb flash_f ,tw_12
sjmp tw_22
tw_12:
mov c,al2_f
mov a,flash
mov acc.2 ,c
mov flash,a
tw_22:
ajmp led_jmp_5_end
led_k4_2: ;al2 timer
jb flash_f ,set_21
inc al2_min_t
mov a,al2_min_t
cjne a,#60 ,set_22
mov al2_min_t,#00h
set_22:
sjmp set_24
set_21:
inc al2_min
mov a,al2_min
cjne a,#60 ,set_24
mov al2_min,#00h
set_24:
ajmp led_jmp_5_end
led_k5_2: ; al3 way off/on
cpl al3_f
jb flash_f ,tw_13
sjmp tw_23
tw_13:
mov c,al3_f
mov a,flash
mov acc.3 ,c
mov flash,a
tw_23:
ajmp led_jmp_5_end
led_k6_2: ;al3 timer
jb flash_f ,set_31
inc al3_min_t
mov a,al3_min_t
cjne a,#60 ,set_32
mov al3_min_t,#00h
set_32:
sjmp set_34
set_31:
inc al3_min
mov a,al3_min
cjne a,#60 ,set_34
mov al3_min,#00h
set_34:
ajmp led_jmp_5_end
led_k7_2: ; turn off/on
setb turn_f
led_jmp_5_end:
ret
end
;****************************************
proc key_2
key_2:
mov dptr,#led_ord_3
mov a,key_power ;data
mov b,#02h
mul ab
jmp @a+dptr
led_ord_3: ajmp led_k0_2
ajmp led_k2_2
ajmp led_k1_2
ajmp led_k4_2
ajmp led_k3_2
ajmp led_k6_2
ajmp led_k5_2
ajmp led_k7_2
led_k0_2: ; nop
ajmp led_jmp_5_end
led_k1_2: ; al1 way off/on
ajmp led_jmp_5_end
led_k2_2: ;al1 timer
jb flash_f ,set_311
inc al1_hour_t
mov a,al1_hour_t
cjne a,#20d ,set_321
mov al1_hour_t,#00h
set_321:
sjmp set_341
set_311:
inc al1_hour
mov a,al1_hour
cjne a,#20d ,set_341
mov al1_hour,#00h
set_341:
ajmp led_jmp_5_end
led_k3_2: ;al2 way off/on
ajmp led_jmp_5_end
led_k4_2: ;al2 timer
jb flash_f ,set_312
inc al2_hour_t
mov a,al2_hour_t
cjne a,#20d ,set_322
mov al2_hour_t,#00h
set_322:
sjmp set_342
set_312:
inc al2_hour
mov a,al2_hour
cjne a,#20d ,set_342
mov al2_hour,#00h
set_342:
ajmp led_jmp_5_end
led_k5_2: ; al3 way off/on
ajmp led_jmp_5_end
led_k6_2: ;al3 timer
jb flash_f ,set_313
inc al3_hour_t
mov a,al3_hour_t
cjne a,#20d ,set_323
mov al3_hour_t,#00h
set_323:
sjmp set_343
set_313:
inc al3_hour
mov a,al3_hour
cjne a,#20d ,set_343
mov al3_hour,#00h
set_343:
ajmp led_jmp_5_end
led_k7_2: ; turn off/on
led_jmp_5_end:
ret
end
;******************************************** led
proc led_jmp_1
led_jmp_1:
mov dptr,#led_ord_3
mov a,key_power ;data
mov b,#02h
mul ab
jmp @a+dptr
led_ord_3: ajmp led_k0_2
ajmp led_k2_2
ajmp led_k1_2
ajmp led_k4_2
ajmp led_k3_2
ajmp led_k6_2
ajmp led_k5_2
ajmp led_k7_2
led_k0_2: ; nop
setb flash_f
mov a,#89d
mov r7,#18d
acall timer_led1
clr flash_f
ajmp led_jmp_5_end
led_k1_2: ; al1 way off/on
mov led1 ,#0bh
jnb al1_f,aal_1
mov a,#0ch
mov led1,a
aal_1:
mov led3,#1d
mov led2,#led_blk
ajmp led_jmp_5_end
led_k2_2: ;al1 timer
jb flash_f ,led_m11
mov a,al1_min_t
mov r7,al1_hour_t
sjmp led_m21
led_m11:
mov a,al1_min
mov r7,al1_hour
led_m21:
acall timer_led1
ajmp led_jmp_5_end
led_k3_2: ;al2 way off/on
mov led1 ,#0bh
jnb al2_f,aal_2
mov a,#0ch
mov led1 ,a
aal_2:
mov led3,#2d
mov led2,#led_blk
ajmp led_jmp_5_end
led_k4_2: ;al2 timer
jb flash_f ,led_m12
mov a,al2_min_t
mov r7,al2_hour_t
sjmp led_m22
led_m12:
mov a,al2_min
mov r7,al2_hour
led_m22:
acall timer_led1
ajmp led_jmp_5_end
led_k5_2: ; al3 way off/on
mov led1 ,#0bh
jnb al3_f,aal_3
mov a,#0ch
mov led1 ,a
aal_3:
mov led3,#3d
mov led2,#led_blk
ajmp led_jmp_5_end
led_k6_2: ;al3 timer
jb flash_f ,led_m13
mov a,al3_min_t
mov r7,al3_hour_t
sjmp led_m23
led_m13:
mov a,al3_min
mov r7,al3_hour
led_m23:
acall timer_led1
ajmp led_jmp_5_end
led_k7_2: ; turn off/on
mov led3,#00h
mov led2,#0bh;led_b
mov led1,#0bh;led_b
led_jmp_5_end:
acall led
ret
end
timer_led1:
;*******************************min to led
MOV B,#10D
DIV AB
mov led2,a
mov a,b
mov led1,a
;*******************************hour to led
mov a,r7
MOV B,#10D
DIV AB
mov r1,a
mov a,b
mov b,r1
mov r1,a
jnz tt_2
tt_1: mov a,#led_blk
tt_2:
mov led3,a
clr all_s
mov a,b
jz tt_3
setb all_s
mov led3 ,r1
tt_3:
ret
;********************
; LED_PROGRAM
;*******************
LED:
mov c,flash_f
jnc ffl_1
mov c,timer_50ms
jc ffl_1
;mov led1,#led_blk
;mov led2,#led_blk
;mov led3,#led_blk
ajmp led_end
ffl_1:
mov dptr,#led_asc
;***************************led1
mov a,led1
movc a,@a+dptr
mov c, timer_50ms
mov acc.0,c
mov p1,a
clr led_1
acall timer_1ms
setb led_1
;*************************led2
led_led2:
mov a,led2
movc a,@a+dptr
mov p1,a
clr led_2
acall timer_1ms
setb led_2
;*************************led3
led_led3:
mov a,led3
movc a,@a+dptr
mov c,all_s
mov acc.0 ,c
mov p1,a
clr led_3
acall timer_1ms
setb led_3
;*************************
led_end:
mov p1,#0ffh
mov a,p3
orl a,#11111000b
mov p3,a
ret
led_asc:
db 11101110B;0 "bafgecdh"
db 10000100B;1
db 11011010B;2
db 11010110B;3
db 10110100B;4
db 01110110B;5
db 01111110B;6
db 11000100B;7
db 11111110B;8
db 11110110B;9
DB 00000000B;BLACK=>0aH
DB 01111000B;f=>b
dB 11101100B;e=>C
DB 00110000B;1,=>D
;**********************************
;delay
;********************************
timer_1ms:
push b
mov r2,#01h
a_1: mov b,#020h
b_1: djnz b,b_1
djnz r2,a_1
pop b
ret
;******************
; TO-T1_INT T1定时器完成100毫秒定时(6M晶振)
;******************
T1:
setb tr0
PUSH ACC ;程序参数保护
PUSH b ;
push psw
inc time_led1
mov a,time_led1
cjne a,#5d,t1_n1;判断time_led1中是否满5?
mov time_led1,#00h
cpl timer_50ms;满50毫秒后对50毫秒标志位取反
; mov a,timers
; cjne a,#10d ,t1_n11;判断timers中是否满10?
;t1_n12:
; clr timer_1s;满1秒后对1秒标志位取反
sjmp t1_n1
;*********************************
t1_end_2: ajmp t1_end_1
;********************************
;t1_n11: jc t1_n12
; setb timer_1s
t1_n1: djnz timers ,t1_end_2
mov timers,#tis
inc time_led3
mov a,time_led3
cjne a,#03d,t1_sec
mov time_led3,#00h
cpl timer_3s
t1_sec:
inc sec
mov a,sec
cjne a,#60d ,t1_end
mov sec,#00d
jnb al1_f ,t1_al1
mov a,al1_min_t
mov b,al1_hour_t
mov c ,aal1_f
acall al_all
mov al1_min_t,a
mov al1_hour_t,b
mov aal1_f,c
t1_al1:
jnb al2_f ,t1_al2
mov a,al2_min_t
mov b,al2_hour_t
mov c ,aal2_f
acall al_all
mov al2_min_t,a
mov al2_hour_t,b
mov aal2_f,c
t1_al2:
jnb al3_f ,t1_al3
mov a,al3_min_t
mov b,al3_hour_t
mov c ,aal3_f
acall al_all
mov al3_min_t,a
mov al3_hour_t,b
mov aal3_f,c
t1_al3:
T1_END:
t1_end_1:
;************************************ sub t0 timer
clr c
clr tr0
mov b,timer_no
mov a,#til
add a,tl0
addc a,b ;*******have 5 order
mov tl1 ,a
mov a,#tih
addc a, th0
mov th1,a
mov th0,#00h
mov tl0,#00h
setb tr1
pop psw
pop b ;
pop acc ;
RETI ;
;************************************
beep:
clr ha
mov r6,#060h
bee_2: mov r4,#0ffh
bee_1:
djnz r4 ,bee_1
djnz r6,bee_2
setb ha
ret
;***************************************
stop:
mov a,#0ffh
mov p1,a
mov p3,a
mov a,pcon
orl a,#00000010b
mov pcon,a
ret
al_out_way:
jnb aal1_f ,al_out1
mov r3,#0ffh
pnp_3:
mov b,#060h
pnp_4:
push b
acall led_jmp_1;led_times
mov c,timer_50ms
mov ha ,c
pop b
djnz b ,pnp_4
djnz r3 ,pnp_3
mov c,al2_f
orl c,al3_f
jc al_out54
acall stop
al_out54:
clr aal1_f
clr al1_f
sjmp al_out4
al_out1:
jnb aal2_f ,al_out2
clr pnp1
mov r3,#0ffh
pnp_1:
mov b,#020h
pnp_2:
push b
acall led_jmp_1;led_times
pop b
djnz b ,pnp_2
djnz r3 ,pnp_1
setb pnp1
mov c,al1_f
orl c,al3_f
jc al_out64
acall stop
al_out64:
clr aal2_f
clr al2_f
sjmp al_out4
al_out2:
jnb aal3_f ,al_out3
clr pnp2
al_out3:
jnb turn_f ,al_out4
acall stop
al_out4:
ret
;*******************************************
al_all:
jc al_all_1
dec a
jnz al_all_1
mov a,b
jz al_all_3
dec b
sjmp al_all_2
al_all_3:
setb c
sjmp al_all_1
al_all_2:
mov a,#60d
al_all_1:
ret
;看懂别人写的汇编程序是很困难的事情,我希望有兴趣的网友能帮我完成这段程序的中文注释,这样可以使初学者能够更加容易学会编程.
这是上面程序汇编以后获得的编程器烧写文件:timer.hex
下载烧写文件的方法:将鼠标移到烧写文件的超级键连处,点击鼠标的右键,选择“目标另存为(A)...”保存文件类型中不要选择“文本文件”,应该选择“所有文件”,这样就能获得*.hex文件了。
相关键连:AT89C2051的智能时钟
相关键连:MEAPC可自编程嵌入式控制器
相关键连:AT89C2051的倒计时器
AT89C2051的倒计时器缺货,停止供货
汇款地址:226200
江苏省启东市城河新村103楼401房间 谢刚 收
点击查看邮购需知! 立即进入订货系统!
电话/传真:0513-3342087
小灵通:0513-3082087 联系人:谢刚
主邮箱:
(推荐使用)
点击返回《电子制作实验室》网站主页
|