Hi Noro,
Sensor API was written according data sheet description, support to be easily migrated to different host MCU.
It is recommended that you use the sensor API to run it well first, then cut the code size.
//Write data in 16 bits
void writeRegister16(uint16_t reg, uint16_t value) {
Wire.beginTransmission(INC_ADDRESS);
Wire.write(reg);
//Low
Wire.write((uint16_t)value & 0xff);
//High
Wire.write((uint16_t)value >> 8);
Wire.endTransmission();
}
//Read data in 16 bits
uint16_t readRegister16(uint8_t reg) {
Wire.beginTransmission(INC_ADDRESS);
Wire.write(reg);
Wire.endTransmission(false);
int n = Wire.requestFrom(INC_ADDRESS, 4);
uint16_t data[20];
int i =0;
while(Wire.available()){
data[i] = Wire.read();
i++;
}
return (data[3] | data[2] << 8);
}
//Read all axis
void readAllAccel() {
Wire.beginTransmission(INC_ADDRESS);
Wire.write(0x03);
Wire.endTransmission();
Wire.requestFrom(INC_ADDRESS, 20);
uint16_t data[20];
int i =0;
while(Wire.available()){
data[i] = Wire.read();
i++;
}
//Offset = 2 because the 2 first bytes are dummy (useless)
int offset = 2;
x = ((data[offset + 1] << 8 | (uint16_t)data[offset + 0])); //0x03
y = ((data[offset + 3] << 8 | (uint16_t)data[offset + 2])); //0x04
z = ((data[offset + 5] << 8 | (uint16_t)data[offset + 4])); //0x05
gyr_x = (data[offset + 7] << 8 | (uint16_t)data[offset + 6]); //0x06
gyr_y = (data[offset + 9] << 8 | (uint16_t)data[offset + 8]); //0x07
gyr_z = (data[offset + 11] << 8 | (uint16_t)data[offset + 10]); //0x08
temperature = (data[offset + 13] << 8 | (uint16_t)data[offset + 12]); //0x09
}
code for arduino working
Hi Noro,
If you looked at the code for the previous attachment, the only hardware-related functions are the SPI write and read functions.
Once the SPI read, write function is normal, the code will run.