DS1820 Temperature Sensor High-resolution Readout Demo

Full high resolution code in a small 1K microcontroller

Full readout range of -55 to +125Deg C

Very cheap to build using the AT90S1200

The Prototype Board!The Prototype Board!
Description

I brought my first DS1280 back in January of 1998. They looked great, no analog or calibration to deal with and only around $10. It's heading towards the end of 2002 and I still think they are still the best temperature sensors.

This little demo board shows just how simple (Hardware wise) the DS1820's are. The DS1820 has 3 pin, one ground, one for VCC (+5V) and one data. All you do is connect the DS1820 to a 5-volt supply and connect the data line to one pin and your microprocessor. The Dallas 1-Wire protocol is based on timing of bits coming up and down the 1 wire-bus, so it is not the easiest thing to get going, but you only have to write that code once for all of the 1 wire interfaces.

By default most people just read the 'tempLSB' and 'tembMSB' to give a temperature reading of 0.5C resolution. This is very easy to do. Like always I like to push the limits, so I wanted to read the temperature in high resolution mode and using one of Atmel's smallest and cheapest microprocessors, the AT90S1200. The AT90S1200 is a great microprocessor, around $4.00 (AUD) in small quantities, runs up to 12 million instructions per second (impressive for an 8 bit micro). The drawbacks, only 1K bytes of programmable flash memory (512 instructions) and NO RAM. No RAM is not a big problem in the AVR series as this series has 32 general purpose working registers! The next problem is where are the multiply and divide instructions?

The DS1820 has 'scratch pad' of 8-bytes that can be read back from the device. In the scratch pad are the temperature LSB and MSB and down the list are the count_remain and count_per_C. It is these two values which are used with the temperature LSB and MSB to produce the reading in a higher resolution.

ScratchPad
Byte Register
0 Temperature_LSB
1 Temperature_MSB
2 Temp Alarm High / User Byte 1
3 Temp Alarm Low / User Byte 2
4 Reserved
5 Reserved
6 Count_Remain
7 Count_per_C
8 CRC

Temperature = Temp_read - 0.25 + ((Count_per_C - Count_Remain)/Count_per_C)

Where Temp_read = The value from the temp_MSB and temp_LSB with the least significant bit removed (the 0.5C bit)

Floating point maths would come in handy here. What do you think? Well floating point in 512 instructions with display and 1 wire code is near impossible, so 16-bit integer maths with fractions is used. In the 16 bits the most significant byte is the whole temperature value with the least significant byte being the fraction.


Assume  MSB = 00h
	LSB = 30h
   	Count_per_c = 10h
   	Count_Remain = 09h


1. First take the MSB and LSB

     MSB LSB
     00  30 h

2. Shift right 1 bit to remove the 0.5deg C bit

   = 00  18 h

This gives us the Temp_read value in hex. If we convert to decimal it becomes 24. 24deg C! As the temperature value is only 9 bits and we have removed the LSB we only need to keep the least significant byte. The high bit in the LSB is the sign bit.

3. We need to setup our whole and fraction bytes

     00  00  The First value is the whole number and the second value is the fraction.

   Therefore
   
     18  00  = 24Deg C

4. We need to subtract 0.25dec C  (100h / 4 = 40hex) 40 is 0.25Deg C in hex!

   - 00  40
   = 17  C0  --> Store this value for now

We need to calculate the ((Count_per_C - Count_Remain)/Count_per_C) now. This is quite simple.

5. Subtract Count_Remain from Count_per_C

        10 h
   -     9 h        
   =     7 h

6. As we are trying to produce a fraction ((Count_per_C - Count_Remain)/Count_per_C) will never be greater than 1. 
   If we multiply the above by 100h then divide by Count_per_C will get our fraction. Multiply by 100 is simple.
   Just move the value by 8 bits left.

     07 00 h
   / 00 10 h
   = 00 70 h --> Store both values (Can never be greater than 1 00h)

7. We need to add the fraction in step 6 to the main values from step 4.

     17  C0  -- From Step 4
   + 00  70  -- From Step 6
   = 18  30  

8. If we take the high byte and convert that to decimal it returns 24. Now 30 is the fraction, 
   but in hex format. It is 30h/100h = 48/256 = 0.1875! So our temperature is 24.19 Deg C

   Convert the fraction to decimal

         30  -- Fraction from Step 7
   *     64  -- 100 in hex 
   = 12  C0  

   If can dump the low byte as we don't need that accuracy. If we convert 12 h to decimal is
   becomes 18.

Our Temperature is 24.18 Deg C. That is simple enough. The code fits in the 512 instruction space with room to move. I have the low-resolution code and the display of register code there as well. This could all be removed. I also have the RS232 version that displays data to the LCD and down a serial line for logging on the PC.

What resolution can be achieved?

This has been very interesting topic for me. I have two of the original DS1820's in the larger 'PR35' package. These look similar to a TO-92 package, just double the length. Over one a half years ago now I brought two of the new TO-92 DS18S20's (One pictured above). Now the old DS1820's show a count_per_C of around 50hex = 80dec, but the new DS18S20's only have a count_per_C of 10hex = 16dec. Therefore the old DS1820's can produce a resolution of 1C / 80 = 0.0125C and the new style DS18S20 can only produce a resolution of 1C / 16 = 0.0625C. The old sensors were found to drift outside of the +/-0.5C accuracy for the 0 to 70C range after a few years where the new DS18S20 should perform to their spec. Looks like we will have to live with the 0.06deg C resolution. Do we need 0.01deg C resolution anyway? It's just overkill!

The Display readout. What do the numbers mean?

The top line of the LCD panel shows the raw hexadecimal numbers from the 'scratch pad' of the DS1820. The first 2 digits are the least significant byte (LSB) of the temperature reading with the next two digits being the most significant byte (MSB). And YES the MSB should be before the MSB!! The next two digits are the 'Count Remain' and then the last two being 'Count per C'. This line is really only for debugging purposes and shows the real state of the DS1820. The Bottom line is the important one, showing the temperature in both standard and high-resolution modes.

Schematics & PCB

Main Processor Board

Click to enlarge Protel File

Click on Schematic to enlarge

Firmware & Downloads
Where from here?

I have just finished a PCB for a 7-segment display. This uses four 7-segment displays and the AT90S1200. Currently only looking at displaying temperatures to a tenth of a degree, as the new DS1820's don't have the resolution required. It will measure temperature in the range from -55.0C to 125.0C. It will be a small and cheap panel readout for lots of applications.

Back to main page


Last Updated: Sunday 2nd February 2003
© Copyright 2001 - 2003 Wayne Peacock