;按“NEXT”键，调整秒位；按“LAST”键,调整分位;按"MON"键，调整时位;
OUTBIT   equ 0e101h  ; 位控制口
CLK164   equ 0e102h  ; 段控制口(接164时钟位)
DAT164   equ 0e102h  ; 段控制口(接164数据位)
IN       equ 0e103h  ; 键盘读入口
LEDBuf： equ   60h            ; 显示缓冲
         ljmp  Start
LEDMAP:                       ; 八段管显示码
         db    3fh, 06h, 5bh, 4fh, 66h, 6dh, 7dh, 07h
         db    7fh, 6fh, 77h, 7ch, 39h, 5eh, 79h, 71h
         db    00H
Delay:                        ; 延时子程序
        mov   r7, #00
DelayLoop:
           djnz  r7, DelayLoop
           djnz  r6, Delay
           ret
DisplayLED:
            mov   r0, #LEDBuf
            mov   r1, #6          ; 共6个八段管
            mov   r2, #00000001b  ; 从左边开始显示
Loop:
       mov   dptr, #OUTBIT
       mov   a, #0
       movx  @dptr, a        ; 关所有八段管
       mov   a, @r0
       mov   B, #8           ; 送164
DLP:
       rlc   a
       mov   r3, a
       mov   acc.0, c
       mov   dptr, #DAT164
       anl a,#0fdh
       movx  @dptr, a
       mov   dptr, #CLK164
       orl a,#02h
       movx  @dptr, a
       anl a,#0fdh
       movx  @dptr, a
       mov   a, r3
       djnz  B, DLP
       mov   dptr, #OUTBIT
       mov   a, r2
       movx  @dptr, a        ; 显示一位八段管
       mov   r6, #1
       call  Delay
       mov   a, r2           ; 显示下一位
       rl    a
       mov   r2, a
       inc   r0
       djnz  r1, Loop
       ret
TestKey:
       mov   dptr, #OUTBIT
       mov   a, #0
       movx  @dptr, a        ; 输出线置为0
       mov   dptr, #IN
       movx  a, @dptr        ; 读入键状态
       cpl   a
       anl   a, #0fh         ; 高四位不用
       ret
KeyTable:                    ; 数字键码定义
       db    00h, 01h, 04h, 07h
       db    0fh, 02h, 05h, 08h
       db    0eh, 03h, 06h, 09h
       db    0dh, 0ch, 0bh, 0ah
       db    10H,11H,12H,13H,14H
       db    15H,16H,10H,10H,10H
GetKey:            MOV   13H,#06H
                   MOV   12H,#20H
Key2:	           MOV   A,12H
                   CPL   A
                   MOV   R7,A
                   MOV   DPTR,#0E101H
                   MOV   A,R7
                   MOVX  @DPTR,A
                   MOV   A,12H
                   CLR   C
                   RRC   A
                   MOV   12H,A
                   MOV   DPTR,#0E103H
                   MOVX  A,@DPTR
                   MOV   R7,A
                   MOV   A,R7
                   CPL   A
                   MOV   R7,A
                   MOV   A,R7
                   ANL   A,#0FH
                   MOV   14H,A
                   DEC   13H
                   MOV   R7,13H
                   MOV   A,R7
                   JZ    Key1
                   MOV   A,14H
                   JZ    Key2
Key1 :             MOV   A,14H
                   JZ   GetKey6
                   MOV   A,13H
                   ADD   A,ACC
                   ADD   A,ACC
                   MOV   13H,A
                   MOV   A,14H
                   JNB   ACC.1,GetKey1
                   INC   13H
                   SJMP  GetKey3
GetKey1:           MOV   A,14H
                   JNB   ACC.2,GetKey2
                   INC   13H
                   INC   13H
                   SJMP  GetKey3
GetKey2:           MOV   A,14H
                   JNB   ACC.3,GetKey3
                   MOV   A,#03H
                   ADD   A,13H
                   MOV   13H,A
GetKey3:           MOV   DPTR,#0E101H
                   CLR   A
                   MOVX  @DPTR,A
GetKey4:           MOV   R7,#0AH
                   LCALL Delay
                   LCALL TestKey
                   MOV   A,R7
                   JNZ   GetKey4
                   MOV   R7,13H
                   MOV   A,R7
                   MOV   DPTR,#KeyTable
                   MOVC  A,@A+DPTR
                   MOV   R2,A
                   RET
GetKey6:          MOV   R2,#0FFH
                   RET
WaitRelease:
             mov   dptr, #OUTBIT  ; 等键释放
             clr   a
             movx  @dptr, a
             mov   r6, #10
             call  Delay
             call  TestKey
             jnz   WaitRelease
             mov   a, r2
             ret
Start:  mov r6,#02h
        call delay
        mov   20h,#00h
        mov   21h,#00h
        mov   22h,#00h
        mov   sp, #40h
        mov   dptr,#0e100h
        mov   a,#03h
        movx  @dptr,a
Start1: mov dptr,#0fe0ah
        movx a,@dptr
        anl a,#70h
        cjne a,#20h,start2 ;判断晶振打开否?
        sjmp start3
start2: mov dptr,#0fe0bh	  ;设置SET=0,芯片正常工作.24/12=1,选24小时制.
        mov a,#82h
        movx @dptr,a
        mov r0,#06h
        mov dptr,#0fe00h    ;;时分秒清零
        mov a,#00h
retun0: movx @dptr,a
        inc dptr
        djnz r0,retun0
        mov dptr,#0fe0ah
        mov a,#27h
        movx @dptr,a         ;打开晶振，ＳＱＷ输出５１２ＨＺ方波。
        inc dptr
        mov a,#5ah
        movx @dptr,a
start3: mov dptr,#0fe0ah
        movx a,@dptr
        jnb acc.7,loop12
        mov r5,#4dh
        djnz r5,$
loop12: mov dptr,#0fe0bh
        mov a,#5ah
        movx @dptr,a
loop13: mov dptr,#0fe00h	  ;读秒,分,时
        mov r1,#60h
        mov r0,#03h
loop11: movx a,@dptr
        lcall Ptreg		  ;读取的值,进行拆字后送显示缓冲器60H-65H
        inc dptr
        inc dptr
        djnz r0,loop11
        mov dptr,#0fe0bh
        call  DisplayLED       ; 调用显示子程序
        call  TestKey          ; 有键入?
        jz   loop12            ; 无键入, 继续显示
        call  GetKey           ; 有键入,读入键码
        cjne a,#14h,keep0
        sjmp keep1		   ;是NEXT键,调整秒位
keep0: cjne a,#15h,keep2
       sjmp keep3		   ;是LAST键,调整分位
keep2: cjne a,#16h,start1
       sjmp keep5  		   ;是MON键,调整时位
keep1: mov dptr,#0fe0bh
       mov a,#0dah
       movx @dptr,a
       mov  a,20h
       lcall Hbcd
       cjne a,#60h,loop20	   ;秒位不能超过60秒
       mov 20h,#00h
       sjmp loop13
loop20:mov dptr,#0fe00h
       movx @dptr,a
       inc 20h
       sjmp loop13
keep3: mov dptr,#0fe0bh
       mov a,#0dah
       movx @dptr,a
       mov  a,21h
       lcall Hbcd
       cjne a,#60h,loop21	  ;分位不能超过60分
       mov 21h,#00h
       sjmp loop13
loop21:mov dptr,#0fe02h
       movx @dptr,a
       inc 21h
       sjmp loop13
keep5: mov dptr,#0fe0bh
       mov a,#0dah
       movx @dptr,a
       mov  a,22h
       lcall Hbcd
       cjne a,#24h,loop22	     ;时位不能超过24小时
       mov 22h,#00h
       sjmp loop13
loop22:mov dptr,#0fe04h
       movx @dptr,a
       inc 22h
       sjmp loop13
Ptreg: push dph		     ;拆字子程序
       push dpl
       push acc
       push b
       mov b,a
       anl a,#0fh
       mov dptr,#LEDMAP
       movc a,@a+dptr
       orl a,#80h
       mov @r1,a
       inc r1
       mov a,b
       swap a
       anl a,#0fh
       mov dptr,#LEDMAP
       movc a,@a+dptr
       mov @r1,a
       inc r1
       pop b
       pop acc
       pop dpl
       pop dph
       ret
Hbcd:  mov b,#100		;单字节十六进制整数转换成单字节BCD码子程序
       div ab
       mov r3,a
       mov a,b
       mov b,#10
       div ab
       swap a
       orl a,b
       ret
END
