2022-11-20 01:47 PM
I am reading data from a magnetic field sensor and storing it in a variable of type float. But what concerns is that I can't see the negative measurements on the debug window. When I store it in a int16_t variable the negative measurements show up. How could I watch the negative float values as well ?
Solved! Go to Solution.
2022-11-22 12:40 AM
@Piranha the data are stored in 8-bit registers. The full data size is 16 bit. So I read each 8 bit aside, join and type cast it to int16_t. At the end divide it by an integer coeficient.
2022-11-22 02:48 PM
Many sensors support reading all channels and data bytes with a single sequential read operation, in which case my first example is relevant. If it doesn't, you can still do something like this:
float mag_fi_meas[3];
union {
uint8_t bytes[2];
int16_t value;
} raw_data;
for (size_t i = 0; i < 3; ++i) {
raw_data.bytes[0] = SensorRead(i, LSB);
raw_data.bytes[1] = SensorRead(i, MSB);
mag_fi_meas[i] = (float)raw_data.value / (float)COEFFICIENT;
}