cancel
Showing results for 
Search instead for 
Did you mean: 

Variable of type float not showing negative float values

Kolab
Senior

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 ?

11 REPLIES 11
Kolab
Senior

@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.

Piranha
Chief II

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;
}