51單片機(jī)對SAA7113初始化和控制的程序代碼
NOP 本文引用地址:http://www.ex-cimer.com/article/172108.htm
SETB SCL
NOP
NOP
MOV C, SDA
MOV I2C_ERROR, C
CLR SCL
NOP
RET
;*******發(fā)送1字節(jié)數(shù)據(jù),待送數(shù)據(jù)在A中******
I2C_SEND_1BYTE:
MOV R0, #8
SEND100: RLC A
MOV SDA, C
NOP
SETB SCL
NOP
NOP
CLR SCL
DJNZ R0, SEND100
RET
;*******接收1字節(jié)數(shù)據(jù),接收數(shù)據(jù)放在A中*****
I2C_RECEIVE_1BYTE:
MOV R0, #8
RECV100: SETB SDA
NOP
SETB SCL
NOP
NOP
NOP
MOV C, SDA
CLR SCL
RLC A
DJNZ R0, RECV100
RET
;*******通過I2C總線向某一寄存器寫入一個(gè)字節(jié)數(shù)據(jù)*********
I2C_WRITE: ACALL I2C_START ;發(fā)啟動(dòng)信號
MOV A, # DeviceaddressW ;調(diào)7113地址+寫
ACALL I2C_SEND_1BYTE ;發(fā)送7113地址及“寫”命令
ACALL CHECK_ACK ;檢查7113的應(yīng)答信號
JNB I2C_ERROR, WR200 ;應(yīng)答正確,繼續(xù)
WR100: ACALL I2C_STOP ;應(yīng)答不對,返回
RET
WR200: MOV A, Subaddress ;調(diào)寄存器地址
ACALL I2C_SEND_1BYTE ;發(fā)送寄存器地址
ACALL CHECK_ACK ;檢查7113的應(yīng)答信號
JB I2C_ERROR, WR100 ;應(yīng)答不對,返回
MOV A, DATA_I2C ;調(diào)準(zhǔn)備寫入的數(shù)據(jù)
ACALL I2C_SEND_1BYTE ;發(fā)送數(shù)據(jù)字節(jié)
ACALL CHECK_ACK
JB I2C_ERROR, WR100
ACALL I2C_STOP ;發(fā)停止信號
評論