PIC16C74B单片机与AT24C64通讯程序
扫描二维码
随时随地手机看文章
;=======================================
;PIC16C74B单片机与AT24C64通讯
;该程序实现的功能是:
;将I2C_A,I2C_B.....I2C_K内容写入EEPROM中
;同时在EEPROM中读出数据从新写入各个寄存器
;=======================================
LISTP=16C74B
#include
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
;---------------------------------------
;将各个REGSTER数据写入I2CBUF缓冲区
;---------------------------------------
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缓冲区数据写入各个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缓冲区内的数据写入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中读出数据到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
;=======================================
;传送设备地址
;入口: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发送写/读的地址
;=======================================
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输出一个8位数据,并检测EEPROM的ACK信号
;入口:TXBUF寄存器
;BUSSTA寄存器
;出口:EEPROM存储器
;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
;---------------------------------------
;检测是EEPROM否有ACK信号产生,产生为正常
;检测方法:
;1.将RC口的SCL.SDA分别设置为输出和输入状态
;2.延时2个指令周期,将SCL设置为高电平
;3.延时2个指令周期,检测RC口SDA的状态
;4.如果为高电平,设置错误状态,返回
;5.如果为低电平,延时2个指令周期后,将RC口SCL设置为低电平,继续执行
;---------------------------------------
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位的数据,并发送发送ACK信号
;入口:EEPROM存储器
;BUSCON寄存器
;出口:RXBUF寄存器
;BUSSTA寄存器
;=======================================
I2CRXBBCFSTATUS,RP0
BSFBUSSTA,RX_PROGRESS
BCFBUSSTA,RX_SUCCESS
MOVLW0X08
MOVWFBCOUNT
RXLOOPCLRWDT
BSFSTATUS,RP0
BCFTRISC,SCL;定义TRISC
BSFTRISC,SDA;定义TRISC
NOP
NOP
BSFTRISC,SCL;TRISC
NOP
BCFSTATUS,RP0
BCFSTATUS,C
BTFSCRC,SDA;原指令为BTFSCTRISC,SDA
BSFSTATUS,C
RLFRXBUF,F
DECFSZBCOUNT,F
GOTORXLOOP
;---------------------------------------
;发送ACK信号到EEPROM
;判断接收的是否为最后一个字节
;LAST_BYTE_RX为1,是SDA发送一个高电平,结束
;LAST_BYTE_RX为0,不是SDA发送一个低电平ACK,继续接收
;---------------------------------------
BSFSTATUS,RP0
BCFTRISC,SCL
BCFTRISC,SDA
BTFSCBUSCON,LAST_BYTE_RX;检测是否是最后一个字节
BSFTRISC,SDA;是最后一个字节
NOP;发送ACK信号
BSFTRISC,SCL
NOP
NOP
NOP
NOP
BCFTRISC,SCL
BCFSTATUS,RP0
BCFBUSSTA,RX_PROGRESS
BSFBUSSTA,RX_SUCCESS
RETURN
;=======================================
;I2C开始信号
;输入:无
;输出:BUFSSTA
;=======================================
I2CSTART
BSFSTATUS,RP0
BSFTRISC,SDA;1US
NOP;1US
BSFTRISC,SCL;1US
NOP
NOP
BCFTRISC,SDA
NOP
NOP
BCFSTATUS,RP0
BSFBUSSTA,BUS_BUSY
RETURN
;=======================================
;I2C结束信号
;输入:无
;输出:BUSSTA
;=======================================
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