Has anybody got any experience of the LSM303 3-axis accelerometer and 3-axis magnetometer?
I’m getting nowhere with this, partly this is my own fault since this is my first time trying to use I2C.
Here’s a subset of my code so far:
public enum WriteAction { AccWrite, MagWrite }
public enum ReadAction { AccRead, MagRead }
I2CDevice.Configuration ConfAccWrite = new I2CDevice.Configuration(0x30, 400);
I2CDevice.Configuration ConfAccRead = new I2CDevice.Configuration(0x31, 400);
I2CDevice.Configuration ConfMagWrite = new I2CDevice.Configuration(0x3C, 400);
I2CDevice.Configuration ConfMagRead = new I2CDevice.Configuration(0x3D, 400);
//Setup the device with a default config, we'll set this later
I2CDevice Device = new I2CDevice(new I2CDevice.Configuration(0x30, 400));
public void Write(WriteAction Action, byte register, byte value)
{
if (Action == WriteAction.AccWrite) { Device.Config = ConfAccWrite; }
else if (Action == WriteAction.MagWrite) { Device.Config = ConfMagWrite; }
I2CDevice.I2CTransaction[] Transactions = new I2CDevice.I2CTransaction[2];
Transactions[0] = I2CDevice.CreateWriteTransaction(new byte[] { register });
Transactions[1] = I2CDevice.CreateWriteTransaction(new byte[] { value });
if (Device.Execute(Transactions, 1000) == 0) { new Exception("Failed to exec WriteMag"); }
}
public byte[] Read(ReadAction Action, byte register)
{
byte[] ReturnedArray = new byte[7];
if (Action == ReadAction.AccRead) { Device.Config = ConfAccRead; }
else if (Action == ReadAction.MagRead) { Device.Config = ConfMagRead; }
I2CDevice.I2CTransaction[] Transactions = new I2CDevice.I2CTransaction[2];
Transactions[0] = I2CDevice.CreateReadTransaction(new byte[] { register });
Transactions[1] = I2CDevice.CreateReadTransaction(ReturnedArray);
if (Device.Execute(Transactions, 1000) == 0) { new Exception("Failed to exec ReadMag"); }
return ReturnedArray;
}
//INITIALISE HERE
public LSM303()
{
// Turns on the LSM303's accelerometer and magnetometers and places them in normal mode.
// Enable Accelerometer
// 0x27 = 0b00100111
// Normal power mode, all axes enabled
Write(WriteAction.AccWrite, LSM303_CTRL_REG1_A, 0x27); //turn on Acc, normal power, 100hz, all axis enabled
// Enable Magnetometer
// 0x00 = 0b00000000
// Continuous conversion mode
Write(WriteAction.MagWrite, LSM303_MR_REG_M, 0x00);
byte[] returnedval;
returnedval = Read(ReadAction.MagRead, LSM303_OUT_X_L_M);
Debug.Print(returnedval[0].ToString() + returnedval[1].ToString() + returnedval[2].ToString() + returnedval[3].ToString() + returnedval[4].ToString() + returnedval[5].ToString() + returnedval[6].ToString());
}
I get nothing back… (zero for all the byte in the array).
For reading data, the documentation for the hardware says you have to send a:
ST - Start command which it describes thus:
“The transaction on the bus is started through a START (ST) signal. A START condition is
defined as a HIGH to LOW transition on the data line while the SCL line is held HIGH.”
I don’t understand what this means…“a HIGH to LOW transition on the data line while the SCL line is held HIGH”???
After this it gets simpler and says I should send:
SAD - The slave address (7bit address with a write or read bit making up the 8th bit)
SAK - The device should then send back an acknowledgement
SUB - Then I should send a sub address (address relating to the register on the device to read from)
SAK - The device should then send back another acknowledgement
SR - I should then send back another start command… again, I don’t get how to do this (see above).
Anybody got any ideas? I’m really frustrated, it must be easy… so I’m feeling a bit dim at the moment.
This is the device: http://www.hobbytronics.co.uk/minimu-9?keyword=imu
Thanks to everyone in advance!
Tim