EN 	REG P2.7
RW 	REG P2.6
RS 	REG P2.5
SW 	REG P2.4
CS 	REG P2.3
SK 	REG P2.2
DI 	REG P2.1
DO 	REG P2.0
ADDR 	EQU 37H
CNT 	EQU 38H
SEC 	EQU 39H
;
	ORG 00H
	AJMP START
	ORG 0BH
	AJMP TIMER0
        ORG  1BH
        AJMP TIMER1        
;------ 
; MAIN
;------
START: 
	MOV SP,#60H
	CLR SK
	SETB SW
	ACALL INIT      
	ACALL TIMER0
        ACALL TIMER1
L0: 
	ACALL ON ;POWER ON
L1: 
	ACALL CONV
	ACALL SCANKEY
	MOV A,R5
	CJNE A,#BH,L2 ;HELP
	AJMP HELP
L2: 
	CJNE A,#CH,L3 ;F1
	AJMP F1
L3: 
	CJNE A,#DH,L4 ;F2
	AJMP F2
L4: 
	CJNE A,#EH,L5 ;F3
	AJMP F3
L5: 
	CJNE A,#FH,L1 ;F4
	AJMP F4
;------
; HELP
;------
HELP: 
	SETB SW
	SJMP L0
;----
; F1
;----
F1: 
	ACALL CLRLCD
	MOV A,#86H
	ACALL W_INS
	MOV CNT,#4
	MOV DPTR,#MSG3 ;'TIME'
	ACALL W_STR
F11: 
	ACALL CONV ;BCD TO ASCII
	MOV A,#C4H
	ACALL W_INS
	ACALL DISP ;LCD DISPLAY DATA
	ACALL SCANKEY
	MOV A,R5
	CJNE A,#BH,F12 ;HELP
	AJMP HELP
F12: 
	CJNE A,#DH,F13 ;F2
	AJMP F2
F13: 
	CJNE A,#EH,F14 ;F3
	AJMP F3
F14: 
	CJNE A,#FH,F11 ;F4
	AJMP F4
;----
; F2
;----
F2: 
	ACALL CLRLCD
	MOV R0,#48H ;INPUT BUFFER
	MOV A,#82H
	ACALL W_INS
	MOV CNT,#11
	MOV DPTR,#MSG4
	ACALL W_STR
	MOV A,#C4H
	ACALL W_INS
F21: 
	ACALL SCANKEY
	MOV A,R5
	XRL A,#16 ;NOT
	JZ F21
	MOV A,R5
	CJNE A,#AH,F22
	AJMP SETF2 ;SET
F22: 
	CJNE A,#BH,F23 ;HELP
	AJMP HELP
F23: 
	CJNE A,#CH,F24 ;F1
	AJMP F1
F24: 
	CJNE A,#DH,F25 ;F2
	AJMP F21
F25: 
	CJNE A,#EH,F26 ;F3
	AJMP F3
F26: 
	CJNE A,#FH,F27 ;F4
	AJMP F4
F27: 
	MOV A,R5
	MOV @R0,A
	ORL A,#30H
	ACALL W_DATA
	INC R0
	CJNE R0,#4EH,F28
	MOV R0,#48H
	MOV A,#C4H
	ACALL W_INS
F28: 
	SJMP F21
;--------
; F2-SET
;--------
SETF2: 
	MOV R2,#3
	MOV R0,#48H
	MOV R1,#33H
T1: 
	MOV A,@R0
	ANL A,#0FH
	SWAP A
	INC R0
	ADD A,@R0
	MOV @R1,A
	INC R0
	INC R1
	DJNZ R2,T1
;
	MOV R0,#30H
	MOV R1,#33H
	MOV A,@R1
	CLR C
	SUBB A,#24H ;HOUR>24?
	JNC T2
	MOV A,@R1
	MOV @R0,A
	INC R0
	INC R1
	MOV A,@R1
	CLR C
	SUBB A,#60H ;MIN>60?
	JNC T2
	MOV A,@R1
	MOV @R0,A
	INC R0
	INC R1
	MOV A,@R1
	CLR C
	SUBB A,#60H ;SEC>60?
	JNC T2
	MOV A,@R1
	MOV @R0,A
T2: 
	AJMP F1
;----
F3: 
	ACALL CLRLCD
	MOV A,#83H
	ACALL W_INS
	MOV CNT,#10
	MOV DPTR,#MSG5
	ACALL W_STR
	MOV A,#C6H
	ACALL W_INS
	MOV R0,#50H
F31: 
	ACALL SCANKEY
	MOV A,R5
	XRL A,#16 ;NOT
	JZ F31
	MOV A,R5
	CJNE A,#AH,F32 ;SET
	AJMP SETF3
F32: 
	CJNE A,#BH,F33 ;CR
	AJMP HELP
F33: 
	CJNE A,#CH,F34 ;F1
	AJMP F1
F34: 
	CJNE A,#DH,F35 ;F2
	AJMP F2
F35: 
	CJNE A,#EH,F36 ;F3
	AJMP F31
F36: 
	CJNE A,#FH,F37 ;F4
	AJMP F4
F37: 
	MOV A,R5
	MOV @R0,A
	ORL A,#30H
	ACALL W_DATA
	INC R0
	CJNE R0,#54H,F38
	MOV R0,#50H
	MOV A,#C6H
	ACALL W_INS
F38: 
	SJMP 	F31
;--------
; F3-SET
;--------
SETF3: 
	CLR 	F0 ;F0=0,ERROR.F0=1,EXACT
	ACALL 	R_CODE
	MOV 	R2,#4
	MOV 	R0,#50H
	MOV 	R1,#54H
C1: 
	MOV 	A,@R0
	XRL 	A,@R1
	JNZ 	C2
	INC 	R0
	INC 	R1
	DJNZ 	R2,C1
	CLR 	SW
	SETB 	F0 ;CODE EXACT
C2: 
	MOV 	A,#C6H
	ACALL 	W_INS
	MOV 	DPTR,#MSG6 ;MSG6='EXACT'
	JB 	F0,C3
        SETB    SW
	MOV 	DPTR,#MSG7 ;MSG7='ERROR'
C3: 
	MOV 	CNT,#5
	ACALL 	W_STR
C4: 
	ACALL 	SCANKEY
	MOV 	A,R5
	CJNE 	A,#BH,C5 ;HELP
	AJMP 	HELP
C5: 
	CJNE 	A,#CH,C6 ;F1
	AJMP 	F1
C6: 
	CJNE A,#DH,C7 ;F2
	AJMP F2
C7: 
	CJNE A,#EH,C8 ;F3
	AJMP F3
C8: 
	CJNE A,#FH,C9 ;F4
	AJMP F4
C9: 
	AJMP C4
;----
; F4
;----
F4: 
	ACALL CLRLCD
	MOV A,#84H
	ACALL W_INS
	MOV CNT,#8
	MOV DPTR,#MSG8
	ACALL W_STR
	MOV A,#C6H
	MOV R0,#54H
	ACALL W_INS
F41: 
	ACALL SCANKEY
	MOV A,R5
	XRL A,#16 ;NOT
	JZ F41
	MOV A,R5
	CJNE A,#AH,F42
	AJMP SETF4 ;SET
F42: 
	CJNE A,#BH,F43 ;HELP
	AJMP HELP
F43: 
	CJNE A,#CH,F44 ;F1
	AJMP F1
F44: 
	CJNE A,#DH,F45 ;F2
	AJMP F2
F45: 
	CJNE A,#EH,F46 ;F3
	AJMP F3
F46: 
	CJNE A,#FH,F47 ;F4
	AJMP F4
F47: 
	MOV A,R5
	MOV @R0,A
	ADD A,#30H
	ACALL W_DATA
	INC R0
	CJNE R0,#58H,F48
	MOV R0,#54H
	MOV A,#C6H
	ACALL W_INS
F48: 
	SJMP F41
;--------
; F4-SET
;--------
SETF4: 
	ACALL W_CODE
	ACALL CLRLCD
	MOV A,#84H
	ACALL W_INS
	MOV CNT,#8
	MOV DPTR,#MSG9
	ACALL W_STR
M0: 
	ACALL SCANKEY
	MOV A,R5
	CJNE A,#BH,M1 ;HELP
	AJMP HELP
M1: 
	CJNE A,#CH,M2 ;F1
	AJMP F1
M2: 
	CJNE A,#DH,M3 ;F2
	AJMP F2
M3: 
	CJNE A,#EH,M4 ;F3
	AJMP F3
M4: 
	CJNE A,#FH,M0 ;F4
	AJMP F4
;----------
; SCAN KEY
;----------
SCANKEY: 
	MOV R5,#0 ;KEY VALUE

	MOV R7,#4 ;COLUME NUM
	MOV R6,#11111110B
COL: 
	MOV A,R6
	MOV P3,A
	JB P3.4,S1
	JNB P3.4,$ ;DEBOUNCE
	SJMP S5
S1: 
	INC R5
	JB P3.5,S2
	JNB P3.5,$ ;DEBOUNCE
	SJMP S5
S2: 
	INC R5
	JB P3.6,S3
	JNB P3.6,$
	SJMP S5
S3:
	INC R5
	JB P3.7,S4
	JNB P3.7,$
	SJMP S5
S4: 
	INC R5
	MOV A,R6
	RL A
	MOV R6,A
	DJNZ R7,COL
S5: 
	RET
;----------
; POWER ON
;----------
ON: 
	PUSH A
	MOV A,#80H
	ACALL W_INS
	MOV CNT,#16

	MOV DPTR,#MSG1
	ACALL W_STR
	MOV A,#C0H
	ACALL W_INS
	MOV CNT,#16
	MOV DPTR,#MSG2
	ACALL W_STR
	POP A
	RET
;------------
; SET TIMER0
;------------
SET0: 
	MOV SEC,#100
	MOV TMOD,#01H
	MOV IE,#10000010B
	MOV TH0,#>(65536-10000)
	MOV TL0,#<(65536-10000)
	SETB TR0
	RET
;------------
; SETĄ@TIMER1
;------------
SET1:
        MOV SEC,#100
	MOV TMOD,#10H
	MOV IE,#10001000B
	MOV TH1,#>(65536-10000)
	MOV TL1,#<(65536-10000)
	SETB TR1
	RET 
;----------------
; INIT RAM & LCD
;----------------
INIT: 
	MOV R0,#6
	MOV R1,#30H
	CLR A
NEXT0: 
	MOV @R1,A
	INC R1
	DJNZ R0,NEXT0
	MOV R0,#14
	MOV R1,#40H
	CLR A
NEXT1: 
	MOV @R1,A

	INC R1
	DJNZ R0,NEXT1
;
	MOV A,#38H
	ACALL W_INS
	MOV A,#38H
	ACALL W_INS
	MOV A,#38H
	ACALL W_INS
	MOV A,#38H
	ACALL W_INS
	MOV A,#08H ;FUNCTION SET
	ACALL W_INS
	MOV A,#01H ;CLEAR DISPLAY
	ACALL W_INS
	MOV A,#06H ;INPUT MODE SET
	ACALL W_INS
	MOV A,#0CH ;DISPLAY ON/OFF
	ACALL W_INS
	RET
;-----------
; CLEAR LCD
;-----------
	CLRLCD: MOV A,#01H
	ACALL W_INS
	RET
;--------------
; BCD TO ASCII
;--------------
CONV: 
	MOV A,#30H
	MOV R0,A
	MOV A,@R0
	SWAP A
	ANL A,#0FH
	ADD A,#30H
	MOV 40H,A
	MOV A,@R0
	ANL A,#0FH
	ADD A,#30H
	MOV 41H,A
	MOV 42H,#":"
	INC R0
	MOV A,@R0
	SWAP A
	ANL A,#0FH
	ADD A,#30H
	MOV 43H,A
	MOV A,@R0
	ANL A,#0FH
	ADD A,#30H
	MOV 44H,A
	MOV 45H,#":"
	INC R0
	MOV A,@R0
	SWAP A
	ANL A,#0FH
	ADD A,#30H
	MOV 46H,A
	MOV A,@R0
	ANL A,#0FH
	ADD A,#30H
	MOV 47H,A
	RET
;--------------
; DISPLAY TIME
;--------------
DISP: 
	MOV R1,#8
	MOV R0,#40H
DSP: 
	MOV A,@R0
	ACALL W_DATA
	INC R0
	DJNZ R1,DSP
	RET
;---------------------
; WRITE STRING TO LCD
;---------------------
W_STR: 
	PUSH A
	MOV R5,#0
STR: 
	MOV A,R5
	MOVC A,@A+DPTR
	ACALL W_DATA
	INC R5
	DJNZ CNT,STR
	POP A
	RET
;------------------
; WRITE INS TO LCD
;------------------
W_INS: 
	PUSH A
	ACALL CHECK
	CLR RS
	CLR RW
	SETB EN
	MOV P1,A
	CLR EN
	POP A
	RET
;-------------------
; WRITE DATA TO LCD
;-------------------
W_DATA: 
	PUSH A
	ACALL CHECK
	SETB RS
	CLR RW
	SETB EN
	MOV P1,A
	CLR EN
	POP A
	RET
;----------------
; CHECK LCD BUSY
;----------------
CHECK: 
	PUSH A
BUSY: 
	CLR RS
	SETB RW
	SETB EN
	MOV A,P1
	CLR EN
	JB ACC.7,BUSY
	ACALL DELAY
	POP A
	RET
;-------
; DELAY
;-------
DELAY: 
	MOV R6,#20
D1: 
	MOV R7,#150
	DJNZ R7,$
	DJNZ R6,D1
	RET
;-------------
; JUDGE H/M/S
;-------------
JUDGE: 
	PUSH A
	MOV A,32H
	ADD A,#1
	DA A
	MOV 32H,A
	CJNE A,#60H,FIX
	MOV 32H,#00
	MOV A,31H
	ADD A,#1
	DA A
	MOV 31H,A
	CJNE A,#60H,FIX
	MOV 31H,#00
	MOV A,30H
	ADD A,#1
	DA A
	MOV 30H,A
	CJNE A,#24H,FIX
	MOV 30H,#00
FIX: 
	POP A
        RET
;--------
; TIMER0
;--------
TIMER0: 
	PUSH A     ;TIMER 0 SEC
	MOV TH0,#>(65536-10000)
	MOV TL0,#<(65536-10000)
	DJNZ SEC,EXIT3
	MOV SEC,#100
	ACALL JUDGE
EXIT3: 
	POP A
	RETI
;-------
; TIMER1
;-------
TIMER1:
        PUSH A     ;TIMER 1 SEC
	MOV TH1,#>(65536-2000)
	MOV TL1,#<(65536-2000)
	ACALL SCANKEY
        POP   A
        RETI
;----------------------
; READ CODE FORM 93CX6
;----------------------
R_CODE: 
	PUSH A
	MOV ADDR,#0
	MOV R0,#54H
	MOV R1,#2
LOOP1: 
	ACALL READ
	INC ADDR
	DJNZ R1,LOOP1
	POP A
	RET
;---------------------
; WRITE CODE TO 93CX6
;---------------------
W_CODE: 
	PUSH A
	MOV ADDR,#0
	MOV R0,#54H
	MOV R1,#2
LOOP2: 
	ACALL ERASE
	ACALL WEN
	ACALL WRITE
	ACALL WDS
	INC ADDR
	DJNZ R1,LOOP2
	POP A
	RET
;-------------
; ERASE 93CX6
;-------------
ERASE: 
	PUSH A
	MOV R6,#00000111B
	MOV A,ADDR
	MOV R7,A
	ACALL SHIFT5
	SETB CS
	MOV CNT,#11
ER: 
	MOV A,R7
	RLC A
	MOV R7,A
	MOV A,R6
	RLC A
	MOV R6,A
	MOV DI,C
	SETB SK
	NOP
	CLR SK
	NOP
	DJNZ CNT,ER
	ACALL CHKBSY
	CLR CS
	POP A
	RET
;--------------------
; 93CX6 WRITE ENABLE
;--------------------
WEN: 
	PUSH A
	MOV R6,#00000100B
	MOV R7,#11000000B
	ACALL SHIFT5
	SETB CS
	MOV CNT,#11
W1: 
	MOV A,R7
	RLC A
	MOV R7,A
	MOV A,R6
	RLC A
	MOV R6,A
	MOV DI,C
	SETB SK
	NOP
	CLR SK
	NOP
	DJNZ CNT,W1
	CLR CS
	POP A
	RET
;---------------------
; 93CX6 WRITE DISABLE
;---------------------
WDS: 
	PUSH A
	MOV R6,#00000100B
	MOV R7,#00000000B
	ACALL SHIFT5
	SETB CS
	MOV CNT,#11
DS: 
	MOV A,R7
	RLC A
	MOV R7,A
	MOV A,R6
	RLC A
	MOV R6,A
	MOV DI,C
	SETB SK
	NOP
	CLR SK
	NOP
	DJNZ CNT,DS
	CLR CS
	POP A
	RET
;-------
; WRITE
;-------
WRITE: 
	PUSH A
	MOV R6,#00000101B
	MOV A,ADDR
	MOV R7,A
	ACALL SHIFT5
	SETB CS
	MOV CNT,#11
WR: 
	MOV A,R7
	RLC A
	MOV R7,A
	MOV A,R6
	RLC A
	MOV R6,A
	MOV DI,C
	SETB SK
	NOP
	CLR SK
	NOP
	DJNZ CNT,WR

	ACALL W_DAT
	ACALL CHKBSY
	CLR CS
	POP A
	RET
;---------------------
; WRITE DATA TO 93CX6
;---------------------
W_DAT: 
	PUSH A
	MOV R3,#2
WD1: 
	MOV A,@R0
	MOV R4,#8
WD2: 
	RLC A
	MOV DI,C
	SETB SK
	NOP
	CLR SK
	NOP
	DJNZ R4,WD2
	INC R0
	DJNZ R3,WD1
	POP A
	RET
;------
; READ
;------
READ: 
	PUSH A
	MOV R6,#00000110B
	MOV A,ADDR
	MOV R7,A
	ACALL SHIFT5
	SETB CS
	MOV CNT,#11
RD: 
	MOV A,R7
	RLC A
	MOV R7,A
	MOV A,R6
	RLC A
	MOV R6,A
	MOV DI,C
	SETB SK
	NOP
	CLR SK
	NOP
	DJNZ CNT,RD
	ACALL R_DAT
	CLR CS
	POP A
	RET
;----------------------
; READ DATA FROM 93CX6
;----------------------
R_DAT: 
	PUSH A
	MOV R3,#2
RD1: 
	MOV R4,#8
RD2: 
	SETB SK
	NOP
	CLR SK
	NOP
	MOV C,DO
	RLC A
	DJNZ R4,RD2
	MOV @R0,A
	INC R0
	DJNZ R3,RD1
	POP A
	RET
;------------------
; SHIFT LEFT 5 BIT
;------------------
SHIFT5: 
	PUSH A
	MOV CNT,#5
SHT: 
	MOV A,R7
	RLC A
	MOV R7,A
	MOV A,R6
	RLC A
	MOV R6,A
	DJNZ CNT,SHT
	POP A
	RET
;------------------
; CHECK 93CX6 BUSY
;------------------
CHKBSY: 
	PUSH A
BSY: 
	CLR CS
	SETB CS
	MOV C,DO
	JNC BSY
	POP A
	RET
;---------
; MESSAGE
;---------

MSG1: 	DB 'F1:TIME F2:ADJ'
MSG2: 	DB 'F3:INPUT F4:SET'
MSG3: 	DB 'TIME'
MSG4: 	DB 'ADJUST TIME'
MSG5: 	DB 'INPUT CODE'
MSG6: 	DB 'EXACT'
MSG7: 	DB 'ERROR'
MSG8: 	DB 'SET CODE'
MSG9: 	DB 'SUCCESS!'
	END
