void writeRegister( I2C& i2c, char registerAdress, char *data, int nbBytes ) { if( nbBytes == 0 ) return; i2c.start(); int ack = i2c.write( MMA8452_SLAVE_ADRESS ); if( ack == 0 ) return; ack = i2c.write( registerAdress ); if( ack == 0 ) return; for( int i = 0; i < nbBytes; i++ ) { if( i2c.write(data[i]) != 1 ) { return; } } i2c.stop(); }
void readRegister( I2C& i2c, int registerAdress, char buff[], size_t nbBytes ) { if( nbBytes == 0 ) return; i2c.start(); int ack = i2c.write( MMA8452_SLAVE_ADRESS ); if( ack == 0 ) return; ack = i2c.write( registerAdress ); if( ack == 0 ) return; i2c.start(); ack = i2c.write( MMA8452_SLAVE_ADRESS | 1 ); if( ack == 0 ) return; for( int i = 0; i < nbBytes; ++i ) { buff[ i ] = i2c.read( i == nbBytes - 1 ? 0 : 1 ); } i2c.stop(); }
/********************************************************** * getExtTemp - Gets the current external temperature from the sensor. * * @return float - The external temperature in degrees C **********************************************************/ float TMP421::getExtTemp(void) { uint8_t highByte = 0x00, lowByte = 0x00; // I2c.beginTransmission(_address); // I2c.write(0x01); // I2c.endTransmission(); // I2c.requestFrom((int)_address, 2); // request 2 byte from address 1001000 // while(I2c.available()) // { // highByte = I2c.read(); // Read the first octet // lowByte = I2c.read(); // Read the second octet // } I2c.write(_address, (byte) 0x01); I2c.read(_address, (uint8_t) 2); if(I2c.available()) { highByte = I2c.receive(); lowByte = I2c.receive(); } return getFraction(highByte, lowByte); }