2013年9月3日 星期二

I2C for EEPROM

The  Cerb Family has one I2C interface that connected to socket1 and socket2. I found a very usable code from GHI Electronics . I test the sorce code for 24LC16 eeprom and it is working.
The source code as follows: 
 
//-------------- setup the I2C device ----------------------
I2CDevice EepI2C = new I2CDevice(new I2CDevice.Configuration(0x50, 400)); // 24LC16 Chip @400KHZ clock

//--------------- Write to EEPROM -----------------------
public void EepWrite(int Address, byte data)
        {
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address &                 0xFF),data });
            EepI2C.Execute(xActions, 1000);
            Thread.Sleep(5); // Mandatory after each Write transaction !!!
        }

//----------- Read from EEPROM ----------------------
public byte EepRead(int Address)
        {
            var Data = new byte[1];
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address & 0xFF) });
            EepI2C.Execute(xActions, 1000);
            Thread.Sleep(5);   // Mandatory after each Write transaction !!!
            xActions[0] = I2CDevice.CreateReadTransaction(Data);
            EepI2C.Execute(xActions, 1000);
            return Data[0];
        }

沒有留言:

張貼留言