Build your own math coprocessor for 3£

mrburnette

Senior Member
Farnell sells the ATmega328P-PU for 2£64. Add a 16MHz xtal and a couple of small load capacitors and you basically have the makings of a program-it-yourself coprocessor at about 3£.

Now on Instructables: Scientific-Calculator/

The idea is to create an instruction set that can be "sent" from the PICAXE over I2C, SPI, or in my example - via serial RS232. The coprocessor will analyze the command, and pull/parse the required data to complete your request. The calculation will be performed and the results sent back over the serial interface. The example is set to 9600 BAUD, but the coprocessor can go handle 115K. So what kinds of commands might we want? For a start, the basic 4 plus a few transcendentals: sin, cos, tan, asn, acs, atn. To simplify our command structure, we will block each command as 3 characters, so the basic 4 become: DIV, MUL, ADD, SUB and we'' code to make the stream non-case sensitive... MUL is also MUl, Mul, mul, and so on.

The reply to the PICAXE must be stored and parsed. There are a number of hints in the forum on doing this, but Hippy's *10 accumulator is likely the most straightforward: 5018l but if you intend on implementing such a scheme remember that the coprocessor is using IEEE 4-byte arithmetic, so you will need to be able to capture and parse the returned string. If you donot want a decimal point, the instruction "DEC 0" is your friend. Fixed decimals, say 2 places, the obvious "DEC 2".

The C'ish code is written in Arduino for the 328P which translates to UNO or any of the variety of products using that architecture... UNO is specified because it is a 28-pin socketted chip. There are a number of ways to get the source into the chip as binary, but my recommendations is to buy a chip and ask a friend with an Arduino to program it for you. Instructions are available on the Internet, but essentially this covers the basics: Arduino-Bare-Bone You really do not need a bootloader since you program can be loaded by your A-friend using the File "Upload Using Programmer" mode. If you plan on making changes to your software coprocessor, you MAY wish to consider something like this: ZIF programmer shield.

The program has 2 modes of operations... Interactive (verbose) and the flip-side of that being non-verbose! For the PICAXE you will want to have the program compiled with #define verbose 0

The examples below are created with verbose mode on:
ADD 5.6 4.66
Enter first number <delimiter> second number: 5.60 4.66
a + b = 10.2600002​

DEC 2 div 1 3
Enter dividend <delimiter> divisor: 1.00 3.00
a / b = 0.33​

dec 7 sin 45
Prompting for X: 45.00 D-->R = 0.79 Sin(X) = 0.7071068​

asn 0.7071068
Prompting for X: ArcSin(X) = (degrees) 45.0000038

This project was undertaken as a fun project, single-percision may not be useful to you in your programs if you are trying to calculate distance and headings from GPS. I'm looking around for a 64-bit C++ library that can be linked with this sketch so that things can get interesting. What may be of interest, is not the math per se, but rather the ability to create your own specialized math functions with the C environment and then spew the results back to PICAXE. The attached program should offer you plenty of ideas.

- Ray

"DIV",
divide​
"MUL",
multiply​
"ADD",
add​
"SUB",
subtract​
"Y^X",
raise Y to X power​
"LOG",
log x​
"NLG",
natural log x​
"10X",
10 raised to X power​
"1/X",
reciprocal​
"e^X",
Euler's number raised to X power​
"SQR",
Square Root​
"X^2",
X squared​
"SIN",
sin of X​
"COS",
cos of X​
"TAN",
tan of X​
"ASN",
arcsin of X​
"ACS",
arccos of X​
"ATN",
arctan of X​
"DEG",
mode = degrees (default)​
"RAD",
mode = radians​
"DEC",
display decimal places​
"RTT",
hypotenuse with x and y legs​
"DPR",
328P digital pin# read (experimental)​
"APR",
328P analog pin# read (not implemented)​
"LTX",
Last value displayed, 0 initially​

View attachment 13523

Rename to dot INO after download. May be viewed in Wordpad.
 
Top