Write to an external EE chip with Higher addresses and 32bit
Posted: Fri Aug 12, 2016 6:10 am
/* Is there something wrong with my code I am writing to chip 5 staring at address 60005 to 60008 to update a 32 bit number on a 24LC512, sometimes it works and sometimes it does not do i need longer delays between writes , i am not sure*/
Device = 18F4685
Clock = 25
Config
CP0 = ON,
CP1 = ON,
CP2 = ON,
CP3 = ON,
WDT = OFF
Include "utils.bas"
Include "I2C.bas"
Include "EEPROM.bas"
dim x as longword
dim y as longword
dim button as portb.0
Sub EEPROM_Write(Data As Byte, Memory_Address As Word)
I2C.Start // Write data to
I2C.WriteByte(EEPROM_Control) // the external EEPROM
I2C.WriteByte(Memory_Address.Byte1) //
I2C.WriteByte(Memory_Address.Byte0) //
I2C.WriteByte(Data) //
I2C.Stop //
DelayMS(30) // Small delay
End Sub // between writes
//------------------------------------------------------------------------------------
Function EEPROM_Read(Memory_Address As Word) As Byte
I2C.Start // Read data from
I2C.WriteByte(EEPROM_Control) // the external EEPROM
I2C.WriteByte(Memory_Address.Byte1) //
I2C.WriteByte(Memory_Address.Byte0) //
I2C.Restart //
I2C.WriteByte(EEPROM_Control + 1) //
Result = I2C.ReadByte(I2C_NOT_ACKNOWLEDGE) // Store the functions
I2C.Stop
DelayMS(2) // result
End Function
sub update_y()
I2C.Initialize()
I2C.Initialize(I2C_100_KHZ, I2C_SLEW_OFF)
EEPROM_Address = %101
EEPROM_Family = %1010
EEPROM_Control = (EEPROM_Family << 4) Or (EEPROM_Address << 1)
DelayMS(200)
x.byte0 = EEPROM_Read(60005)
x.byte1 = EEPROM_Read(60006)
x.byte2 = EEPROM_Read(60007)
x.byte3 = EEPROM_Read(60008)
y = x + 10
EEPROM_Write(y.byte0,60005)
EEPROM_Write(y.byte1,60006)
EEPROM_Write(y.byte2,60007)
EEPROM_Write(y.byte3,60008)
end sub
main:
if button = 0 then
update_y()
endif
goto main
Device = 18F4685
Clock = 25
Config
CP0 = ON,
CP1 = ON,
CP2 = ON,
CP3 = ON,
WDT = OFF
Include "utils.bas"
Include "I2C.bas"
Include "EEPROM.bas"
dim x as longword
dim y as longword
dim button as portb.0
Sub EEPROM_Write(Data As Byte, Memory_Address As Word)
I2C.Start // Write data to
I2C.WriteByte(EEPROM_Control) // the external EEPROM
I2C.WriteByte(Memory_Address.Byte1) //
I2C.WriteByte(Memory_Address.Byte0) //
I2C.WriteByte(Data) //
I2C.Stop //
DelayMS(30) // Small delay
End Sub // between writes
//------------------------------------------------------------------------------------
Function EEPROM_Read(Memory_Address As Word) As Byte
I2C.Start // Read data from
I2C.WriteByte(EEPROM_Control) // the external EEPROM
I2C.WriteByte(Memory_Address.Byte1) //
I2C.WriteByte(Memory_Address.Byte0) //
I2C.Restart //
I2C.WriteByte(EEPROM_Control + 1) //
Result = I2C.ReadByte(I2C_NOT_ACKNOWLEDGE) // Store the functions
I2C.Stop
DelayMS(2) // result
End Function
sub update_y()
I2C.Initialize()
I2C.Initialize(I2C_100_KHZ, I2C_SLEW_OFF)
EEPROM_Address = %101
EEPROM_Family = %1010
EEPROM_Control = (EEPROM_Family << 4) Or (EEPROM_Address << 1)
DelayMS(200)
x.byte0 = EEPROM_Read(60005)
x.byte1 = EEPROM_Read(60006)
x.byte2 = EEPROM_Read(60007)
x.byte3 = EEPROM_Read(60008)
y = x + 10
EEPROM_Write(y.byte0,60005)
EEPROM_Write(y.byte1,60006)
EEPROM_Write(y.byte2,60007)
EEPROM_Write(y.byte3,60008)
end sub
main:
if button = 0 then
update_y()
endif
goto main