单片机课程设计时间控制器.docx
《单片机课程设计时间控制器.docx》由会员分享,可在线阅读,更多相关《单片机课程设计时间控制器.docx(22页珍藏版)》请在冰豆网上搜索。
单片机课程设计时间控制器
单片机课程设计
——时间控制器
一.设计要求:
设计一个以AT89C52为核心的时间控制器。
可以实现时间显示和设置闹钟功能。
用89S52单片机设计一个时间控制器,有8位数码管显示年月日及时分秒,具有日历和时钟功能,有一路输出控制电灯,有一路输出控制蜂鸣器,可以设置年月日及时分秒,可以设置一张时间表,该时间表记录什么时间开、关电灯,什么时间开、关蜂鸣器,而单片机按这张时间表来控制电灯和蜂鸣器。
该时间表要求掉电时仍能保存不掉数据,所以需要用一片EEPROM来保存数据,如用24C02/04/08等。
二.设计原理:
在AT80C52单片机内部对机器周期进行计数,从而得到定时。
由于AT89C52的内部计数器是16位的,振荡脉冲频率为12MHZ,这样一个机器周期为1μs,计满一次为65536μs。
显示器选用8位共阳极数码管,分别表示时、分、秒。
同时选用两个74HC273作为片选和位选锁存器。
同时经过P0口外加四个键盘用以调整时间。
软件部分分为四大模块:
调整初始时间、设置闹铃时间实现闹铃功能、显示时间和脉冲计数模块。
脉冲计数模块,通过AT89C52的T0进行计数,设置T0计数溢出时,由内部硬件置位,同时开放中断,使系统进入中断服务程序。
在定时器运行前,在其中先预置入计数初值,为了计算方便,在次设置中预置的初值为(65536-1000),这样一次“硬件计时周期”就可定时1000μs。
在中断服务程序设置进行1000次“硬件计时周期”就可得到1S。
同理进行分、小时的计时。
三.流程图:
四.设计原理图:
五.程序清单:
SYSTEMequ0
NMLIGHTONequ6
NMLIGHTOFFequ7
NMALARMONequ8
ANALARMOFFequ9
YEAR1equ10H
YEAR2equ11H
YEAR3equ12H
YEAR4equ13H
MONTH1equ14H
MONTH2equ15H
DAY1equ16H
DAY2equ17H
HOUR1equ18H
HOUR2equ19H
MINUTE1equ1AH
MINUTE2equ1BH
SECOND1equ1CH
SECOND2equ1DH
WEEKDAY1equ1EH
WEEKDAY2equ1FH
;timetable
LIGHTONTequ100
LIGHTOFFequ200
ALARMONequ300
ALARMOFFequ400
org00h
jmpstart
org03h
jmpInt0Proc
org1BH
jmpT1Proc
org13H
jmpInt1Proc
org30h
start:
movDPTR,#SYSTEM
movxA,@DPTR
jnzNext3;havevalueinexternalROM
movA,#01H
movx@DPTR,A
callInit
movR4,#1;firstdisplayDate
clrC;LEDturnoffinthefirst
movP3.0,C
Next3:
movTL1,#9cH
movTH1,#0ffH
movTMOD,#11H;Timer1mode1
setbTR1
setbET1
setbIT0;smarthere
setbIT1
setbPX0
setbEX0
setbEX1
setbEA
SECOND:
movR5,#2
callWaitSecond
cplP3.1
jbP1.3,Next7
callSetDate
Next7:
jbP1.4,Next8
callSetTime
Next8:
movDPTR,#SECOND2
movxA,@DPTR
incA
movx@DPTR,A;secondsincrease1,thenwritetoROM
xrlA,#10
jnzSECOND
movA,#0
movDPTR,#SECOND2
movx@DPTR,A;clearSECOND2
movDPTR,#SECOND1
movxA,@DPTR
incA
movx@DPTR,A;incSECOND1
movxA,@DPTR
xrlA,#6
jnzSECOND
movA,#0
movx@DPTR,A
MINUTE:
movDPTR,#MINUTE2
movxA,@DPTR
incA
movx@DPTR,A;writetoROM
movxA,@DPTR
xrlA,#10;BCDcode
jnzSECOND
movA,#0
movx@DPTR,A
movDPTR,#MINUTE1
movxA,@DPTR
incA
movx@DPTR,A;writetoROM
movxA,@DPTR
xrlA,#6
jnzSECOND
movA,#0
movx@DPTR,A
HOUR:
movDPTR,#HOUR2
movxA,@DPTR
incA
movx@DPTR,A;writetoROM
movxA,@DPTR
xrlA,#10;BCDcode
jnzNext4
movA,#0
movx@DPTR,A
movDPTR,#HOUR1
movxA,@DPTR
incA
movx@DPTR,A;writetoROM
jmpSECOND
Next4:
movDPTR,#HOUR1
movxA,@DPTR
movB,A;savehour1toB
movA,#10
mulAB
movR0,A;savetheresulttoR0
movDPTR,#HOUR2
movxA,@DPTR
movR1,A;savehour2toR1
movA,B;restoretheresult
addA,R1
xrlA,#24
jnzSECOND
movA,#0
movDPTR,#HOUR2
movx@DPTR,A;resetHOUR2
movDPTR,#HOUR1
movx@DPTR,A
mpSECOND
T1Proc:
movTH1,#0FfH
movTL1,#9cH
cplP3.1
reti
Int0Proc:
pushDPL
pushDPH
pushACC
clrEA
movA,R4
jnzNext5
callWriteTime
jmpReturn3
Next5:
callWriteDate
Return3:
setbEA
popACC
popDPH
popDPL
reti
Int1Proc:
pushACC
pushB
clrEA
incR6;R6tomarkDate/TimeontheLED
movA,R6
movB,#3
divAB
movA,B;judgetheremainder
jzNext6
movR4,#0
jmpReturn5
Next6:
movR4,#1H
Return5:
setbEA
popB
popACC
reti
WriteTime:
incR7;R7togeneratescansignalofLED
movA,R7
xrlA,#8
jnzI0
movR7,#0
I0:
movA,R7
xrlA,#0
jnzI1
callWriteHour1
jmpReturn4
I1:
movA,R7
xrlA,#1
jnzI2
callWriteHour2
jmpReturn4
I2:
movA,R7
xrlA,#2
jnzI3
callWriteMinute1
jmpReturn4
I3:
movA,R7
xrlA,#3
jnzI4
callWriteMinute2
jmpReturn4
I4:
movA,R7
xrlA,#4
jnzI5
callWriteSECOND1
jmpReturn4
I5:
movA,R7
xrlA,#5
jnzI6
callWriteSECOND2
jmpReturn4
I6:
movA,R7
xrlA,#6
jnzI7
callWriteWeekday1
jmpReturn4
l7:
movA,R7
xrlA,#7
jnzReturn4
callWriteWeekday2
Return4:
ret
WriteDate:
incR7
movA,R7
xrlA,#8
jnzIS0
movR7,#0
IS0:
movA,R7
xrlA,#0
jnzIS1
callWriteYear1
jmpReturn1
IS1:
movA,R7
xrlA,#1
jnzIS2
callWriteYear2
jmpReturn1
IS2:
movA,R7
xrlA,#2
jnzIS3
callWriteYear3
jmpReturn1
IS3:
movA,R7
xrlA,#3
jnzIS4
callWriteYeaR5
jmpReturn1
IS4:
movA,R7
xrlA,#4
jnzIS5
callWriteMonth1
jmpReturn1
IS5:
movA,R7
xrlA,#5
jnzIS6
callWriteMonth2
jmpReturn1
IS6:
movA,R7
xrlA,#6
jnzIS7
callWriteDay1
jmpReturn1
IS7:
movA,R7
xrlA,#7
jnzReturn1
callWriteDay2
Return1:
ret
WriteYear1:
movDPTR,#YEAR1
movxA,@DPTR
anlP1,#11111000B;P1=0
movP2,A
ret
WriteYear2:
movDPTR,#YEAR2
movxA,@DPTR
orlP1,#00000001B;P1=1
movP2,A
ret
WriteYear3:
movDPTR,#YEAR3
movxA,@DPTR
xrlP1,#00000011B;P1=2
movP2,A
ret
WriteYeaR5:
movDPTR,#YEAR4;P1=3
movxA,@DPTR
orlP1,#00000001B
movP2,A
ret
WriteMonth1:
movDPTR,#MONTH1
movxA,@DPTR
xrlP1,#00000111B;P1=4
movP2,A
ret
WriteMonth2:
movDPTR,#MONTH2
movxA,@DPTR
orlP1,#00000001B;P1=5
movP2,A
ret
WriteDay1:
movDPTR,#DAY1
movxA,@DPTR
xrlP1,#00000011B;P1=6
movP2,A
ret
WriteDay2:
movDPTR,#DAY2
movxA,@DPTR
orlP1,#00000001B;P1=7
movP2,A
ret
WriteHour1:
movDPTR,#HOUR1
movxA,@DPTR
anlP1,#11111000B;P1=0
movP2,A
ret
WriteHour2:
movDPTR,#HOUR2
movxA,@DPTR
orlP1,#00000001B;P1=1
movP2,A
ret
WriteMinute1:
movDPTR,#MINUTE1
movxA,@DPTR
xrlP1,#00000011B;P1=2
movP2,A
ret
WriteMinute2:
movDPTR,#MINUTE2
movxA,@DPTR
orlP1,#00000001B;P1=3
movP2,A
ret
WriteSECOND1:
movDPTR,#SECOND1
movxA,@DPTR
xrlP1,#00000111B;P1=4
movP2,A
ret
WriteSECOND2:
movDPTR,#SECOND2
movxA,@DPTR
orlP1,#00000001B;P1=5
movP2,A
ret
WriteWeekday1:
movDPTR,#WEEKDAY1
movxA,@DPTR
xrlP1,#00000011B;P1=6
movP2,A
ret
WriteWeekday2:
movDPTR,#WEEKDAY2
movxA,@DPTR
orlP1,#00000001B;P1=7
movP2,A
ret
Init:
movA,#2
movDPTR,#YEAR1
movx@DPTR,A
movA,#0
movDPTR,#YEAR2
movx@DPTR,A
movA,#0
movDPTR,#YEAR3
movx@DPTR,A
movA,#8
movDPTR,#YEAR4
movx@DPTR,A
movA,#0
movDPTR,#MONTH1
movx@DPTR,A
movA,#1
movDPTR,#MONTH2
movx@DPTR,A
movA,#0
movDPTR,#DAY1
movx@DPTR,A
movA,#1
movDPTR,#DAY2
movx@DPTR,A
movA,#1
movDPTR,#HOUR1
movx@DPTR,A
movA,#7
movDPTR,#HOUR2
movx@DPTR,A
movA,#1
movDPTR,#MINUTE1
movx@DPTR,A
movA,#6
movDPTR,#MINUTE2
movx@DPTR,A
movA,#1
movDPTR,#SECOND1
movx@DPTR,A
movA,#0
movDPTR,#SECOND2
movx@DPTR,A
movA,#0
movDPTR,#WEEKDAY1
movx@DPTR,A
movA,#2
movDPTR,#WEEKDAY2
movx@DPTR,A
ret
SetTime:
clrEX1;forbidinterrupt1
movR4,#00H;showtimealways
movA,#0
movDPTR,#SECOND2
movx@DPTR,A;clearseconds
movDPTR,#SECOND1
movx@DPTR,A
settime1:
jbP1.4,Return7
jbP1.5,Next9
callSetHour;P1.5=0,keypress
Next9:
jbP1.6,settime1
callSetMinute;P1.6=0,keypress
jmpsettime1
Return7:
setbEX1
ret
SetMinute:
movDPTR,#MINUTE2
movxA,@DPTR
incA
movx@DPTR,A;increaseMinute2
xrlA,#10
jnzJ
movA,#0
movx@DPTR,A
SetYear:
movDPTR,#YEAR4
movxA,@DPTR
incA
movx@DPTR,A
xrlA,#10
jnzJ5
movA,#0
movx@DPTR,A
movDPTR,#YEAR3
movxA,@DPTR
incA
movx@DPTR,A
xrlA,#10
jnzJ5
movA,#0
movx@DPTR,A
movDPTR,#YEAR2
movxA,@DPTR
incA
movx@DPTR,A
xrlA,#10
jnzJ5
movA,#0
movx@DPTR,A
movDPTR,#YEAR1
movxA,@DPTR
incA
movx@DPTR,A
xrlA,#10
jnzJ5
movA,#0
movx@DPTR,A
J5:
jbP1.5,Return13;waitthekeytorelease
jmpJ5
Return13:
ret
WaitSecond:
movA,R5
jzReturn6
WaitSecond1:
movTL0,#0B0H;50000(0xc350)ticksbeforeoverflow=0.5sat1.2MHzClock
movTH0,#3cH
movA,TMOD
orlA,#00000001B
movTMOD,A;Timer0mode1
setbTR0
;Timer0runs
WaitSecond2:
jnbTF0,WaitSecond2
clrTR0;Timer0stops(nointerrupt,can'tautoclear,sosoftwareclear)
clrTF0
decR5
movA,R5
jnzWaitSecond1
Return6:
ret
END