9 years, 3 months ago.

using I2C on the LPC1278 for a gyro

hi, i have worked with mbeds before but i am having problems with using the i2c pins on the mbed...

at first i thought i was using them correctly and came to the conclusion that my first gyro was faulty which was a MPU6050. the i realized i was doing it totally wrong by then i had already ordered a new gyro, the L3G4200D.

for now i am sticking to trying to code the L3G4200D, i used the code below just to simply try and communicate with it, for this i am trying to retrieve the value in WHO_AM_I register. but i am not getting any errors, i am just getting an incorrect value.

i would be really grateful if someone could point me in the right direction. thanks in advance.

L3G4200D

#include "mbed.h"
//L3G4200D I2C address
#define GYRO_ADDRESS = 0x68;
//L3G4200D register addresses
#define WHO_AM_I = 0x0f;
#define CTRL_REG1 = 0x20;
#define CTRL_REG2 = 0x21;
#define CTRL_REG3 = 0x22;
#define CTRL_REG4 = 0x23;
#define CTRL_REG5 = 0x24;
#define REFERENCE = 0x25;
#define OUT_TEMP = 0x26;
#define STATUS_REG = 0x27;
#define OUT_X_L = 0x28;
#define OUT_X_H = 0x29;
#define OUT_Y_L = 0x2a;
#define OUT_Y_H = 0x2b;
#define OUT_Z_L = 0x2c;
#define OUT_Z_H = 0x2d;
#define FIFO_CTRL_REG = 0x2e;
#define FIFO_SRC_REG = 0x2f;
#define INT1_CFG = 0x30;
#define INT1_SRC = 0x31;
#define INT1_THS_XH = 0x32;
#define INT1_THS_XL = 0x33;
#define INT1_THS_YH = 0x34;
#define INT1_THS_YL = 0x35;
#define INT1_THS_ZH = 0x36;
#define INT1_THS_ZL = 0x37;
#define INT1_DURATION = 0x38;

Serial pc(USBTX, USBRX);

I2C gyro(p28, p27); //sda, scl

int main() {
char whoami_reg[1] = {0x0f};
gyro.write(0x68, whoami_reg , 1, true);
char whoami_data[1] = {0};
gyro.read(0x68, whoami_data, 1, false);
int whoami = ((int) whoami_data[1]);
pc.printf("0.%x",whoami);
}

2 Answers

9 years, 3 months ago.

According to the datasheet the 7bit I2C slaveaddress of the L3G4200D is 110100xb. The mbed lib expects the 8bit address. Your code uses 0x68, which is the 7bit address. Try 0xD0 instead. That is assuming the SA0 pin, which codes the addressbit marked 'x', is at low level. The mbed slaveaddress is the 7bit address leftshifted by one position.

Accepted Answer

Worked like a charm. i added an extra bit of code to shift the slave address bits to the left by 1. also i had did not specify the frequency of the i2c interface... by doing these two things i got it successfully working.

Added code to fix the problem

int Gyro_Address = 0x68 << 1;
gyro.frequency(400000);

posted by Mobeen Yasin 28 Jan 2015
9 years, 3 months ago.

Corrected by Erik Thanks

The mbed libraries do this automatically, you don't need to do it manually.

posted by Erik - 28 Jan 2015

the mbed libraries change the LSB to either a 1 or 0 depending on if u write to or read from the slave peripheral , but if i simply injected the 0x68 address the LSB would have changed, but by shifting the slave address by 1 bit it would not affect the slave address because the LSB would have now been freed up for the R/W bit... i may be wrong but this is what i picked up from a few tutorials i used. correct me if i am wrong.

i also used this guys youtube tutorials for the i2c interface on the mbed:

posted by Mobeen Yasin 28 Jan 2015