
If you need floating point math with a Javelin, the PAK-I can easily interface to the Javelin. Here is a class that will let you access the PAK-I commands:
package com.al-williams;
import stamp.core.*;
/**
* PAK-I object
* <p>
* This class interfaces with a PAK-I using 2-wire mode
* Connect the PAK-I as shown in the manual.
*
*
* @version 1.0 15 Jan 2002
* @author Al Williams
*/
public class PAK1 {
/** data pin */
protected int datap;
/** clock pin */
protected int clkp;
/** The high order 16-bit word of the 32-bit result */
public int resultHigh;
/** The low order 16-bit word of the 32-bit result */
public int resultLow;
/** Status of last operation */
public int status;
/** Set this option for rounding */
public final static int ROUND=0x40;
/** Set this option for saturation to infinity */
public final static int SAT=0x80;
/** Construct a PAK-I object
@param datapin The pin connected to the PAK-I's SIN/SOUT pins
@param clkpin The pin connected to the PAK-I's CLK pin
*/
public PAK1(int datapin, int clkpin) {
datap=datapin;
clkp=clkpin;
pakreset();
}
public void pakreset() {
CPU.writePin(datap,false); // data
CPU.writePin(clkp,false); // clock
CPU.writePin(clkp,true); // clock
CPU.writePin(datap,true); // data
CPU.writePin(clkp,false); // clock
send(8);
status=rcv();
}
public void loadX(int highPart, int lowPart) {
send(1);
send(highPart>>8);
send(highPart&0xFF);
send(lowPart>>8);
send(lowPart&0xFF);
}
public void loadY (int highPart, int lowPart) {
send(2);
send(highPart>>8);
send(highPart&0xFF);
send(lowPart>>8);
send(lowPart&0xFF);
}
public int readX() {
send(3);
resultHigh=rcv()<<8;
resultHigh+=rcv();
resultLow=rcv()<<8;
resultLow+=rcv();
return resultLow;
}
public void swap() {
send(4);
}
public int intLoadX(int n) {
loadX(0,n);
send(7);
wait();
status=rcv();
return status;
}
public int intLoadY(int n) {
int rc;
swap();
rc=intLoadX(n);
swap();
return rc;
}
public char digit(int n) {
send(5);
send(n);
wait();
return (char) rcv();
}
public int chs() {
send(0xA);
wait();
status=rcv();
return status;
}
public int mult() {
send(0xC);
wait();
status=rcv();
return status;
}
public int div() {
send(0xD);
wait();
status=rcv();
return status;
}
public int sub() {
send(0xE);
wait();
status=rcv();
return status;
}
public void setOpt(int n) {
send(0x10);
send(n);
}
public void abs() {
send(0x11);
}
public void sto(int n) {
int r=0x12;
if (n<0 || n>1) return;
if (n==1) r+=0x80;
send(r);
}
public void rcl(int n) {
int r=0x13;
if (n<0 || n>1) return;
if (n==1) r+=0x80;
send(r);
}
public void setDir(int n) {
send(0x14);
send(n);
}
public int read() {
send(0x15);
return rcv();
}
public void write(int n) {
send(0x16);
send(n);
}
public void XtoY() {
send(0x17);
}
public void YtoX() {
send(0x18);
}
public int square() {
XtoY();
return mult();
}
public void power(int n) {
if (n==0) {
intLoadX(1);
return;
}
if (n==1) return;
XtoY();
while (--n!=0) {
mult();
}
}
public int getXInt() {
send(0xB);
wait();
status=rcv();
return readX();
}
public int add() {
send(0xF);
wait();
status=rcv();
return status;
}
public void debug(int n) {
debug(n,3);
}
public void debug(int n, int debugPrec) {
int i;
System.out.print(digit(0)); // print sign
for (i=n;i>0;i--)
System.out.print(digit(i));
System.out.print(".");
for (i=0x81;i<=0x80+debugPrec;i++)
System.out.print(digit(i));
}
public void sqrt() {
sqrt(0x7503,0x126F);
}
public void sqrt(int sqrtLimitHigh,
int sqrtLimitLow) {
char sgn;
sto(1); // save target number in R1
loadY(makeUint(0x80,00),0); // divide by 2
div();
sto(0); // R0=guess
do {
square();
swap();
rcl(1);
sub();
abs();
loadY(sqrtLimitHigh,sqrtLimitLow); // tollerance .001
sub();
sgn=digit(0);
if (sgn=='+') {
rcl(0);
square();
swap();
rcl(1);
swap();
sub();
readX(); // store temporary
rcl(0);
swap();
loadX(makeUint(0x80,00),0); // load 2
mult();
swap();
// restore temporary
loadX(resultHigh,resultLow);
div();
swap();
rcl(0);
sub();
sto(0); // new guess
}
} while (sgn=='+');
rcl(0);
}
public static int makeUint(int highByte, int lowByte) {
int work;
if (highByte<=0x7F) return highByte<<8+lowByte;
if (highByte==0x80 && lowByte==0) return -32768;
lowByte--;
lowByte^=0xFF;
highByte^=0xFF; // sure to have top bit OFF
return -((highByte<<8)+lowByte);
}
protected void send(int n) {
CPU.shiftOut(datap,clkp,8,CPU.SHIFT_MSB,n<<8);
}
protected void wait() {
while (CPU.readPin(datap));
}
protected void clock() {
CPU.writePin(clkp,true);
CPU.writePin(clkp,false);
}
protected int rcv() {
return CPU.shiftIn(datap,clkp,8,CPU.PRE_CLOCK_MSB);
}
}
Using the class is easy. Here's a simple test program:
import stamp.core.*;
import com.al_williams.PAK1;
public class pak1test {
public static int makeUint(int highByte, int lowByte) {
return (highByte<<8)|lowByte;
}
public static void sqrttable(PAK1 pak1) {
int i;
for (i=1;i<=1000;i++) {
pak1.intLoadX(i);
pak1.debug(4);
System.out.print(" ");
pak1.sqrt();
pak1.debug(2);
System.out.println(" ");
}
}
public static void main() {
int v;
PAK1 pak1=new PAK1(CPU.pin15,CPU.pin14);
sqrttable(pak1);
pak1.intLoadX(22);
pak1.intLoadY(7);
pak1.div(); // pi is almost = 22/7
pak1.debug(1); // prints +3.142 (the 1 argument is digits b4 decimal)
System.out.println("Done");
pak1.setDir(0xFF); // Set spare I/O to output
v=0xAA;
while (true) {
pak1.write(v); // write to I/O port
v^=0xFF; // Flip from AA/55
CPU.delay(100); // wait a bit so we can see LEDs flash
}
}
}
Site contents © 1997-2018 by AWC, Houston TX (281) 334-4341