Pages: [1]   Go Down
Author Topic: Convert sketch from wire.h to I2C.h for MMA8452 Accelerometer  (Read 599 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
God Member
*****
Karma: 2
Posts: 535
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using sparkfun breakout board for MMA8452 accelerometer.  The sketch works fine on an Uno but hangs on the Leonardo after after 15 to 45 seconds.  I want to try using this I2C.h library instead of wire.h.  I took a shot and changing the code for I2C.h, but I'd like someone to tell me if I'm doing it right.  I'm hesitant to just try it and see if it works because I don't know if incorrect code might change some registers or something in the MMA8452 and mess up the configuration.  My whole sketch can be found here: http://pastebin.com/3K58YXg7.  But the functions in question are below:

Code:
// Read bytesToRead sequentially, starting at addressToRead into the dest byte array
void readRegisters(uint8_t addressToRead, uint8_t bytesToRead, uint8_t * dest) {
//  Wire.beginTransmission(MMA8452_ADDRESS);
//  Wire.write(addressToRead);
//  Wire.endTransmission(false); //endTransmission but keep the connection active
//  Wire.requestFrom(MMA8452_ADDRESS, bytesToRead); //Ask for bytes, once done, bus is released by default
//  while(Wire.available() < bytesToRead); //Hang out until we get the # of bytes we expect
//  for(int x = 0 ; x < bytesToRead ; x++)
//    dest[x] = Wire.read();   

  I2c.read(MMA8452_ADDRESS, addressToRead, bytesToRead, dest);
}

// Read a single byte from addressToRead and return it as a byte
uint8_t readRegister(uint8_t addressToRead) {
//  Wire.beginTransmission(MMA8452_ADDRESS);
//  Wire.write(addressToRead);
//  Wire.endTransmission(false); //endTransmission but keep the connection active
//  Wire.requestFrom(MMA8452_ADDRESS, 1); //Ask for 1 byte, once done, bus is released by default
//  while(!Wire.available()) ; //Wait for the data to come back
//  return Wire.read(); //Return this one byte

  uint8_t getByte;
  I2c.read(addressToRead, getByte, getByte);
  return getByte;
}

// Writes a single byte (dataToWrite) into addressToWrite
void writeRegister(uint8_t addressToWrite, uint8_t dataToWrite) {
//  Wire.beginTransmission(MMA8452_ADDRESS);
//  Wire.write(addressToWrite);
//  Wire.write(dataToWrite);
//  Wire.endTransmission(); //Stop transmitting

  I2c.write(MMA8452_ADDRESS, addressToWrite, dataToWrite);
}
Logged

Pages: [1]   Go Up
Jump to: