ban xem thử cái này
http://machdientu.net/read.php?56
http://machdientu.net/read.php?56
;=====ADD_LOW LA THANH GHI CHUA DIA CHI CUA CUA CAC THANH GHI CUA RTC
;=====I2CDATA LA BIEN LUU GIA TRI THU DUOC KHI LAY TU RTC
ADD_LOW data 0bH //dia chi ban thay doi tuy` y'
I2CDATA data 0cH //dia chi ban thay doi tuy` y'
CONT_BYTE_W EQU 0D0H //byte dieu khien ghi
CONT_BYTE_R EQU 0D1H //byte dieu khien doc
scl bit P1.6 //xung clock cho rtc //dat lai tuy` y'
sda bit P1.7 //dat lai tuy` y'
//====================================
;=====STOP I2C
I2C_STOP:
CLR SDA
SETB SCL
NOP
SETB SDA
RET
;*****************************************************
WRITE_BYTE:
CLR SDA ;BIT START CHO I2C
CLR SCL
MOV A,#CONT_BYTE_W ;GUI BYTE DIEU KHIEN
CALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;HOI VONG` DE KIEM TRA CO` BAN
CLR SCL
MOV A,ADD_LOW ;GUI DIA CHI VAO RTC QUA I2C
CALL LOOP_BYTE ;CHUONG TRINH CON GUI DIA CHI BANG I2C
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;HOI VONG DE KIEM TRA CO BAN
CLR SCL
MOV A,I2CDATA ;GUI DATA VAO DIA CHI TRONG RTC
CALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;LAP LAI NEU CO` BAN CHUA DUOC XOA'
CLR SDA
CLR SCL
SETB SCL ;STOP BIT
SETB SDA
RET
;******************************************************CHUONG TRINH CON DOC BYTE TU RTC KIEU I2C
READ_BYTE:
CLR SDA ;START BIT
CLR SCL
MOV A,#CONT_BYTE_W ;GUI BYTE DIEU KHIEN
CALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN
CLR SCL
MOV A,ADD_LOW ;GUI DIA CHI
CALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN SDA CHUA DC XOA'
CLR SCL
SETB SCL
SETB SDA
CLR SDA ;STOP BIT
CLR SCL
MOV A,#CONT_BYTE_R ;GUI BYTE DIEU KHIEN
CALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN CHUA DUOC XOA'
CLR SCL
CALL LOOP_READ
SETB SDA
SETB SCL
CLR SCL
SETB SCL ;STOP BIT
SETB SDA
RET
;*******************************************************************************
;* GHI VAO RTC THEO PHUONG THUC I2C *
;* 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
;*******************************************************************************
;* DOC TU RTC THEO PHUONG THUC I2C *
;* 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 I2CDATA,A
POP 02H
RET
;=====ADD_LOW LA THANH GHI CHUA DIA CHI CUA CUA CAC THANH GHI CUA RTC ;=====I2CDATA LA BIEN LUU GIA TRI THU DUOC KHI LAY TU RTC ADD_LOW EQU 20H //dia chi ban thay doi tuy` y' I2CDATA EQU 21H //dia chi ban thay doi tuy` y' CONT_BYTE_W EQU 0D0H //byte dieu khien ghi CONT_BYTE_R EQU 0D1H //byte dieu khien doc scl bit P3.0 //xung clock cho rtc // nhớ kết nối cho đúng sda bit P3.1 //nhớ kết nối cho đúng //==================================== ORG 0000H MAIN: mov ADD_LOW,#0 ;thanhghi 0 mov I2CDATA,#0 ;CH=0 call WRITE_BYTE mov ADD_LOW,#7 ;thanhghi 7 mov I2CDATA,#00010000b ;tao xung 1hz tai chan 7 ds1307 call WRITE_BYTE jmp $ ;=====STOP I2C I2C_STOP: CLR SDA SETB SCL NOP SETB SDA RET ;***************************************************** WRITE_BYTE: CLR SDA ;BIT START CHO I2C CLR SCL MOV A,#CONT_BYTE_W ;GUI BYTE DIEU KHIEN CALL LOOP_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;HOI VONG` DE KIEM TRA CO` BAN CLR SCL MOV A,ADD_LOW ;GUI DIA CHI VAO RTC QUA I2C CALL LOOP_BYTE ;CHUONG TRINH CON GUI DIA CHI BANG I2C SETB SDA SETB SCL JB SDA,WRITE_BYTE ;HOI VONG DE KIEM TRA CO BAN CLR SCL MOV A,I2CDATA ;GUI DATA VAO DIA CHI TRONG RTC CALL LOOP_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;LAP LAI NEU CO` BAN CHUA DUOC XOA' CLR SDA CLR SCL SETB SCL ;STOP BIT SETB SDA RET ;******************************************************CHUONG TRINH CON DOC BYTE TU RTC KIEU I2C READ_BYTE: CLR SDA ;START BIT CLR SCL MOV A,#CONT_BYTE_W ;GUI BYTE DIEU KHIEN CALL LOOP_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN CLR SCL MOV A,ADD_LOW ;GUI DIA CHI CALL LOOP_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN SDA CHUA DC XOA' CLR SCL SETB SCL SETB SDA CLR SDA ;STOP BIT CLR SCL MOV A,#CONT_BYTE_R ;GUI BYTE DIEU KHIEN CALL LOOP_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN CHUA DUOC XOA CLR SCL CALL LOOP_READ SETB SDA SETB SCL CLR SCL SETB SCL ;STOP BIT SETB SDA RET ;******************************************************************************* ;* GHI VAO RTC THEO PHUONG THUC I2C * ;* 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 ;******************************************************************************* ;* DOC TU RTC THEO PHUONG THUC I2C * ;* 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 I2CDATA,A POP 02H RET END
MAIN: mov ADD_LOW,#0 ;thanhghi 0 mov I2CDATA,#0 ;CH=0 call WRITE_BYTE mov ADD_LOW,#7 ;thanhghi 7 mov I2CDATA,#00010000b ;tao xung 1hz tai chan 7 ds1307 call WRITE_BYTE jmp $ ;=====STOP I2C I2C_STOP: CLR SDA SETB SCL NOP SETB SDA RET
MAIN: mov ADD_LOW,#0 ;thanhghi 0 mov I2CDATA,#0 ;CH=0 call WRITE_BYTE mov ADD_LOW,#7 ;thanhghi 7 mov I2CDATA,#00010000b ;tao xung 1hz tai chan 7 ds1307 call WRITE_BYTE jmp $ ;=====STOP I2C I2C_STOP: CLR SDA SETB SCL NOP SETB SDA RET
Comment