Maharashtra HSC Computer Technology (Vocational) study materials
Maharashtra HSC Computer Technology (Vocational) study resources

Frozen Store Display: Reading a Two's Complement Sensor Byte

Maharashtra HSC Computer Technology (Vocational) · Put your knowledge to work in context.

All case studies

Frozen Store Display: Reading a Two's Complement Sensor Byte

An 8-bit microcontroller in a cold-storage room reads a digital temperature sensor and sends the raw byte to a 3-digit 7-segment display driver at the packing bay. Last week the bay display showed 246 while the room thermometer read -10 °C. The service engineer had written the routine with the sensor value held in an unsigned byte variable and converted it straight to decimal. The sensor datasheet states that the output is 8-bit two's complement in degrees Celsius, range -40 to +125, and the calibration certificate notes that the lowest value recorded this season was -18 °C. The programmer suggests a quick fix: 'if the value is more than 127, subtract 256 before displaying'. The store supervisor also wants the sign shown on a spare sign segment, and wants a genuine reading of 0 to appear as 0 and not as a blank digit. The driver chip accepts one BCD nibble per digit.

1. The bay display reads 246 while the thermometer reads -10 °C. Using the raw byte 0xF6, show where 246 comes from, prove the correct value, and state the rule the routine must apply for every one of the 256 possible byte values.

Read the worked answer

Where 246 comes from. 0xF6 = 15 x 16 + 6 = 240 + 6 = 246. That is simply hexadecimal converted as an unsigned 8-bit quantity; the sign bit was never examined.

Correct value. The datasheet declares two's complement, so bit 7 is the sign bit. 0xF6 = 1111 0110. Bit 7 = 1, therefore the number is negative. Take the magnitude by inverting the bits and adding 1: 0000 1001 + 1 = 0000 1010 = 10. So the reading is -10 °C. Check the other way: -10 in 8 bits = 256 - 10 = 246 = 0xF6. Both directions agree, so the display is wrong by 256 °C, not by a sensor fault.

The general rule. Treat the byte as signed 8-bit:

  • if bit 7 = 0, value = byte (0 to +127);
  • if bit 7 = 1, value = byte - 256 (-1 down to -128).

Boundary values to test: 0x7F = +127, 0x80 = 128 - 256 = -128, 0xFF = 255 - 256 = -1. The declared range -40 to +125 fits inside -128 to +127, so one signed byte is enough for storage, but the variable doing the subtraction must be a signed int, not an unsigned byte.

2. Evaluate the programmer's fix 'if raw > 127 then value = raw - 256'. Is it correct? What should the routine do at 0x7E, 0x00 and 0xD7, given the declared range of -40 to +125 °C?

Read the worked answer

The arithmetic is correct. For all 256 codes, 'subtract 256 when the top bit is set' produces exactly the same result as the formal sign rule. 0xF6: 246 - 256 = -10. 0x80: 128 - 256 = -128. 0xFF: 255 - 256 = -1. Codes with bit 7 = 0 pass through unchanged. So the fix is mathematically sound and cheaper than a branch on the sign bit.

But it is not sufficient on its own. Three guards are still needed:

  1. The comparison must be done on the raw unsigned byte before any arithmetic, and the result stored in a signed 16-bit variable. If the result is stored back into an unsigned byte, 246 - 256 wraps and the display is wrong again in a different way.
  2. Range checking is missing. 0x7E = +126 and 0x7F = +127 are outside the sensor's declared +125 °C limit, so they are fault codes, not temperatures and must show FAULT, not a number. 0xD7 = 256 - 41 = -41 °C is below the -40 limit and is also invalid; 0xD8 = -40 is the valid minimum.
  3. Zero handling and sign segment. 0x00 = 0 °C is a valid reading: the routine must display 0 (not blank) with the sign segment off. Negative readings must suppress leading zeros and light the sign segment.

Verdict: accept the fix, but add a range test and an explicit sign-segment rule.

3. The driver needs BCD input. Show the BCD bytes that must be fed for -10 °C, and explain what the operator would wrongly read if 0xF6 were simply split into three decimal digits.

Read the worked answer

BCD for -10 °C. The routine must work on the magnitude (10), not on the raw byte. Tens digit = 1, units digit = 0, so the driver receives two nibbles: 0001 0000 (0x10), with the hundreds digit blanked and the sign segment driven high. The sign is not part of BCD; it is a separate segment line, which is why the routine must keep magnitude and sign apart.

What goes wrong with the raw byte. Splitting 246 as decimal gives digits 2, 4, 6, i.e. 0010 0100 0110 on the three digit lines, which the operator reads as 246 °C with the sign segment dark - a positive number, 256 °C away from the truth. In a cold store that is worse than a blank display, because 246 °C looks like a plausible 'high temperature alarm' and staff may waste time checking the refrigeration plant instead of a software bug.

Design judgement. BCD is used here because each 4-bit nibble maps directly onto the 7-segment decoder with no binary-to-decimal division in the display routine. The display has 3 digits plus a sign, and the largest magnitude the sensor can legally report is 125, so the format is adequate; the real defect was never the display hardware but the missing signed interpretation and range check.