-1

I was looking into a datasheet of a Real Time Clock (RTC) with a temperature sensor MAX31328. (You can find the datasheet easily). I'd like to convert the raw binary value into decimal format. I have no problem with positive value but I'm not sure regarding negative especially if there is a fraction.

So they say that the temperature is represented in two's complement format. They show an example of a positive number 00011001 01b = +25.25 But how would you represent -25.25?

enter image description here

J. Mini
1,00510 silver badges23 bronze badges
asked Dec 3, 2021 at 20:59
6
  • 8
    This is a fixed-point format, not floating-point. Commented Dec 3, 2021 at 21:51
  • If it was floating point, it would include an exponent too. Commented Dec 3, 2021 at 23:26
  • 3
    The documentation clearly states that the temperature is encoded in two's complement format. That should be reasonably convenient for programming on pretty much any modern microcontroller or CPU. Commented Dec 4, 2021 at 10:51
  • 3
    @Nuz: as a friendly reminder, this question does not involve any of the topics related software architecture, software design, or software development life cycle. As such, it is a programming question, and is better asked on Stack Overflow. Since this question is also related to a discrete electronic component, it may also be on-topic for Electronics Stack Exchange. However, Stack Exchange is not meant for general product support questions. If the product supplier has a Q&A section, it is best to refer to the product supplier as the first stop for answers. Thank you. Commented Dec 4, 2021 at 20:51
  • 1
    Thank you all for your answers and clarification, i'm still trying to get familiar with the different parts of the forum. I'll ask better questions next time! I'll contact the support of the supplier to erase all the ambiguity. Commented Dec 4, 2021 at 23:23

3 Answers 3

3

So as promised, I asked the technical support, they gave me a first answer saying that the decimal value is separate from the integer value and that it shouldn't be taken as two's complement (possibility #2). I asked to double check due to the ambiguity. Afterward he confirmed that indeed he was mistaken and that all the values should be two's complement. So the answer is possibility #1 that @rwong was describing.

I recommended that they modify the datasheet to include an example of negative value. They accepted.

answered Dec 8, 2021 at 14:26
1
  • 4
    Thumbs up for digging to the source! Commented Dec 8, 2021 at 15:19
2

The unspecified or potentially ambiguous aspect only applies to negative temperatures. If your electronic application never needs to undergo negative temperature in Celsius, this concern may be sidestepped.

What is ambiguous is:

--------------------------------
| Temp deg C | MSB.LSB7.LSB6 |
| +1.00 | 0000'0001'00 |
| +0.75 | 0000'0000'11 |
| +0.50 | 0000'0000'10 |
| +0.25 | 0000'0000'01 |
| +0.00 | 0000'0000'00 |
|------------| --------------| -------------------------|
| Temp deg C | Possibly #1 | Possibly #2 (unlikely) |
| -0.25 | 1111'1111'11 | 1111'1111'01 |
| -0.50 | 1111'1111'10 | 1111'1111'10 |
| -0.75 | 1111'1111'01 | 1111'1111'11 |
| -1.00 | 1111'1111'00 | same as #1, no ambiguity |

For each listed ambiguous representation, the first column is more likely to be correct. This is because the first column allows straight-forward conversion into floating point values in software, as follows:

uint8_t msb = ...;
uint8_t lsb = ...;
uint16_t msb_lsb = (((uint16_t)msb) << 8) | lsb;
/* Below, treats top bit as the sign of two's complement.
 * After conversion into int16_t, further convert into float.
 */
float_t temp_c = (float_t)(int16_t)msb_lsb / 256.0f;

However, to be absolutely sure, one has to verify that through experiment, by chilling the chip to some negative Celsius temperatures. I've read that some types of canned air duster can be used for this purpose.

Note that the types of canned air duster capable of chilling to negative Celsius temperatures is considered a workplace hazard (also capable of causing frostbites and/or abused as an inhalant), and might be tightly controlled or banned by local jurisdiction or workplace rules.

answered Dec 4, 2021 at 7:29
1
  • 1
    Possible simpler version, given that the whole MSB is the integer part: int8_t msb = read_reg(0x11); uint8_t lsb = read_reg(0x12); double temp_c = msb + lsb/256.0;. N.B. different signedness of MSB and LSB. Commented Dec 5, 2021 at 8:03
1

The 8-bit two's complement representation of -25 is 11100111b, so -25.25 is represented as 11100110 11b. The chip represents temperature as a signed 8.2 fixed radix point number. If you are unfamiliar with fixed radix point numbers, you can find good information with an Internet search.

Bart van Ingen Schenau
78.8k20 gold badges129 silver badges196 bronze badges
answered Dec 3, 2021 at 21:31
4
  • What is unspecified or potentially ambiguous from the datasheet is whether the two's complement negation for negative temperature values should have applied to the two fractional bits (the two used bits on the LSB). For example, for any temperature at -(n + 0.25), if the fractional bits are negated as well, the LSB should read out as 0b11xx'xxxx, not as 0b01xx'xxxx. Since the documentation doesn't clarify this, the only way to test is to actually chill the unit to slightly negative temperature (gradually from 0 deg C to -0.25 deg C) and observe. Commented Dec 4, 2021 at 7:01
  • It is highly unlikely that the fraction bits are handled specially, that would make the device and the code handling it needlessly complex. Just concatenate the bits, sign extend if necessary, and multiply by 0.25 to yield °C. Commented Dec 4, 2021 at 8:22
  • I think it's pretty clear from the data sheet that it's a single twos-complement value split across two registers. -25.25°C is 0.25 degrees less than -25.00°C, so should be 11100110 11. Adding is just insane. Commented Dec 4, 2021 at 10:56
  • Due to the complexity of the project and the fact that the sensor is embedded in a PCB I unfortunately cannot perform the temperature check and see what's the right format. I'll let you people know what the supplier meant to say in the datasheet once I contact their support. Commented Dec 4, 2021 at 23:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.