Questions on control of serial busses
Post Reply
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am


Post by YahooArchive »

Hi guys,

I'm trying to interface (in C) with an LSM303DLH sensor with the I2C functions.

I need to use I2COUT to write to reg address 0x20 value 0x27 at i2c
address 0x30 (to start with) and I2CIN to read from the sensor. I've
been using an LSM303DLH library written in C++ using mbed.h as a
guide, but I just can't get my head around how the I2COUT and ITCIN
functions take the parameters. I'm new to C and ARM, I'm afraid.

Here's the section of code I'm talking about:

[From the library]
const int addr_acc = 0x30;
CTRL_REG1_A = 0x20, //in enum REG_ADDRS
char reg_v;
reg_v = 0;
reg_v |= 0x01 << 5; /* Normal mode */
reg_v |= 0x07; /* X/Y/Z axis enable on the accelerometer. */
reg_v = 0;
read_reg(addr_acc,CTRL_REG1_A,®_v); //not sure why it's read at this point
read_reg_short(addr_acc, OUT_X_A, &a_x); //just pulling the values back
read_reg_short(addr_acc, OUT_Y_A, &a_y);
read_reg_short(addr_acc, OUT_Z_A, &a_z);

[My code, cribbed from Csample.c]
shortMessage[0] = CTRL_REG1_A;
shortMessage[1] = reg_v;
present = I2COUT (SDA, SCL, addr_acc, 9, shortMessage); // address,
followed by data
I2CIN(SDA, SCL, addr_acc, -1, -1, -1, -1, -1, 8, shortResponse);

How do you write to, and then read specific registers using
I2COUT/I2CIN? I'm assuming it's some configuration of shortMessage
for I2COUT and the ints in I2CIN's parameters. I'm sorry, I really
don't want to waste people's time - I can see the documentation on I2C
on the support pages - but I just don't understand it.

Also, I've been reading through this group and it's pretty amazing to
see the level of support it's given. Kudos!


Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am


Post by YahooArchive »

Looking at the data sheet for the LSM303

table 15 -- reading multiple bytes

would be done as

char bytes[20]; // however many you need

I2CIN (sda_pin, scl_pin, SAD, SUB, -1,-1,-1, count, bytes);

the +W +R of the diagram, read and write are handled by I2CIN

table 12 -- writing multiple bytes

bytes[0] = SUB;
bytes[1] = DATA1;
bytes[2] = ...

I2COUT (sda_pin, scl_pin, SAD, count, bytes);


It looks like the 303 is 2 chips in one package, as the addresses are different,
the 2 sda pins could be tied together, and the 2 scl pins could be tied

Post Reply