|
Practical Temperature with the PAK-IWhat's the easiest way to measure temperature with the Stamp? Easy -- use a thermistor, right? Thermistors are cheap two-terminal devices that change resistance in relation to temperature. The Stamp can measure resistance using the RCTime command, therefore it can also measure temperature. Well, sort of. The problem is, RCTime doesn't return resistance, it returns "units". And even if you know the resistance, it isn't trivial to convert that into a temperature -- especially with the Stamp's integer-only math. That's where a PAK-I can come in very handy. The ability to do real floating point math is a big help when you want to do engineering conversions. The hardware for this project is very simple. Just connect a PAK-I like it shows in the manual (use pin 0 for data and pin 1 for clock). Then connect a Radio Shack 271-110A thermistor between pin 7 and ground. Put a .1uF capacitor between pin 7 and +5V. Then you are all set! The SolutionReading a raw value from the thermistor is very easy. You can read more about the RCTime command in the Stamp manual, but the idea is: HIGH Therm Pause 1 RCTIME Therm,1,raw The Stamp manual says that the raw count will equal to 600 x R x C where R is in kohms and C is in microfarads. Since C is fixed at .1uF in this case, a little algebra reveals that R = raw/60. I wanted to work in ohms, so I'll multiply this result by 1000. Next, you need to relate the R value to temperature. The back of the thermistor's cardboard package has a table of temperatures from -60C to 110C. I decided to limit my interest to 0C to 40C. It doesn't usually get to either extreme in my lab! You can use any curve fit program that you want. However, since this is a Stamp project, I used Parallax's GAUSFIT program (available on their Web site -- it is part of one of the Stamp I application notes). I prepared a file that had the resistance in ohms related to the temperature in centigrade. Be careful not to add any blank lines at the end of the file: 22050 5 14680 15 12090 20 10000 25 8313 30 6941 35 5928 40 This file, named GF.DAT by default, provides input to GAUSFIT. If you run it with no arguments, it creates a 3rd order polynomial to fit the curve. In other words, the program will generate 4 constants (C0 to C3). The temperature will then be: temp = C3 x R**3 + C2 x R**2 + C1 x R + C0 (where **3 is the 3rd power, **2 is the 2nd power). This works pretty well, but the maximum error (reported by the program) is over 0.5 at 40 degrees (1.35%) -- not very accurate. Adding a number to the command line lets you change the order of the polynomial. Setting the order to 4 give you an extra term in the polynomial and reduces the maximum error to around 0.25 at 35 degrees (0.7%) -- a good deal better. The thermistor is rated at 1%, but the capacitor is probably 10% or more depending on the kind you use. The PAK-II can perform exponentiation directly, but the PAK-I can't. You could compute x**4 as x*x*x*x, but it is simpler to rewrite the polynomial like this: temp = C0+R x (C1+R x (C2+R x (C3+R x C4))) Note: I could have done a little work and reformulated the GF.DAT file so that it correlated raw counts to temperatures. However, I decided to do it in 2 steps so there would be more code to study. You can try working out a new GF.DAT file on your own if you like. According to GAUSFIT, the numbers you need are: C0 = 84.78281 C1= -1.099857E-02 C2 = 7.037247E-07 C3 = -2.325349E-11 C4 = 2.954709E-16 The PAK only has 2 registers, X and Y. It also has two temporary holding registers. The best way to approach the problem is like you were doing it by hand or using an RPN calculator. So the sequence will be: Load R Load C4 * Load C3 + Load R * Load C2 + Load R * Load C1 + Load R * Load C0 + You can load integers directly into the PAK, but it is more efficient to load them as floating point numbers using a 32-bit hex format. The PAK includes a program FConvert that runs on your PC that converts numbers into the PAK's hex format. The numbers we need for our program are: 60 - $84700000 1000 - $887A0000 C4 - $4B2A53CF C3 - $5BCC8A34 C2 - $6A3CE798 C1 - $78B43359 C0 - $852990CC You can follow the program's flow below. The program reads the raw value 3 times and averages it. Then it converts to resistance. It stores the resistance value into the PAK's temporary register and uses it to perform the polynomial expansion. The program uses the Stamp PAK library which is just a handy way to make the appropriate SHIFTIN and SHIFTOUT calls. Here are a few key calls used:
Of course, if you wanted to actually do something with the temperature, you would probably convert it to an integer (FInt). You might not want to read degrees. For example, suppose you wanted to display the result on a serial LCD. You could multiply the result by 10 in the PAK and then convert it to an integer (using FInt). Then you could display the temperature like this: Serout LCD,baudrate,[dec fpxlow/10, ".", fpxlow//10] Dividing by 10 (integer division) results in the whole number part; the // operator gives the remainder from that division which gives you the part to the right of the decimal point. You could also use the FDigit command (like FDump does) to pick numbers apart. You can read the PAK manual online, and also read technical notes about the PAK in our new document library. Source Code' Thermistor temp meter with EU conversion ' Note: Not all of this is used in this program ' You could remove unused routines to save space
This article is copyright 1999, 2000 by AWC. All Rights Reserved. |
![]()
[Kits] |
[Math] | [PS/2] |
[Pulse In] | [Pulse Out] |
[I/O] | [A/D] |
[PWM] | [Position Sensing] [PIC Programming] | [RS232]
| [PLD/FPGA] | [NetPorter]
[MicroTasks/Consulting] |
[Components] |
[Products] |
[News] | [Search] |
[Documents] | [Projects] |
[Resources] | [Updates] |
[FAQ] |
[Support]
[PDF Catalog] |
[Secure e-mail]
Jump to PAKs: I, II,
III, IV, V,
VI, VII, VIII,
IX, X, XI,
XII PicoPAK:
VIII
Site contents © 1997-2012 by AWC, 310 Ivy Glen, League City, TX 77573 (281) 334-4341