My second post! This is exhilarating. I admittedly know very little about I2C. However, I’m using a library to interface to the MMA8452 Accelerometer. I know that the library utilizes pull ups, does the Spark I2C library support them?
byte readRegister(uint8_t address)
{
byte data;
i2cSendStart();
i2cWaitForComplete();
i2cSendByte((MMA8452_ADDRESS<<1)); // write 0xB4
i2cWaitForComplete();
i2cSendByte(address); // write register address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte((MMA8452_ADDRESS<<1)|0x01); // write 0xB5
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
data = i2cGetReceivedByte(); // Get MSB result
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
return data;
}
void eHealthClass::readRegisters(byte address, int i, byte * dest)
{
i2cSendStart();
i2cWaitForComplete();
i2cSendByte((MMA8452_ADDRESS<<1)); // write 0xB4
i2cWaitForComplete();
i2cSendByte(address); // write register address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte((MMA8452_ADDRESS<<1)|0x01); // write 0xB5
i2cWaitForComplete();
for (int j=0; j<i; j++) {
i2cReceiveByte(TRUE);
i2cWaitForComplete();
dest[j] = i2cGetReceivedByte(); // Get MSB result
}
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN);// Disable TWI
sbi(TWCR, TWEN);// Enable TWI
}
The writeRegister function looks like this:
void writeRegister(unsigned char address, unsigned char data)
{
i2cSendStart();
i2cWaitForComplete();
i2cSendByte((MMA8452_ADDRESS<<1));// write 0xB4
i2cWaitForComplete();
i2cSendByte(address);// write register address
i2cWaitForComplete();
i2cSendByte(data);
i2cWaitForComplete();
i2cSendStop();
}
Perhaps I’m in over my head with this one! I just really don’t understand much about I2C. Do all of these I2C commands have equivalents in the Spark Wire library? That being said, is it possible to develop libraries for Spark? Or does everything have to be on one page? Or, would it be simpler to just have a small arduino talk to the accelerometer and then talk to the Spark over I2C. I’ve written a lot of code around those two functions, so if they don’t have replacements, thats my only other option.
Thanks for any help! It’s been a blast.