PAK-I Data Sheet © 1998, 1999 by AWC AWC 310 Ivy Glen Court League City, TX 77573 (281) 334-4341 http://www.al-williams.com/awce.htm V1.4 Overview The Stamp Pak I is a 32-bit floating-point math coprocessor with a versatile interface that makes it easy to interface it with a variety of microcontrollers including the Basic Stamp from Parallax. However, you can easily interface the Pak I with practically any modern microprocessor. The Pak I can add, subtract, multiply, and divide numbers between +/- 6.80564693 x 1038. The smallest number the Pak I can represent is +/1.17549435 x 10-38. Here are some of the Pak I's major features: • Robust, speed independent serial interface • Uses as few as 2 pins to connect to the host • Can connect multiple devices to the same 2 pins with one additional pin per device • Can work with IEEE754 floating point numbers • Works with Stamp II's ShiftOut and ShiftIn commands • Synchronous operation – read results when you are ready for them • Adds 8-bits of general-purpose digital I/O pins • Fast 10Mhz speed (can be reduced to save power) • Easy to use The Pak I is a standard 18-pin IC. In order to operate it must have a regulated supply of 5V and connection to a clock element. The Stamp Pak I includes a 10MHz ceramic resonator that you can use to clock the chip. Other than the power and clock connections, the Pak I requires at least two wires to connect between your microcontroller (the host) and itself. Depending on the host's capabilities and your application, you may want to use more pins for I/O (up to 5 pins). If You Need Help If you require assistance with your PAK-I, please feel free to contact us. The best way to get support is via e-mail ([email protected]). However, you may also call between 9AM - 4PM Central Time at (281) 334-4341. You can also fax to (281) 538-2147. Be sure to check out our Web page for updates at www.al-williams.com/awce.htm. Registering Your PAK-I Please take a moment to register your e-mail address with AWC. Simply send an e-mail message to [email protected]. AWC will not make your address available to other companies, but we may periodically send you updated technical notes. You’ll also receive information about new microcontroller products and specials. 1 2 3 4 5 6 7 8 9 SIN SOUT BUSY/MODE ENABLE/BUSY CLK RES2 RESET RES1 Vss B0 Vdd PAK-I B7 B1 B6 B2 B5 B3 B4 18 17 16 15 14 13 12 11 10 Pin Connections Pin Name Type Description 18 Busy/Mode Input/Output 17 Enable/ Busy Input or Open Collector Output 3 CLK Input 1 SIN Input 2 SOUT Open Collector Output 4 RESET Input This pin has two functions. At start up, if the pin is pulled up by a resistor, the PAK1 uses this pin as a busy indicator. The pin will read 0 when the PAK1 is not busy. However, if at startup the pin is pulled to ground, the PAK1 will indicate its busy status on the Enable/ Busy pin. If Busy/Mode is pulled high at start up, the Enable/Busy pin is only an input. Bring it low to disable the PAK1, or high to enable it. However, if Busy/Mode is pulled low at start up, this pin serves as an open-collector I/O pin. If the host pulls the pin low, it disables the PAK1. If the PAK1 is busy, it pulls the pin low. The host pulses the CLK pin to shift data in or out of the PAK1. The PAK1 reads data from this pin. You may short SIN and SOUT and use the same pin for input and output if the host is capable of treating a single pin as an I/O pin. The PAK1 writes data to the host on this pin. Since the pin is open collector, you'll need a resistor to 5V on this pin. You may short SIN and SOUT together to use one pin for I/O Hardware resets the PAK1 when low. 5 VSS Power Ground 14 VDD Power +5V 15 RES1 Clock Connects to resonator 16 RES2 Clock Connects to resonator 613 B0-B7 I/O General purpose I/O Software Commands Code $01 $02 $03 $04 $05 $06 Name LOADX LOADY READX SWAP DIGIT IEEECVT Alternate N/A N/A N/A N/A N/A N/A Argument FP FP None None DIGIT None Returns Nothing Nothing FP Nothing ASCIIDIGIT Nothing $07 FLOAT $87 None STATUS $08 $09 COMMCK FPCVT N/A N/A None None $22 Nothing $0A $0B CHS INT N/A $8B None None Nothing STATUS $0C $0D $0E $0F $10 $11 $12 $13 $14 $15 MUL DIV SUB ADD OPT ABS STO0 RCL0 DIR RIO $8C $8D $8E $8F N/A N/A N/A N/A N/A N/A None None None None OPTS None None None DIRS None STATUS STATUS STATUS STATUS Nothing Nothing Nothing Nothing Nothing IBYTE $16 $17 $18 $92 $93 WIO XTOY YTOX STO1 RCL1 N/A N/A N/A N/A N/A OBYTE None None None None Nothing Nothing Nothing Nothing Nothing Description Load X Load Y Read X Swap X and Y Find ASCII digit from X Convert X to IEEE format 24-bit integer in X to floating point Communications check Convert X from IEEE format X=-X Convert X to 24-bit integer X=X*Y X=X/Y X=X-Y X=X+Y Sets option bits X=|X| R0=X X=R0 Sets direction register Reads input byte from port Writes byte to output port X=Y Y=X R1=X X=R1 Notes and Key: • • • • • • FP – 32 bit floating point number (see text for format) DIGIT – A digit number. Zero returns +, -, or space if the number is positive, negative, or zero. See reference for more details. ASCIIDIGIT – The ASCII representation of the requested digit STATUS – Floating point error flags OPTS – Options ( $80 turns on saturation; $40 enables rounding) R0, R1 – Temporary storage registers • • • DIRS – Byte indicating port directions (0=input, 1=output) IBYTE, OBYTE – Input or output byte Using the alternate form of a command prevents it from returning the status result. Floating Point Numbers and Errors The 32-bit floating point representation used by the Pak I is a modified version of the IEEE754 format. The first byte is the exponent (biased by 127). If the exponent is zero, the number is zero. The second byte contains the most significant bits of the mantissa. The most-significant bit is the sign bit (1 is negative) and the mantissa starts with an implied 1 before the decimal point. The next two bytes are the remainder of the mantissa. This differs from the IEEE745 format (the one used by PC C compilers, for example) only slightly. In the IEEE745 format, the sign is in the most-significant bit of the exponent. The mostsignificant bit of the first byte is actually the least significant bit of the exponent. Otherwise, the two formats are identical. The Pak I has functions to convert back and forth between the two formats. The accompanying disk contains a Windows program (fconvert.exe) that will convert between a normal floating point number, the Pak I's format, and the IEEE745 format. Simply enter the number you know and the program will display the other two formats. The status return bits contain the following information: Bit 0 – Integer overflow Bit 1 – Floating point overflow Bit 2 – Floating point underflow Bit 3 – Divide by zero Bits 4-7 – Reserved; always 0. A return of zero indicates no error. Resetting There are several ways you can reset the Pak I. It is a good idea to reset the unit before using it, or any time that you want to make sure it is in a known state. This is especially true when using the Stamp. Each time the Stamp resets or wakes up from sleep, the I/O pins briefly become inputs. This can fool the Pak I into starting a data transfer. Always reset the Pak I first. The best way to reset a single Pak I is to send a special reset sequence over the clock and data lines. This has the advantage that it doesn't require any extra I/O from the host. To send a reset sequence, bring the data pin to 0 and raise the clock to a 1. While the clock remains in the 1 state, bring the data pin high. This will indicate to the Pak I that you wish to reset. When the clock returns to a zero state, the Pak I will reset. The reset doesn't change any register values or port pins, but it does reset communications to a known state. You can get the same result by bringing the enable pin low and then returning it to high to enable the Pak I. This is useful if you are connecting more than one Pak to the same data and clock lines. You'll need to use the enable pin then anyway, and it makes sure that the selected Pak is always in a known state. Using the data sequence method may be unreliable when using multiple Paks together. Finally, you can force a hardware reset by bringing the reset pin low. Holding the clock pin high for more than 2400mS will also force a reset. This might be useful if your circuit generates a hardware reset signal based on a brown-out detector or other master reset circuit. Normally, you'll just connect the reset pin to the +5V supply and allow the Pak I to reset itself on power up. If you do want to drive this pin, make sure that it is at 5V for normal operation. You can use a reset switch or other device if you pull up the reset pin with a 10K-22K resistor. Communications There are several schemes you can use to communicate with the Pak I. All of them revolve around a synchronous protocol involving a clock pin and 1 or 2 data pins. Data is shifted most significant bit first, and samples at the rising edge of the clock. All signals are positive logic (that is, a 1 is a high logic level). The Pak I exposes a separate input (SIN) and output (SOUT) pins for hosts that can't easily handle bi-directional I/O lines. However, for hosts like the Stamp or PIC, it is a simple matter to tie these lines together since SOUT is open collector. Therefore, the minimum number of lines you need are two. An output for the clock and an I/O line to connect to SIN and SOUT. You'll connect the Enable/Busy pin to +5V and use a pull up resistor to 5V on the Busy/Mode pin. This allows you to reset the device and communicate with it. The only problem is, you will have to make sure not to ask the device for data while it is processing. Most operations are quite fast, but some operations (calculating digits and division, for example) can take as much as 400uS with the provided components. For some applications, this isn't a problem. For example, a standard Basic Stamp II can't operate fast enough for this to be a problem except for division and digit calculations. However, you may want to know when the device is ready to process. There are two ways you can accomplish this. If you need to disable and enable the device, you'll want to drive the Enable/Busy pin. Using this pin allows you to share the SIN, SOUT, and CLK lines with other devices that use a similar protocol. So to connect 5 Paks you'd only need 7 I/O lines (5 enables, 1 SIN/SOUT, and 1 CLK line). You drive the Enable/Busy pin low to disable the device. If you don't plan to use this capability, just pull the pin high with a pull up resistor. If you are using the Busy/Mode input (see below) you can still use a pull up resistor, or just tie the Enable/Busy pin directly to +5V. If you ground the Busy/Mode input at start up, the device will also use the Enable/Busy pin to tell you when it is unavailable. It does this by driving the line low when it is not ready. That means the host must drive the pin with an open collector output if it wants to disable the device. If you don't need to disable the device, you can just connect the pin to a pull up resistor and connect the Enable/ Busy pin to an input on the host. If you want to use a normal output to drive the Enable/Busy pin, but you still want a busy indication, you can connect the Busy/Mode pin to 5V through a pull up resistor. Then the Pak I will use the Busy/Mode pin to signify it is busy (the pin is high when busy). In this case, the Enable/Busy pin is always an input and you can drive it normally. Many commands have an alternate form. These commands are the ones that return the floating point error flags on completion. Using the alternate form suppresses the status return value. One thing to note about the status returns. All of the commands that take any significant amount of time will return zeros in the top bit of the response. You can use this as a form of busy indication if you are unwilling to use any other pins. Simply read the data pin until it reads zero. This indicates that the device is sending the response. The READX and RIO commands do not follow this convention, but they take very little time to execute anyway (less than 8uS). To summarize, here are the ways you can synchronize with the Pak I: 1. Delay long enough to insure the operation is complete 2. Monitor the Enable/Busy line while Busy/Mode is grounded. If you bring Enable/Busy low it indicates that the Pak I is disabled. If the Pak I brings the pin low, it means it is engaged in operations. 3. Place a pull up resistor on the Busy/Mode pin. This will cause the Busy/Mode pin to be high when the Pak I is busy. 4. On commands that return status or ASCII digits, you can wait until the data pin goes low. Command Reference ABS Finds the absolute value of X. That is if X is negative, negate it. If X is zero or positive, leave it alone. ADD Adds X and Y leaving the result in X. Y is unchanged. This command may take approximately 110uS in the worst case. CHS Negates the X register. COMMCK This command causes the Pak I to issue a $22 response. You can use this to check communications. The Pak I will always respond with $20 plus the version number. DIGIT The DIGIT command accepts a single byte as an argument and returns a single byte. If the input byte is a zero, DIGIT returns an ASCII +, and ASCII – or a blank character depending on if the X register is positive, negative, or zero. You can use this as a SGN function, but it is mostly useful when writing numbers in readable form. If the input number is less than $7F, DIGIT returns the ASCII digit from the X register that you ask for. For example, if X contains 13.141 and you issue a DIGIT 1 command, the return will be '3'. DIGIT 2 will return '1'. Numbers greater than $80 return ASCII characters to the right of the decimal point. So using the same example, if you issue a DIGIT $81, the return will be '1'. DIGIT $82 will return '4'. This command does not affect the X or Y registers. It may take some time to execute depending on the digit you request, and the number in the X register. This command performs a complex algorithm which involves converting the number to an integer and back to a float in several places. Since integer to float conversion is limited to 24 bits, this places an upper limit on the size of the number you can convert with DIGIT. Note that this does not apply to using it as a replacement for SGN. If you operate on a large number (or a number with many significant digits) and an intermediate result exceeds 24 bits, you may get an incorrect answer, or the PAK may fail to converge. DIR This command allows you to set the directions of the 8 bit I/O port. On power up, all pins are inputs. Setting a bit to 1 in the DIR command causes the corresponding I/O bit to be an output. So issuing a DIR $83 will make B7, B1, and B0 outputs and all other pins inputs. DIV Divides X by Y leaving the result in X. Y is unchanged. This command may take approximately 380uS in the worst case. FLOAT The float command converts the 24-bit integer in the X register's mantissa into a floating point number ready for calculations. IEEECVT, FPCVT These commands convert the X register to IEEE format, or to the Pak I's internal format. If you load an IEEE number, you must convert it before using it in calculations. Once you convert to IEEE, you must convert back before using the number in any calculations. These operations are very fast, so it isn't a problem to convert several times. INT Converts the X register to a 24-bit integer. To read the integer, issue a READX. In order to continue calculations with the same number, you'll need to issue a FLOAT command, or reload the X register. It is often quicker to use a STO command to save the X register, perform the INT command, read the result, and then RCL the original contents. LOADX, LOADY These commands take the next four bytes you send and places them in the X or Y register. The format is exponent first, followed by the mantissa (most significant bit first). You can also load integers or IEEE format numbers for conversion using this command. MUL Multiplies X and Y leaving the result in X. Y is unchanged. This command may take approximately 240uS in the worst case. OPT Sets options. Send the option byte after this command. Using $80 turns on saturation (which represents over and underflows as minimum and maximum values). Using $40 enables rounding. If this option is not on, results are truncated which is faster, but less accurate. On a power up reset, both flags are off. RCL0, RCL1 Recalls register 0 or 1 to X. The register is unchanged. READX This command sends the four bytes that comprise the X register to the host. The host must clock in 4 bytes. The first byte will be the exponent, and the following three bytes the mantissa (MSB first). Note that if you execute an IEEECVT command the result will be in IEEE format. If you execute an INT command, the exponent will be meaningless, and the mantissa will be the 24 bit binary integer result. RIO This command reads 8 bits from the I/O port. Of course, bits set to outputs will read whatever you are writing to them. The Pak sends the byte immediately. STO0, STO1 Stores X in register 0 or 1. SUB Computes X-Y leaving the result in X. Y is unchanged. This command may take approximately 110uS in the worst case. SWAP Swaps X and Y WIO This command writes 8 bits to the I/O port. This only affects pins that are set as outputs. Send the byte immediately after the command. XTOY Stores X in Y without changing X. YTOX Stores Y in X without changing Y. Example 1. Connections to Stamp or similar host Typical Circuits and Software Connecting the Pak-I to its external components is simple. Supply 5V to Vdd and ground the Vss pin. Connect the two outer pins of the ceramic resonator to the RES1 and RES2 pins (the order does not matter). Ground the center pin of the resonator. Connect the RESET pin to 5V. For the simplest mode of operation connect the ENABLE/ BUSY pin to 5V and the BUSY/MODE pin to 5V through a pull up resistor (10K-22K). Of course, you'll also need to make connections to the host microprocessor. You may also want to make different connections to ENABLE/BUSY and BUSY/MODE if you want to use different methods of synchronizing. If you are using the Basic Stamp II or Basic Stamp IISX, you'll have no problems using the ShiftIn and ShiftOut commands. Many Basic Stampcompatible compilers have these commands too. If you don't have access to these commands, don't worry. Here is some simple code (written in simple PBasic) that shows the actions you have to take: ' B1 is the byte to shift shiftoutput: output datapin for b7=0 to 7 ' Set data pin to 0 or 1 low datapin if (b1 & $80) <> $80 then so0 high datapin so0: b1=b1*2 ' shift byte left high clkpin low clkpin ' could use pulsout next return ' B1 is byte to input byte shiftinput: input datapin b1=0 for b7=0 to 7 b1=b1*2 b1=b1 | in15 high clkpin low clkpin ' could use pulsout next return You'll find a library of Basic Stamp II routines that allow you to access the PakI on the companion disk. Here is a simple program that counts from 1 to 1000 while blinking an LED connected to the PakI's B4 pin: i var word gosub FReset fpx=$10 gosub IODir ' reset ' set B4 to output gosub FZeroX ' x=0 gosub FSwap ' x<>y fpx=1 gosub FLoadInt ' x=1.0 gosub FSwap ' x<>y so x=0.0, y=1.0 Debug "Start...", CR for i=1 to 1000 fpx=i//2*$10 gosub IOWrite ' Blink light gosub FAdd ' x=x+y fpx=4 gosub FDump ' print it out next Debug "End",cr Here is the same program, but instead of loading 1 as an integer, it loads .5: i var word gosub FReset fpx=$10 gosub IODir gosub FZeroX gosub fSwap fpxhigh=$7e00 fpxlow=0 ' reset ' x=0 ' x<>y ' x=.5 gosub FLoadX fpx=3 gosub FDump gosub FSwap ' x<>y so x=0.0, y=0.5 Debug "Start...",CR for i=1 to 1000 fpx=i//2*$10 gosub IOWrite gosub FAdd ' x=x+y fpx=4 gosub FDump next Debug "End",cr Frequently Asked Questions Q: How can I load a particular number to the X or Y registers? A: There are two methods. First, you can use the fpconvert program to determine the 32-bit number that corresponds to the number you want. Then pass it to the FLoadX (or FLoadY) subroutine. You can also use the LOADX or LOADY command directly. The other way you can load a number is via the FLOAT or FLoadInt commands. Q: Can I run the Pak I from a different clock source? A: Yes. Any crystal or ceramic resonator up to 10Mhz may work. Remember, the ceramic resonator has capacitors that you will have to supply if you use a crystal or a resonator without capacitors. You'll use two capacitors (one on each RES pin). You'll have to experimentally determine the values. Start with about 15pf and work up to as much as 100pf for very low frequency crystals. You can also feed an existing TTL-level clock signal into pin 16. Q: Will running the Pak I at a lower speed reduce its power consumption? Will it run faster. A: Yes running at slower speeds will save power. You will probably not have success overclocking the Pak I. However, contact AWC for information on faster devices and other customizations. Q: Why are my results backwards? A: Make sure you are using MSBPRE with the ShiftIn command and not MSBFIRST. Use MSBFIRST with the ShiftOut command. Q: Is there a second source for the PakI? A: No. However, if you have a high-volume application and you are concerned about availability, contact AWC about obtaining a license to produce your own Pak I's or obtain them from third parties. Q: How can I compare two numbers? A: To compare X and Y, store X away (using one of the STO commands). Subtract Y from X and then call DIGIT 0 to determine if the result is positive, negative, or zero (this operation is very fast). Then restore X. Of course, if you don't mind destroying X, you can skip the STO and RCL. Q: Can the PakI handle square roots? A: Not directly. The Pak II, however, will. You can use the FSqrt function to calculate a square root indirectly, however. Q: Can I use more than one Pak at one time? A: Yes. You can drive multiple Paks on the same data and clock lines as long as you only enable one at a time. Of course, you could keep them always enabled if you use separate data and clock lines. You can use a resonator for each, or you can use a single resonator and feed pin 15 of the Pak with the resonator to pin 16 of the other Paks (this will only work for a small number of Paks). Basic Stamp II Library On the enclosed disk you'll find FUTIL.BS2 which contains a simple library for the PAK-I. The calls in that file appear on the next page. Call FReset FAdd, FSub, FMult, FDiv FSqrt Function Reset the coprocessor Basic operations Notes Always call first Operate on X and Y Square root approximation Newton approximation; destroys all registers FSquare FChs FAbs FSto0, FSto1, FRcl0, FRcl1 FLoadX, FLoadY Square X Change sign Absolute value Store and recall scratchpad FZeroX, FZeroY FPI Fe FLoadInt FReadX Load 0 to X or Y Load X with pi Load X with e Load integer to X Read X FInt Read X as integer FSwap FGetDigit Swap X and Y Decode an ASCII digit or compute signum FDump Dump number on debug terminal FOption FXtoY FYtoX IODir Set options Y=X X=Y Set I/O direction register IOWrite IORead Write to I/O port Read from I/O port Load values to X and Y Place hex numbers in fpxhigh and fpxlow Place integer in fpx Places result in fpxhigh and fpxlow Leaves result in fpxhigh and fpxlow Place digit number if fpx, returns in fpdigit Set fpx to number of digits before decimal point Options in fpx Directions bits in fpx Data out in fpx Returns byte in fpx Specifications Absolute Maximum Ratings Parameter Minimum Typical Maximum Supply voltage Vdd rise time on power up Supply current 4.5V 5V 5.5V .05V/ms - - - 5mA 10mA DC Characteristics (at 10MHz) Ambient temperature under bias Storage temperature Voltage on VDD with respect to VSS Total power dissipation (1) Maximum current out of VSS pin Maximum current into VDD pin Maximum output current sunk by any I/O pin Maximum output current sourced by any I/ O pin Maximum current sunk by B0-B7 Maximum current sourced by B0-B7 -55°C to +125° C -65°C to +150° C -0.3 to +7.5V 800 mW 150 mA 100 mA 25 mA 20 mA 150 mA 100 mA Notes When configured as inputs, all of the generalpurpose I/O pins have weak pull up resistors (about 20K) internally configured.