chipKIT® Development Platform

Inspired by Arduino™

I2C not working propperly

Created Mon, 23 Apr 2012 14:39:28 +0000 by Pedes


Pedes

Mon, 23 Apr 2012 14:39:28 +0000

Hi, I am using some code for reading from a adxl345 sparkfun sensor on a max 32, and it sends out the correct values but when the accelerometer sends out the inverted values it suddanly jumps to 65000..

This code is something i have used on my arduino mega board, so should it not work on the max32 board??

here i am posting the code, the same error occurs for the gyroscope.


/* This file is part of the Razor AHRS Firmware */

// I2C code to read the sensors

// Sensor I2C addresses

#define ACCEL_ADDRESS ((int) 0x53) // 0x53 = 0xA6 / 2 //#define MAGN_ADDRESS ((int) 0x1E) // 0x1E = 0x3C / 2 #define GYRO_ADDRESS ((int) 0x69) // 0x68 = 0xD0 / 2

// Arduino backward compatibility macros #if ARDUINO >= 100 #define WIRE_SEND(b) Wire.write((byte) b) #define WIRE_RECEIVE() Wire.read() #else #define WIRE_SEND(b) Wire.send(b) #define WIRE_RECEIVE() Wire.receive() #endif

void I2C_Init() { Wire.begin(); }

void Accel_Init() { Wire.beginTransmission(ACCEL_ADDRESS); WIRE_SEND(0x2D); // Power register WIRE_SEND(0x08); // Measurement mode Wire.endTransmission(); delay(5); Wire.beginTransmission(ACCEL_ADDRESS); WIRE_SEND(0x31); // Data format register WIRE_SEND(0x08); // Set to full resolution Wire.endTransmission(); delay(5);

// Because our main loop runs at 50Hz we adjust the output data rate to 50Hz (25Hz bandwidth) Wire.beginTransmission(ACCEL_ADDRESS); WIRE_SEND(0x2C); // Rate WIRE_SEND(0xB); // 200 Hz Wire.endTransmission(); delay(5); }

// Reads x, y and z accelerometer registers void Read_Accel() { int i = 0; byte buff[6];

Wire.beginTransmission(ACCEL_ADDRESS); WIRE_SEND(0x32); // Send address to read from Wire.endTransmission();

Wire.beginTransmission(ACCEL_ADDRESS); Wire.requestFrom(ACCEL_ADDRESS, 6); // Request 6 bytes while(Wire.available()) // ((Wire.available())&&(i<6)) { buff[i] = WIRE_RECEIVE(); // Read one byte i++; } Wire.endTransmission();

if (i == 6) // All bytes received? { // No multiply by -1 for coordinate system transformation here, because of double negation: // We want the gravity vector, which is negated acceleration vector. accel[0] = (((int) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis) accel[1] = (((int) buff[1]) << 8) | buff[0]; // Y axis (internal sensor x axis) accel[2] = (((int) buff[5]) << 8) | buff[4]; // Z axis (internal sensor z axis) } else { num_accel_errors++; if (output_errors) Serial.println("!ERR: reading accelerometer"); } }

void Gyro_Init() { // Power up reset defaults Wire.beginTransmission(GYRO_ADDRESS); WIRE_SEND(0x3E); WIRE_SEND(0x80); Wire.endTransmission(); delay(5);

// Select full-scale range of the gyro sensors // Set LP filter bandwidth to 42Hz Wire.beginTransmission(GYRO_ADDRESS); WIRE_SEND(0x16); WIRE_SEND(0x19); // DLPF_CFG = 1 (188 Hz), FS_SEL = 3 Wire.endTransmission(); delay(5);

// Set sample rato to 50Hz Wire.beginTransmission(GYRO_ADDRESS); WIRE_SEND(0x15); WIRE_SEND(0x0A); // SMPLRT_DIV = 10 (50Hz) Wire.endTransmission(); delay(5);

// Set clock to PLL with z gyro reference Wire.beginTransmission(GYRO_ADDRESS); WIRE_SEND(0x3E); WIRE_SEND(0x00); Wire.endTransmission(); delay(5); }

// Reads x, y and z gyroscope registers void Read_Gyro() { int i = 0; byte buff[6];

Wire.beginTransmission(GYRO_ADDRESS); WIRE_SEND(0x1D); // Sends address to read from Wire.endTransmission();

Wire.beginTransmission(GYRO_ADDRESS); Wire.requestFrom(GYRO_ADDRESS, 6); // Request 6 bytes while(Wire.available()) // ((Wire.available())&&(i<6)) { buff[i] = WIRE_RECEIVE(); // Read one byte i++; } Wire.endTransmission();

if (i == 6) // All bytes received? { gyro[0] = -1 * ((((int) buff[2]) << 8) | buff[3]); // X axis (internal sensor -y axis) gyro[1] = -1 * ((((int) buff[0]) << 8) | buff[1]); // Y axis (internal sensor -x axis) gyro[2] = -1 * ((((int) buff[4]) << 8) | buff[5]); // Z axis (internal sensor -z axis) } else { num_gyro_errors++; if (output_errors) Serial.println("!ERR: reading gyroscope"); } }


Regards

Petar


les1943

Mon, 23 Apr 2012 18:48:45 +0000

Have you added pull up resistors to I2C clock and data ? they are needed on chipkit .


Pedes

Tue, 24 Apr 2012 05:01:23 +0000

Thanks for your reply les1943

Yes two 10 K, and i get an output, and it is super, on the positive side of the x axis as an example, but on the negative side it suddenly jumps from 0 gravity to 65536 and than it counts down from this value down. And if a I am not all wrong this is the maximal value a 16 bit representation can have.
The same thing goes for the y and z axis..

Greetings

Petar


Pedes

Wed, 25 Apr 2012 11:26:49 +0000

It was a sign issue, the casting should have been made to char instead of int.

accel[0] = (((char) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis) instead of: accel[0] = (((int) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis)

Petar


bafonso

Sun, 23 Dec 2012 20:42:52 +0000

It was a sign issue, the casting should have been made to char instead of int. accel[0] = (((char) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis) instead of: accel[0] = (((int) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis) Petar

I'm sorry for the newbie question but why do we cast to char in the chipkit instead of int that works on arduino?