SCL BIT P1.0
SDA BIT P1.1
DA_TA BIT P3.0
SH BIT P3.1
ST BIT P3.2
MODE BIT P3.3
UP BIT P3.4
DOWN BIT P3.5
DV_M EQU 60H
CH_M EQU 61H
DV_H EQU 62H
CH_H EQU 63H
CDOI1 EQU 38H
CDOI2 EQU 39H
TEMP EQU 37H
F_SEND_TIME BIT 00H
F_SEND_DATE BIT 01H
F_SEND BIT 02H
SECONDS DATA 30H
MINUTES DATA 31H
HOUR DATA 32H
DAY DATA 33H
DATE DATA 34H
MONTH DATA 35H
YEAR DATA 36H
ORG 00H
CLR SH
CLR ST
LJMP MAIN
MAIN:
CALL I2C_INIT
CALL DS1307_INIT
CALL DS1307_OUTPUT
;MOV SECONDS,#31H
;MOV MINUTES,#31H
;MOV HOUR,#10H
DOC_GIO:
SETB P3.7
CALL DS1307_READ_TIME
MOV TEMP,HOUR
CALL BCD2HEX
MOV DV_H,CDOI2
MOV CH_H,CDOI1
MOV TEMP,MINUTES
CALL BCD2HEX
MOV DV_M,CDOI2
MOV CH_M,CDOI1
CALL SERIAL
JNB MODE,CHINH_GIO
JMP DOC_GIO
CHINH_GIO:
JNB MODE,$
MOV DV_M,#10
MOV CH_M,#10
CALL SERIAL
SCAN_GIO:
JNB MODE,THOAT_CHINH_GIO
JNB UP,TANG_GIO
JNB DOWN,GIAM_GIO
CALL SERIAL
JMP SCAN_GIO
THOAT_CHINH_GIO:
JNB MODE,$
MOV CDOI1,CH_H
MOV CDOI2,DV_H
CALL HEX2BCD
MOV HOUR,A
CALL DS1307_WRITE_TIME
NOP
JMP DOC_GIO
TANG_GIO:
JNB UP,$
INC DV_H
MOV A,CH_H
CJNE A,#2,SKIP
MOV A,DV_H
CJNE A,#5,TANG_GIO
MOV CH_H,#0
MOV DV_H,#0
JMP SCAN_GIO
SKIP:
MOV A,DV_H
CJNE A,#10,SCAN_GIO
MOV DV_H,#0
INC CH_H
JMP SCAN_GIO
GIAM_GIO:
;****************************************
BCD2HEX:
MOV A,TEMP
SWAP A
ANL A,#0FH
MOV CDOI1,A
MOV A,TEMP
ANL A,#0FH
MOV CDOI2,A
RET
HEX2BCD:
MOV A,CDOI1
ADD A,#0F0H
MOV CDOI1,a
MOV A,CDOI2
SWAP A
ANL A,#0FH
add a,CDOI1
RET
;****************************************
SERIAL:
PUSH ACC
MOV DPTR,#DU_LIEU
MOV A,DV_M
MOVC A,@a+dptr
CALL DICH
MOV A,CH_M
MOVC A,@A+DPTR
CALL DICH
MOV A,DV_H
MOVC A,@A+DPTR
CALL DICH
MOV A,CH_H
MOVC A,@A+DPTR
CALL DICH
SETB ST
CLR ST
;CALL DELAY
POP ACC
RET
;**************************************************
DICH:
MOV R5,#8
LOOP:
CLR C
RLC A
MOV DA_TA,C
SETB SH
NOP
CLR SH
DJNZ R5,LOOP
RET
DS1307_INIT:
CALL I2C_START
MOV A,#0D0H ;DIA CHI DS1307
LCALL I2C_WRITE ;
;JC $-5
MOV A,#00H ;DIA CHI THANH GHI 0
LCALL I2C_WRITE
MOV A,#00H ;ENABLE THE oscillator (CH bit = 0)
LCALL I2C_WRITE
CALL I2C_STOP
RET
DS1307_OUTPUT:
CALL I2C_START
MOV A,#0D0H ;DIA CHI DS1307
CALL I2C_WRITE ;
MOV A,#07H ;DIA CHI THANH GHI 0
CALL I2C_WRITE
MOV A,#10H ;ENABLE THE OUTPUT oscillator F=1HZ
CALL I2C_WRITE
CALL I2C_STOP
RET
DS1307_WRITE_TIME:
CALL I2C_START
MOV A,#0D0H ;DIA CHI DS1307
CALL I2C_WRITE ;
MOV A,#00H ;DIA CHI THANH GHI 0
CALL I2C_WRITE
MOV A,SECONDS ;
CALL I2C_WRITE
MOV A,MINUTES ;
CALL I2C_WRITE
MOV A,HOUR ;
CALL I2C_WRITE
CALL I2C_STOP
RET
DS1307_WRITE_DATE:
CALL I2C_START
MOV A,#0D0H ;DIA CHI DS1307
CALL I2C_WRITE ;
MOV A,#03H ;DIA CHI THANH GHI 3
CALL I2C_WRITE
MOV A,DAY ;THU
CALL I2C_WRITE
MOV A,DATE ;NGAY
CALL I2C_WRITE
MOV A,MONTH ;THANG
CALL I2C_WRITE
MOV A,YEAR ;NAM
CALL I2C_WRITE
CALL I2C_STOP
RET
DS1307_READ_TIME:
CALL I2C_START
MOV A,#0D0H ;DIA CHI DS1307 VA WRITE
CALL I2C_WRITE ;
MOV A,#00H ;DIA CHI THANH GHI 0
CALL I2C_WRITE
CALL I2C_RESTART
MOV A,#0D1H ;DIA CHI DS1307 VA READ
CALL I2C_WRITE
CALL I2C_READ_ACK
MOV SECONDS,A
CALL I2C_READ_ACK
MOV MINUTES,A
CALL I2C_READ_NACK
MOV HOUR,A
CALL I2C_STOP
RET
DS1307_READ_DATE:
CALL I2C_START
MOV A,#0D0H ;DIA CHI DS1307 VA WRITE
CALL I2C_WRITE ;
MOV A,#03H ;DIA CHI THANH GHI 3
CALL I2C_WRITE
CALL I2C_RESTART
MOV A,#0D1H ;DIA CHI DS1307 VA READ
CALL I2C_WRITE
CALL I2C_READ_ACK
MOV DAY,A
CALL I2C_READ_ACK
MOV DATE,A
CALL I2C_READ_ACK
MOV MONTH,A
CALL I2C_READ_NACK
MOV YEAR,A
CALL I2C_STOP
RET
I2C_WRITE:
PUSH 07H
MOV R7,#8
___LOOP_I2C_WRITE:
CLR SCL
RLC A
MOV SDA,C
NOP
NOP
SETB SCL
NOP
NOP
DJNZ R7,___LOOP_I2C_WRITE
CLR SCL
NOP
NOP
NOP
NOP
SETB SDA ;CAU HINH NGO VAO SDA DOC ACK
SETB SCL
NOP
MOV C,SDA ;KIEM TRA CO "C" DE XAC DINH LOI
NOP
NOP
CLR SCL
POP 07H
RET
I2C_READ_ACK:
PUSH 07H
MOV R7,#8
SETB SDA ;CAU HINH NGO VAO SDA
___LOOP_I2C_READ_ACK:
SETB SCL
NOP
MOV C,SDA
RLC A ;BYTE DOC VE LUU TRONG THANH GHI "A"
NOP
CLR SCL
NOP
NOP
NOP
DJNZ R7,___LOOP_I2C_READ_ACK
CLR SDA
NOP
SETB SCL
NOP
NOP
NOP
NOP
CLR SCL
POP 07H
RET
I2C_READ_NACK:
PUSH 07H
MOV R7,#8
SETB SDA ;CAU HINH NGO VAO SDA
___LOOP_I2C_READ_NACK:
SETB SCL
NOP
MOV C,SDA
RLC A ;BYTE DOC VE LUU TRONG THANH GHI "A"
NOP
CLR SCL
NOP
NOP
NOP
DJNZ R7,___LOOP_I2C_READ_NACK
SETB SDA
NOP
SETB SCL
NOP
NOP
NOP
NOP
CLR SCL
POP 07H
RET
I2C_INIT:
SETB SCL
SETB SDA
RET
I2C_START:
SETB SDA
SETB SCL
NOP ;DELAY 4.7US
NOP ;Bus Free Time Between a STOP and START Condition
NOP
NOP
NOP
CLR SDA
NOP ;DELAY 4US
NOP ;Hold Time (Repeated) START Condition
NOP
NOP
CLR SCL
RET
I2C_STOP:
CLR SDA
SETB SCL
NOP ;DELAY 4.7US
NOP ;Setup Time for STOP Condition
NOP
NOP
NOP
SETB SDA
RET
I2C_RESTART:
SETB SDA
SETB SCL
NOP ;DELAY 4.7US
NOP ;Setup Time for a Repeated START Condition
NOP
NOP
NOP
CLR SDA
NOP ;DELAY 4US
NOP ;Hold Time (Repeated) START Condition
NOP
NOP
CLR SCL
NOP
NOP
RET
SCL_HIGH:
NOP ;DELAY 4US
NOP ;HIGH Period of SCL Clock
SETB SCL
NOP
NOP
RET
SCL_LOW:
NOP ;DELAY 4.7US
NOP ;LOW Period of SCL Clock
CLR SCL
NOP
NOP
NOP
RET
DU_LIEU:
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0FFH
END