Hello all !
I’m currently testing a 24LC256 EEprom (I2C device) and have a big problem : I can’t read or write to it :-[
In fact, I don’t know if I really write to it but can’t read or if the write has failed (so that the value I read is not the one expected).
Here’s the (little) I2C code that deals with it :
public class PandaEEprom
{
private static I2CDevice.Configuration EEpromConfig;
private static I2CDevice.Configuration LCDConfig;
private static I2CDevice I2C;
public PandaEEprom(byte DeviceAddress, int ClockRate)
{
EEpromConfig = new I2CDevice.Configuration((ushort)(0x50 >> 1), 400); // 400 KHz, A0-A2 shorted to ground = 0x50
LCDConfig = new I2CDevice.Configuration((ushort)(0xC6 >> 1),91); // Devantech LCD, for testing I2C
I2C = new I2CDevice(EEpromConfig);
}
public void Write(int Address, byte data)
{
I2C.Config = EEpromConfig;
var xActions = new I2CDevice.I2CTransaction[1];
xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address & 0xFF), data });
I2C.Execute(xActions, 1000);
}
public void WriteLCD(byte data)
{
I2C.Config = LCDConfig;
var xActions = new I2CDevice.I2CTransaction[1];
xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0, data });
I2C.Execute(xActions, 1000);
}
public byte Read(byte Address)
{
I2C.Config = EEpromConfig;
var Data = new byte[1];
var xActions = new I2CDevice.I2CTransaction[2];
xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address & 0xFF) });
xActions[1] = I2CDevice.CreateReadTransaction(Data);
I2C.Execute(xActions, 1000);
return Data[0];
}
}
Pretty simple, isn’t it ?
Now the code for testing :
static PandaEEprom MyEeeprom = new PandaEEprom(0x50,400);
static OutputPort Led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED,false);
public static void Main()
{
Led.Write(true);
MyEeeprom.WriteLCD(19); // Backlight ON. This works.
//MyEeeprom.WriteLCD(20); // Backlight OFF. This works.
MyEeeprom.Write(1, 19); // Does this work ?
var MyVar = MyEeeprom.Read(1); // MyVar is always 0 :-(
Thread.Sleep(Timeout.Infinite);
}
Of course, the WP pin is grounded
I know that I2C is working because the LCD is doing what I want. So, there’s something wrong elsewhere, but where ? :-[
Is it so obvious that I can’t see it ?
I’ve also had a look at Arduino code and it is almost the same. I thought at addresses bytes order and tried both (MSB/LSB and LSB/MSB), but to no avail.
I’m stuck. If anyone has an idea or any clue to solve this, he will have my eternal gratitude.