Electronics Projects

MSP430 based thermometer with LCD

Temperature sensor with SPI interface

Sensors implementing the I2C or 1-wire interfaces are very universal a sense that only one or two wires are required to connect it with microcontroller and a single bus can serve several sensors. However, in simple thermometers with just one sensor the bus creates a problem. On one hand, the bus pulling-up resistors should be as large as possible to prevent any power lost. On the other hand, using large resistors automatically means that the clock frequency cannot be high.

The SPI interface provides a good alternative. Well, it needs one more wire, but usually there is a plenty of free ones in a LCD driving microcontroller. Another advantage of SPI is a simplicity of its protocol. I mean that the sensor TMP121, manufactured by TI, has just one internal register for storing the temperature, so no information is needed to be send to the sensor to configure it. The communication is reduced to just reading from sensor, which drops the session length down in a factor of 2 at least.

A disadvantage is that the sensor consumes more power compared with MCP9803. In the CS pin is pulled low, the sensor initiates periodic temperature conversion with a period of about 500msec, out of which one half is devoted to the conversion itself. During this phase the sensor consumes 50μA and this value is 20μA between the conversions resulting in 35μA of average current consumption. However, by pulling the CS pin high the sensor enters a sleep mode and draws just 0.1μA. Therefore, the general strategy is to turn the sensor on by pulling CS pin down, wait for conversion (typically 240msec in which CPU can be in its sleep mode), read the temperature as fast as possible and put it on sleep till the next conversion by pulling the CS pin high.

The sensor provides a 0.5°C accuracy for room temperatures (actually, in a much wider range) with a 12-bit resolution (0.0625°C) and comes in a tiny SOT23-6 package. One does not need to read all bits from sensor. In our application we only display integer values of temperature, so I read only 10 bits out of 13 from sensor (the sign bit, 8 bits of integer part and one fractional bit for rounding off). I have to admit, the sensor works just great an fits perfectly into this design.

Schematic Board Temp in °C

Texas Instruments MSP430F4260 microcontroller

Finally, I made time to try those devices. This microcontroller has a huge memory of 24K (in assembler scale) and primarily intended to be programmed on C. I thought that this might be because of a more complicated assembly language compared with, say, Microchip devices. It turns out to be not the case, however. The assembly language is somewhat different, of course, but if one has some experience with Microchip assembler, it should not be a problem to learn this one. Actually, after several days of programming I just love it. The language and the architecture has several nice features that are missing in PIC16 devices. First of all, this concerns the stack, which can be used for parameter passing. Second, very flexible interrupt control with vectored interrupts and several priority levels. Third, it is 16-bit... OK, just read a book or data-sheet - at the end of all it is a personal preference which microcontroller to use.

The MSP430 devices are famous with their low power consumption (see details below). The 400-series controllers all have LCD module built-in. Its configuration is amazingly simple - it takes just two assembler lines to set it up! The power consumption in a sleep mode with working LCD is just 2.9μA. In this mode the master clock, hence, the CPU is off. TI guarantees that master clock stabilizes upon an awake from sleep in 6μsec. This is a very impressive parameter. There are plenty of configuration options allowing to tune the clock very accurately in order to save every microwatt of power. I used default master clock settings of 1,048MHz vs 32,768 kHz during sleep, which is good enough for my application.

An unusual (for me) CPU feature is a large number of dedicated pins. Thus, 7 pins connect it with a JTAG programmer interface, out of which 2 are for the power. The board is shown above is rather experimental. I left a place on it for mounting a DC-DC voltage converter on MAX1724 for powering the device from a single AA or AAA cell. However, it works great from a 3V lithium battery without any converter. The LCD is attached to the circuit by using a home-made socket, built from a standard 18-pin IC socket.

Software

The entire code is written in assembly language and is intended to be used with EW430 IDE produced by IAR Systems. If one compares it with the code for a similar thermometer based on a Microchip PIC16 device, the first noticeable thing is a difference in the code length, which is about 3 times shorter now. It is partially due to a very flexible addressing system and partially to a 16-bit architecture.

After necessary modules setup and configuring all unused ports for output (to save power) the program proceeds with clearing the LCD register and setting up basic timer1 interrupts. This timer is used to generate all timing intervals and awake the CPU from sleep. The main loop starts with turning on the sensor and, thus, requesting a temperature conversion. Right after that the CPU goes in sleep for 256msec. During this time the sensor is supposed to complete temperature conversion. The typical conversion time is 240msec according to the data-sheet, which is a bit close to the time we give it. So, if your sensor needs a longer time to complete conversion, you can increase it to 512msec by loading the value 0x25 into the BTCTL register in the second line after the label loop. Once the CPU is awake from sleep, it gets the temperature reading from sensor (10 bits as explained above) and rounds it off by adding 0.5 and dropping the fractional part (for negative values of temperature, we subtract 0.5 instead). The value of temperature since the last reading is stored in register R6. If the current value is different from the old one, we convert it into BCD and send to the screen. Upon completing this, the CPU goes into a 2sec sleep again, thus providing a 2sec intervals between the temperature samplings and screen updates.

The bin2BCD() procedure that computes the BCD representation of temperature stored in R4 is amazingly simple due to the powerful CPU instructions (dadd) for dealing with numbers in the BCD representation. It takes about 235 CPU cycles to compute the BCD representation in R5 and about 145 cycles to update the display. The display updating procedure just takes the LCD codes from a table by using the BCD representations of digits as keys. It takes 4 instructions to update each digit, and this is where the auto-incrementing addressing mode of this CPU helps. The value below -10°C are displayed as "-_", while the ones above 99°C are displayed by showing two upper bars on display "--". Anyway, those extreme values are not typical for living rooms and are outside of the working range of the used LCD.

Current consumption

What is the average current consumption? Let us first use the current drawing data taken from the device data-sheets: max 50μA for the sensor in active mode, typical 400μA for CPU in active mode (1.046MHz master clock @3V), and 1.6μA in for CPU sleep mode with active LCD working in static mode. I also add some current for the LCD which I estimate as 1.4μA, resulting in total 3μA in sleep mode:

Therefore, the average current consumption is approximately

(53·250,000 + 450·380 + 3·2,000,000) / (2,000,000 + 250,000 + 380) = 8.7μA

By powering the thermometer from a CR2032 battery (capacity 225mAh), the device is expected to work from one cell for

225/0.0087 = 25,862 hours = 1,077 days = 2.95 years

Too good to be true? I have measured current consumption in each of the modes. It turns out that CPU running at full speed and the sensor together consume 450μA. The working sensor and CPU in sleep mode draw together about 44.5μA. Here is an evidence of a low current consumption in sleep mode. The blue box is my micro-ampermeter project and it shows the current drawn from the battery in microamperes. As one can compute by using the above timing values, the real average current consumption is 7.64μA, which is even better than the estimated one, theoretically allowing CR2032 to last for over 3 years.

Sleep mode Sensing mode

I apology for a poor quality of the second image - it was a bit difficult to catch the 0.25sec time interval and not moving the camera. In order to measure the current consumption at full CPU speed, I had to recompile the program by leaving the sensor on for the entire time and put the CPU into an infinite loop instead of a sleep. In real conditions to catch a 450μA pulse is practically impossible, as it takes about 0.46msec.

This was my first experience with MSP430 micro-controllers and it turns out to be very positive. I applaud to its designers for many aspects, including a clever compromise between the CPU productivity and its current consumption by setting the default clock speeds. I plan to play with the clock settings later - this was the main reason for building the micro-ampermeter. The CPU is very powerful and thermometer is just a trivial task for it.

Downloads


Last modified:Mon, Jan 23, 2023.

12903