Fosc_KHZ EQU 22118 ;22118KHZ
STACK_POIRTER EQU 0D0H ;堆栈开始地质
P4 DATA 0C0H
P5 DATA 0C8H
P0M1 DATA 0x93
P0M0 DATA 0x94
P1M1 DATA 0x91
P1M0 DATA 0x92
P2M1 DATA 0x95
P2M0 DATA 0x96
P3M1 DATA 0xB1
P3M0 DATA 0xB2
P4M1 DATA 0xB3
P4M0 DATA 0xB4
P5M1 DATA 0xC9
P5M0 DATA 0xCA
P6M1 DATA 0xCB
P6M0 DATA 0xCC
P7M1 DATA 0xE1
P7M0 DATA 0xE2
P_HC595_SER BIT P4.0
P_HC595_RCLK BIT P5.4
P_HC595_SRCLK BIT P4.3
hour DATA 0x30
minute DATA 0x31
second DATA 0x32
ORG 0000H
LJMP F_Main
TestString1: DB "---Clock demo---", 0x00
ORG 0100H
F_Main:
CLR A
MOV P0M1, A ;设置为准双向口
MOV P0M0, A
MOV P1M1, A ;设置为准双向口
MOV P1M0, A
MOV P2M1, A ;设置为准双向口
MOV P2M0, A
MOV P3M1, A ;设置为准双向口
MOV P3M0, A
MOV P4M1, A ;设置为准双向口
MOV P4M0, A
MOV P5M1, A ;设置为准双向口
MOV P5M0, A
MOV P6M1, A ;设置为准双向口
MOV P6M0, A
MOV P7M1, A ;设置为准双向口
MOV P7M0, A
MOV SP, #STACK_POIRTER
MOV PSW, #0
USING 0
LCALL F_Initialize_LCD
MOV A, #0
LCALL F_ClearLine
MOV A, #1
LCALL F_ClearLine
ANL P2M1, #NOT (1 SHL 4)
ORL P2M0, # (1 SHL 4)
SETB P2.4
LCALL F_DisableHC595
MOV R2, #0
MOV R3, #0
MOV DPTR, #TestString1
LCALL F_PutString
MOV hour, #12
MOV minute, #0
MOV second, #0
LCALL F_DisplayRTC
LCALL F_DisableHC595
L_MainLoop:
MOV A, #250 ;延时250 ms
LCALL F_delay_ms
MOV A, #250 ;延时250 ms
LCALL F_delay_ms
MOV A, #250 ;延时250 ms
LCALL F_delay_ms
MOV A, #250 ;延时250 ms
LCALL F_delay_ms
LCALL F_RTC
LCALL F_DisplayRTC
LJMP L_MainLoop
F_DisableHC595:
PUSH AR7
SETB P_HC595_SER
MOV R7, #20
L_DisableHC595_Loop:
SETB P_HC595_SRCLK
NOP
CLR P_HC595_SRCLK
NOP
DJNZ R7, L_DisableHC595_Loop
SETB P_HC595_RCLK = 1;
NOP
CLR P_HC595_RCLK = 0; //锁存输出数据
SETB P_HC595_RCLK = 1;
NOP
CLR P_HC595_RCLK = 0; //锁存输出数据
POP AR7
RET
F_delay_ms:
PUSH AR2 ;入栈R2
PUSH AR3 ;入栈R3
PUSH AR4 ;入栈R4
MOV R2,A
L_Delay_Nms_Loop1:
MOV R3, #HIGH (Fosc_KHZ / 14)
MOV R4, #LOW (Fosc_KHZ / 14)
L_Delay_Nms_Loop2:
MOV A, R4 ; 1T
CLR C ; 1T
SUBB A, #1 ; 2T
MOV R4, A ; 1T
MOV A, R3 ; 1T
SUBB A, #0 ; 2T
MOV R3, A ; 1T
ORL A, R4 ; 1T
JNZ L_Delay_Nms_Loop2 ; 4T
DJNZ R2, L_Delay_Nms_Loop1
POP AR4 ;出栈R2
POP AR3 ;出栈R3
POP AR2 ;出栈R4
RET
F_DisplayRTC:
MOV R2, #1 ;第2行
MOV R3, #4 ;第4个字符
MOV A, hour
MOV B, #10
DIV AB
ADD A, #'0'
LCALL F_WriteChar
INC R3 ;第5个字符
MOV A, B
ADD A, #'0'
LCALL F_WriteChar
INC R3 ;第6个字符
MOV A, #'-'
LCALL F_WriteChar
INC R3 ;第7个字符
MOV A, minute
MOV B, #10
DIV AB
ADD A, #'0'
LCALL F_WriteChar
INC R3 ;第8个字符
MOV A, B
ADD A, #'0'
LCALL F_WriteChar
INC R3 ;第9个字符
MOV A, #'-'
LCALL F_WriteChar
INC R3 ;第10个字符
MOV A, second
MOV B, #10
DIV AB
ADD A, #'0'
LCALL F_WriteChar
INC R3 ;第11个字符
MOV A, B
ADD A, #'0'
LCALL F_WriteChar
RET
F_RTC:
INC second
MOV A, second
CLR C
SUBB A, #60
JNC $+3
RET
MOV second, #0
INC minute
MOV A, minute
CLR C
SUBB A, #60
JNC $+3
RET
MOV minute, #0
INC hour
MOV A, hour
CLR C
SUBB A, #24
JNC $+3
RET
MOV hour, #0
RET
LineLength EQU 16 ;16x2
LCD_BUS DATA 0x80
LCD_B7 BIT LCD_BUS.7
LCD_B6 BIT LCD_BUS.6
LCD_B5 BIT LCD_BUS.5
LCD_B4 BIT LCD_BUS.4
LCD_B3 BIT LCD_BUS.3
LCD_B2 BIT LCD_BUS.2
LCD_B1 BIT LCD_BUS.1
LCD_B0 BIT LCD_BUS.0
LCD_ENA BIT P2.7
LCD_RW BIT P2.6
LCD_RS BIT P2.5
C_CLEAR EQU 0x01
C_HOME EQU 0x02
C_CUR_L EQU 0x04
C_RIGHT EQU 0x05
C_CUR_R EQU 0x06
C_LEFT EQU 0x07
C_OFF EQU 0x08
C_ON EQU 0x0C
C_FLASH EQU 0x0D
C_CURSOR EQU 0x0E
C_FLASH_ALL EQU 0x0F
C_CURSOR_LEFT EQU 0x10
C_CURSOR_RIGHT EQU 0x10
C_PICTURE_LEFT EQU 0x10
C_PICTURE_RIGHT EQU 0x10
C_BIT8 EQU 0x30
C_BIT4 EQU 0x20
C_L1DOT7 EQU 0x30
C_L1DOT10 EQU 0x34
C_L2DOT7 EQU 0x38
C_4bitL2DOT7 EQU 0x28
C_CGADDRESS0 EQU 0x40
C_DDADDRESS0 EQU 0x80
F_LCD_DelayNop:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RET
F_CheckBusy:
PUSH ACC
PUSH AR2
PUSH AR3
MOV R2, #HIGH 10000
MOV R3, #LOW 10000
L_CheckBusyLoop:
JNB LCD_B7, L_QuitCheckBusy ; 5T
MOV A, R3 ; 1T
CLR C ; 1T
SUBB A, #1 ; 2T
MOV R3, A ; 1T
MOV A, R2 ; 1T
SUBB A, #0 ; 2T
MOV R2, A ; 1T
ORL A, R3 ; 1T
JNZ L_CheckBusyLoop ; 4T
L_QuitCheckBusy:
POP AR3
POP