I2C Help Transfeering Arduinno code to C#

Hey Guys and Gals …I need some assistance with this pesky #C & I2C … I have an Arduino code I need transferred to C# and its got my head hurting today …If possible could someonedo a quick transpose for me …Itll be a good starting point…Thanks

Arduino Code for the LTC2945 Power sensor I2C

#include <Wire.h>
#define LTCADDR 0x6A

byte ADCvinMSB, ADCvinLSB, curSenseMSB, curSenseLSB, AinVMSB, AinVLSB;
unsigned int ADCvin, ADCcur, AinV;
float inputVoltage, ADCvoltage, current10, current1, current0p1, current0p01;

void setup() {//setup
Serial.begin(9600);
Wire.begin();
lcd.begin();

}

void loop() {

Wire.beginTransmission(LTCADDR);//first get Input Voltage - 80V max
Wire.write(0x1E);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR, 2, true);
delay(1);
ADCvinMSB = Wire.read();
ADCvinLSB = Wire.read();
ADCvin = ((unsigned int)(ADCvinMSB) << 4) + ((ADCvinLSB >> 4) & 0x0F);//formats into 12bit integer
inputVoltage = ADCvin * 0.025; //25mV resolution

Wire.beginTransmission(LTCADDR);//get ADC Input 2V max
Wire.write(0x28);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR, 2, true);
delay(1);
AinVMSB = Wire.read();
AinVLSB = Wire.read();
AinV = ((unsigned int)(AinVMSB) << 4) + ((AinVLSB >> 4) & 0x0F);//12 bit format
ADCvoltage = AinV * 0.5E-3; //500uV resolution

Wire.beginTransmission(LTCADDR);//get sense current
Wire.write(0x14);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR, 2, true);
delay(1);
curSenseMSB = Wire.read();
curSenseLSB = Wire.read();
ADCcur = ((unsigned int)(curSenseMSB) << 4) + ((curSenseLSB >> 4) & 0x0F);//12 bit format
//gets voltage across, 25uV resolution, then this converts to voltage for each sense resistor
current10 = ADCcur * (25E-3) / 10.0; //10mA max, unit is mA
current1 = ADCcur * (25E-3) / 1.0; //100mA max, unit is mA
current0p1 = ADCcur * (25E-3) / 0.1; //1A max, unit is mA
current0p01 = ADCcur * (25E-6) / 0.01;//10A max, unit is A

//Print everything out
Serial.print(“Vin:25mV/80V>”);
Serial.print(inputVoltage, 2);
// Serial.print(“V 10ohm:2.5uA/10mA>”);
//Serial.print(current10, 3);
//Serial.print(“mA 1ohm:25uA/100mA>”);
//Serial.print(current1, 2);
//Serial.print(“mA 0.1ohm:250uA/1A>”);
//Serial.print(current0p1, 1);
//Serial.print(“mA 0.01ohm:2.5mA/10A>”);
Serial.print(current0p01, 3);
Serial.print(“A ADC:0.5mV/2V>”);
Serial.print(ADCvoltage, 4);
Serial.println(“V”);
lcd.println(inputVoltage);
lcd.println(current0p01);

delay(10000);
//Check to see if data is ready with .dataAvailable()

This is an I2C device so I would use the I2CBus class in Netduino.Foundation. The address specified is 0x6a. So the first thing you need is a variable to allow access to the device:

var ltc = new I2CBus(0x6a, 100);

Where 100 represents the speed of the I2C communication (100kHz). You should check the data sheet for the component to verify that the device can work at this speed.

Next, lets have a look at the code in loop:

Wire.beginTransmission(LTCADDR);//first get Input Voltage - 80V max
Wire.write(0x1E);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR, 2, true);
delay(1);
ADCvinMSB = Wire.read();
ADCvinLSB = Wire.read();

This looks to be be reading from device register 0x1e. The following code puts 0x1e on the I2C bus:

Wire.beginTransmission(LTCADDR);//first get Input Voltage - 80V max
Wire.write(0x1E);
Wire.endTransmission(false);

And this then reads two bytes from the device:

Wire.requestFrom(LTCADDR, 2, true);
delay(1);
ADCvinMSB = Wire.read();
ADCvinLSB = Wire.read();

The Netduino.Foundation I2CBus class wraps all of this up into a single method and you can get the two byes from the 0x1e register with the following statement:

var data = ltc.ReadRegisters(0x1e, 2);

This statement will return a byte array containing two bytes, data[0] is the ADCvinMSB value and data[1] is the ADCvinLSB value.

The rest of the rest of the application in the loop method should translate directly to C# or is a repetition of the above logic.

For more information / hints, have a look at the MPL311A2 class in the Netduino.Foundation library.

Hope this helps,
Mark

1 Like

Thanks Nevyn …I’ll give this a try this am

using System.Threading;

using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace I2C_Sensor_Array_2

{
public class Program
{
private static int unsigned;

    public static object LTCADDR { get; private set; }
    public static object Wire { get; private set; }

    public static void Main()
    {
        I2CDevice ltc = new I2CDevice(new I2CDevice.Configuration(0x6a, 100));           

        byte[] buffer = new byte[2];
        I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[0x1e];
        reading[0] = I2CDevice.CreateReadTransaction(buffer);

        OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        // run forever
        while (true)
        {
            int bytesRead = ltc.Execute(reading, 2);

            // var data = ltc(0x1e, 2);
            led.Write(true); // turn on the LED
            Thread.Sleep(250); // sleep for 250ms
            led.Write(false); // turn off the LED
            Thread.Sleep(250); // sleep for 250ms

            byte ADCvinMSB, ADCvinLSB, curSenseMSB, curSenseLSB, AinVMSB, AinVLSB;
            int ADCvin;
            int ADCcur;
            float AinV;
            float inputVoltage;
            float ADCvoltage;
            float current10;
            float current1;
            float current0p1;
            float current0p01;
            //==========================================================================

            //==========================================================================
            ADCvinMSB = (reading());
            ADCvinLSB = (reading());
            ADCvin = (int Program.unsigned(ADCvinMSB) >> 4) & 0x0F;//formats into 12bit integer
            inputVoltage = ADCvin * 0.025; //25mV resolution

// Wire.beginTransmission(LTCADDR);//get ADC Input 2V max
// Wire.write(0x28);
// Wire.endTransmission(false);
// Wire.requestFrom(LTCADDR, 2, true);
AinVMSB = reading();
AinVLSB = reading();
AinV = ((unsigned int)(AinVMSB) <> 4) &0x0F);//12 bit format
ADCvoltage = AinV * 0.5E-3; //500uV resolution
// Wire.beginTransmission(LTCADDR);//get sense current
// Wire.write(0x14);
// Wire.endTransmission(false);
// Wire.requestFrom(LTCADDR, 2, true);
// delay(1);
curSenseMSB = reading();
curSenseLSB = reading();
ADCcur = ((unsigned int)(curSenseMSB) <> 4) & 0x0F);//12 bit format
//gets voltage across, 25uV resolution, then this converts to voltage for each sense resistor
current10 = ADCcur * (25E-3) / 10.0; //10mA max, unit is mA
current1 = ADCcur * (25E-3) / 1.0; //100mA max, unit is mA
current0p1 = ADCcur * (25E-3) / 0.1; //1A max, unit is mA
current0p01 = ADCcur * (25E-6) / 0.01;//10A max, unit is A
//Print everything out
//==============================================================================
}
}
}
}

This is what I have so far …I am getting errors because f the lower program
//==========================================================================
ADCvinMSB = (reading());
ADCvinLSB = (reading());
ADCvin = (int Program.unsigned(ADCvinMSB) >> 4) & 0x0F;//formats into 12bit integer
inputVoltage = ADCvin * 0.025; //25mV resolution
// Wire.beginTransmission(LTCADDR);//get ADC Input 2V max
// Wire.write(0x28);
// Wire.endTransmission(false);
// Wire.requestFrom(LTCADDR, 2, true);
AinVMSB = reading();
AinVLSB = reading();
AinV = ((unsigned int)(AinVMSB) <> 4) &0x0F);//12 bit format
ADCvoltage = AinV * 0.5E-3; //500uV resolution
// Wire.beginTransmission(LTCADDR);//get sense current
// Wire.write(0x14);
// Wire.endTransmission(false);
// Wire.requestFrom(LTCADDR, 2, true);
// delay(1);
curSenseMSB = reading();
curSenseLSB = reading();
ADCcur = ((unsigned int)(curSenseMSB) <> 4) & 0x0F);//12 bit format
//gets voltage across, 25uV resolution, then this converts to voltage for each sense resistor
current10 = ADCcur * (25E-3) / 10.0; //10mA max, unit is mA
current1 = ADCcur * (25E-3) / 1.0; //100mA max, unit is mA
current0p1 = ADCcur * (25E-3) / 0.1; //1A max, unit is mA
current0p01 = ADCcur * (25E-6) / 0.01;//10A max, unit is A
//Print everything out
//==============================================================================

It looks like you are using the I2CDevice class in the Microframework to try and access the device but you don’t seem to be executing the transactions to perform the read operation.

Have you looked at the I2C Guide in the documentation section of this site?

As I mentioned earlier, the I2CBus class in Netduino.Fountation does a lot of the heard work for you.

Regards,
Mark