PIC16C74B單片機(jī)與AT24C64通訊
;PIC16C74B單片機(jī)與AT24C64通訊
;該程序?qū)崿F(xiàn)的功能是:
;將I2C_A,I2C_B.....I2C_K內(nèi)容寫(xiě)入EEPROM中
;同時(shí)在EEPROM中讀出數(shù)據(jù)從新寫(xiě)入各個(gè)寄存器
;=======================================
LISTP=16C74B
#includep16c74.inc>
ERRORLEVEL-302
I2C_AEQU0X20
I2C_BEQU0X21
I2C_CEQU0X22
I2C_DEQU0X23
I2C_EEQU0X24
I2C_FEQU0X25
I2C_GEQU0X26
REG1EQU0X2B
REG2EQU0X2C
COUNTEQU0X71;I2C
BCOUNTEQU0X72;I2C
TXBUFEQU0X73;I2C
RXBUFEQU0X74;I2C
BUSCONEQU0X75;I2C
BUSSTAEQU0X76;I2C
I2CBUFEQU0X77;I2C
SLAVEADDREQU0XA0;I2C
DATAADDRHEQU0X01;I2C
DATAADDRLEQU0X00;I2C
;---------------------------------------
;BUSCON
;---------------------------------------
SLAVE_RWEQU7
LAST_BYTE_RXEQU6
SLAVE_ACTIVEEQU5
TIME_OUTEQU4
;---------------------------------------
;BUSSTA
;---------------------------------------
BUS_BUSYEQU7
TX_PROGRESSEQU6
RX_PROGRESSEQU5
TX_SUCCESSEQU4
RX_SUCCESSEQU3
FATAL_ERROREQU2;SLAVEEEPROM
ABORTEQU1
ACK_ERROREQU0
;---------------------------------------
R_NOT_WEQU0
TRUEEQU1
FALSEEQU0
SDAEQU0X04;I2C
SCLEQU0X03;I2C
;=======================================
ORG0X00
GOTOSTART
ORG0X20
STARTBCFSTATUS,RP1
BCFSTATUS,RP0
CLRFRA
CLRFRB
CLRFRC
CLRFRD
CLRFRE
BSFSTATUS,RP0
CLRFTRISA
CLRFTRISB
CLRFTRISC
CLRFTRISD
CLRFTRISE
MOVLW0X02
MOVWFI2C_A
MOVLW0X04
MOVWFI2C_B
MOVLW0X03
MOVWFI2C_C
MOVLW0X03
MOVWFI2C_D
MOVLW0X05
MOVWFI2C_E
MOVLW0X06
MOVWFI2C_F
MOVLW0X04
MOVWFI2C_G
;=======================================
START1
CALLI2CBUS_INIT
CALLREGTOI2CBUF
CALLI2CWRITE
CALLCLRI2CBUF
CALLD10
CALLI2CREAD
CALLI2CBUFTOREG
GOTOSTART1
;=======================================
;CLRI2CBUF
;---------------------------------------
CLRI2CBUF
BCFSTATUS,RP0
MOVLWI2CBUF
MOVWFFSR
MOVLW0X08
MOVWFCOUNT
CLRLOOPCLRFINDF
INCFFSR,F
DECFSZCOUNT,F
GOTOCLRLOOP
RETURN
;---------------------------------------
;將各個(gè)REGSTER數(shù)據(jù)寫(xiě)入I2CBUF緩沖區(qū)
;---------------------------------------
REGTOI2CBUF
BCFSTATUS,RP0
MOVLWI2CBUF
MOVWFFSR
MOVFI2C_A,W
MOVWFINDF
INCFFSR,F
MOVFI2C_B,W
MOVWFINDF
INCFFSR,F
MOVFI2C_C,W
MOVWFINDF
INCFFSR,F
MOVFI2C_D,W
MOVWFINDF
INCFFSR,F
MOVFI2C_E,W
MOVWFINDF
INCFFSR,F
MOVFI2C_F,W
MOVWFINDF
INCFFSR,F
MOVFI2C_G,W
MOVWFINDF
RETURN
;---------------------------------------
;將I2CBUF緩沖區(qū)數(shù)據(jù)寫(xiě)入各個(gè)REGSTER
;---------------------------------------
I2CBUFTOREG
BCFSTATUS,RP0
MOVLWI2CBUF
MOVWFFSR
MOVFINDF,W
MOVWFI2C_A
INCFFSR,F
MOVFINDF,W
MOVWFI2C_B
INCFFSR,F
MOVFINDF,W
MOVWFI2C_C
INCFFSR,F
MOVFINDF,W
MOVWFI2C_D
INCFFSR,F
MOVFINDF,W
MOVWFI2C_E
INCFFSR,F
MOVFINDF,W
MOVWFI2C_F
INCFFSR,F
MOVFINDF,W
MOVWFI2C_G
RETURN
;=======================================
;將I2CBUF緩沖區(qū)內(nèi)的數(shù)據(jù)寫(xiě)入EEPROM
;=======================================
I2CWRITE
CALLI2CSTART
BTFSSBUSSTA,BUS_BUSY
GOTOTX_FAIL
BCFBUSCON,SLAVE_RW
CALLTX_SLAVE_ADDR
BTFSSBUSSTA,TX_SUCCESS;SUCESSFULL?
GOTOTX_FAIL
CALLTX_DATA_ADDR
BTFSSBUSSTA,TX_SUCCESS
GOTOTX_FAIL
MOVLWI2CBUF
MOVWFFSR
MOVLW07
MOVWFCOUNT
T_BYTE_LOOP
MOVFINDF,W
MOVWFTXBUF
CALLI2CTXB
BTFSSBUSSTA,TX_SUCCESS
GOTOTX_FAIL
INCFFSR,F
DECFSZCOUNT,F
GOTOT_BYTE_LOOP
GOTOTX_PASS
TX_FAILCALLI2CSTOP
BCFBUSSTA,TX_PROGRESS
BCFBUSSTA,TX_SUCCESS
RETLWFALSE
TX_PASS
CALLI2CSTOP
BCFBUSSTA,TX_PROGRESS
RETLWTRUE
;---------------------------------------
;從EEPROM中讀出數(shù)據(jù)到I2CBUF寄存器
;---------------------------------------
I2CREADBCFSTATUS,RP0
BCFBUSSTA,RX_SUCCESS
CALLI2CSTART
BTFSSBUSSTA,BUS_BUSY
GOTORX_FAIL
BCFBUSCON,SLAVE_RW
CALLTX_SLAVE_ADDR
BTFSSBUSSTA,TX_SUCCESS
GOTORX_FAIL
CALLTX_DATA_ADDR
BTFSSBUSSTA,TX_SUCCESS
GOTORX_FAIL
CALLI2CSTART
BSFBUSCON,SLAVE_RW
CALLTX_SLAVE_ADDR
BTFSSBUSSTA,TX_SUCCESS
GOTORX_FAIL
BCFBUSSTA,LAST_BYTE_RX
MOVLWI2CBUF
MOVWFFSR
MOVLW07
MOVWFCOUNT
R_BYTE_LOOP
MOVLW0X01
XORWFCOUNT,W
BTFSCSTATUS,Z
BSFBUSSTA,LAST_BYTE_RX
CALLI2CRXB
BTFSSBUSSTA,RX_SUCCESS
GOTORX_FAIL
MOVFRXBUF,W
MOVWFINDF
INCFFSR,F
DECFSZCOUNT,F
GOTOR_BYTE_LOOP
GOTORX_PASS
RX_FAILCALLI2CSTOP
BCFBUSSTA,RX_PROGRESS
BCFBUSSTA,RX_SUCCESS
RETLWFALSE
RX_PASSCALLI2CSTOP
BCFSTATUS,RP0
BCFBUSSTA,TX_PROGRESS
BCFBUSSTA,RX_PROGRESS
BSFBUSSTA,RX_SUCCESS
RETLWTRUE
;=======================================
;I2C初始化
;=======================================
I2CBUS_INIT
BCFSTATUS,RP0
MOVFRC,W
ANDLW0X18
MOVWFRC
CLRFBUSSTA
CLRFBUSCON
RETURN
;=======================================
;傳送設(shè)備地址
;入口:BUSCON
;出口:BUSSTA
;=======================================
TX_SLAVE_ADDR
BCFSTATUS,RP0
BCFBUSSTA,ACK_ERROR
MOVLWSLAVEADDR
MOVWFTXBUF
BTFSCBUSCON,SLAVE_RW
BSFTXBUF,R_NOT_W
CALLI2CTXB
BTFSCBUSSTA,TX_SUCCESS
GOTOTXADDR_SUCCESS
CLRWDT
BTFSSBUSSTA,ACK_ERROR
CALLI2CSTOP
GOTOTXADDR_END
TXADDR_SUCCESS
CLRWDT
TXADDR_END
RETURN
;=======================================
;TX_DATA_ADDR發(fā)送寫(xiě)/讀的地址
;=======================================
TX_DATA_ADDR
BCFSTATUS,RP0
MOVLWDATAADDRH
MOVWFTXBUF
CALLI2CTXB
BCFSTATUS,RP0
BTFSSBUSSTA,TX_SUCCESS
GOTOTX_DATA_ADDR_ERROR
MOVLWDATAADDRL
MOVWFTXBUF
CALLI2CTXB
BCFSTATUS,RP0
BTFSSBUSSTA,TX_SUCCESS
GOTOTX_DATA_ADDR_ERROR
GOTOTX_DATA_END
TX_DATA_ADDR_ERROR
BCFBUSSTA,TX_PROGRESS
BCFBUSSTA,TX_SUCCESS
BSFBUSSTA,ACK_ERROR
TX_DATA_END
RETURN
;=======================================
;向EEPROM輸出一個(gè)8位數(shù)據(jù),并檢測(cè)EEPROM的ACK信號(hào)
;入口:TXBUF寄存器
;BUSSTA寄存器
;出口:EEPROM存儲(chǔ)器
;BUSSTA寄存器
;======================================
I2CTXBBCFSTATUS,RP0
BSFBUSSTA,TX_PROGRESS
BCFBUSSTA,TX_SUCCESS
MOVLW0X08
MOVWFBCOUNT
TXLOOPCLRWDT
BSFSTATUS,RP0
BCFTRISC,SCL
NOP
BCFTRISC,SDA
BCFSTATUS,RP0
RLFTXBUF,F
BSFSTATUS,RP0
BTFSCSTATUS,C
BSFTRISC,SDA
NOP
BSFTRISC,SCL
NOP
NOP
BCFSTATUS,RP0
DECFSZBCOUNT,F
GOTOTXLOOP
;---------------------------------------
;檢測(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í)行
;---------------------------------------
BSFSTATUS,RP0
BCFTRISC,SCL
BSFTRISC,SDA
NOP
NOP
BSFTRISC,SCL
NOP
BCFSTATUS,RP0
BTFSCRC,SDA
GOTOTX_ERR
BSFSTATUS,RP0
BCFTRISC,SCL
BCFSTATUS,RP0
BCFBUSSTA,TX_PROGRESS
BSFBUSSTA,TX_SUCCESS
BCFBUSSTA,ACK_ERROR
GOTOTX_END
TX_ERRBCFBUSSTA,TX_PROGRESS
BCFBUSSTA,TX_SUCCESS
BSFBUSSTA,ACK_ERROR
TX_ENDRETURN
;=======================================
;從EEPROM中讀出8位的數(shù)據(jù),并發(fā)送發(fā)送ACK信號(hào)
;入口:EEPROM存儲(chǔ)器
;BUSCON寄存器
;出口:RXBUF寄存器
;BUSSTA寄存器
;=======================================
I2CRXBBCFSTATUS,RP0
BSFBUSSTA,RX_PROGRESS
BCFBUSSTA,RX_SUCCESS
MOVLW0X08
MOVWFBCOUNT
RXLOOPCLRWDT
BSFSTATUS,RP0
BCFTRISC,SCL;定義TRISCSCL>為輸出
BSFTRISC,SDA;定義TRISCSDA>為輸入
NOP
NOP
BSFTRISC,SCL;TRISCSCL>輸出高電平
NOP
BCFSTATUS,RP0
BCFSTATUS,C
BTFSCRC,SDA;原指令為BTFSCTRISC,SDA
BSFSTATUS,C
RLFRXBUF,F
DECFSZBCOUNT,F
GOTORXLOOP
;---------------------------------------
;發(fā)送ACK信號(hào)到EEPROM
;判斷接收的是否為最后一個(gè)字節(jié)
;LAST_BYTE_RX為1,是SDA發(fā)送一個(gè)高電平,結(jié)束
;LAST_BYTE_RX為0,不是SDA發(fā)送一個(gè)低電平ACK,繼續(xù)接收
;---------------------------------------
BSFSTATUS,RP0
BCFTRISC,SCL
BCFTRISC,SDA
BTFSCBUSCON,LAST_BYTE_RX;檢測(cè)是否是最后一個(gè)字節(jié)
BSFTRISC,SDA;是最后一個(gè)字節(jié)
NOP;發(fā)送ACK信號(hào)
BSFTRISC,SCL
NOP
NOP
NOP
NOP
BCFTRISC,SCL
BCFSTATUS,RP0
BCFBUSSTA,RX_PROGRESS
BSFBUSSTA,RX_SUCCESS
RETURN
;=======================================
;I2C開(kāi)始信號(hào)
;輸入:無(wú)
;輸出:BUFSSTABUS_BUSY>
;=======================================
I2CSTART
BSFSTATUS,RP0
BSFTRISC,SDA;1US
NOP;1US
BSFTRISC,SCL;1US
NOP
NOP
BCFTRISC,SDA
NOP
NOP
BCFSTATUS,RP0
BSFBUSSTA,BUS_BUSY
RETURN
;=======================================
;I2C結(jié)束信號(hào)
;輸入:無(wú)
;輸出:BUSSTABUS_BUSY>
;=======================================
I2CSTOP
BSFSTATUS,RP0
BCFTRISC,SCL
BCFTRISC,SDA
BSFTRISC,SCL
NOP
NOP
BSFTRISC,SDA
NOP
BCFSTATUS,RP0
BCFBUSSTA,BUS_BUSY
RETURN
;=======================================
;D10@
;clockin4Mhz
;delay10ms
;=======================================
D10MOVLW0X0D
MOVWFREG1
LOOP2MOVLW0XFF
MOVWFREG2
LOOP1DECFSZREG2,f
GOTOLOOP1
DECFSZREG1,f
GOTOLOOP2
RETURN
;=======================================
I2CERRCLRWDT
NOP
NOP
NOP
RETURN
END
51單片機(jī)相關(guān)文章:51單片機(jī)教程
單片機(jī)相關(guān)文章:單片機(jī)教程
單片機(jī)相關(guān)文章:單片機(jī)視頻教程
單片機(jī)相關(guān)文章:單片機(jī)工作原理
通信相關(guān)文章:通信原理
評(píng)論