# MSP430 - ADC Interface - Digital Thermometer

Now that we have verified that the ADC is configured properly and working, we can connect the LM35 and then look at the numbers.

Connect the LM35 to Vcc and GND of the MSP430G2553. Connect Vout from the LM35 to P1.7 (pin-15) of the MSP430G2553.

It is educational to look at the math.

The LM35 outputs 10mV/°C and hence the output is 250mV at 25°C.

The ADC range is 0V to Vcc, i.e. 0 to 3.6V. Hence we are only using a fraction of the full ADC range. At 3.6V fullscale that would be equivalent to a temperature of 360°C. I don't think the LM35 is going to survive at that temperature!

1024*250/3600 = 71

that is, we are only at 7% of the full ADC range.

One ADC count is 360/1024 = 0.35 °C
Hence the step size in °C is rather course.

In order to improve on the resolution we would have to amplify the output of the LM35 by at least a factor of 3.

But what the heck. Let's go ahead and try it just the way it is.

At 25°C, we want to take advantage of the three digit display and show 25.0°C. Hence we need to scale up the ADC reading by a factor of

250/71 = 3.52

At some point we should perform a temperature calibration. Normally you could perform a 2-point calibration, one in an ice bath and the other at a higher temperature. If the LM35 outputs 0V at 0°C and is reasonably linear, then a 1-point calibration would suffice. This would allow you to arrive at the correct calibration factor.

Floating point vs fixed point arithmetic

This brings me to a very important point about math on microcontrollers.

A lot of my work is developing computer systems for science, engineering and industry using embedded microcontrollers. I need to implement solutions that are fast and efficient with limited amount of memory space.

I am very conscious of the fact that floating point arithmetic requires extra resources, CPU cycles and memory space. Hence I rarely use floating-point math in my applications.

The simpler solution is to use fixed point arithmetic.

Suppose I need to multiply by 3.5
A simple solution is to multiply by 35 and divide by 10.

If I need to improve my precision, I can multiply by 352 and divide by 100.

Division by 100 will require extra time to execute. A better solution is to multiply by 451 and divide by 128.

Or I could multiply by 901 and divide by 256. In this case I will have to extend the multiplication by using long integers (32-bit integers). To divide by 256, I simply drop the least significant byte of the four bytes.

Here are the additions to the program using 16-bit integer arithmetic:

Code:
#define CAL 451

unsigned short v;

// changes to main program

v = v * CAL;
v = v >> 7;
display_BCD(v);
Notes:

1) With 16-bit integer arithmetic the temperature range is limited to 0 to 50°C.

If you choose to amplify the output of the LM35 you will have to use 32-bit instead of 16-bit arithmetic.

Author
MrChips
Views
4,043