I have to connect a sensor to and EMX module.
Producer of module say that I have to read data from his sensor like that
Producer of sensor suggest to do something like that but I don’t know how to do!!
byte byte_msb, byte_lsb; // 8bit values
int16 pressure; // 16bit value
// Set I2C unit to I2C master mode, clock speed 100 kHz and 7 bit addressing
configureI2C (I2C_MASTER | CLK_SPEED_100KHZ | ADDRESSING_7BIT);
// Set the target address of the sensor (0x78 = 120dec)
I2C_set_target(0x78);
// Send start condition for reading from sensor (slave)
I2C_send_start_read();
// Read first (MSB) data byte and answer with ACK (continue communication)
I2C_read (&byte_msb, SEND_ACK);
// Read second (LSB) data byte and answer with NACK (end communication)
I2C_read (&byte_lsb, SEND_NACK);
// Send Stop condition
I2C_send_stop();
// Put both values together
I don’t know how to do with i2c library. Actually code that I’m using replay evry time 0 for evry pressure applied to this sensor.
Sensor is working fine.
The second sensor is a temperatur sensor on i2c bus and it works fine, problem is on the first one…
Ideas???
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO;
namespace change_this_to_your_namespace
{
public class Program
{
public static void Main()
{
//create I2C Device object representing both devices on our bus
I2CDevice.Configuration conDeviceA =
new I2CDevice.Configuration(0x78, 100);
I2CDevice.Configuration conDeviceB =
new I2CDevice.Configuration(0x48, 100);
//create I2C Bus object using one of the devices on the bus
I2CDevice MyI2C = new I2CDevice(conDeviceA);
// Note you could have chosen to create the bus using the conDeviceB parameter, which ever you choose it will be the "selected" device on the bus to start with. Here's how you would do that:
// I2CDevice MyI2C = new I2CDevice(conDeviceB);
//create transactions (we need 2 in this example)
I2CDevice.I2CTransaction[] xActions =
new I2CDevice.I2CTransaction[2];
// create write buffer (we need one byte)
byte[] RegisterNum = new byte[1] { 0 };
xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
// create read buffer to read the register
byte[] RegisterValue = new byte[2];
xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);
// Explicitly set the I2C bus to access device A by setting the I2C Config to the Device A's config.
MyI2C.Config = conDeviceA;
if (MyI2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
else
{
Debug.Print("Register value: " + RegisterValue[0].ToString() + RegisterValue[1].ToString());
}
// Explicitly set the I2C bus to access device B by setting the I2C Config to the Device B's config.
MyI2C.Config = conDeviceB;
if (MyI2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
else
{
Debug.Print("Register value: " + RegisterValue[0].ToString());
}
}
}
}