;DS1302时钟芯片
;说明:
;入口条件: 日(30H) 月(31H) 年(32H)
; 秒(30H) 分(31H) 时(32H)
; 星期(30H)
;出口信息: 日(30H) 月(31H) 年(32H)
; 秒(30H) 分(31H) 时(32H)
; 星期(30H)
;影响资源:PSW、R7、A 堆栈需求: 6字节
DS_SCK BIT P1.6
DS_RST BIT P3.5
DS_IO BIT P1.7
READ_HMS:
CLR DS_IO
JB DS_IO, READ_HMS_END
MOV A, #81H
LCALL DS_READ1
MOV 30H, A
MOV A, #83H
LCALL DS_READ1
MOV 31H, A
MOV A, #85H
LCALL DS_READ1
MOV 32H, A
CLR DS_RST
MOV A, 30H
CLR C
SUBB A, #60H
JC READ_HMS_END
MOV 30H, #00H
LCALL SET_HMS
READ_HMS_END:
RET
READ_WEEK:
CLR DS_IO
JB DS_IO, READ_WEEK_END
MOV A, #8BH
LCALL DS_READ1
MOV 32H, A
READ_WEEK_END:
RET
DS_SEND1:
CLR DS_RST
CLR DS_SCK
NOP
NOP
NOP
SETB DS_RST
LCALL DS_SEND
MOV A, 10H
LCALL DS_SEND
RET
SET_YMD: ;去除写保护
MOV A, #8EH
MOV 10H, #00H
LCALL DS_SEND1
MOV A, #8CH
MOV 10H, 32H
LCALL DS_SEND1
MOV R0, #86H
MOV R1, #30H
MOV R6, #02H
LJMP NEXT_WRITE_SP
RET
SET_HMS: ;去除写保护
MOV A, #8EH
MOV 10H, #00H
LCALL DS_SEND1
MOV R0, #80H
MOV R1, #30H
MOV R6, #03H
LJMP NEXT_WRITE_SP
SET_WEEK:
MOV A, #8EH
MOV 10H, #00H
LCALL DS_SEND1
MOV A, #8AH
MOV 10H, 30H
LCALL DS_SEND1
RET
NEXT_WRITE_SP:
MOV A, R0
MOV 10H, @R1
LCALL DS_SEND1
INC R0
INC R0
INC R1
DJNZ R6, NEXT_WRITE_SP
MOV A, #8EH ;打上写保护
MOV 10H, #80H
LCALL DS_SEND1
CLR DS_RST
RET
NEXT_READ_SP:
MOV A, R1
LCALL DS_READ1
MOV @R0, A
INC R1
INC R1
INC R0
DJNZ R6, NEXT_READ_SP
CLR DS_RST
RET
READ_YMD:
MOV A, #8DH
LCALL DS_READ1
MOV 32H, A
MOV R1, #87H
MOV R6, #02H
MOV R0, #30H
LJMP NEXT_READ_SP
RET
DS_SEND: ;发命令
MOV R7, #08H
NEXT_BIT:
NOP
NOP
NOP
RRC A
MOV DS_IO, C
SETB DS_SCK
NOP
NOP
NOP
NOP
NOP
CLR DS_SCK
NOP
NOP
NOP
NOP
NOP
DJNZ R7, NEXT_BIT
RET
DS_READ: ;收数据
MOV R7, #08H
NEXT_BIT1:
NOP
NOP
NOP
MOV C, DS_IO
RRC A
SETB DS_SCK
NOP
NOP
NOP
NOP
NOP
CLR DS_SCK
NOP
NOP
NOP
NOP
NOP
DJNZ R7, NEXT_BIT1
RET
DS_READ1:
CLR DS_RST
CLR DS_SCK
NOP
NOP
NOP
SETB DS_RST
LCALL DS_SEND
LCALL DS_READ
RET