Morning folks, got some basic understanding questions about the I2C methods.
I am new to I2C in general, but thought I understood it. For a device I am using (ADS122C04) this is the process I am trying to do:
//Initialization starts with a reset
//Then set the MUX
//Then set the voltage reference
//Start/Sync begin data conversions
//Read data
//convert data to usable information
To initialize I use a device.Write(new byte[] { 0x06 }); everything goes ok so far (I think, no exceptions anyways). Next, to set the multiplexer up, which reg address is 0x00, I need to set to 0x40, and I want to read an address to confirm the changes took place so I send device.WriteRead({ 0x00, 0x40 }, readBuffer).
This doesn’t work, my real question is how do you write to a specific address a specific value, seems any time I use more than one byte in the write buffer it sends an exception. Also, I get that the readbuffer is where it then stores the read data, but how to set which reg to read?
my code so far:
static void Main()
{
//START/SYNC Start or restart conversions 0000 100X = 0x08
//RESET Reset the device 0000 011X = 0x06
//POWERDOWN Enter power-down mode 0000 001X = 0x02
//RDATA Read data by command 0001 XXXX = 0x10
//RREG Read register at address 0010 RRXX = 0x20, 0x24, 0x28, 0x2C
//WREG Write register at address 0100 RRXX = 0x40, 0x44, 0x48, 0x4C
byte start = 0x08, reset = 0x06, powerdown = 0x02, rData = 0x10;
byte rRegA = 0x20, rRegB = 0x24, rRegC = 0x28, rRegD = 0x2C;
byte wRegA = 0x40, wRegB = 0x44, wRegC = 0x48, wRegD = 0x4C;
Thread.Sleep(10);//delay to allow power up cycle to settle.
byte _420mA_Sensor = 0x4C;//Slave Address
uint _Bus_Speed = 400_000;//
byte[] readBuffer = new byte[3];
var settings = new I2cConnectionSettings(_420mA_Sensor, _Bus_Speed);
var controller = I2cController.FromName(SC20100.I2cBus.I2c2);
var device = controller.GetDevice(settings);
try
{
//Initialization starts with a reset
device.Write(new byte[] { reset });
Thread.Sleep(10);
//Then set the MUX
device.Write(new byte[] { 0x00 });
device.WriteRead(new byte[] { wRegA }, readBuffer);
//Then set the voltage reference
//device.Write(new byte[] { 0x01, 0x04 });
//Start/Sync begin data conversions
//Read data
//convert data to usable information
foreach (byte a in readBuffer)
{
Debug.WriteLine(a.ToString());
}
}
catch(Exception exe)
{
Debug.WriteLine(exe.Message);
}
}