HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite, "How To Use It" main page

Pascalite hardware: Temperature Monitoring


Temperature Monitoring



The following assumes you've got your Pascalite set up and working. If it gives you what you want, great. Be advised, however, that this page is subordinate to others. Visit my Pascalite, "How To Use It" main page if you came drectly here and there are things "missing".




(You can show numbers on the LCD if you have one, but the following programs will also send at least part of every number to the four LEDs for those of you without an LCD display.)

Control Plus sell a nice little temperature sensor for about $10. Like the LCD module, and RTC, it uses the I2C bus, so you can monitor temperature without giving up other inputs. You can only attach one sensor easily, but if you're up to some very fiddly soldering to a SMT device, you could have 8 temperature sensors. The I2C bus only involves 4 wires, but there are limits to how long they can be. How long you can get away with, I don't know. The sensor reads temperatures to a quarter of a Celcius degree, over a wide range. (-55 -> +125 deg C).

The first program below will read the temperature from the device and display it on the LCD... giving only the integer part of the temperature... truncated, not rounded, so a temperature of 22.6 would appear as 22. It will also only work for temperatures above zero. (Fixing those limitations is the topic of the next program.) Even if you have no LCD, you should be able to see the program respond to changes in the sensor's temperature via the LEDs on d0, d1, d2, d3.

program IntTutTTure;
{For positive temperatures, without using the fractional degree information}
var bHi,bLo,bTmpG:byte;

procedure NumToLCD(bDisp:byte);
(as fro IntTut4)
end; {of procedure}

begin
def_out(d0,d1,d2,d3);
write(LCD,255); {clears LCD}
repeat
write(i2c,start);
write(i2c,159);
delay(80);         {minimal delay: 80 ms}
read(i2c,bHi,bLo);
write(i2c,stop);
NumToLCD(bHi);
bTmpG:=bHi Mod 10;{This line for people without LCD displays}
bTmpG:=bTmpG+48;{This line for people without LCD displays}
write(portd,bTmpG);{This line for people without LCD displays}
delay(100);
until 4=5;
end.


Before we go forward and make better use of the chip, we're going to re-write the above using a procedure to "wrap up" the code which fetches a temperature reading from the chip. (Features discussed in a moment...

program IntTutTTure2;
{For positive temperatures, without using the fractional degree information}
var bHi,bLo,bTmpG:byte;

procedure NumToLCD(bDisp:byte);
(as fro IntTut4)
end; {of procedure}

procedure GetTture(var bGTHi,bGTlo:byte);
begin
write(i2c,start);
write(i2c,159);
delay(80);         {minimal delay: 80 ms}
read(i2c,bGTHi,bGTLo);
write(i2c,stop);
end;

begin
def_out(d0,d1,d2,d3);
write(LCD,255); {clears LCD}
repeat
GetTTure(bHi,bLo);
NumToLCD(bHi);
bTmpG:=bHi Mod 10;{This line for people without LCD displays}
bTmpG:=bTmpG+48;{This line for people without LCD displays}
write(portd,bTmpG);{This line for people without LCD displays}
delay(100);
until 4=5;
end.
In the last ("main") begin...end block you will see a "new word for the language", GetTture, which we create by the "procedure GetTture(var bGTHi..." stuff. Note those three little letters "var" in the procdure header. The effect of them is to send back whatever is in bGTHi and bGTLo when the procedure finished executing. If we called it with GetTture(foo,bar), then what was in bGTHi will appear in foo, and what was in bGTlo will be in bar.

I should thank Ad Verheijen at Control Plus for a sample program which reminded me of how nicely things can be done if you use var. Thank you, Ad! Moving on....

=====
The device reads temperatures to 0.25 degree Celcius. If the temperature is above zero, then the two most significant bits of bLo tells you about the quarter degrees. We'll talk about negative temperatures in a moment.

Thus, as long as temperatures remain positive, you could have....

gotoxy(3,0){assuming degrees displayed in the first three character positions of the first line}
if (bLo and 192)=192 then then write(LCD,".75 degrees C");
if (bLo and 192)=128 then then write(LCD,".50 degrees C");
if (bLo and 192)= 64 then then write(LCD,".25 degrees C");
if bLo<64 then write(LCD,".00 degrees C");
Now.. to deal with negative temperatures, we need to look more closely at the messages sent to us by the temperature sensor.

First of all, I'm going to write things in binary for a while, so if I want to show five I'll write 0000 0101. The leading zeros are nothing special, even if we don't often use them in everyday numbers. The space is just to help you keep track of where you are in the number. (It's like the commas in "one million, two hundred thousand...." when writeen "1,242,152")

I'm going to show all of the bits (1s or 0s) of bHi, but only the first (most significant) bit of bLo, and the one after that.

If the temperature is +5 degrees, bHi + the first two bits of bLow will be: 0000 0101 00

More examples:
Temperature / bHi + the first bit of bLow
+5.5 0000 0101 10
0 0000 0000 00
-0.5 1111 1111 11
-25 1110 0111 00
-55 1100 1001 00
+25 0001 1001 00

They're just using twos complement, with the LSB worth 0.25. You knew that! (And if you didn't....)

program IntTutTTur3;
{Version: 17 Mar 03}
{By: TK Boyd, http://sheepdogsoftware.co.uk
Displays negative and positive ttures, and quarter degrees
Analog Devices sensor number 7416 returns the temperature
as a 10 bit twos complement number. The most significant 8 bits are
returned to bHi MSB in bHi(7) in the following, the LSB in bit 6
of bLo, and the remaining bit in bLo(7)}

var bHi,bLo,bTmpG:byte;

procedure NumToLCD(bTmp:byte);
var bTens,bTmp2:byte;
begin
gotoxy(1,0);{It appears that the parameters of GOTOXY cannot be variables.}
write(LCD,"      ");{6 spaces}
gotoxy(1,0);
if bTmp>199 then begin
  write(LCD,"2");
  bTmp:=bTmp-200;
  end {was>199} else
    if bTmp>99 then begin
      write(LCD,"1");
      bTmp:=bTmp-100;
      end {was >99} else write(LCD," ");
{100's digit now done}
if bTmp>9 then begin
         bTens:=bTmp DIV 10;
  bTmp2:=bTens*10;
  bTmp:=bTmp-bTmp2;
  bTens:=bTens+48;
  write(LCD,bTens);
  end else write(LCD," ");
{10's digit now done}
bTmp:=bTmp+48;
write(LCD,bTmp);{And finally: Write units digit}
end; {of procedure}

procedure GetTture(var bGTHi,bGTlo:byte);
begin
write(i2c,start);
write(i2c,159);
delay(80);         {minimal delay: 80 ms}
read(i2c,bGTHi,bGTLo);
write(i2c,stop);
end;

begin
write(LCD,255); {clears LCD}
settime(0,0,0,0);
repeat
GetTture(bHi,bLo);
{Here begins code to interpret bHi, bLo as
 10 bit 2s complement...
 If the number is positive, the contents of bHi
 and bLo arrive at the "write" statements unchanged.
 If the number is negative, the contents of bHi and
 bLo are concerted to the positive equivalent. E.g.,
 if the temperature is -12.25, then by the time the
 "write" statement is reached, bHi holds 12, and bLo
 holds just what was in the 2 MS bits of bLo. (e.g.
 10xx xxxx will have become 0000 0010}
gotoxy(0,0);
write(LCD,"+");{provisionally}
bLo:=bLo DIV 64; {shift top two bits to right-hand end}
if bHi>127 then begin {number is negative}
  gotoxy(0,0);
  write(LCD,"-");
  bHi:=bHi XOR 255; {complement}
  bTmpG:=bLo;
  {following crude... but works! (I hope!)}
  bLo:=0;{to avoid begin..end with first if below}
  if bTmpG=0 then inc(bHi);{bLo can stay zero}
  if bTmpG=1 then bLo:=3;{no need to inc(bHi)}
  if bTmpG=2 then bLo:=2;{no need to inc(bHi)}
  if bTmpG=3 then bLo:=1;{no need to inc(bHi)}
  end;
NumToLCD(bHi);
if bLo=0 then write(LCD,".00 degrees C");
if bLo=1 then write(LCD,".25 degrees C");
if bLo=2 then write(LCD,".50 degrees C");
if bLo=3 then write(LCD,".75 degrees C");
if bLo>3 then write(LCD,"ERROR IN TTure CODE!");
delay(100);
until 4=5;
end.



To search THIS site.... (Go to the site's above, and use their search buttons if you want to seach them.)... Way to search this site without using forms
Search this site or the web powered by FreeFind

Site search Web search

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to editor's (Arunet) homepage
How to email or write this page's editor, Tom Boyd