PIC16C74B單片機(jī)與AT24C64通訊
互聯(lián)網(wǎng)
摘要: ;=======================================;PIC16C74B單片機(jī)與AT24C64通訊;該程序?qū)崿F(xiàn)的功能是:;將I...
Abstract:
Key words :
;=======================================
;PIC16C74B單片機(jī)與AT24C64通訊
;該程序?qū)崿F(xiàn)的功能是:
;將I2C_A,I2C_B.....I2C_K內(nèi)容寫(xiě)入EEPROM中
;同時(shí)在EEPROM中讀出數(shù)據(jù)從新寫(xiě)入各個(gè)寄存器
;=======================================
LIST P=16C74B
#include
ERRORLEVEL -302
I2C_A EQU 0X20
I2C_B EQU 0X21
I2C_C EQU 0X22
I2C_D EQU 0X23
I2C_E EQU 0X24
I2C_F EQU 0X25
I2C_G EQU 0X26
REG1 EQU 0X2B
REG2 EQU 0X2C
COUNT EQU 0X71 ;I2C
BCOUNT EQU 0X72 ;I2C
TXBUF EQU 0X73 ;I2C
RXBUF EQU 0X74 ;I2C
BUSCON EQU 0X75 ;I2C
BUSSTA EQU 0X76 ;I2C
I2CBUF EQU 0X77 ;I2C
SLAVEADDR EQU 0XA0 ;I2C
DATAADDRH EQU 0X01 ;I2C
DATAADDRL EQU 0X00 ;I2C
;---------------------------------------
;BUSCON
;---------------------------------------
SLAVE_RW EQU 7
LAST_BYTE_RX EQU 6
SLAVE_ACTIVE EQU 5
TIME_OUT EQU 4
;---------------------------------------
;BUSSTA
;---------------------------------------
BUS_BUSY EQU 7
TX_PROGRESS EQU 6
RX_PROGRESS EQU 5
TX_SUCCESS EQU 4
RX_SUCCESS EQU 3
FATAL_ERROR EQU 2 ;SLAVE EEPROM
ABORT EQU 1
ACK_ERROR EQU 0
;---------------------------------------
R_NOT_W EQU 0
TRUE EQU 1
FALSE EQU 0
SDA EQU 0X04 ;I2C
SCL EQU 0X03 ;I2C
;=======================================
ORG 0X00
GOTO START
ORG 0X20
START BCF STATUS,RP1
BCF STATUS,RP0
CLRF RA
CLRF RB
CLRF RC
CLRF RD
CLRF RE
BSF STATUS,RP0
CLRF TRISA
CLRF TRISB
CLRF TRISC
CLRF TRISD
CLRF TRISE
MOVLW 0X02
MOVWF I2C_A
MOVLW 0X04
MOVWF I2C_B
MOVLW 0X03
MOVWF I2C_C
MOVLW 0X03
MOVWF I2C_D
MOVLW 0X05
MOVWF I2C_E
MOVLW 0X06
MOVWF I2C_F
MOVLW 0X04
MOVWF I2C_G
;=======================================
START1
CALL I2CBUS_INIT
CALL REGTOI2CBUF
CALL I2CWRITE
CALL CLRI2CBUF
CALL D10
CALL I2CREAD
CALL I2CBUFTOREG
GOTO START1
;=======================================
;CLRI2CBUF
;---------------------------------------
CLRI2CBUF
BCF STATUS,RP0
MOVLW I2CBUF
MOVWF FSR
MOVLW 0X08
MOVWF COUNT
CLRLOOP CLRF INDF
INCF FSR,F
DECFSZ COUNT,F
GOTO CLRLOOP
RETURN
;---------------------------------------
;將各個(gè)REGSTER數(shù)據(jù)寫(xiě)入 I2CBUF 緩沖區(qū)
;---------------------------------------
REGTOI2CBUF
BCF STATUS,RP0
MOVLW I2CBUF
MOVWF FSR
MOVF I2C_A,W
MOVWF INDF
INCF FSR,F
MOVF I2C_B,W
MOVWF INDF
INCF FSR,F
MOVF I2C_C,W
MOVWF INDF
INCF FSR,F
MOVF I2C_D,W
MOVWF INDF
INCF FSR,F
MOVF I2C_E,W
MOVWF INDF
INCF FSR,F
MOVF I2C_F,W
MOVWF INDF
INCF FSR,F
MOVF I2C_G,W
MOVWF INDF
RETURN
;---------------------------------------
;將 I2CBUF 緩沖區(qū)數(shù)據(jù)寫(xiě)入各個(gè)REGSTER
;---------------------------------------
I2CBUFTOREG
BCF STATUS,RP0
MOVLW I2CBUF
MOVWF FSR
MOVF INDF,W
MOVWF I2C_A
INCF FSR,F
MOVF INDF,W
MOVWF I2C_B
INCF FSR,F
MOVF INDF,W
MOVWF I2C_C
INCF FSR,F
MOVF INDF,W
MOVWF I2C_D
INCF FSR,F
MOVF INDF,W
MOVWF I2C_E
INCF FSR,F
MOVF INDF,W
MOVWF I2C_F
INCF FSR,F
MOVF INDF,W
MOVWF I2C_G
RETURN
;=======================================
;將I2CBUF緩沖區(qū)內(nèi)的數(shù)據(jù)寫(xiě)入EEPROM
;=======================================
I2CWRITE
CALL I2CSTART
BTFSS BUSSTA,BUS_BUSY
GOTO TX_FAIL
BCF BUSCON,SLAVE_RW
CALL TX_SLAVE_ADDR
BTFSS BUSSTA,TX_SUCCESS ;SUCESSFULL?
GOTO TX_FAIL
CALL TX_DATA_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_FAIL
MOVLW I2CBUF
MOVWF FSR
MOVLW 07
MOVWF COUNT
T_BYTE_LOOP
MOVF INDF,W
MOVWF TXBUF
CALL I2CTXB
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_FAIL
INCF FSR,F
DECFSZ COUNT,F
GOTO T_BYTE_LOOP
GOTO TX_PASS
TX_FAIL CALL I2CSTOP
BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
RETLW FALSE
TX_PASS
CALL I2CSTOP
BCF BUSSTA,TX_PROGRESS
RETLW TRUE
;---------------------------------------
;從 EEPROM 中讀出數(shù)據(jù)到 I2CBUF 寄存器
;---------------------------------------
I2CREAD BCF STATUS,RP0
BCF BUSSTA,RX_SUCCESS
CALL I2CSTART
BTFSS BUSSTA,BUS_BUSY
GOTO RX_FAIL
BCF BUSCON,SLAVE_RW
CALL TX_SLAVE_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO RX_FAIL
CALL TX_DATA_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO RX_FAIL
CALL I2CSTART
BSF BUSCON,SLAVE_RW
CALL TX_SLAVE_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO RX_FAIL
BCF BUSSTA,LAST_BYTE_RX
MOVLW I2CBUF
MOVWF FSR
MOVLW 07
MOVWF COUNT
R_BYTE_LOOP
MOVLW 0X01
XORWF COUNT,W
BTFSC STATUS,Z
BSF BUSSTA,LAST_BYTE_RX
CALL I2CRXB
BTFSS BUSSTA,RX_SUCCESS
GOTO RX_FAIL
MOVF RXBUF,W
MOVWF INDF
INCF FSR,F
DECFSZ COUNT,F
GOTO R_BYTE_LOOP
GOTO RX_PASS
RX_FAIL CALL I2CSTOP
BCF BUSSTA,RX_PROGRESS
BCF BUSSTA,RX_SUCCESS
RETLW FALSE
RX_PASS CALL I2CSTOP
BCF STATUS,RP0
BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,RX_PROGRESS
BSF BUSSTA,RX_SUCCESS
RETLW TRUE
;=======================================
; I2C 初始化
;=======================================
I2CBUS_INIT
BCF STATUS,RP0
MOVF RC,W
ANDLW 0X18
MOVWF RC
CLRF BUSSTA
CLRF BUSCON
RETURN
;=======================================
; 傳送設(shè)備地址
;入口: BUSCON
;出口: BUSSTA
;=======================================
TX_SLAVE_ADDR
BCF STATUS,RP0
BCF BUSSTA,ACK_ERROR
MOVLW SLAVEADDR
MOVWF TXBUF
BTFSC BUSCON,SLAVE_RW
BSF TXBUF,R_NOT_W
CALL I2CTXB
BTFSC BUSSTA,TX_SUCCESS
GOTO TXADDR_SUCCESS
CLRWDT
BTFSS BUSSTA,ACK_ERROR
CALL I2CSTOP
GOTO TXADDR_END
TXADDR_SUCCESS
CLRWDT
TXADDR_END
RETURN
;=======================================
;TX_DATA_ADDR 發(fā)送寫(xiě)/讀的地址
;=======================================
TX_DATA_ADDR
BCF STATUS,RP0
MOVLW DATAADDRH
MOVWF TXBUF
CALL I2CTXB
BCF STATUS,RP0
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_DATA_ADDR_ERROR
MOVLW DATAADDRL
MOVWF TXBUF
CALL I2CTXB
BCF STATUS,RP0
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_DATA_ADDR_ERROR
GOTO TX_DATA_END
TX_DATA_ADDR_ERROR
BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
BSF BUSSTA,ACK_ERROR
TX_DATA_END
RETURN
;=======================================
;向 EEPROM 輸出一個(gè)8位數(shù)據(jù),并檢測(cè)EEPROM的ACK信號(hào)
;入口: TXBUF 寄存器
; BUSSTA 寄存器
;出口: EEPROM 存儲(chǔ)器
; BUSSTA 寄存器
;======================================
I2CTXB BCF STATUS,RP0
BSF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
MOVLW 0X08
MOVWF BCOUNT
TXLOOP CLRWDT
BSF STATUS,RP0
BCF TRISC,SCL
NOP
BCF TRISC,SDA
BCF STATUS,RP0
RLF TXBUF,F
BSF STATUS,RP0
BTFSC STATUS,C
BSF TRISC,SDA
NOP
BSF TRISC,SCL
NOP
NOP
BCF STATUS,RP0
DECFSZ BCOUNT,F
GOTO TXLOOP
;---------------------------------------
;檢測(cè)是EEPROM否有ACK信號(hào)產(chǎn)生,產(chǎn)生為正常
;檢測(cè)方法:
; 1. 將RC口的SCL.SDA分別設(shè)置為輸出和輸入狀態(tài)
; 2. 延時(shí)2個(gè)指令周期,將SCL設(shè)置為高電平
; 3. 延時(shí)2個(gè)指令周期,檢測(cè)RC口SDA的狀態(tài)
; 4. 如果為高電平,設(shè)置錯(cuò)誤狀態(tài),返回
; 5. 如果為低電平,延時(shí)2個(gè)指令周期后,將RC口SCL設(shè)置為低電平,繼續(xù)執(zhí)行
;---------------------------------------
BSF STATUS,RP0
BCF TRISC,SCL
BSF TRISC,SDA
NOP
NOP
BSF TRISC,SCL
NOP
BCF STATUS,RP0
BTFSC RC,SDA
GOTO TX_ERR
BSF STATUS,RP0
BCF TRISC,SCL
BCF STATUS,RP0
BCF BUSSTA,TX_PROGRESS
BSF BUSSTA,TX_SUCCESS
BCF BUSSTA,ACK_ERROR
GOTO TX_END
TX_ERR BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
BSF BUSSTA,ACK_ERROR
TX_END RETURN
;=======================================
;從EEPROM中讀出 8 位的數(shù)據(jù),并發(fā)送發(fā)送ACK信號(hào)
;入口: EEPROM 存儲(chǔ)器
; BUSCON 寄存器
;出口: RXBUF 寄存器
; BUSSTA 寄存器
;=======================================
I2CRXB BCF STATUS,RP0
BSF BUSSTA,RX_PROGRESS
BCF BUSSTA,RX_SUCCESS
MOVLW 0X08
MOVWF BCOUNT
RXLOOP CLRWDT
BSF STATUS,RP0
BCF TRISC,SCL ;定義TRISC為輸出
BSF TRISC,SDA ;定義TRISC為輸入
NOP
NOP
BSF TRISC,SCL ;TRISC輸出高電平
NOP
BCF STATUS,RP0
BCF STATUS,C
BTFSC RC,SDA ;原指令為BTFSC TRISC,SDA
BSF STATUS,C
RLF RXBUF,F
DECFSZ BCOUNT,F
GOTO RXLOOP
;---------------------------------------
;發(fā)送ACK信號(hào)到EEPROM
;判斷接收的是否為最后一個(gè)字節(jié)
;LAST_BYTE_RX為1,是 SDA發(fā)送一個(gè)高電平,結(jié)束
;LAST_BYTE_RX為0,不是 SDA發(fā)送一個(gè)低電平ACK,繼續(xù)接收
;---------------------------------------
BSF STATUS,RP0
BCF TRISC,SCL
BCF TRISC,SDA
BTFSC BUSCON,LAST_BYTE_RX ;檢測(cè)是否是最后一個(gè)字節(jié)
BSF TRISC,SDA ;是最后一個(gè)字節(jié)
NOP ;發(fā)送ACK信號(hào)
BSF TRISC,SCL
NOP
NOP
NOP
NOP
BCF TRISC,SCL
BCF STATUS,RP0
BCF BUSSTA,RX_PROGRESS
BSF BUSSTA,RX_SUCCESS
RETURN
;=======================================
;I2C 開(kāi)始信號(hào)
;輸入: 無(wú)
;輸出: BUFSSTA
;=======================================
I2CSTART
BSF STATUS,RP0
BSF TRISC,SDA ;1US
NOP ;1US
BSF TRISC,SCL ;1US
NOP
NOP
BCF TRISC,SDA
NOP
NOP
BCF STATUS,RP0
BSF BUSSTA,BUS_BUSY
RETURN
;=======================================
;I2C 結(jié)束信號(hào)
;輸入: 無(wú)
;輸出: BUSSTA
;=======================================
I2CSTOP
BSF STATUS,RP0
BCF TRISC,SCL
BCF TRISC,SDA
BSF TRISC,SCL
NOP
NOP
BSF TRISC,SDA
NOP
BCF STATUS,RP0
BCF BUSSTA,BUS_BUSY
RETURN
;=======================================
; D10 @
; clock in 4Mhz
; delay 10ms
;=======================================
D10 MOVLW 0X0D
MOVWF REG1
LOOP2 MOVLW 0XFF
MOVWF REG2
LOOP1 DECFSZ REG2,f
GOTO LOOP1
DECFSZ REG1,f
GOTO LOOP2
RETURN
;=======================================
I2CERR CLRWDT
NOP
NOP
NOP
RETURN
END
;PIC16C74B單片機(jī)與AT24C64通訊
;該程序?qū)崿F(xiàn)的功能是:
;將I2C_A,I2C_B.....I2C_K內(nèi)容寫(xiě)入EEPROM中
;同時(shí)在EEPROM中讀出數(shù)據(jù)從新寫(xiě)入各個(gè)寄存器
;=======================================
LIST P=16C74B
#include
ERRORLEVEL -302
I2C_A EQU 0X20
I2C_B EQU 0X21
I2C_C EQU 0X22
I2C_D EQU 0X23
I2C_E EQU 0X24
I2C_F EQU 0X25
I2C_G EQU 0X26
REG1 EQU 0X2B
REG2 EQU 0X2C
COUNT EQU 0X71 ;I2C
BCOUNT EQU 0X72 ;I2C
TXBUF EQU 0X73 ;I2C
RXBUF EQU 0X74 ;I2C
BUSCON EQU 0X75 ;I2C
BUSSTA EQU 0X76 ;I2C
I2CBUF EQU 0X77 ;I2C
SLAVEADDR EQU 0XA0 ;I2C
DATAADDRH EQU 0X01 ;I2C
DATAADDRL EQU 0X00 ;I2C
;---------------------------------------
;BUSCON
;---------------------------------------
SLAVE_RW EQU 7
LAST_BYTE_RX EQU 6
SLAVE_ACTIVE EQU 5
TIME_OUT EQU 4
;---------------------------------------
;BUSSTA
;---------------------------------------
BUS_BUSY EQU 7
TX_PROGRESS EQU 6
RX_PROGRESS EQU 5
TX_SUCCESS EQU 4
RX_SUCCESS EQU 3
FATAL_ERROR EQU 2 ;SLAVE EEPROM
ABORT EQU 1
ACK_ERROR EQU 0
;---------------------------------------
R_NOT_W EQU 0
TRUE EQU 1
FALSE EQU 0
SDA EQU 0X04 ;I2C
SCL EQU 0X03 ;I2C
;=======================================
ORG 0X00
GOTO START
ORG 0X20
START BCF STATUS,RP1
BCF STATUS,RP0
CLRF RA
CLRF RB
CLRF RC
CLRF RD
CLRF RE
BSF STATUS,RP0
CLRF TRISA
CLRF TRISB
CLRF TRISC
CLRF TRISD
CLRF TRISE
MOVLW 0X02
MOVWF I2C_A
MOVLW 0X04
MOVWF I2C_B
MOVLW 0X03
MOVWF I2C_C
MOVLW 0X03
MOVWF I2C_D
MOVLW 0X05
MOVWF I2C_E
MOVLW 0X06
MOVWF I2C_F
MOVLW 0X04
MOVWF I2C_G
;=======================================
START1
CALL I2CBUS_INIT
CALL REGTOI2CBUF
CALL I2CWRITE
CALL CLRI2CBUF
CALL D10
CALL I2CREAD
CALL I2CBUFTOREG
GOTO START1
;=======================================
;CLRI2CBUF
;---------------------------------------
CLRI2CBUF
BCF STATUS,RP0
MOVLW I2CBUF
MOVWF FSR
MOVLW 0X08
MOVWF COUNT
CLRLOOP CLRF INDF
INCF FSR,F
DECFSZ COUNT,F
GOTO CLRLOOP
RETURN
;---------------------------------------
;將各個(gè)REGSTER數(shù)據(jù)寫(xiě)入 I2CBUF 緩沖區(qū)
;---------------------------------------
REGTOI2CBUF
BCF STATUS,RP0
MOVLW I2CBUF
MOVWF FSR
MOVF I2C_A,W
MOVWF INDF
INCF FSR,F
MOVF I2C_B,W
MOVWF INDF
INCF FSR,F
MOVF I2C_C,W
MOVWF INDF
INCF FSR,F
MOVF I2C_D,W
MOVWF INDF
INCF FSR,F
MOVF I2C_E,W
MOVWF INDF
INCF FSR,F
MOVF I2C_F,W
MOVWF INDF
INCF FSR,F
MOVF I2C_G,W
MOVWF INDF
RETURN
;---------------------------------------
;將 I2CBUF 緩沖區(qū)數(shù)據(jù)寫(xiě)入各個(gè)REGSTER
;---------------------------------------
I2CBUFTOREG
BCF STATUS,RP0
MOVLW I2CBUF
MOVWF FSR
MOVF INDF,W
MOVWF I2C_A
INCF FSR,F
MOVF INDF,W
MOVWF I2C_B
INCF FSR,F
MOVF INDF,W
MOVWF I2C_C
INCF FSR,F
MOVF INDF,W
MOVWF I2C_D
INCF FSR,F
MOVF INDF,W
MOVWF I2C_E
INCF FSR,F
MOVF INDF,W
MOVWF I2C_F
INCF FSR,F
MOVF INDF,W
MOVWF I2C_G
RETURN
;=======================================
;將I2CBUF緩沖區(qū)內(nèi)的數(shù)據(jù)寫(xiě)入EEPROM
;=======================================
I2CWRITE
CALL I2CSTART
BTFSS BUSSTA,BUS_BUSY
GOTO TX_FAIL
BCF BUSCON,SLAVE_RW
CALL TX_SLAVE_ADDR
BTFSS BUSSTA,TX_SUCCESS ;SUCESSFULL?
GOTO TX_FAIL
CALL TX_DATA_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_FAIL
MOVLW I2CBUF
MOVWF FSR
MOVLW 07
MOVWF COUNT
T_BYTE_LOOP
MOVF INDF,W
MOVWF TXBUF
CALL I2CTXB
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_FAIL
INCF FSR,F
DECFSZ COUNT,F
GOTO T_BYTE_LOOP
GOTO TX_PASS
TX_FAIL CALL I2CSTOP
BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
RETLW FALSE
TX_PASS
CALL I2CSTOP
BCF BUSSTA,TX_PROGRESS
RETLW TRUE
;---------------------------------------
;從 EEPROM 中讀出數(shù)據(jù)到 I2CBUF 寄存器
;---------------------------------------
I2CREAD BCF STATUS,RP0
BCF BUSSTA,RX_SUCCESS
CALL I2CSTART
BTFSS BUSSTA,BUS_BUSY
GOTO RX_FAIL
BCF BUSCON,SLAVE_RW
CALL TX_SLAVE_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO RX_FAIL
CALL TX_DATA_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO RX_FAIL
CALL I2CSTART
BSF BUSCON,SLAVE_RW
CALL TX_SLAVE_ADDR
BTFSS BUSSTA,TX_SUCCESS
GOTO RX_FAIL
BCF BUSSTA,LAST_BYTE_RX
MOVLW I2CBUF
MOVWF FSR
MOVLW 07
MOVWF COUNT
R_BYTE_LOOP
MOVLW 0X01
XORWF COUNT,W
BTFSC STATUS,Z
BSF BUSSTA,LAST_BYTE_RX
CALL I2CRXB
BTFSS BUSSTA,RX_SUCCESS
GOTO RX_FAIL
MOVF RXBUF,W
MOVWF INDF
INCF FSR,F
DECFSZ COUNT,F
GOTO R_BYTE_LOOP
GOTO RX_PASS
RX_FAIL CALL I2CSTOP
BCF BUSSTA,RX_PROGRESS
BCF BUSSTA,RX_SUCCESS
RETLW FALSE
RX_PASS CALL I2CSTOP
BCF STATUS,RP0
BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,RX_PROGRESS
BSF BUSSTA,RX_SUCCESS
RETLW TRUE
;=======================================
; I2C 初始化
;=======================================
I2CBUS_INIT
BCF STATUS,RP0
MOVF RC,W
ANDLW 0X18
MOVWF RC
CLRF BUSSTA
CLRF BUSCON
RETURN
;=======================================
; 傳送設(shè)備地址
;入口: BUSCON
;出口: BUSSTA
;=======================================
TX_SLAVE_ADDR
BCF STATUS,RP0
BCF BUSSTA,ACK_ERROR
MOVLW SLAVEADDR
MOVWF TXBUF
BTFSC BUSCON,SLAVE_RW
BSF TXBUF,R_NOT_W
CALL I2CTXB
BTFSC BUSSTA,TX_SUCCESS
GOTO TXADDR_SUCCESS
CLRWDT
BTFSS BUSSTA,ACK_ERROR
CALL I2CSTOP
GOTO TXADDR_END
TXADDR_SUCCESS
CLRWDT
TXADDR_END
RETURN
;=======================================
;TX_DATA_ADDR 發(fā)送寫(xiě)/讀的地址
;=======================================
TX_DATA_ADDR
BCF STATUS,RP0
MOVLW DATAADDRH
MOVWF TXBUF
CALL I2CTXB
BCF STATUS,RP0
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_DATA_ADDR_ERROR
MOVLW DATAADDRL
MOVWF TXBUF
CALL I2CTXB
BCF STATUS,RP0
BTFSS BUSSTA,TX_SUCCESS
GOTO TX_DATA_ADDR_ERROR
GOTO TX_DATA_END
TX_DATA_ADDR_ERROR
BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
BSF BUSSTA,ACK_ERROR
TX_DATA_END
RETURN
;=======================================
;向 EEPROM 輸出一個(gè)8位數(shù)據(jù),并檢測(cè)EEPROM的ACK信號(hào)
;入口: TXBUF 寄存器
; BUSSTA 寄存器
;出口: EEPROM 存儲(chǔ)器
; BUSSTA 寄存器
;======================================
I2CTXB BCF STATUS,RP0
BSF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
MOVLW 0X08
MOVWF BCOUNT
TXLOOP CLRWDT
BSF STATUS,RP0
BCF TRISC,SCL
NOP
BCF TRISC,SDA
BCF STATUS,RP0
RLF TXBUF,F
BSF STATUS,RP0
BTFSC STATUS,C
BSF TRISC,SDA
NOP
BSF TRISC,SCL
NOP
NOP
BCF STATUS,RP0
DECFSZ BCOUNT,F
GOTO TXLOOP
;---------------------------------------
;檢測(cè)是EEPROM否有ACK信號(hào)產(chǎn)生,產(chǎn)生為正常
;檢測(cè)方法:
; 1. 將RC口的SCL.SDA分別設(shè)置為輸出和輸入狀態(tài)
; 2. 延時(shí)2個(gè)指令周期,將SCL設(shè)置為高電平
; 3. 延時(shí)2個(gè)指令周期,檢測(cè)RC口SDA的狀態(tài)
; 4. 如果為高電平,設(shè)置錯(cuò)誤狀態(tài),返回
; 5. 如果為低電平,延時(shí)2個(gè)指令周期后,將RC口SCL設(shè)置為低電平,繼續(xù)執(zhí)行
;---------------------------------------
BSF STATUS,RP0
BCF TRISC,SCL
BSF TRISC,SDA
NOP
NOP
BSF TRISC,SCL
NOP
BCF STATUS,RP0
BTFSC RC,SDA
GOTO TX_ERR
BSF STATUS,RP0
BCF TRISC,SCL
BCF STATUS,RP0
BCF BUSSTA,TX_PROGRESS
BSF BUSSTA,TX_SUCCESS
BCF BUSSTA,ACK_ERROR
GOTO TX_END
TX_ERR BCF BUSSTA,TX_PROGRESS
BCF BUSSTA,TX_SUCCESS
BSF BUSSTA,ACK_ERROR
TX_END RETURN
;=======================================
;從EEPROM中讀出 8 位的數(shù)據(jù),并發(fā)送發(fā)送ACK信號(hào)
;入口: EEPROM 存儲(chǔ)器
; BUSCON 寄存器
;出口: RXBUF 寄存器
; BUSSTA 寄存器
;=======================================
I2CRXB BCF STATUS,RP0
BSF BUSSTA,RX_PROGRESS
BCF BUSSTA,RX_SUCCESS
MOVLW 0X08
MOVWF BCOUNT
RXLOOP CLRWDT
BSF STATUS,RP0
BCF TRISC,SCL ;定義TRISC
BSF TRISC,SDA ;定義TRISC
NOP
NOP
BSF TRISC,SCL ;TRISC
NOP
BCF STATUS,RP0
BCF STATUS,C
BTFSC RC,SDA ;原指令為BTFSC TRISC,SDA
BSF STATUS,C
RLF RXBUF,F
DECFSZ BCOUNT,F
GOTO RXLOOP
;---------------------------------------
;發(fā)送ACK信號(hào)到EEPROM
;判斷接收的是否為最后一個(gè)字節(jié)
;LAST_BYTE_RX為1,是 SDA發(fā)送一個(gè)高電平,結(jié)束
;LAST_BYTE_RX為0,不是 SDA發(fā)送一個(gè)低電平ACK,繼續(xù)接收
;---------------------------------------
BSF STATUS,RP0
BCF TRISC,SCL
BCF TRISC,SDA
BTFSC BUSCON,LAST_BYTE_RX ;檢測(cè)是否是最后一個(gè)字節(jié)
BSF TRISC,SDA ;是最后一個(gè)字節(jié)
NOP ;發(fā)送ACK信號(hào)
BSF TRISC,SCL
NOP
NOP
NOP
NOP
BCF TRISC,SCL
BCF STATUS,RP0
BCF BUSSTA,RX_PROGRESS
BSF BUSSTA,RX_SUCCESS
RETURN
;=======================================
;I2C 開(kāi)始信號(hào)
;輸入: 無(wú)
;輸出: BUFSSTA
;=======================================
I2CSTART
BSF STATUS,RP0
BSF TRISC,SDA ;1US
NOP ;1US
BSF TRISC,SCL ;1US
NOP
NOP
BCF TRISC,SDA
NOP
NOP
BCF STATUS,RP0
BSF BUSSTA,BUS_BUSY
RETURN
;=======================================
;I2C 結(jié)束信號(hào)
;輸入: 無(wú)
;輸出: BUSSTA
;=======================================
I2CSTOP
BSF STATUS,RP0
BCF TRISC,SCL
BCF TRISC,SDA
BSF TRISC,SCL
NOP
NOP
BSF TRISC,SDA
NOP
BCF STATUS,RP0
BCF BUSSTA,BUS_BUSY
RETURN
;=======================================
; D10 @
; clock in 4Mhz
; delay 10ms
;=======================================
D10 MOVLW 0X0D
MOVWF REG1
LOOP2 MOVLW 0XFF
MOVWF REG2
LOOP1 DECFSZ REG2,f
GOTO LOOP1
DECFSZ REG1,f
GOTO LOOP2
RETURN
;=======================================
I2CERR CLRWDT
NOP
NOP
NOP
RETURN
END
此內(nèi)容為AET網(wǎng)站原創(chuàng),未經(jīng)授權(quán)禁止轉(zhuǎn)載。