I see several issues here.
1) Slave address is incorrect
2) you need to use the slave address in every transaction, not just the first one.
3) you're printing binary data as string
The Slave Address associated with the KXPS5 is 001100X, where the programmable bit, X, is determined by the assignment of ADDR (pin 3) to GND or Vdd.
Since ADDR=GND this seems to mean that X=0. 0011000 is 0x18, however mbed's I2C class needs the full 8-bit address (with the r/w bit added), so that will be 0x30.
Try the following sequence:
address = 0x30;
data[0] = CTRL_REGB;
data[1] = 0xC2; //set CLKhld=1 so that we're forced to wait until a/d finishes
i2c.write(address, data, 2);
data[0] = CTRL_REGC;
data[1] = 0;
i2c.write(address, data, 2);
data[0] = XOUT_H; // read x msb
i2c.write(address, data, 1); // send register number
i2c.read(address, data, 1); // data[0] <- x msb
int msb = data[0];
data[0] = XOUT_L; // read x lsb
i2c.write(address, data, 1); // send register number
i2c.read(address, data, 1); // data[0] <- x msb
int lsb = data[0];
printf("x: %d\n", msb<<8 | lsb);
I'm having some trouble connecting to my Crodnet KXPS6 accelerometer. Datasheet
It's already got the pullup resistors, and it's wired as follows:
mbed - accelerometer
vout-vcc
gnd-gnd
p28-sda
p27-scl
p26-enable
ADDR is low (grounded), and CS is high (vcc on accelerometer).
Code so far:
All I get is:
"Data: '/'" printed 10 times. In binary it's 00101111, or 47 in decimal. I'm not sure what it means.
Any tips?