Mọi người cứu em với. em mới làm một cái đồng hồ dùng 8052 với con 1307. nó chạy rất chính xác nhưng mỗi khi mất điện rồi mở lại thì giây của nó lại về 00. thế nên sau mỗi lần mất điện nó sai vài chục giây >>>>>>
đây là code của em sưu tầm và chỉnh sửa từ nhiều nguồn. mong mọi người chỉ giáo. em chân thành cám ơn
; KHAI BAO
;===========
CONT_BYTE_W EQU 11010000B
CONT_BYTE_R EQU 11010001B
ORG 06H
ADD_LOW EQU 62H
DAVAVA EQU 63H
SEC DATA 24H
MIN DATA 25H
HOUR DATA 26H
DAY DATA 27H
DATE DATA 28H
MONTH DATA 49H
YEAR DATA 2AH
SCL BIT P3.0
SDA BIT P3.1
LED_H_C BIT P2.0
LED_H_DV BIT P2.1
LED_M_C BIT P2.2
LED_M_DV BIT P2.3
LED_DAY BIT P2.4
LED_DE_C BIT P2.5
LED_DE_DV BIT P2.6
LED_MO_C BIT P0.3 ;P2.7
LED_MO_DV BIT P0.2 ;P0.7
LED_Y_C BIT P0.6
LED_Y_DV BIT P0.5
DOT BIT P0.4
LED_S_C BIT P2.7 ;P0.3
LED_S_DV BIT P0.7 ;P0.2
SETUP BIT P3.2
UP BIT P3.3
DOWN BIT P3.4
DATA_LED EQU P1
FLAGS DATA 20H
LASTREAD BIT FLAGS.0
ACK BIT FLAGS.5
SQW BIT FLAGS.4
BUS_FLT BIT FLAGS.6
BUSY BIT FLAGS.7
;================================================= ===============
;================================================= ===============
ORG 0100H
BEGIN:
CLR SCL
CLR SDA
NOP
SETB SCL
SETB SDA
NOP
MOV ADD_LOW,#00H
MOV DAVAVA,#00H
LCALL WRITE_BYTE
;================================================= ================
MAIN:
LCALL INIT_PORT
LCALL DIS_PLAY
LCALL SCAN_KEY
SJMP MAIN
;================================================= ================
INIT_PORT:
;================================================= =READS SECONDS
MOV ADD_LOW,#00h
LCALL READ_BYTE
MOV SEC,DAVAVA
LCALL I2C_STOP
;================================================= =READS MINUTES
MOV ADD_LOW,#01h
LCALL READ_BYTE
MOV MIN,DAVAVA
LCALL I2C_STOP
;================================================= =READS HOURS
MOV ADD_LOW,#02h
LCALL READ_BYTE
MOV HOUR,DAVAVA
LCALL I2C_STOP
;================================================= =READS DAY
MOV ADD_LOW,#03h
LCALL READ_BYTE
MOV DAY,DAVAVA
LCALL I2C_STOP
;================================================= =READS DATE OF WEEK
MOV ADD_LOW,#04h
LCALL READ_BYTE
MOV DATE,DAVAVA
LCALL I2C_STOP
;================================================= =READS MONTHS
MOV ADD_LOW,#05h
LCALL READ_BYTE
MOV MONTH,DAVAVA
LCALL I2C_STOP
;================================================= =READS YEARS
MOV ADD_LOW,#06H
LCALL READ_BYTE
MOV YEAR,DAVAVA
LCALL I2C_STOP
;=====
RET
;================================================= =================================
;=====stop I2C communication
I2C_Stop:
CLR SDA
SETB SCL
NOP
SETB SDA
RET
;================================================= =================================
;=====ANYTHING BELOW IS JUST PROCEDURES TO GET THE DATA, JUST COPY AND PASTE FROM
;=====HERE DOWN, IT'S EXPLAINED AS WELL, IF YOU INSIST. CHANGE THE PROCEDURE NAMES IF
;=====ANY CONFLICT HAPPENS WHEN YOU WRITE A LONGER PROGRAM
;=====
;************************************************* ****
;* WRITE DAVAVA TO DS1307 1 BYTE *
;* INPUT : ADD_LOW *
;* : DAVAVA *
;************************************************* ****
WRITE_BYTE: CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_W ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SCL
MOV A,ADD_LOW ;send address low
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SCL
MOV A,DAVAVA ;send DAVAVA
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SDA
CLR SCL
SETB SCL ;stop bit
SETB SDA
RET
;************************************************* *****
;* READ DAVAVA FROM DS1307 1 BYTE *
;* INPUT : ADD_HIGH *
;* : ADD_LOW *
;* OUTPUT : DAVAVA *
;************************************************* *****
READ_BYTE: CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_W ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
MOV A,ADD_LOW ;send address low
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
SETB SCL
SETB SDA
CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_R ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
LCALL LOOP_READ
SETB SDA
SETB SCL
CLR SCL
SETB SCL ;stop bit
SETB SDA
RET
;************************************************* ***
;* WRITE *
;* INPUT: ACC *
;************************************************* ***
LOOP_BYTE: PUSH 02H
MOV R2,#08H
LOOP_SEND: RLC A
MOV SDA,C
SETB SCL
CLR SCL
DJNZ R2,LOOP_SEND
POP 02H
RET
;************************************************* ****
;* READ *
;* OUTPUT: ACC *
;************************************************* ****
LOOP_READ: PUSH 02H
MOV R2,#08H
LOOP_READ1: SETB SCL
MOV C,SDA
CLR SCL
RLC A
DJNZ R2,LOOP_READ1
MOV DAVAVA,A
POP 02H
RET
;================================================= ====
; HIEN THI
;================================================= ====
DIS_PLAY:
LCALL DAU_CHAM
LCALL MINN
LCALL HOURR
LCALL DAYY
LCALL DATEE
LCALL MONN
LCALL YEARR
RET
DAU_CHAM:
MOV DPTR, #TABLE2
MOV 30H,SEC ; CHUYEN GIA TRI GIAY CO TRONG 24H VAO THANH GHI TAM 30H
MOV A, 30H
ANL A, #0FH ; CHO XOA PHAN BYTE CAO, CHI LAY PHAN BYTE THAP
MOVC A, @A + DPTR
CJNE A, #80H,SS ; SO SANH VOI GIA TRI TRONG TABLE2
CLR DOT
SS: CJNE A, #00H,SECC ; SO SANH VOI GIA TRI TRONG TABLE2
SETB DOT
SECC:
MOV DPTR,#LED_7SEG
MOV A,SEC
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_S_DV
CALL DELAY_S1
CLR LED_S_DV
MOV A,SEC
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_S_C
CALL DELAY_S1
CLR LED_S_C
RET
MINN:
MOV DPTR,#LED_7SEG
MOV A,MIN
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_M_DV
CALL DELAY_S
CLR LED_M_DV
MOV A,MIN
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_M_C
CALL DELAY_S
CLR LED_M_C
RET
HOURR:
MOV DPTR,#LED_7SEG
MOV A,HOUR
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_H_DV
CALL DELAY_S
CLR LED_H_DV
MOV A,HOUR
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_H_C
CALL DELAY_S
CLR LED_H_C
RET
DAYY: MOV DPTR,#LED_7SEG
MOV A,DAY
ANL A,#00000111B
CJNE A,#01H,TTDD
MOV A,#08H
TTDD:
MOVC A,@A+DPTR
MOV DATA_LED,A
SETB LED_DAY
CALL DELAY_S1
CLR LED_DAY
RET
DATEE:
MOV DPTR,#LED_7SEG
MOV A,DATE
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_DE_DV
CALL DELAY_S1
CLR LED_DE_DV
MOV A,DATE
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_DE_C
CALL DELAY_S1
CLR LED_DE_C
RET
MONN:
MOV DPTR,#LED_7SEG
MOV A,MONTH
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_MO_DV
CALL DELAY_S1
CLR LED_MO_DV
MOV A,MONTH
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_MO_C
CALL DELAY_S1
CLR LED_MO_C
RET
YEARR:
MOV DPTR,#LED_7SEG
MOV A,YEAR
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_Y_DV
CALL DELAY_S1
CLR LED_Y_DV
MOV A,YEAR
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_Y_C
CALL DELAY_S1
CLR LED_Y_C
RET
;888888888888888888888888888888888888888
SCAN_KEY:
JB SETUP,EXIT_SCAN
ACALL DELAY
JNB SETUP,$
SJMP SET_HOUR
LCALL DELAY_K
EXIT_SCAN:
LJMP EXIT
SET_HOUR:
LCALL DELAY_K
JB UP,SCAN_DOWN_1
JNB UP,$
INC HOUR
MOV A,HOUR
CJNE A,#24H,SCAN_DOWN_1
MOV HOUR,#00H
SCAN_DOWN_1:
JB DOWN,HIEN_HOUR
JNB DOWN,$
DEC HOUR
MOV A,HOUR
CJNE A,#0FFH,HIEN_HOUR
MOV HOUR,#23H
HIEN_HOUR:
LCALL DELAY_K
MOV R5,HOUR
ACALL GIAI_MA
MOV HOUR,R5
ACALL HOURR
JB SETUP,SET_HOUR
ACALL DELAY
JNB SETUP,$
SET_MIN:
LCALL DELAY_K
JB UP,SCAN_DOWN_2
JNB UP,$
INC MIN
MOV A,MIN
CJNE A,#5AH,SCAN_DOWN_2
MOV MIN,#00H
SCAN_DOWN_2:
JB DOWN,HIEN_MIN
JNB DOWN,$
DEC MIN
MOV A,MIN
CJNE A,#0FFH,HIEN_MIN
MOV MIN,#59H
HIEN_MIN:
LCALL DELAY_K
MOV R5,MIN
ACALL GIAI_MA
MOV MIN,R5
ACALL MINN
JB SETUP,SET_MIN
ACALL DELAY
JNB SETUP,$
SET_SEC:
LCALL DELAY_K
JB UP,SCAN_DOWN_3
JNB UP,$
INC SEC
MOV A,SEC
CJNE A,#5AH,SCAN_DOWN_3
MOV SEC,#00H
SCAN_DOWN_3:
JB DOWN,HIEN_SEC
JNB DOWN,$
DEC SEC
MOV A,SEC
CJNE A,#0FFH,HIEN_SEC
MOV SEC,#59H
HIEN_SEC:
LCALL DELAY_K
MOV R5,SEC
ACALL GIAI_MA
MOV SEC,R5
ACALL SECC
JB SETUP,SET_SEC
ACALL DELAY
JNB SETUP,$
SET_DATE:
LCALL DELAY_K
JB UP,SCAN_DOWN_4
JNB UP,$
INC DATE
MOV A,DATE
CJNE A,#32H,SCAN_DOWN_4
MOV DATE,#01H
SCAN_DOWN_4:
JB DOWN,HIEN_DATE
JNB DOWN,$
DEC DATE
MOV A,DATE
CJNE A,#00H,HIEN_DATE
MOV DATE,#31H
HIEN_DATE:
LCALL DELAY_K
MOV R5,DATE
ACALL GIAI_MA
MOV DATE,R5
ACALL DATEE
JB SETUP,SET_DATE
ACALL DELAY
JNB SETUP,$
SET_MONTH:
LCALL DELAY_K
JB UP,SCAN_DOWN_5
JNB UP,$
INC MONTH
MOV A,MONTH
CJNE A,#13H,SCAN_DOWN_5
MOV MONTH,#01H
SCAN_DOWN_5:
JB DOWN,HIEN_MONTH
JNB DOWN,$
DEC MONTH
MOV A,MONTH
CJNE A,#00H,HIEN_MONTH
MOV MONTH,#12H
HIEN_MONTH:
MOV R5,MONTH
ACALL GIAI_MA
MOV MONTH,R5
ACALL MONN
JB SETUP,SET_MONTH
ACALL DELAY
JNB SETUP,$
SET_YEAR:
LCALL DELAY_K
JB UP,SCAN_DOWN_6
JNB UP,$
INC YEAR
MOV A,YEAR
CJNE A,#9AH,SCAN_DOWN_6
MOV YEAR,#00H
SCAN_DOWN_6:
JB DOWN,HIEN_YEAR
JNB DOWN,$
DEC YEAR
MOV A,YEAR
CJNE A,#0FFH,HIEN_YEAR
MOV YEAR,#99H
HIEN_YEAR:
LCALL DELAY_K
MOV R5,YEAR
ACALL GIAI_MA
MOV YEAR,R5
; MOV R4,MIN
; MOV MIN,#20H
; ACALL MINN
; MOV MIN,R4
ACALL YEARR
JB SETUP,SET_YEAR
ACALL DELAY
JNB SETUP,$
SET_DAY:
JB UP,SCAN_DOWN_7
JNB UP,$
INC DAY
MOV A,DAY
CJNE A,#08H,SCAN_DOWN_7
MOV DAY,#01H
SCAN_DOWN_7:
JB DOWN,HIEN_DAY
JNB DOWN,$
DEC DAY
MOV A,DAY
CJNE A,#00H,HIEN_DAY
MOV DAY,#07H
HIEN_DAY:
; MOV R5,DAY
; ACALL GIAI_MA
; MOV DAY,R5
ACALL DAYY
JB SETUP,SET_DAY
ACALL DELAY
JNB SETUP,$
ACALL SET_TIME
EXIT:
RET
;=======================================
GIAI_MA:
CJNE R5,#0AH,TEST_1
MOV R5,#10H
SJMP THOAT_TEST
TEST_1:
CJNE R5,#1AH,TEST_2
MOV R5,#20H
SJMP THOAT_TEST
TEST_2:
CJNE R5,#2AH,TEST_3
MOV R5,#30H
SJMP THOAT_TEST
TEST_3:
CJNE R5,#3AH,TEST_4
MOV R5,#40H
SJMP THOAT_TEST
TEST_4:
CJNE R5,#4AH,TEST_5
MOV R5,#50H
SJMP THOAT_TEST
TEST_5:
CJNE R5,#5AH,TEST_6
MOV R5,#60H
SJMP THOAT_TEST
TEST_6:
CJNE R5,#6AH,TEST_7
MOV R5,#70H
SJMP THOAT_TEST
TEST_7:
CJNE R5,#7AH,TEST_8
MOV R5,#80H
SJMP THOAT_TEST
TEST_8:
CJNE R5,#8AH,TEST_9
MOV R5,#90H
SJMP THOAT_TEST
TEST_9:
CJNE R5,#8FH,TEST_10
MOV R5,#89H
SJMP THOAT_TEST
TEST_10:
CJNE R5,#7FH,TEST_11
MOV R5,#79H
SJMP THOAT_TEST
TEST_11:
CJNE R5,#6FH,TEST_12
MOV R5,#69H
SJMP THOAT_TEST
TEST_12:
CJNE R5,#5FH,TEST_13
MOV R5,#59H
SJMP THOAT_TEST
TEST_13:
CJNE R5,#4FH,TEST_14
MOV R5,#49H
SJMP THOAT_TEST
TEST_14:
CJNE R5,#3FH,TEST_15
MOV R5,#39H
SJMP THOAT_TEST
TEST_15:
CJNE R5,#2FH,TEST_16
MOV R5,#29H
SJMP THOAT_TEST
TEST_16:
CJNE R5,#1FH,TEST_17
MOV R5,#19H
SJMP THOAT_TEST
TEST_17:
CJNE R5,#0FH,THOAT_TEST
MOV R5,#09H
THOAT_TEST:
RET
;==========================================
SET_TIME:
ACALL START_DS
MOV A,#0D0H
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,#00H
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,SEC
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,MIN
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,HOUR
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,DAY
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,DATE
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,MONTH
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,YEAR
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,#10H ;OUT: 1HZ
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,#15H ;GIA TRI KIEM TRA DS
ACALL LOOP_BYTE
SETB SCL
CLR SCL
ACALL STOP_DS
RET
;==========================================
START_DS:
CLR SCL
SETB SCL
SETB SDA
SETB SCL
NOP
CLR SDA
CLR SCL
RET
;==========================================
STOP_DS:
SETB SCL
CLR SCL
CLR SDA
SETB SCL
NOP
SETB SDA
RET
;************************************************* ****
;* DELAYS *
;* *
;************************************************* ****
DELAY4M: PUSH DPH
PUSH DPL
MOV DPTR,#0000H
DEL4: INC DPTR
MOV A,DPL
ORL A,DPH
JNZ DEL4
POP DPL
POP DPH
RET
;================================================= ====
DELAY:
PUSH 1
PUSH 0
MOV R1,#50
MOV R0,#100
LOOP:
DJNZ R0,LOOP
DJNZ R1,LOOP
POP 0
POP 1
RET
DELAY_K: MOV R0,#100
DELK: MOV R1,#50
DJNZ R1,$
DJNZ R0,DELK
RET
DELAY_S: MOV R0,#25
DEL: MOV R1,#25
DJNZ R1,$
DJNZ R0,DEL
RET
DELAY_S1: MOV R0,#15
DEL1: MOV R1,#15
DJNZ R1,$
DJNZ R0,DEL1
RET
; MA LED
LED_7SEG:
DB 01000000b,01111001b,00100100b,00110000b,000011001b ,00010010b,00000010b,01111000b,00h,00010000b
;DB 00111111B,00000110B,01011011B,01001111B,01100110B, 01101101B,01111101B,00000111B,01111111B,01101111B
;DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
TABLE2:
DB 80H,00H,80H,00H,80H,00H,80H,00H,80H,00H
END
đây là code của em sưu tầm và chỉnh sửa từ nhiều nguồn. mong mọi người chỉ giáo. em chân thành cám ơn
; KHAI BAO
;===========
CONT_BYTE_W EQU 11010000B
CONT_BYTE_R EQU 11010001B
ORG 06H
ADD_LOW EQU 62H
DAVAVA EQU 63H
SEC DATA 24H
MIN DATA 25H
HOUR DATA 26H
DAY DATA 27H
DATE DATA 28H
MONTH DATA 49H
YEAR DATA 2AH
SCL BIT P3.0
SDA BIT P3.1
LED_H_C BIT P2.0
LED_H_DV BIT P2.1
LED_M_C BIT P2.2
LED_M_DV BIT P2.3
LED_DAY BIT P2.4
LED_DE_C BIT P2.5
LED_DE_DV BIT P2.6
LED_MO_C BIT P0.3 ;P2.7
LED_MO_DV BIT P0.2 ;P0.7
LED_Y_C BIT P0.6
LED_Y_DV BIT P0.5
DOT BIT P0.4
LED_S_C BIT P2.7 ;P0.3
LED_S_DV BIT P0.7 ;P0.2
SETUP BIT P3.2
UP BIT P3.3
DOWN BIT P3.4
DATA_LED EQU P1
FLAGS DATA 20H
LASTREAD BIT FLAGS.0
ACK BIT FLAGS.5
SQW BIT FLAGS.4
BUS_FLT BIT FLAGS.6
BUSY BIT FLAGS.7
;================================================= ===============
;================================================= ===============
ORG 0100H
BEGIN:
CLR SCL
CLR SDA
NOP
SETB SCL
SETB SDA
NOP
MOV ADD_LOW,#00H
MOV DAVAVA,#00H
LCALL WRITE_BYTE
;================================================= ================
MAIN:
LCALL INIT_PORT
LCALL DIS_PLAY
LCALL SCAN_KEY
SJMP MAIN
;================================================= ================
INIT_PORT:
;================================================= =READS SECONDS
MOV ADD_LOW,#00h
LCALL READ_BYTE
MOV SEC,DAVAVA
LCALL I2C_STOP
;================================================= =READS MINUTES
MOV ADD_LOW,#01h
LCALL READ_BYTE
MOV MIN,DAVAVA
LCALL I2C_STOP
;================================================= =READS HOURS
MOV ADD_LOW,#02h
LCALL READ_BYTE
MOV HOUR,DAVAVA
LCALL I2C_STOP
;================================================= =READS DAY
MOV ADD_LOW,#03h
LCALL READ_BYTE
MOV DAY,DAVAVA
LCALL I2C_STOP
;================================================= =READS DATE OF WEEK
MOV ADD_LOW,#04h
LCALL READ_BYTE
MOV DATE,DAVAVA
LCALL I2C_STOP
;================================================= =READS MONTHS
MOV ADD_LOW,#05h
LCALL READ_BYTE
MOV MONTH,DAVAVA
LCALL I2C_STOP
;================================================= =READS YEARS
MOV ADD_LOW,#06H
LCALL READ_BYTE
MOV YEAR,DAVAVA
LCALL I2C_STOP
;=====
RET
;================================================= =================================
;=====stop I2C communication
I2C_Stop:
CLR SDA
SETB SCL
NOP
SETB SDA
RET
;================================================= =================================
;=====ANYTHING BELOW IS JUST PROCEDURES TO GET THE DATA, JUST COPY AND PASTE FROM
;=====HERE DOWN, IT'S EXPLAINED AS WELL, IF YOU INSIST. CHANGE THE PROCEDURE NAMES IF
;=====ANY CONFLICT HAPPENS WHEN YOU WRITE A LONGER PROGRAM
;=====
;************************************************* ****
;* WRITE DAVAVA TO DS1307 1 BYTE *
;* INPUT : ADD_LOW *
;* : DAVAVA *
;************************************************* ****
WRITE_BYTE: CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_W ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SCL
MOV A,ADD_LOW ;send address low
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SCL
MOV A,DAVAVA ;send DAVAVA
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SDA
CLR SCL
SETB SCL ;stop bit
SETB SDA
RET
;************************************************* *****
;* READ DAVAVA FROM DS1307 1 BYTE *
;* INPUT : ADD_HIGH *
;* : ADD_LOW *
;* OUTPUT : DAVAVA *
;************************************************* *****
READ_BYTE: CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_W ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
MOV A,ADD_LOW ;send address low
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
SETB SCL
SETB SDA
CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_R ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
LCALL LOOP_READ
SETB SDA
SETB SCL
CLR SCL
SETB SCL ;stop bit
SETB SDA
RET
;************************************************* ***
;* WRITE *
;* INPUT: ACC *
;************************************************* ***
LOOP_BYTE: PUSH 02H
MOV R2,#08H
LOOP_SEND: RLC A
MOV SDA,C
SETB SCL
CLR SCL
DJNZ R2,LOOP_SEND
POP 02H
RET
;************************************************* ****
;* READ *
;* OUTPUT: ACC *
;************************************************* ****
LOOP_READ: PUSH 02H
MOV R2,#08H
LOOP_READ1: SETB SCL
MOV C,SDA
CLR SCL
RLC A
DJNZ R2,LOOP_READ1
MOV DAVAVA,A
POP 02H
RET
;================================================= ====
; HIEN THI
;================================================= ====
DIS_PLAY:
LCALL DAU_CHAM
LCALL MINN
LCALL HOURR
LCALL DAYY
LCALL DATEE
LCALL MONN
LCALL YEARR
RET
DAU_CHAM:
MOV DPTR, #TABLE2
MOV 30H,SEC ; CHUYEN GIA TRI GIAY CO TRONG 24H VAO THANH GHI TAM 30H
MOV A, 30H
ANL A, #0FH ; CHO XOA PHAN BYTE CAO, CHI LAY PHAN BYTE THAP
MOVC A, @A + DPTR
CJNE A, #80H,SS ; SO SANH VOI GIA TRI TRONG TABLE2
CLR DOT
SS: CJNE A, #00H,SECC ; SO SANH VOI GIA TRI TRONG TABLE2
SETB DOT
SECC:
MOV DPTR,#LED_7SEG
MOV A,SEC
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_S_DV
CALL DELAY_S1
CLR LED_S_DV
MOV A,SEC
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_S_C
CALL DELAY_S1
CLR LED_S_C
RET
MINN:
MOV DPTR,#LED_7SEG
MOV A,MIN
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_M_DV
CALL DELAY_S
CLR LED_M_DV
MOV A,MIN
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_M_C
CALL DELAY_S
CLR LED_M_C
RET
HOURR:
MOV DPTR,#LED_7SEG
MOV A,HOUR
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_H_DV
CALL DELAY_S
CLR LED_H_DV
MOV A,HOUR
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_H_C
CALL DELAY_S
CLR LED_H_C
RET
DAYY: MOV DPTR,#LED_7SEG
MOV A,DAY
ANL A,#00000111B
CJNE A,#01H,TTDD
MOV A,#08H
TTDD:
MOVC A,@A+DPTR
MOV DATA_LED,A
SETB LED_DAY
CALL DELAY_S1
CLR LED_DAY
RET
DATEE:
MOV DPTR,#LED_7SEG
MOV A,DATE
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_DE_DV
CALL DELAY_S1
CLR LED_DE_DV
MOV A,DATE
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_DE_C
CALL DELAY_S1
CLR LED_DE_C
RET
MONN:
MOV DPTR,#LED_7SEG
MOV A,MONTH
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_MO_DV
CALL DELAY_S1
CLR LED_MO_DV
MOV A,MONTH
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_MO_C
CALL DELAY_S1
CLR LED_MO_C
RET
YEARR:
MOV DPTR,#LED_7SEG
MOV A,YEAR
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_Y_DV
CALL DELAY_S1
CLR LED_Y_DV
MOV A,YEAR
SWAP A
ANL A, #0FH
MOVC A, @A + DPTR
MOV DATA_LED,A
SETB LED_Y_C
CALL DELAY_S1
CLR LED_Y_C
RET
;888888888888888888888888888888888888888
SCAN_KEY:
JB SETUP,EXIT_SCAN
ACALL DELAY
JNB SETUP,$
SJMP SET_HOUR
LCALL DELAY_K
EXIT_SCAN:
LJMP EXIT
SET_HOUR:
LCALL DELAY_K
JB UP,SCAN_DOWN_1
JNB UP,$
INC HOUR
MOV A,HOUR
CJNE A,#24H,SCAN_DOWN_1
MOV HOUR,#00H
SCAN_DOWN_1:
JB DOWN,HIEN_HOUR
JNB DOWN,$
DEC HOUR
MOV A,HOUR
CJNE A,#0FFH,HIEN_HOUR
MOV HOUR,#23H
HIEN_HOUR:
LCALL DELAY_K
MOV R5,HOUR
ACALL GIAI_MA
MOV HOUR,R5
ACALL HOURR
JB SETUP,SET_HOUR
ACALL DELAY
JNB SETUP,$
SET_MIN:
LCALL DELAY_K
JB UP,SCAN_DOWN_2
JNB UP,$
INC MIN
MOV A,MIN
CJNE A,#5AH,SCAN_DOWN_2
MOV MIN,#00H
SCAN_DOWN_2:
JB DOWN,HIEN_MIN
JNB DOWN,$
DEC MIN
MOV A,MIN
CJNE A,#0FFH,HIEN_MIN
MOV MIN,#59H
HIEN_MIN:
LCALL DELAY_K
MOV R5,MIN
ACALL GIAI_MA
MOV MIN,R5
ACALL MINN
JB SETUP,SET_MIN
ACALL DELAY
JNB SETUP,$
SET_SEC:
LCALL DELAY_K
JB UP,SCAN_DOWN_3
JNB UP,$
INC SEC
MOV A,SEC
CJNE A,#5AH,SCAN_DOWN_3
MOV SEC,#00H
SCAN_DOWN_3:
JB DOWN,HIEN_SEC
JNB DOWN,$
DEC SEC
MOV A,SEC
CJNE A,#0FFH,HIEN_SEC
MOV SEC,#59H
HIEN_SEC:
LCALL DELAY_K
MOV R5,SEC
ACALL GIAI_MA
MOV SEC,R5
ACALL SECC
JB SETUP,SET_SEC
ACALL DELAY
JNB SETUP,$
SET_DATE:
LCALL DELAY_K
JB UP,SCAN_DOWN_4
JNB UP,$
INC DATE
MOV A,DATE
CJNE A,#32H,SCAN_DOWN_4
MOV DATE,#01H
SCAN_DOWN_4:
JB DOWN,HIEN_DATE
JNB DOWN,$
DEC DATE
MOV A,DATE
CJNE A,#00H,HIEN_DATE
MOV DATE,#31H
HIEN_DATE:
LCALL DELAY_K
MOV R5,DATE
ACALL GIAI_MA
MOV DATE,R5
ACALL DATEE
JB SETUP,SET_DATE
ACALL DELAY
JNB SETUP,$
SET_MONTH:
LCALL DELAY_K
JB UP,SCAN_DOWN_5
JNB UP,$
INC MONTH
MOV A,MONTH
CJNE A,#13H,SCAN_DOWN_5
MOV MONTH,#01H
SCAN_DOWN_5:
JB DOWN,HIEN_MONTH
JNB DOWN,$
DEC MONTH
MOV A,MONTH
CJNE A,#00H,HIEN_MONTH
MOV MONTH,#12H
HIEN_MONTH:
MOV R5,MONTH
ACALL GIAI_MA
MOV MONTH,R5
ACALL MONN
JB SETUP,SET_MONTH
ACALL DELAY
JNB SETUP,$
SET_YEAR:
LCALL DELAY_K
JB UP,SCAN_DOWN_6
JNB UP,$
INC YEAR
MOV A,YEAR
CJNE A,#9AH,SCAN_DOWN_6
MOV YEAR,#00H
SCAN_DOWN_6:
JB DOWN,HIEN_YEAR
JNB DOWN,$
DEC YEAR
MOV A,YEAR
CJNE A,#0FFH,HIEN_YEAR
MOV YEAR,#99H
HIEN_YEAR:
LCALL DELAY_K
MOV R5,YEAR
ACALL GIAI_MA
MOV YEAR,R5
; MOV R4,MIN
; MOV MIN,#20H
; ACALL MINN
; MOV MIN,R4
ACALL YEARR
JB SETUP,SET_YEAR
ACALL DELAY
JNB SETUP,$
SET_DAY:
JB UP,SCAN_DOWN_7
JNB UP,$
INC DAY
MOV A,DAY
CJNE A,#08H,SCAN_DOWN_7
MOV DAY,#01H
SCAN_DOWN_7:
JB DOWN,HIEN_DAY
JNB DOWN,$
DEC DAY
MOV A,DAY
CJNE A,#00H,HIEN_DAY
MOV DAY,#07H
HIEN_DAY:
; MOV R5,DAY
; ACALL GIAI_MA
; MOV DAY,R5
ACALL DAYY
JB SETUP,SET_DAY
ACALL DELAY
JNB SETUP,$
ACALL SET_TIME
EXIT:
RET
;=======================================
GIAI_MA:
CJNE R5,#0AH,TEST_1
MOV R5,#10H
SJMP THOAT_TEST
TEST_1:
CJNE R5,#1AH,TEST_2
MOV R5,#20H
SJMP THOAT_TEST
TEST_2:
CJNE R5,#2AH,TEST_3
MOV R5,#30H
SJMP THOAT_TEST
TEST_3:
CJNE R5,#3AH,TEST_4
MOV R5,#40H
SJMP THOAT_TEST
TEST_4:
CJNE R5,#4AH,TEST_5
MOV R5,#50H
SJMP THOAT_TEST
TEST_5:
CJNE R5,#5AH,TEST_6
MOV R5,#60H
SJMP THOAT_TEST
TEST_6:
CJNE R5,#6AH,TEST_7
MOV R5,#70H
SJMP THOAT_TEST
TEST_7:
CJNE R5,#7AH,TEST_8
MOV R5,#80H
SJMP THOAT_TEST
TEST_8:
CJNE R5,#8AH,TEST_9
MOV R5,#90H
SJMP THOAT_TEST
TEST_9:
CJNE R5,#8FH,TEST_10
MOV R5,#89H
SJMP THOAT_TEST
TEST_10:
CJNE R5,#7FH,TEST_11
MOV R5,#79H
SJMP THOAT_TEST
TEST_11:
CJNE R5,#6FH,TEST_12
MOV R5,#69H
SJMP THOAT_TEST
TEST_12:
CJNE R5,#5FH,TEST_13
MOV R5,#59H
SJMP THOAT_TEST
TEST_13:
CJNE R5,#4FH,TEST_14
MOV R5,#49H
SJMP THOAT_TEST
TEST_14:
CJNE R5,#3FH,TEST_15
MOV R5,#39H
SJMP THOAT_TEST
TEST_15:
CJNE R5,#2FH,TEST_16
MOV R5,#29H
SJMP THOAT_TEST
TEST_16:
CJNE R5,#1FH,TEST_17
MOV R5,#19H
SJMP THOAT_TEST
TEST_17:
CJNE R5,#0FH,THOAT_TEST
MOV R5,#09H
THOAT_TEST:
RET
;==========================================
SET_TIME:
ACALL START_DS
MOV A,#0D0H
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,#00H
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,SEC
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,MIN
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,HOUR
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,DAY
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,DATE
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,MONTH
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,YEAR
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,#10H ;OUT: 1HZ
ACALL LOOP_BYTE
SETB SCL
CLR SCL
MOV A,#15H ;GIA TRI KIEM TRA DS
ACALL LOOP_BYTE
SETB SCL
CLR SCL
ACALL STOP_DS
RET
;==========================================
START_DS:
CLR SCL
SETB SCL
SETB SDA
SETB SCL
NOP
CLR SDA
CLR SCL
RET
;==========================================
STOP_DS:
SETB SCL
CLR SCL
CLR SDA
SETB SCL
NOP
SETB SDA
RET
;************************************************* ****
;* DELAYS *
;* *
;************************************************* ****
DELAY4M: PUSH DPH
PUSH DPL
MOV DPTR,#0000H
DEL4: INC DPTR
MOV A,DPL
ORL A,DPH
JNZ DEL4
POP DPL
POP DPH
RET
;================================================= ====
DELAY:
PUSH 1
PUSH 0
MOV R1,#50
MOV R0,#100
LOOP:
DJNZ R0,LOOP
DJNZ R1,LOOP
POP 0
POP 1
RET
DELAY_K: MOV R0,#100
DELK: MOV R1,#50
DJNZ R1,$
DJNZ R0,DELK
RET
DELAY_S: MOV R0,#25
DEL: MOV R1,#25
DJNZ R1,$
DJNZ R0,DEL
RET
DELAY_S1: MOV R0,#15
DEL1: MOV R1,#15
DJNZ R1,$
DJNZ R0,DEL1
RET
; MA LED
LED_7SEG:
DB 01000000b,01111001b,00100100b,00110000b,000011001b ,00010010b,00000010b,01111000b,00h,00010000b
;DB 00111111B,00000110B,01011011B,01001111B,01100110B, 01101101B,01111101B,00000111B,01111111B,01101111B
;DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
TABLE2:
DB 80H,00H,80H,00H,80H,00H,80H,00H,80H,00H
END
Comment