专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

基于AT89S52和HD7279A的多功能电子钟

作者:浪子   来源:浪子   点击数:  更新时间:2014年06月10日   【字体:

功能:整点报时,时分秒调整,闹钟的设置,闹钟开关,万年历及其调整,闰年

 

判断,秒表,芯片:AT89S52,HD7279A

切换到年月日显示当前日期

将日期调整到2008年验证闰年

 

PCB图

 

 系统原理图

 

计时开始,黄灯为闹钟就绪灯

 

 

 

万年历从0000年可到9999年

 

59分59秒了,整点报时灯红灯准备亮起

红灯亮了,整点到了,30S后整点报时灯(红)自动熄灭

下一步验证闹钟,闹钟就绪灯(黄)为开,当前闹钟时间为01时01分

闹钟灯(绿)亮了,其实还有一个蜂鸣器在响,关掉闹钟,黄灯和绿灯都被关掉,若不关闭则一分钟后自动关闭,但是闹钟就绪灯不会自动关闭,下图为手动关闭

秒表准备

秒表

秒表停止,计时1S90MS

 

ORG  0000H
     LJMP   MAIN
ORG  000BH
     LJMP     CLOCK
ORG  001BH
     LJMP     BRT
ORG  0030H
KEY BIT  P1.4
CLK BIT  P1.5
DAT BIT  P1.6
CS  BIT  P1.7
                   ;说明:P1.0为正点报时标志,在正点时亮器,半分钟后自动关闭,
                   ;P1.1为闹钟开关标志,可由键盘设置 ,初始状态为开,亮30秒
                   ;P1.3,P1.2为闹钟,一分钟后自动关闭,亮的前提是闹钟开关打开
                   ;P1.4为KEY,P1.7CS
                   ;可通过在正常显示状态下按3键随时关闭闹铃
                   ;在走时状态下按1键进入时间调整,按3小时加1,按4时加1,再按1切换到分,按3加1,按4减1,按F返回
                   ;在时钟状态下,按2进入闹钟设置,其它同时间调整
                   ;在时钟状态下,按3进入闹钟开关,可随时开关闹铃?若不关则1分钟后  自动关闭
                   ;在时钟状态下按C进入秒表,按D开始,按E停止,按c清零,再按D可重新开始,按f退出秒表
                   ;在时钟状态下按0显示年月日,按D进入日期调整,可自动识别闰年。
MAIN: MOV     SP,#60H
      MOV     TMOD,#11H           ;定时器0,1工作在方式1
      MOV     IE,#8AH               ;开定时器中断
      MOV     TH1,#0D8H
      MOV     TL1,#0F0H
      MOV     TL0,#0B0H             ;定时器地低位初始化
      MOV     TH0,#3CH              ;定时器高位初始化
      MOV     2AH,#14H              ;年高位
      MOV     2BH,#09H              ;年低位
      MOV     2CH,#05H              ;月
      MOV     2DH,#19H              ;日
      MOV     20H,#17H              ;时单位
      MOV     21H,#3BH              ;分单元
      MOV     22H,#30H              ;秒单元
      MOV     23H,#00H              ;定时器计数单元
      MOV     50H,#00H              ;闹钟时位
      MOV     51H,#00H              ;闹钟分位
      MOV     52H,#00H              ;闹钟秒位
      SETB    TR0                   ;开始定时
      CLR     P1.1                  ;闹铃状态开关
      SETB    P1.0                 ;关整点报时灯
      SETB    P1.3                  ;闹铃关
      SETB    P1.2
       ;*******************初始化完毕********************************
SS:LCALL CUTIN0
   LCALL DISP
   JB KEY,LK
   LCALL READ
   LCALL  LDELAY
   MOV A,28H
LP1:JNB  KEY,LP1
   CJNE A,#01H,NAO           ;按1键进入时间设置
   LCALL KEY0
   SJMP  LK
NAO:CJNE A,#02H,SWITCH             ;按2键键入闹钟设置
    LCALL  LDELAY
    LCALL  KEY1
    SJMP  LK
SWITCH:CJNE A,#03H,MB               ;按3键进入闹钟开关
       MOV C,P1.1
       CPL C
       LCALL  LONG
       MOV P1.1,C
       SJMP LK
MB:  CJNE A,#0CH,DATE               ;按C键进入秒表
     LCALL MBWAIT                   ;按0键进入年月日显示
       SJMP  LK
DATE: CJNE  A,#00H,SETD
 D1:  LCALL CUTIN2
      LCALL DISPD
      LCALL SHORT
      JB KEY,D1
      LCALL  READ
      MOV A,28H
      CJNE  A,#0FH,D1
      MOV 29H,#98H
      LCALL SEND
      MOV 29H,#03FH
      CALL SEND
      SJMP  SS
SETD:CJNE    A,#0DH,LK           ;按D键进入年月日调整
     LCALL     KEY2
   LK:SJMP  SS
CUTIN0: MOV     25H,R0
        MOV     R0,#78H
        MOV     A,22H
        ACALL   BCD
        MOV     A,21H
        ACALL   BCD
        MOV     A,20H
        ACALL   BCD
        MOV     R0,25H
        RET
CUTIN1: MOV     25H,R0
        MOV     R0,#78H
        MOV     A,42H
        ACALL   BCD
        MOV     A,41H
        ACALL   BCD
        MOV     A,40H
        ACALL   BCD
        MOV     R0,25H
        RET
CUTIN2:MOV 25H,R0
       MOV R0,#78H;78H中为日个位
       MOV A , 2DH
       ACALL BCD
       MOV A ,2CH
       ACALL BCD
       MOV A,2BH
       ACALL BCD
       MOV A,2AH
       ACALL BCD
       MOV  R0,25H;7FH中为年千?
       RET
BCD:    MOV     B,#0AH        ;把数据调整为两位BCD码
        DIV     AB
        MOV     @R0,B
        INC     R0
        MOV     @R0,A
        INC     R0
        RET
CLOCK:  MOV     TL0,#0B0H     ;分秒单元进位
        MOV     TH0,#3CH
        PUSH    PSW
        PUSH    ACC
        INC     23H
        MOV     A,23H
        CJNE    A,#14H,RET0
        MOV     23H,#00H
        INC     22H
        JNB     P1.1,NZD
        SETB    P1.2
        SETB    P1.3
NZD:    MOV     A,21H
        CJNE    A,#00H,CLO
OPEN:   MOV     A,22H
        CJNE    A,#1EH,ZD        ;正点报时半分钟后关闭
        SETB    P1.0
ZD:     CJNE    A,#3CH,RET0
        MOV     22H,#00H
        INC     21H
        SETB     P1.2             ;闹1分钟后钟关
        SETB     P1.3
        MOV     A,21H
        CJNE    A,#3CH,RET0
        MOV     21H,#00H
        CLR     P1.0
        INC     20H
        MOV     A,20H
        CJNE    A,#18H,RET0
        MOV     20H,#00H
        MOV     A,2CH
        CJNE    A,#02H,OTHERMON
        LCALL YEAR
        CJNE    A,#00H,NEXT7
OTHERMON:LCALL DAYS
         CJNE  A,2DH,NEXT6
         MOV   2DH,#01H
         SJMP  ADM
  CLO:   SETB   P1.0
         SJMP  OPEN
NEXT6:   INC   2DH
         SJMP  B0
NEXT7:   MOV   A,2DH
         CJNE  A,#1DH,NEXT6
         MOV   2DH,#01H
ADM:     INC   2CH
         MOV   A,2CH
         CJNE  A,#0DH,B0
         MOV   2CH,#01H
         INC   2BH
         MOV   A,2BH
         CJNE  A,#64H,B0
         MOV   2BH,#00H
         INC   2AH
         MOV   A,2AH
         CJNE  A,#64H,B0
         MOV   2AH,#00H
RET0:   JB      P1.1, B0
        MOV     A,20H           ;开关为开时判断时间,为关则直接退出中断
        CJNE    A,50H,B0
        MOV     A,21H
        CJNE    A,51H,B0
        CLR    P1.3         ;开闹钟
        CLR    P1.2
B0:     POP     ACC
        POP     PSW
FH:     RETI
YEAR:   MOV     A,2BH
        MOV     B,#04H
        DIV     AB
        MOV     A,B
        CJNE    A,#00H,NL
        MOV     A,2BH
        MOV     B,#64H
        DIV     AB
        MOV     A,B
        CJNE    A,#00H,LEAP
        MOV     A,2AH
        MOV     B,#04H
        DIV     AB
        MOV     A,B
        CJNE    A,#00H,NL
LEAP:   MOV     A,#01H
        SJMP    BACKY
NL:     MOV     A,#00H
BACKY:  RET
              ;****************************每月天数(按平年计)***************************
DAYS:   DEC     2CH
        MOV     A,2CH
        MOV     DPTR,#TAB
        MOVC    A,@A+DPTR
        INC     2CH
        RET
 TAB:DB 1FH,1CH,1FH,1EH,1FH,1EH
     DB 1FH,1FH,1EH,1FH,1EH,1FH
      ;*********年月日调整************1键切换,3键+1,4键-1,F键返回****************
KEY2:  LCALL CUTIN2
       LCALL DISPD
        MOV   29H,#88H
        LCALL SEND
        MOV   29H,#3FH
        LCALL SEND
        JB    KEY,KEY2
        LCALL READ
LP2:    JNB   KEY,LP2
        LCALL  LDELAY
        MOV   A,28H
SYH2:   CJNE  A,#02H,SYH3
        INC   2AH
        MOV   A,2AH
        CJNE  A,#64H,KEY2
        MOV   2AH,#00H
        SJMP  KEY2
SYH3:   CJNE  A,#03H,YLK1
        DEC   2AH
        MOV   A,2AH
        CJNE  A,#0FFH,KEY2
        MOV   2AH,#63H
        SJMP  KEY2
YLK1:   CJNE  A,#01H,KEY2
YLK:    LCALL   CUTIN2
        LCALL    DISPD
        MOV   29H,#88H
        LCALL SEND
        MOV    29H,#0CFH
        LCALL SEND
        LCALL  SHORT
        LCALL  LDELAY
        JB    KEY,YLK
        LCALL  READ
LP3:    JNB   KEY,LP3
        LCALL  LDELAY
        MOV    A,28H
SYL2:   CJNE   A,#02H,SYL3
        INC    2BH
        MOV    A,2BH
        CJNE   A,#64H,YLK
        MOV    2BH,#00H
        SJMP   YLK
SYL3:   CJNE   A,#03H,MK1
        DEC    2BH
        MOV    A,2BH
        CJNE   A,#0FFH,YLK
        MOV    2BH,#63H
        LCALL  SHORT
        SJMP   YLK
MK1 :   CJNE   A,#01H,YLK
SM:     LCALL DISPD
        MOV   29H,#88H
        LCALL SEND
        MOV   29H,#0F3H
        LCALL SEND
        LCALL   CUTIN2
        LCALL DISPD
        JB     KEY,SM
        LCALL  READ
LP4:    JNB    KEY,LP4
        LCALL  LDELAY
        MOV    A,28H
SETM2:  CJNE   A,#02H,SETM3
        INC    2CH
        MOV    A,2CH
        CJNE   A,#0DH,SM
        MOV    2CH,#01H
        SJMP   SM
SETM3:  CJNE   A,#03H,DK1
        DEC    2CH
        MOV    A,2CH
        CJNE   A,#00H,SM
        MOV    2CH,#0CH
        SJMP   SM
DK1:    CJNE   A,#01H,SM
DK:     LCALL  CUTIN2
        LCALL DISPD
        MOV   29H,#88H
        LCALL   SEND
        MOV     29H,#0FCH
        LCALL   SEND
        JB      KEY,DK
        LCALL   READ
        LCALL   LDELAY
LP5:    JNB     KEY,LP5
        MOV     A,28H
SD2:    CJNE    A,#02H,SD3
        MOV     A,2CH
        CJNE    A,#02H,OTHER1
        LCALL   YEAR
        CJNE    A,#01H,OTHER1
        INC     2DH
        MOV     A,2DH
        CJNE    A,#1EH,DK
        MOV     2DH,#01H
        SJMP    DK
OTHER1: LCALL   DAYS
        CJNE    A,2DH,AD
        MOV     2DH,#01H
        SJMP    DK
AD:     INC     2DH
        SJMP    DK
SD3:    CJNE    A,#03H,FK0
        DEC     2DH
        LCALL   SHORT
        MOV     A,2DH
        CJNE    A,#00H,DK
        MOV     A,2CH
        CJNE    A,#02H,OTHER2
        LCALL   YEAR
        CJNE    A,#01H,NEXT4
        MOV     2DH,#1DH
        LCALL  SHORT
        SJMP    DK
NEXT4:  MOV     2DH,#1CH
DBK:    LJMP  DK
OTHER2: LCALL   DAYS
        MOV     2DH,A
        LJMP    DK
FK0:    CJNE    A,#01H,FK
        LJMP    KEY2
FK:     CJNE    A,#0FH,DBK
        MOV     29H,#88H
        LCALL   SEND
        MOV     29H,#0FFH
        LCALL   SEND
        MOV     29H,#98H
        LCALL  SEND
        MOV   29H,#03FH
        LCALL  SEND
        RET
       ;************秒表*********

MBWAIT:     MOV 78H,#00H             ;D键开始
            MOV 79H,#00H             ; E键停止
            MOV 7AH,#00H              ; 按C键清零
            MOV 7BH,#00H              ; 再按D键重新开始
            MOV 7CH,#00H               ;F键退出
            MOV 7DH,#00H
            MOV 7EH,#00H
            MOV 7FH,#00H
            LCALL  DISPD
            JB   KEY,MBWAIT
            LCALL  READ
            MOV  A,28H
            CJNE  A,#0DH,MBWAIT
KAISHI:     SETB TR1  ;开启定时器T1
WAIT0:    LCALL DISPD
          JB  KEY,WAIT0  ; 等按键,按E停止
          LCALL  READ
          MOV A,28H
          CJNE A,#0EH,WAIT0
          CLR  TR1
WAIT1:    LCALL  DISPD
          JB   KEY,WAIT1          ;按F键清零
          LCALL  READ
          MOV  A,28H
          CJNE A,#0CH,WAIT1
          MOV  78H,#00H
          MOV  79H,#00H
          MOV  7AH,#00H
          MOV  7BH,#00H
          MOV  7CH,#00H
          MOV  7DH,#00H
          MOV  7EH,#00H
          MOV  7FH,#00H
WAIT2:    LCALL  DISPD
          JB  KEY,WAIT2
          LCALL  READ
          MOV  A,28H
          CJNE A,#0DH,WAIT3         ; 按D键重新开始,其他键退出秒表
          SJMP  KAISHI
WAIT3:    CJNE A,#0FH,WAIT2           ;按E键退出秒表
EXIT:     MOV  29H,#98H
          LCALL  SEND
          MOV   29H,#3FH
          LCALL   SEND
              RET
DISPM:CALL DISP
      MOV   29H,#88H
      LCALL  SEND
      MOV   29H,#0F3H
      LCALL  SEND
      RET
DISPH:CALL  DISP
      MOV    29H,#88H
      LCALL   SEND
      MOV    29H,#0CFH
      LCALL   SEND
      RET
DISPD:MOV   29H,#98H
      LCALL   SEND
      MOV    29H,#0FFH
      LCALL    SEND
      LCALL DISP
      MOV 29H,#0CEH
      CALL SEND
      MOV  29H,7EH
      ACALL SEND
      MOV 29H,#0CFH
      LCALL SEND
      MOV  29H,7FH
      LCALL SEND
      RET
DISP:MOV R1,#78H
     MOV R4,#0C8H
     MOV R2,#06
DISP_LOOP:MOV 29H,R4
          LCALL SEND
          MOV 29H,@R1
          LCALL SEND
          INC R4
          INC R1
          DJNZ R2,DISP_LOOP
          RET
SEND:     MOV R3,#08H
          CLR CS
          LCALL LONG
SEND_LOOP:MOV C,29H.7
          MOV DAT,C
          SETB CLK
          LCALL SHORT
          CLR CLK
          LCALL SHORT
          MOV A,29H
          RL A
          MOV 29H,A
          DJNZ R3,SEND_LOOP
          CLR DAT
          RET
BRT:      MOV     TL1,#0F0H
          MOV     TH1,#0D8H     ;初值,10ms中断一次
          PUSH    PSW
          PUSH    ACC
          INC 78H
          MOV A,78H
          CJNE A,#0AH,FH1
          MOV  78H,#00H
          INC  79H
          MOV A,79H
          CJNE A,#0AH,FH1
          MOV  79H,#00H
          INC  7AH
          MOV A,7AH
          CJNE A,#0AH,FH1
          MOV  7AH,#00H
          INC  7BH
          MOV A,7BH
          CJNE A,#06H,FH1
          MOV  7BH,#00H
          INC  7CH
          MOV A,7CH
          CJNE A,#0AH,FH1
          MOV  7CH,#00H
          INC  7DH
          MOV A,7DH
          CJNE A,#06H,FH1
          MOV  7DH,#00H
          INC  7EH
          MOV  A,7EH
          CJNE  A,#0AH,FH1
          INC   7FH
          MOV   A,7FH
          CJNE   A,#0AH,FH1
          MOV    7FH,#00H
FH1:            POP  ACC
                POP PSW
                RETI
LONG:           MOV  70H,#25    ; 50uS
DELAY_LOOP:     DJNZ  70H,DELAY_LOOP
                RET
SHORT:          MOV  70H,#4    ; 8uS
SHORT_LP:       DJNZ 70H,SHORT_LP
                RET
READ:           MOV 29H,#15H                      ;发送读键盘指令
  ACALL SEND
                LCALL LONG
                LCALL RESE
                LCALL LONG
                RET
RESE:           MOV 71H,#8   ; =8
                SETB DAT   ; P1.2(DATA)
                CALL LONG  ;
RECEIVE_LOOP:   SETB CLK ; CLK
                CALL SHORT  ;
                MOV A,28H  ;
                RL A
                MOV 28H,A
                MOV C,DAT   ;
                MOV 28H.0,C
                CLR CLK   ; CLK
                CALL SHORT
                DJNZ 71H,RECEIVE_LOOP; 8
                CLR DAT ; DAT
                RET
SKEY:MOV  42H,#00H
    LCALL  CUTIN1
    LCALL  DISPH
    JB  KEY,SKEY
    LCALL READ
    LCALL  LDELAY
LP6:JNB  KEY,LP6
    MOV A,28H
HK2:CJNE A,#02H,HK3
    INC  40H
    MOV  A,40H
    CJNE A,#18H,NEXT0
    MOV  40H,#00H
NEXT0:AJMP SKEY
HK3: CJNE A,#03H,K1
     DEC 40H
     MOV A,40H
     CJNE A,#0FFH,NEXT1
     MOV  40H,#17H
NEXT1:AJMP SKEY
K1:CJNE A,#01H,NEXT1
TURNTO:LCALL CUTIN1
       LCALL DISPM
       JB KEY,TURNTO
       LCALL READ
       LCALL  LDELAY
LP7:   JNB  KEY,LP7
       MOV A,28H
MK2:   CJNE A,#02H,MK3
       INC 41H
       MOV A,41H
       CJNE A,#3CH,TURNTO
       MOV 41H,#00H
NEXT2: LCALL SHORT
       AJMP TURNTO
MK3:   CJNE A,#03H,F4
       DEC 41H
       LCALL SHORT
       MOV A,41H
       CJNE A,#0FFH,TURNTO
       MOV 41H,#3BH
       AJMP  TURNTO
F4:    CJNE   A,#01H,BACK
       AJMP  SKEY
BACK:  CJNE A,#0FH,TURNTO
       MOV 29H,#88H
       LCALL SEND
       MOV 29H,#0FFH
       LCALL SEND
       RET
       ;************校时子程序**********
KEY0:  MOV 41H,21H
       MOV 40H,20H
       CLR TR0
       MOV  23H,#00H
       ACALL SKEY
       MOV 22H,42H
       MOV 21H,41H
       MOV 20H,40H
       MOV TH0,#3CH
       MOV TL0,#0B0H
       SETB TR0
       RET
KEY1:  MOV 41H,51H     ;************闹钟设置子程序************
       MOV 40H,50H
       ACALL SKEY
       MOV 51H,41H
       MOV 50H,40H
       SETB  P1.2
       SETB  P1.3
       RET
       ;**********延时子程序******
LDELAY: MOV     53H,#0AFH
LDLY1:  MOV     54H,#0AFH
LDLY:   DJNZ    54H,LDLY
        DJNZ    53H,LDLY1
        RET
END

终于做完了,硬件:
关闭窗口

相关文章