This code example demonstrates how to connect to multiple sensor(s) and/or device(s) connected to an I2C (IIC) port on a GHI Electronics FEZ board.
This code is written in C# and assumes the development workstation has all required software installed.
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
public class Program
{
public static void Main()
{
I2CDevice.Configuration conDeviceA = new I2CDevice.Configuration(0x38, 400);
I2CDevice.Configuration conDeviceB = new I2CDevice.Configuration(0x48, 400);
I2CDevice MyI2C = new I2CDevice(conDeviceA);
I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
byte[] RegisterNum = new byte[1] { 2 };
xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
byte[] RegisterValue = new byte[1];
xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);
MyI2C.Config = conDeviceA;
if (MyI2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
else
{
Debug.Print("Register value: " + RegisterValue[0].ToString());
}
MyI2C.Config = conDeviceB;
if (MyI2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
else
{
Debug.Print("Register value: " + RegisterValue[0].ToString());
}
}
}
For more information on specific namespaces please refer to the following resources: