Many people have trouble getting started with the PAK-I. Here are some tips to help you understand how to use this powerful chip:

The thing to keep in mind is that the PAK is designed to give the best possible speed and accuracy. Therefore it uses a 4 byte hex representation of floating point numbers. Nearly all computers (including the PC) do this, but the format varies depending on the machine in question.

This document assumes you are using the Stamp II library included on the disk. However, the principles apply in any case. There are 3 main items to consider:

  1. Loading numbers
  2. Computing
  3. Reading numbers

Loading Numbers

If you want to load an input from an A/D, RCTime, etc. then you have an integer. You can load an integer by calling FLoadInt. Just set FPX and do a gosub to FLoadInt. This will put the correct representation of the integer in the X register.

If you want to load a floating point constant, get the 4-byte value using the provided fconvert program. For example, 3.14159265 (PI) is 80490FDB. You can use FLoadX to put this in the X register or FLoadY for the Y register. The number goes in fpxhigh and fpxlow ($8049 and $0FDB).

You can also load an IEEE-compliant number like a PC floating point number. Simply put it in the X register and call the IEEE conversion function (see the manual).

Finally, suppose you read a floating point number like "3.15" from a serial stream. While you can write code to convert that to hex, it is troublesome on the Stamp. It would be easier to load 15 as integer and 100 as a constant. Then divide. Then load 3 as an integer and add it in.

Computing

As for computation, place everything in the X and Y register and call the right function FAdd, FSub, etc. That's the easy part.

Reading Numbers

Now reading numbers. The examples use gosub fdump, but this is not the most efficient way. Load FPX with the number of digits to the left of the DP you want to see. The routine assume 6 digits to the right.

However, for most purposes, consider this. Suppose you want to display a voltage down to 1/10 of a volt on a serial LCD. You do your calculations and the result is in the X register, say 4.8V. Notice that 10 is hex 82200000 and load that into the Y register and then multiply. This gives you 48 "decivolts" if you will. Now call FInt. This will return a 24-bit integer in fpxhigh and fpxlow. So fpxlow will equal 48. You can then write to the LCD:

Serout pin,baud,[fpxlow/10,".",fpxlow//10]

You could also have saved the X register and called FInt first to get the 4. You could print that and a decimal point. Then you could restore the X register and multiply by 10, 100, 1000, etc. And finally read that using FInt to get 8, 80, or 800 and print that last. Really the same thing.

Simple Example

Here is a small example that would replace the "test" code in FUtility.bs2. It squares the number 4080 showing you the various conversions along the way.

fpx=4080 ' number 
debug dec fpx,"=" 
gosub floadint ' read it as integer 
fpx=8 ' dump it (just a test) 
gosub fdump 
debug cr 
gosub freadx ' get the hex equiv. (again, just a test) 
debug "fpx=", hex4 fpxhigh, hex4 fpxlow, cr 
gosub fsquare ' square the number 
fpx=8 ' dump that 
gosub fdump 
debug cr 
gosub freadx ' and show the hex equiv 
debug "fpx=", hex4 fpxhigh, hex4 fpxlow, cr 
debug cr

Also, have a look at http://geocities.com/SiliconValley/Orchard/6633/altimeter.html -- this page has an altimeter that uses the PAK-II. This chip is pretty much the same as the PAK-I but it is faster and has more functions, but if you can understand this program you'll have no trouble.


Site contents © 1997-2018 by AWC, Houston TX    (281) 334-4341