Design Idea
Microcontroller converts digital-temperature-sensor readings without floating-point arithmetic
Convert digital-temperature-sensor readings without floating-point arithmetic.
Jordan Dimitrov, Toronto, ON, Canada; Edited by Martin Rowe and Fran Granville -- EDN, 3/5/2009
Digital temperature sensors combine a sensor, an ADC, and a serial interface in a single chip. They feature wide enough measurement range, good accuracy and resolution, no need of external parts, easy interface to microcontrollers, small size, and low price. In a review of 10 digital sensors from seven companies, all parts deliver signed-number data in two's-complement format. They feature temperature ranges of –25 to +85 or –40 to +125°C, accuracy of 0.5 to 2 or 2 to 4°C, and output data of 9 to 13 bits with 0.5 to 0.0312°C resolution. The devices' conversion time is 26 to 750 msec, and they use an SPI (serial-peripheral interface), an I2C (inter-integrated-circuit), or a 1-Wire interface. Power supplies are 1.5 to 3.6 or 3 to 5.5V, and prices range from 80 cents to $2.10 (1000).
These sensors connect to microcontrollers; hence, size, speed, and time to develop firmware are also important. The standard approach is to use a high-level language and a compiler. Development time is short, and performing even complex calculations is not a problem. However, compilers produce machine code that occupies more memory and runs at lower speed than code from an assembler. Also, compiler IDEs (integrated development environments) cost hundreds of dollars, whereas many companies offer free assembly-language IDEs. If you work on a tight budget or memory-space allotment, assembly language is the better option. The problem is to find a simple way to avoid the necessary floating-point calculations to convert sensor data into human-understandable format, both in Celsius and Fahrenheit. This Design Idea presents an effective approach.
Consider the TMP121 sensor from Texas Instruments. It provides 13-bit data in a 16-bit frame with resolution of 0.0625°C/bit. Hence, the transfer function is tC=0.0625×NS, where tC is the temperature in degrees Celsius and NS is the sensor data after you remove the three meaningless least-significant bits. You can easily rearrange the above equation to:
![]() |
(1) |
To get readings in degrees Fahrenheit, use the following equation, which converts degrees Celsius into degrees Fahrenheit: tF=[(9/5)×tC+32]. Replacing tC from the above equations yields:
![]() |
(2) |
The benefit of equation 1 and equation 2 is that you can perform the calculations with integer arithmetic only. They require divisions by powers of two, which you can replace with shifts, and division by 10, which you perform by introducing a decimal point in the display.
|
The circuit underwent testing with the popular 68HC11 microcontroller from Motorola (Figure 1). Besides a sensor and a controller, it includes a unit-selection switch and a dot-matrix LCD. The display resolution is 0.1°. The core of the supporting firmware is an endless loop in which the 68HC11 uses an output-compare function to generate a square-wave signal with a period of 1 sec and a duty cycle of 50%. The OC2 signal connects to the
input of the sensor and controls its operation: When
is high, the sensor measures temperature. The HC11 does nothing except display M on the LCD. When
becomes low, the last measurement latches in a shift register inside the sensor. The HC11 deletes M from the display, reads the switch and the sensor, manipulates the data, and displays the temperature.
Equation 1 and equation 2 provide the basis for two source codes. Listing 1 generates machine code of 981 bytes. Listing 2 generates machine code of 392 bytes. Despite the C-language approach with integer arithmetic, it needs 2.5 times more memory to do the job. The ratio is well above 10 if the C code goes with equations that need floating-point arithmetic. The benefit is clear: Modified equation 1 and equation 2 and assembly-language programming let you select a microcontroller with less memory and reduce the price of your design.


















