Page 124

All variables named temp1 should be temp.

So the bottom part  of the page should read:

 

All we now have to do do is to put the two bytes together as a 16-bit integer. As the micro:bit supports a 16-bit int we can do this very easily:

 

int16_t temp= (b2<<8 | b1) ;

 

Finally we need to convert this to a floating point value. As already discussed, the micro:bit doesn’t support this in hardware. As all we really want is two digits to give the tens and two digits to give the fractional part it is easier to work in integer arithmetic using temperature*100:

 

temp = temp * 100 / 16;

This gives us an integer value that represents the temperature in hundredths of a degree centigrade, e.g. 25.45C is represented as 2545. Notice that this only works because int16_t really is a 16-bit integer. If you were to use a 32‑bit int:

 

int temp= (b2<<8 | b1);

then temp1 would be correct for positive temperatures but it would give the wrong answer for negative values because the sign bit isn't propagated into the top 16 bits. So if using a 32-bit integer, propagate the sign bit manually:

 

int temp=(b2<<8 | b1); if(b2 & 0x80) temp=temp | 0xFFFF0000;

Assuming we have the temperature in 100ths in temp, we can now display it on the micro:bit's LED matrix. First we need to convert it into a string:

 

char buff[10];

 

 

Note: It is correct in the final listing.