Raw Register Decode and Thermocouple Volts

Raw Register Decode and Thermocouple Volts

Ada re-derives this chapter’s own numbers step by step, at full precision

foundations
math-foundations
calculation-audit
sensors
Ada ADA · CALCULATION AUDIT

Raw Register Decode and Thermocouple Volts

The chapter walks through raw DS18B20 register pairs to decode into Celsius, including a case that combines to 0xFF90 — either -7.0°C once two’s complement is applied, or a nonsensical 4089°C if the sign bit is ignored — alongside a K-type thermocouple example with hot junction 250°C, cold junction 25°C, and a 200x amplifier gain. This audit asks the question that discrepancy invites: is the naive unsigned read of 0xFF90 just a rounding slip, or does skipping the sign convention turn a valid cold reading into complete nonsense?

Companion to the chapter Lab: Temperature Sensors — every number here comes from that chapter.

See the relationship before changing it

The figure reads from left to right. The blue card is signed raw register value. The middle card applies this page's rule. The green card is decoded temperature. Walk the arrows once: set the input, apply the rule, then read the result with its unit.

The retained audit below checks several chapter fixtures. This model keeps those stated values fixed and changes only signed raw register value, so the numeric fixture does not switch without explanation.

Signed raw register value changes decoded temperature An input card leads through the rule temperature = signed raw value / 16 to the decoded temperature result. INPUT PAGE INPUT APPLY THE RULE predict calculate check units OUTPUT RESULT
Walk the arrows. Two-s-complement decoding turns the bit pattern into a valid cold reading before scaling.

Derive the baseline in four named moves

  1. 1

    Name the input. The chapter baseline is -112 counts.

  2. 2

    Name the relationship. temperature = signed raw value / 16

  3. 3

    Substitute with units. -112 / 16 = -7.0 deg C

  4. 4

    Read the result. Keep the unit beside the value. Use it only inside the technical boundary on this page.

Predict, then change signed raw register value

Try Predict the direction of temperature = signed raw value / 16. Test another signed raw register value, then compare decoded temperature.

-112 counts
Chapter baseline
Decoded temperature

Observe Two-s-complement decoding turns the bit pattern into a valid cold reading before scaling. Reset signed raw register value to -112 and compare decoded temperature.

Explain Two-s-complement decoding turns the bit pattern into a valid cold reading before scaling.

Check yourself

What should you do before trusting a moved-control result?
Answer: Predict its direction, apply the shown relationship, keep the units, and reset to the worked baseline.
What does this small model leave out?
Answer: Only signed raw register value moves here. Field effects named in the technical boundary stay fixed.
Try

Decode little-endian bytes 0xD0 0x01, then load 0x90 0xFF and activate Check audit with signed 16-bit interpretation.

Observe

The temperature field reads 29.0 C for 0x01D0 and -7.0 C for 0xFF90; unsigned decoding instead produces the impossible 4089 C state.

Explain

Two's-complement interpretation subtracts 65536 when bit 15 is set, turning the same bit pattern from a large unsigned magnitude into the intended negative temperature.

Ada: This chapter runs two conversions worth checking to the digit: the DS18B20 register-to-Celsius decode – including the signed case, where a naive parse is dangerous – and the K-type thermocouple voltage. The DS18B20 rule is T = raw / 16, because the low four bits hold the fraction, so one LSB is 1 / 16 = 0.0625 C.

The three register cases:

  • 0xD0 0x01 little-endian combines MSB:LSB to 0x01D0 = 464, so T = 464 / 16 = 29.0 C.
  • 0x90 0xFF combines to 0xFF90. Read as unsigned that is 65424, but bit 15 is set, so two’s complement gives 65424 - 65536 = -112, and T = -112 / 16 = -7.0 C. Skip the signed step and you would report 65424 / 16 = 4089 C – the difference between a valid cold reading and nonsense.
  • 0x0550 = 1360, so T = 1360 / 16 = 85.0 C, the power-on default.
  • At 9-bit resolution the low three bits are undefined, so the step widens to 2^3 / 16 = 0.5 C.

The K-type thermocouple (Seebeck coefficient ~41 uV/C, hot 250 C, cold 25 C, 200x amplifier, 12-bit 3.3 V ADC):

  • V = 41 x (250 - 25) = 41 x 225 = 9225 uV = 9.225 mV.
  • After gain: 9.225 mV x 200 = 1845 mV = 1.845 V, safely under the 3.3 V rail.
  • One ADC step is 3.3 V / 4096 = 0.8057 mV, which maps back to 0.8057 / (41 x 200 / 1000) = 0.0983 C per step.

The audit’s lesson is that the signed decode is not optional bookkeeping: the same 16 bits read as +4089 C or -7.0 C depending on whether two’s complement is honored, so the register width and sign convention belong in the acceptance test right next to the temperature.

Every number above is taken from the chapter’s own material and re-derived step by step.

Technical boundaries: The register and thermocouple arithmetic omits sensor conversion error, endian variation between devices, cold-junction measurement uncertainty, Seebeck nonlinearity, amplifier offset, lead effects, and ADC reference drift.

Ready: work the ledger before checking it.