;DS1302+AT89C2051的LED钟

;遥控器各按键数据码,可根据不同的遥控器修改对应的键码
muteM   EQU 10h                 ;静音键            闹铃开/关
timerT  EQU 0Ch                 ;菜单键            进入/退出设置状态
leftL   EQU 46h                 ;左移键      
rightR  EQU 47h                 ;右移键
upU     EQU 19h                 ;增加键
downD   EQU 1dh                 ;减少键
d_off   EQU 16H                 ;显示键            LED数码管显示/关闭
recall  EQU 44H                 ;日历键            DS1302初始化操作

 K_on     BIT P3.0               ;闹铃输出
 LED1     BIT P3.3
 LED2     BIT P3.4
 LED3CLK  BIT P3.5               ;DS1302
 LED4IO   BIT P3.7
 RST      BIT P3.1

SECOND  EQU 60H
;60H  秒
;61h  分
;62h  时
;63h  日
;64h  月
;65h  DAY
;66h  年
;6Ah  闹分    from 00(1302)
;6BH  闹时    from 01(1302)
;***************************
        ORG   0000H
        AJMP  START
        ORG   0003H
        LJMP  S_INT            ;IRF
        ORG   000BH            ;200Us
        LJMP  P_INT            ;
        ORG   001BH            ;display LED
        LJMP  DISPLAY
;****************************  ;main program
       ORG    0030H
START: CLR     RST
       MOV SP,#2AH
       SETB    05H
       CLR     06H
       SETB 07H;CLR     07H
       CLR     08H
       CLR     09H
       CLR     0AH
       CLR     0BH
       CLR     0CH
       SETB    0DH
       CLR     0EH
       CLR     0FH
       CLR     10H
       CLR     11H

       MOV R2,#00H             ;
       MOV R3,#00H             ;

       MOV TMOD,#12H
       MOV TL1,#0e5H           ;wait 
       MOV TH1,#0BeH
       MOV TH0,#3FH            ;200uS  3f
       MOV TL0,#3FH
       SETB  ET0
       SETB  ET1
       setb  pt0
       SETB  TR0
       SETB  TR1
       SETB  IT0               ;
       SETB  EX0               ;
       CLR   PX0
       ;LCALL SET1302          ;初始化
       MOV   R1,#0C1H          ;取定时参数从1302中
       acall T_6061
       MOV   6AH,A             ;M
       MOV   R1,#0C3H
       ACALL T_6061
       MOV   6BH,A             ;H

MOV  61H,#0
MOV  62H,#0
       SETB  EA
       sjmp  $
;********************************
DISPLAY: PUSH  ACC
         PUSH  PSW
         MOV   TL1,#0e0H          ;wait how long to display (20ms)
         MOV   TH1,#0b0H
         mov  dptr,#tab1
         JB    0EH,DIS2
         JB    0FH,DIS3
         JB    10H,DIS4
         JB    11H,DIS5
         JNB    0DH,D_OUT

         JNB   08H,D_GO          ;定时到时,显示闪烁
         JNB   07H,D_OUT
D_GO:    MOV   DPTR,#TAB1
         MOV   A,62H
         ANL   A,#0FH
         MOV   B,A
         MOV   A,62H
         SWAP  A
         ANL   A,#07H
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED1
         LCALL DL2M
         SETB  LED1
         MOV   A,B
         MOVC  A,@A+DPTR
                     MOV   C,07H
                     MOV   acc.7,C   ;秒灯闪烁
         MOV   P1,A
         CLR   LED2
         LCALL DL2M
         SETB  LED2

         mov  dptr,#tab2             ;倒装LED字段表
         MOV   A,61H
         ANL   A,#0FH
         MOV   B,A
         MOV   A,61H
         SWAP  A
         ANL   A,#0FH

         MOVC  A,@A+DPTR

                     MOV   C,07H
                     MOV   acc.6,C   ;秒灯闪烁
         MOV   P1,A
         CLR   LED3CLK
         LCALL DL2M
         SETB  LED3CLK

         mov  dptr,#tab1
         MOV   A,B
         MOVC  A,@A+DPTR
                    MOV   C,06H
                    cpl   c
                    MOV   acc.7,C    ;闹铃指示灯
         MOV   P1,A
         CLR   LED4IO
         LCALL DL2M
         SETB  LED4IO
D_OUT:   SETB  TR1
         POP   PSW
         POP   ACC
         RETI
;*********************
DIS2:    AJMP DIS_2
DIS3:    AJMP DIS_3
DIS4:    AJMP DIS_4
DIS5:    AJMP DIS_5
;********************
DIS_2:   mov   dptr,#tab1             ;显示设置小时
         MOV   6FH,62H
         ACALL BCD_HEX
         MOV   B,#10
         DIV   AB
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED1
         LCALL DL2M
         SETB  LED1
         MOV   A,B
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED2
         LCALL DL2M
         SETB  LED2

         SETB  TR1
         POP   PSW
         POP   ACC
         RETI
;*****************
DIS_3:   MOV   DPTR,#TAB2              ;显示设置分
         MOV   6FH,61H
         ACALL BCD_HEX
         MOV   B,#10
         DIV   AB
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED3CLK
         LCALL DL2M
         SETB  LED3CLK
         MOV   A,B
         MOV   DPTR,#TAB1
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED4IO
         LCALL DL2M
         SETB  LED4IO

         SETB  TR1
         POP   PSW
         POP   ACC
         RETI
;*****************
DIS_4:   mov    dptr,#tab1           ;显示设置闹时
         JNB   07H,D4_OUT            ;显示时闪烁
         MOV   A,6BH
         MOV   B,#0AH
         DIV   AB
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED1
         LCALL DL2M
         SETB  LED1
         MOV   A,B
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED2
         LCALL DL2M
         SETB  LED2
D4_OUT:  SETB  TR1
         POP   PSW
         POP   ACC
         RETI
;***********************
DIS_5:   MOV   DPTR,#TAB2
         JNB   07H,D5_OUT              ;显示时闪烁
         MOV   A,6AH                   ;显示设置闹分
         MOV   B,#0AH
         DIV   AB
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED3CLK
         LCALL DL2M
         SETB  LED3CLK
         MOV   DPTR,#TAB1
         MOV   A,B
         MOVC  A,@A+DPTR
         MOV   P1,A
         CLR   LED4IO
         LCALL DL2M
         SETB  LED4IO
D5_OUT:  SETB  TR1
         POP   PSW
         POP   ACC
         RETI
;*****************************
DL2M:    MOV 43H,#14H           ; every LED(2ms)
DL2:     MOV 44H,#14H
DL1:     NOP
         NOP
         DJNZ 44H,DL1
         DJNZ 43H,DL2
         RET
;************************定时读取1302时间及判断闹铃时间是否到*******
P_INT:   PUSH  ACC
         PUSH  PSW
         INC   R2
         CJNE  R2,#250D,P_OUT
         MOV   R2,#00H
         INC   R3
         CJNE  R3,#10D,P_OUT
         MOV   R3,#00H
         CPL   07H
         MOV   P1,#0FFH
         ACALL R_1302
OUT:     JNB   06H,P_OUT
         MOV   A,6BH

         MOV   B,#10
         DIV   AB
         SWAP  A
         ADD   A,B

         CJNE  A,62H,PP_OUT
         MOV   A,6AH

         MOV   B,#10
         DIV   AB
         SWAP  A
         ADD   A,B

         CJNE  A,61H,PP_OUT
         SETB  08H               ;定时到标志
         MOV   C,07H
         cpl   c
         MOV   K_ON,C

P_OUT:   POP   PSW
         POP   ACC
         RETI
PP_OUT:  SETB  K_ON
         CLR   08H
         POP   PSW
         POP   ACC
         RETI
;************************有接收到遥控器信号*******
S_INT:CLR EA
      PUSH   ACC
      PUSH   PSW
      PUSH   DPH
      PUSH   DPL
      PUSH   02H
      PUSH   03H
      MOV    R6,#10D   ;
SB:   ACALL  YS1
      JB     P3.2,EXIT
      DJNZ   R6, SB
      JNB    P3.2, $   ;
      ACALL  YS2       ;
      JB     P3.2,EXIT
      MOV    R1,#1AH   ;
      MOV    R2,#4D
PP:   MOV    R3,#8D
JJJJ: JNB    P3.2,$
      ACALL  YS1
      MOV    C,P3.2
      JNC    UUU
      JB     P3.2,$
UUU:  MOV    A,@R1
      RRC    A
      MOV    @R1,A
      DJNZ   R3,JJJJ
      INC    R1
      DJNZ   R2,PP
;-------------------------------------
      MOV  A,1AH      ;
      XRL  A,#40h     ;判断1AH的值是否等于遥控器低8位地址(40h)
      JNZ  EXIT       ;
      MOV  A,1bH
      XRL  A,#0bfh    ;再判断高8位地址是否正确(0bfh)
      JNZ  EXIT
      MOV  A,1CH
      CPL  A
      XRL  A,1DH      ;将1CH的值取反后和1DH比较 不同则无效丢弃(核对数据是否准确)
      JNZ  EXIT
      ACALL DO        ;相应按键的处理
EXIT: SETB EA
      POP  03H
      POP  02H
      POP  DPL
      POP  DPH
      POP  PSW
      POP  ACC
      RETI
;--------------------delay-----------------------
YS1:  MOV    70H,#16D   ;延时1
D1:   MOV    71H,#21D
      DJNZ   71H,$
      DJNZ   70H,D1
      RET
YS2:  MOV    70H,#09D   ;延时2
D2:   MOV    71H,#220D
      DJNZ   71H,$
      DJNZ   70H,D2
      RET
;---------------------------------------------------------
TAB:   DB 00H,01H,02H,03H,04H,05H,06H,07H,08H,09H      ;TAB1  LED数码管显示表 TAB遥控器键值表(未用)
;TAB1:  DB 3FH,11H,5EH,5BH,71H,6BH,6FH,19H,7FH,7bH,77H,7CH,39H,5EH,79H,71H
tab1:  DB  0C0H,0F9H,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h,88h,83h,0a7h,0a1h,86h,8EH
TAB2:  DB  0c0H,0f9H,64H,70H,59H,52H,42H  ;倒装LED段码表
;0CH 为设置标志,0DH为全显示标志,0EH为显示设置小时标志,0FH为显示设置分标志
;10H 为显示设置闹时标志,11H为显示设置闹分标志
;增加闹时静音功能
;mute-10h,timer-0eh,left-46h,right-47h,up-19h,down-1dh

DO:     MOV     A,1CH
        CJNE    A,#TIMERT,REDO  ;代码不是设置/退出键
        JNB     0CH,SET1        ;是,则进入
        CLR     0CH             ;是,已经进入,则退出
        SETB    0DH             ;恢复正常显示
        CLR     0EH
        CLR     0FH
        CLR     10H
        CLR     11H
        RET

SET1:   SETB    0CH
        SETB    0EH
        CLR     0DH
        CLR     0FH
        CLR     10H
        CLR     11H
        RET

REDO:   CJNE    A,#MUTEM,REDO1  ;不是设置键的处理REDO1,2,3,4...
        LJMP    MUTE            ;MUTE?
REDO1:  CJNE    A,#D_OFF,DO_SET2
        LJMP    DIS_OFF         ;关闭显示
DO_SET2:JB      0CH,SET2        ;时间设置的处理
        RET

SET2:   CJNE    A,#UPU,SET2A
        LJMP    INC1            ;+加(^)
SET2A:  CJNE    A,#DOWND,SET2B
        LJMP    DEC2            ;- 减
SET2B:  CJNE    A,#RIGHTR,SET2C
        LJMP    RIGHT           ;RIGHT右移(>)
SET2C:  CJNE    A,#LEFTL,SET2D
        LJMP    LEFT            ;LEFT左移(<)
SET2D:  CJNE    A,#RECALL,SET2EXT ;DS1302初始化
        LJMP    SET1302
SET2EXT:RET
;--------------------------------INC------------------------
INC1:   JB      0EH,INCH
        JB      0FH,INCM
        JB      10H,INC_H
        JB      11H,INC_M
        RET

INC_H:  INC     6BH               ;闹钟小时增加
        MOV     A,6BH
        CJNE    A,#24D,INCH3
        MOV     6BH,#0
INCH3:  MOV     R1,#0C2H
        MOV     R0,6BH
        ACALL   SETONE            ;写入DS1302
        RET

INC_M:  INC     6AH               ;闹钟分增加
        MOV     A,6AH
        CJNE    A,#60D,INCH4
        MOV     6AH,#0
INCH4:  MOV     R1,#0C0H
        MOV     R0,6AH
        ACALL   SETONE             ;
        RET

INCH:   ;INC     62H               ;
        MOV     6FH,62H
        ACALL   BCD_HEX
        INC     A
        CJNE    A,#24D,INCH1
        MOV     A,#0
INCH1:  MOV     6FH,A
        ACALL   HEX_BCD
        MOV     62H,A
        MOV     R1,#84H
        MOV     R0,62H
        ACALL   SETONE             ;
        RET

INCM:   ;INC     61H               ;
        MOV     6FH,61H
        ACALL   BCD_HEX
        INC     A
        CJNE    A,#60D,INCH2
        MOV     A,#0
INCH2:  MOV     6FH,A
        ACALL   HEX_BCD
        MOV     61H,A
        MOV     R1,#82H
        MOV     R0,61H
        ACALL    SETONE             ;
        RET

;----------------------------------DEC----------------------
DEC2:   JB      0EH,DECH
        JB      0FH,DECM
        JB      10H,DEC_H
        JB      11H,DEC_M
        RET
DECH:   MOV     6FH,62H
        ACALL   BCD_HEX
        DEC     A
        CJNE    A,#0FFH,DECH1
        MOV     A,#23
DECH1:  MOV     6FH,A
        ACALL   HEX_BCD
        MOV     62H,A
        MOV     R1,#84H
        MOV     R0,62H
        ACALL   SETONE
        RET


DECM:   MOV     6FH,61H
        ACALL   BCD_HEX
        DEC     A
        CJNE    A,#0FFH,DECM1
        MOV     A,#59
DECM1:  MOV     6FH,A
        ACALL   HEX_BCD
        MOV     61H,A
        MOV     R1,#82H
        MOV     R0,61H
        ACALL   SETONE
        RET

DEC_H:  DEC     6BH
        MOV     A,6BH
        CJNE    A,#0FFH,DECH3
        MOV     6BH,#23D
DECH3:  MOV     R1,#0C2H
        MOV     R0,6BH
        ACALL   SETONE
        RET

DEC_M:  DEC     6AH
        MOV     A,6AH
        CJNE    A,#0FFH,DECH4
        MOV     6AH,#59D
DECH4:  MOV     R1,#0C0H
        MOV     R0,6AH
        ACALL   SETONE
        RET
;--------------------------------RIGHT----------------------
RIGHT:  JB      0EH,RIGHT1
        JB      0FH,RIGHT2
        JB      10H,RIGHT3
        JB      11H,RIGHT4
        RET                       ;右移
RIGHT1: CLR     0EH
        SETB    0FH
        RET
RIGHT2: CLR     0FH
        SETB    10H
        RET
RIGHT3: CLR     10H
        SETB    11H
        RET
RIGHT4: CLR     11H
        SETB    0EH
        RET
;--------------------------------LEFT-----------------------
LEFT:   JB      0EH,LEFT1
        JB      0FH,LEFT2
        JB      10H,LEFT3
        JB      11H,LEFT4
        RET                       ;左移
LEFT1:  SETB    11H
        CLR     0EH
        RET
LEFT2:  SETB    0EH
        CLR     0FH
        RET
LEFT3:  SETB    0FH
        CLR     10H
        RET
LEFT4:  SETB    10H
        CLR     11H
        RET
;------------------------------MUTE--------------
MUTE:   setb    k_ON
        CPL     06H              ;静音
        CLR     08H
        RET
;-----------------------------DISPLAY OFF--------
DIS_OFF:CLR     ET1
        CPL     05H
        JNB     05H,REDORE
        SETB    ET1
REDORE: RET
;------------------------------------初始化1302-------------------------
SET1302: CLR    RST           ;2002年1月1日12:00:00
         CLR    LED3CLK
        MOV     SECOND,#0
        MOV     61H,#0
        MOV     62H,#18       ;
        MOV     63H,#1
        MOV     64H,#1
        MOV     65H,#1
        MOV     66H,#2
         SETB   RST
         MOV    A,#8EH
         LCALL  R_BIT
         MOV    A,#00H
         LCALL  R_BIT
         SETB   LED3CLK
         CLR    RST

         MOV    R0,#SECOND
         MOV    R7,#7
         MOV    R1,#80H
SET1302A:CLR    RST
         CLR    LED3CLK
         SETB   RST
         MOV    A,R1
         LCALL  R_BIT
         MOV    A,@R0
         LCALL  R_BIT
         INC    R0
         INC    R1
         INC    R1
         SETB   LED3CLK
         CLR    RST
         DJNZ   R7,SET1302A
         CLR    RST
         CLR    LED3CLK

         sETB   LED3CLK
         nop
         CLR    RST
         nop
         clr    led3clk
         SETB   RST
         MOV    A,#90H
         LCALL  R_BIT
         MOV    A,#10100101B         ;充电器设置
         LCALL  R_BIT
         SETB   LED3CLK
         CLR    RST
         CLR    LED3CLK

         SETB   RST
         MOV    A,#8EH
         LCALL  R_BIT
         MOV    A,#80H
         LCALL  R_BIT
         SETB   LED3CLK
         CLR    RST
         RET
;----------------------读取一RAM ------
T_6061:  CLR    RST
         CLR    LED3CLK
         SETB   RST
         MOV    A,R1
         LCALL  R_BIT
         LCALL  W_BIT
         SETB   LED3CLK
         CLR    RST
         RET
;-----------------------------
R_1302:  MOV    R0,#SECOND
         MOV    R7,#7
         MOV    R1,#81H
R_1302A: CLR    RST
         CLR    LED3CLK
         SETB   RST
         MOV    A,R1
         LCALL  R_BIT
         LCALL  W_BIT
         MOV    @R0,A
         INC    R0
         INC    R1
         INC    R1
         SETB   LED3CLK
         CLR    RST
         DJNZ   R7,R_1302A
         RET
;------------------R_BIT------------------
R_BIT:   MOV    R4,#8
R_BIT1:  RRC    A
         MOV    LED4IO,C
         SETB   LED3CLK
         CLR    LED3CLK
         DJNZ   R4,R_BIT1
         RET
;-------------------W_BIT-----------------------
W_BIT:   MOV    R4,#8
W_BIT1:  MOV    C,LED4IO
         RRC    A
         SETB   LED3CLK
         CLR    LED3CLK
         DJNZ   R4,W_BIT1
         RET
;--------------------------------------------------
BCD_HEX:
         MOV    A,6FH
         ANL    A,#0FH
         MOV    B,A
         MOV    A,6FH
         MOV    6FH,B
         SWAP   A
         ANL    A,#0FH
         MOV    B,#0AH
         MUL    AB
         ADD    A,6FH
         ;MOV    6FH,A
         RET

HEX_BCD:
         MOV   A,6FH
         MOV   B,#10
         DIV   AB
         SWAP  A
         ADD   A,B
         ;MOV   6FH,A
         RET

;-----写一地址---R1 为地址,R0 为数据----------
SETONE:  CLR    LED3CLK
         CLR    RST
         CLR    LED3CLK
         NOP
         SETB   RST
         MOV    A,#8EH
         LCALL  R_BIT
         MOV    A,#00H
         LCALL  R_BIT
         SETB   LED3CLK
         CLR    RST
         CLR    LED3CLK
         NOP
         CLR    RST
         CLR    LED3CLK
         SETB   RST
         MOV    A,R1
         LCALL  R_BIT
         MOV    A,R0
         LCALL  R_BIT
         NOP
         SETB   LED3CLK
         CLR    RST
         NOP
         NOP
         CLR    RST
         CLR    LED3CLK

         SETB   RST
         MOV    A,#8EH
         LCALL  R_BIT
         MOV    A,#80H
         LCALL  R_BIT
         SETB   LED3CLK
         CLR    RST
         RET