Skip to main content
November 20, 2022
Solved

Variable of type float not showing negative float values

  • November 20, 2022
  • 7 replies
  • 4818 views

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 ?

This topic has been closed for replies.
Best answer by Tesla DeLorean

Can you see the right sign of a float you directly assign with a constant?

I​f so perhaps look at how you're converting or casting.

7 replies

Tesla DeLorean
Guru
November 21, 2022

Can you see the right sign of a float you directly assign with a constant?

I​f so perhaps look at how you're converting or casting.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
November 21, 2022

I type casted my variable to int16_t before I store it into a float variable and I could see the negative values!

Wijeden RHIMI
ST Employee
November 21, 2022

Hello BKola.2241,

I would like to thank you for proposing this issue.

Can you please inform me about the exact type of debug window if it was expression view, live expression or something else...?

Also, can you check if you have chosen somewhere casting in your code or the option <Cast To Type> inside your view as shown the figure bellow.

0693W00000WJ5foQAD.png 

Finally, to know the right solution of this stuff: I would like to see a screenshot of the different values displayed on your debug window! "You can't see the negative measurements on the debug window", so what do you see exactly: a positive value or some other errors?

Wijeden;

November 21, 2022

It is from the live expression window.At this moment they should go under 0. But they get stuck somewhere around(above) 0.

0693W00000WJ8wYQAT.pngJust to clarify, each cell of mag_fi_meas is a combination of two 8-bits words, build up through (raw_data[1]<<8 | raw_data[0]); (raw_data[2]<<8 | raw_data[3]); and (raw_data[4]<<8 | raw_data[5]); where the MSB of raw_data[1], raw_data[3] and raw_data[5] is the sign bit (data are stored in the registers using 2's complement)

November 22, 2022

@Wijeden RHIMI​ the issue was solved. Thank you for your effort

Piranha
Principal III
November 22, 2022

What's the problem of just reading the int16_t data and converting (assigning) it normally to float?

int16_t raw_data[3];
float mag_fi_meas[3];
 
SensorRead(&raw_data, sizeof(raw_data));
 
for (size_t i = 0; i < 3; ++i) {
	mag_fi_meas[i] = raw_data[i];
}

November 22, 2022

@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
Principal III
November 22, 2022

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