' Example routines to use the 24LC256 EEPROM
' This is not a complete program, but a set of routines that
' work with the 24LC256 I2C EEPROM from Microchip.
'
' Created 8/2002 by Eric James
'
' * The MSSP Port must be properly set up before these routines
' will work
'
' * An I2C library from <libs\interface> must be included for these
' routines to work.
'
' Routines:
' =========
'
' Sub write_eeprom(chip address, data address, value)
' Write "value" to chip # "chip address" in location "data address"
'
' Function read_eeprom(chip address, data address)
' Return the data value stored in chip # "chip address" in location "data address"
'
' Function MemoryPoll(chip address)
' Return a 0 if the chip at "chip address" has completed it's last write cycle
'
' Sub MemoryWait(chip address)
' Wait for the chip at "chip address" to complete it's last write cycle
'
Include <libs\interface\hwI2C.bas>
Function MemoryPoll(ch As uByte)
Dim retval As uByte
Call StartBit()
retval = 0xA0 Or Shl(ch,1)
SSPBUF=retval ' transmit the data
Call i2c_waitForIdle() ' wait for the data to transmit
retval=SSPCON2 And 0x40
Call StopBit()
End Function retval
Sub MemoryWait(ch As uByte)
While MemoryPoll(ch)<>0
clrwdt
End While
End Sub
Sub write_eeprom(chp As uByte,add As uInteger,val As uByte)
Call StartBit() ' send start bit
chp=0b1010.0000 Or Shl(chp,1)
Call i2c_txByte(chp) ' send control byte
Call i2c_txByte(add.Byte1) ' send high address
Call i2c_txByte(add.Byte0) ' send low address
Call i2c_txByte(val) ' send data
Call StopBit() ' send stop bit
End Sub
Function read_eeprom(chp As uByte,add As uInteger)
Dim retval As uByte
Call StartBit() ' send start bit
chp = 0b1010.0000 Or Shl(chp,1)
Call i2c_txByte(chp) ' send control byte
Call i2c_txByte(add.Byte1) ' send high address
Call i2c_txByte(add.Byte0) ' send low address
Call ReStartBit() ' send restart bit
Call i2c_txByte(0b1010.0001) ' send control byte
ACKDT=1 ' do not send ACK
retval = i2c_rxByte()
Call StopBit() ' send stop bit
End Function retval