Có anh chị nào trong điễn đàn đã làm thành công mạch thực tế đồng hồ số DS1307 dùng 89 không xin share cho em chút kinh nghiệm với, em làm 2 cái rồi mà vẫn không chạy, mô phỏng thì chạy ok, nên bây giờ không biết là sai code hay là phần cứng nữa anh chị nào có mạch hay code assembly thì xin giúp đỡ em với. Em coi trong datasheet của 1307 thì nó có cái giản đồ thời gian, không biết mình có phải tuân theo cái giản đồ này khi lập trình ko
mạch phần cứng của em
code:
;KHAI BAO
GIAY DATA 60H
PHUT DATA 61H
GIO DATA 62H
DONVI_GIAY DATA 63H
CHUC_GIAY DATA 64H
DONVI_PHUT DATA 65H
CHUC_PHUT DATA 66H
DONVI_GIO DATA 67H
CHUC_GIO DATA 68H
LED_GIAY BIT P2.0
LED_C_GIAY BIT P2.1
LED_PHUT BIT P2.2
LED_C_PHUT BIT P2.3
LED_GIO BIT P2.4
LED_C_GIO BIT P2.5
;--------I2C-------
SCL BIT P3.0
SDA BIT P3.1
LED_DATA EQU P0
BYTE_W EQU 11010000B
BYTE_R EQU 11010001B
ADD_REG EQU 73H
DATA_RTC EQU 74H
DATA_GIAY EQU 75H
DATA_PHUT EQU 76H
DATA_GIO EQU 77H
ORG 0000H
MAIN:
MOV GIAY,#0
MOV PHUT,#0
MOV GIO,#0
MOV TMOD,#10H
MOV ADD_REG,#0
MOV DATA_RTC,#0
MOV DATA_GIAY,#66
MOV DATA_PHUT,#89
MOV DATA_GIO,#8
MOV A,#00H
MOV LED_DATA,#00H
;**********WRITE_CLOCK**********
WRITE_CLOCK:
SETB SCL ;KIEM TRA BUS ROI
SETB SDA
CALL START_CLOCK
MOV A,#BYTE_W
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,ADD_REG
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,DATA_GIAY
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,DATA_PHUT
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,DATA_GIO
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
CALL STOP_CLOCK
;********CHUONG TRINH NHAN DU LIEU VA HIEN THI***********
RECEIVE_DATA:
;========READS SECONDS=============
READ_SEC:
MOV ADD_REG,#00h
LCALL READ_RTC
MOV GIAY,DATA_RTC
;========READS MINUTES=============
READ_MIN:
MOV ADD_REG,#01h
LCALL READ_RTC
MOV PHUT,DATA_RTC
;========READS HOURS=============
READ_HOUR:
MOV ADD_REG,#02h
LCALL READ_RTC
MOV GIO,DATA_RTC
;***********HIEN THI RA LED***********
CALL HIENTHI_S
CALL HIENTHI_P
CALL HIENTHI_G
LJMP RECEIVE_DATA
;*****CHUONG TRINH DOC DU LIEU TU 1307*****
READ_RTC:
SETB SCL ;KIEM TRA BUS ROI
SETB SDA
CALL START_CLOCK
MOV A,#BYTE_W
CALL TACH_BYTE
;4 HANG ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,ADD_REG
CALL TACH_BYTE
;4 HANG ACK
SETB SCL
JB SDA,$
CLR SCL
SETB SCL ;KIEM TRA BUS ROI
SETB SDA
CALL START_CLOCK
MOV A,#BYTE_R
CALL TACH_BYTE
;4 HANG ACK
SETB SCL
JB SDA,$
CLR SCL
CALL SEND_DATA
SETB SDA ;NOT ACK
SETB SCL
CLR SCL
CALL STOP_CLOCK
RET
;--------------CTR CON HIENTHI_RTC---------------
;**********************************
HIENTHI_S:
CALL SWAP_GIAY
MOV DPTR,#MALED
MOV A,DONVI_GIAY
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_GIAY
CLR LED_C_GIAY
CALL DELAY
CLR LED_GIAY
MOV DPTR,#MALED
MOV A,CHUC_GIAY
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_C_GIAY
CALL DELAY
CLR LED_C_GIAY
RET
HIENTHI_P:
CALL SWAP_PHUT
MOV DPTR,#MALED
MOV A,DONVI_PHUT
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_PHUT
CLR LED_C_PHUT
CALL DELAY
CLR LED_PHUT
MOV DPTR,#MALED
MOV A,CHUC_PHUT
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_C_PHUT
CALL DELAY
CLR LED_C_PHUT
RET
HIENTHI_G:
CALL SWAP_GIO
MOV DPTR,#MALED
MOV A,DONVI_GIO
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_GIO
CLR LED_C_GIO
CALL DELAY
CLR LED_GIO
MOV DPTR,#MALED
MOV A,CHUC_GIO
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_C_GIO
CALL DELAY
CLR LED_C_GIO
RET
;**********CHUONG TRINH TACH BYTE***********
TACH_BYTE:
MOV R7,#8
GG: RLC A
MOV SDA,C
SETB SCL
CLR SCL
DJNZ R7,GG
RET
;----------DUA DU LIEU RA MANG I2C--------------
SEND_DATA:
MOV R7,#8
CLR SCL
DG: MOV C,SDA
SETB SCL
CLR SCL
RLC A
DJNZ R7,DG
MOV DATA_RTC,A
RET
;---------START-------------
START_CLOCK:
SETB SCL ;trong khi SCL=1
NOP
SETB SDA ;SDA tu 1
NOP
CLR SDA ;xuong 0
NOP
CLR SCL
RET
;---------STOP-----------
STOP_CLOCK:
SETB SCL ;trong khi SCL=1
NOP
CLR SDA ;SDA tu 0
NOP
SETB SDA ; len 1
NOP
CLR SCL
RET
;**********CHUONG TRINH DOI SANG MA BCD************
SWAP_GIAY:
MOV A,GIAY
ANL A,#00001111B
MOV DONVI_GIAY,A
MOV A,GIAY
SWAP A
ANL A,#00001111B
MOV CHUC_GIAY,A
RET
SWAP_PHUT:
MOV A,PHUT
ANL A,#00001111B
MOV DONVI_PHUT,A
MOV A,PHUT
SWAP A
ANL A,#00001111B
MOV CHUC_PHUT,A
RET
SWAP_GIO:
MOV A,GIO
ANL A,#00001111B
MOV DONVI_GIO,A
MOV A,GIO
SWAP A
ANL A,#00001111B
MOV CHUC_GIO,A
RET
DELAY:
clr tr1
mov th1,#high(-3000)
mov tl1,#low(-3000)
setb tr1
jnb tf1,$
clr tf1
ret
MALED:
DB 03H,9FH,25H,0DH,99H,49H,41H,1FH,01H,09H
END
mạch phần cứng của em
code:
;KHAI BAO
GIAY DATA 60H
PHUT DATA 61H
GIO DATA 62H
DONVI_GIAY DATA 63H
CHUC_GIAY DATA 64H
DONVI_PHUT DATA 65H
CHUC_PHUT DATA 66H
DONVI_GIO DATA 67H
CHUC_GIO DATA 68H
LED_GIAY BIT P2.0
LED_C_GIAY BIT P2.1
LED_PHUT BIT P2.2
LED_C_PHUT BIT P2.3
LED_GIO BIT P2.4
LED_C_GIO BIT P2.5
;--------I2C-------
SCL BIT P3.0
SDA BIT P3.1
LED_DATA EQU P0
BYTE_W EQU 11010000B
BYTE_R EQU 11010001B
ADD_REG EQU 73H
DATA_RTC EQU 74H
DATA_GIAY EQU 75H
DATA_PHUT EQU 76H
DATA_GIO EQU 77H
ORG 0000H
MAIN:
MOV GIAY,#0
MOV PHUT,#0
MOV GIO,#0
MOV TMOD,#10H
MOV ADD_REG,#0
MOV DATA_RTC,#0
MOV DATA_GIAY,#66
MOV DATA_PHUT,#89
MOV DATA_GIO,#8
MOV A,#00H
MOV LED_DATA,#00H
;**********WRITE_CLOCK**********
WRITE_CLOCK:
SETB SCL ;KIEM TRA BUS ROI
SETB SDA
CALL START_CLOCK
MOV A,#BYTE_W
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,ADD_REG
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,DATA_GIAY
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,DATA_PHUT
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,DATA_GIO
CALL TACH_BYTE
;4 HANG KT ACK
SETB SCL
JB SDA,$
CLR SCL
CALL STOP_CLOCK
;********CHUONG TRINH NHAN DU LIEU VA HIEN THI***********
RECEIVE_DATA:
;========READS SECONDS=============
READ_SEC:
MOV ADD_REG,#00h
LCALL READ_RTC
MOV GIAY,DATA_RTC
;========READS MINUTES=============
READ_MIN:
MOV ADD_REG,#01h
LCALL READ_RTC
MOV PHUT,DATA_RTC
;========READS HOURS=============
READ_HOUR:
MOV ADD_REG,#02h
LCALL READ_RTC
MOV GIO,DATA_RTC
;***********HIEN THI RA LED***********
CALL HIENTHI_S
CALL HIENTHI_P
CALL HIENTHI_G
LJMP RECEIVE_DATA
;*****CHUONG TRINH DOC DU LIEU TU 1307*****
READ_RTC:
SETB SCL ;KIEM TRA BUS ROI
SETB SDA
CALL START_CLOCK
MOV A,#BYTE_W
CALL TACH_BYTE
;4 HANG ACK
SETB SCL
JB SDA,$
CLR SCL
MOV A,ADD_REG
CALL TACH_BYTE
;4 HANG ACK
SETB SCL
JB SDA,$
CLR SCL
SETB SCL ;KIEM TRA BUS ROI
SETB SDA
CALL START_CLOCK
MOV A,#BYTE_R
CALL TACH_BYTE
;4 HANG ACK
SETB SCL
JB SDA,$
CLR SCL
CALL SEND_DATA
SETB SDA ;NOT ACK
SETB SCL
CLR SCL
CALL STOP_CLOCK
RET
;--------------CTR CON HIENTHI_RTC---------------
;**********************************
HIENTHI_S:
CALL SWAP_GIAY
MOV DPTR,#MALED
MOV A,DONVI_GIAY
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_GIAY
CLR LED_C_GIAY
CALL DELAY
CLR LED_GIAY
MOV DPTR,#MALED
MOV A,CHUC_GIAY
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_C_GIAY
CALL DELAY
CLR LED_C_GIAY
RET
HIENTHI_P:
CALL SWAP_PHUT
MOV DPTR,#MALED
MOV A,DONVI_PHUT
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_PHUT
CLR LED_C_PHUT
CALL DELAY
CLR LED_PHUT
MOV DPTR,#MALED
MOV A,CHUC_PHUT
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_C_PHUT
CALL DELAY
CLR LED_C_PHUT
RET
HIENTHI_G:
CALL SWAP_GIO
MOV DPTR,#MALED
MOV A,DONVI_GIO
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_GIO
CLR LED_C_GIO
CALL DELAY
CLR LED_GIO
MOV DPTR,#MALED
MOV A,CHUC_GIO
MOVC A,@A+DPTR
MOV LED_DATA,A
SETB LED_C_GIO
CALL DELAY
CLR LED_C_GIO
RET
;**********CHUONG TRINH TACH BYTE***********
TACH_BYTE:
MOV R7,#8
GG: RLC A
MOV SDA,C
SETB SCL
CLR SCL
DJNZ R7,GG
RET
;----------DUA DU LIEU RA MANG I2C--------------
SEND_DATA:
MOV R7,#8
CLR SCL
DG: MOV C,SDA
SETB SCL
CLR SCL
RLC A
DJNZ R7,DG
MOV DATA_RTC,A
RET
;---------START-------------
START_CLOCK:
SETB SCL ;trong khi SCL=1
NOP
SETB SDA ;SDA tu 1
NOP
CLR SDA ;xuong 0
NOP
CLR SCL
RET
;---------STOP-----------
STOP_CLOCK:
SETB SCL ;trong khi SCL=1
NOP
CLR SDA ;SDA tu 0
NOP
SETB SDA ; len 1
NOP
CLR SCL
RET
;**********CHUONG TRINH DOI SANG MA BCD************
SWAP_GIAY:
MOV A,GIAY
ANL A,#00001111B
MOV DONVI_GIAY,A
MOV A,GIAY
SWAP A
ANL A,#00001111B
MOV CHUC_GIAY,A
RET
SWAP_PHUT:
MOV A,PHUT
ANL A,#00001111B
MOV DONVI_PHUT,A
MOV A,PHUT
SWAP A
ANL A,#00001111B
MOV CHUC_PHUT,A
RET
SWAP_GIO:
MOV A,GIO
ANL A,#00001111B
MOV DONVI_GIO,A
MOV A,GIO
SWAP A
ANL A,#00001111B
MOV CHUC_GIO,A
RET
DELAY:
clr tr1
mov th1,#high(-3000)
mov tl1,#low(-3000)
setb tr1
jnb tf1,$
clr tf1
ret
MALED:
DB 03H,9FH,25H,0DH,99H,49H,41H,1FH,01H,09H
END
Comment