I am using the following code to read a block of data (256 bytes) from an external eeprom 24LC256. PIC is a 26K40.
The below code works on the first power up of the device and first run through the sub, but on subsequent reads, it sometimes locks up somewhere between the Start and stop commands.
Whats frustrating is that it only does it on average 1 time in 3 or 4 attempts.
I have made sure that the Variable "XBUS_model" is always within bounds (0-30) that result in readable addresses from the Eeprom.
Any ideas as to why this might be happening?
I have noticed some examples of code with "I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)" instead of what i have "I2C.Acknowledge(I2C_ACKNOWLEDGE)" but i could not get it to work at all with "I2C_NOT_ACKNOWLEDGE"
I have also noticed that if i pre-program my entire eeprom (bench programmer) with $00 instead of $FF then it always locks up with the below code.
As soon as i re-program it with $FF it will complete the read cycle again.
This is across 3 separate boards, two with surface mount eeproms and one with socketed DIP so it's defiantly not a faulty chip.
Cheers
Lee
Code: Select all
Sub EXT_EEPROM_READ()
TimerEnabled = false //turn off timer interrupts
Model_number_offset= XBUS_Model*256
Address=Model_number_offset //+Address_counter
I2C.Start
I2C.WriteByte(Eprom_cmd)
I2C.WriteByte(Address.Byte1)
I2C.WriteByte(Address.Byte0)
I2C.Restart //set address for first read.
I2C.WriteByte(Eprom_cmd + 1)
For Page_counter =0 To 255 //63
Model_system_data(Page_counter)= I2C.ReadByte
I2C.Acknowledge(I2C_ACKNOWLEDGE)
Next
I2C.Stop
TimerEnabled = True //turn timer interrupts back on
End Sub