//KM 2018 03 23: getting lots of form spam; screen out dates before 2000 (book publication date) Calibrating the A/D converter and temperature | Byte Craft Limited

Calibrating the A/D converter and temperature

To sense the temperature, the thermostat uses a thermistor in an A/D converter. The thermistor's temperature response is known, and the output resistor can be chosen to bring the input voltage within a very close range around the input trigger level.

The routine ReadVoltage() loops through the reading routine 255 times: the read voltage is therefore determined as a value out of 255.

The temperature conversion software must be calibrated to ensure correct performance. This is the method we used:

We used a test routine to take some samples at full speed: the test routine was running on a J1A part, not the emulator.

  • At 24 degrees C, the count was 0x65.

  • At 16 degrees C, the count was 0x6C.

This gives us a slope. We used these numbers to calculate the following:

  • the slope is -1.142 degrees C / count. Multiply this by 256 to obtain 0x125.

  • the slope is also -2.057 degrees F / count. Multiply this by 256 to obtain 0x20F.

  • the count at 0 degrees C is 0x125.

  • the count at 0 degrees F is 0x20F.

The formula to calculate temperature is:

((Nzero - Nread) * slope)>>8

Substitute the count received from ReadVoltage() for Nread. To calculate Celsius, substitute the Celsius Nzero and slope. To calculate Fahrenheit, do the same with the other numbers.

You do not have to shift the result right by 8 to divide by 256. You can just take the most significant byte. To do this through C, use a union of an unsigned long and a struct of two unsigned ints. Assign to the long, and access the first int as the MSB.