Hello,
I'm new to PICAXE after spending time with the BASIC Stamp and then Arduino hardware. I was looking for a micro controller that was smaller and used less power than the Arduino boards so I though I would take a look at PICAXE. I wanted to use floating point, fp, math in a power meter project but quickly found that PICAXE BASIC doesn't do fp. I took the challenge and developed a system for doing fp math on a 08M2 chip. If you need to calculate with numbers from -32768e-128 to 32767e127 and don't need speed or a lot of program memory, then this system might be useful to someone.
The system does fp addition, subtraction, multiplication, division, square root, and factorial. More functions could be added but I had already used more than half of the chip's program memory and general purpose variables. I suspect that chip resource usage could be improved, but it's good enough for now.
Floating point numbers are expressed in scientific notation as m x 10^p. The power of 10, p, is stored in a byte variable, b, associated with an integer word, w, representing m. Therefore, a fp variable uses 3 bytes. The overflow and underflow of b around 0 is used to denote positive and negative powers. If p is positive then b = 0+p, but if b is negative then b = 0 - p = 256 - p. P is restricted to the range of -128 <= p < 128. If p >=0 then 0 <= b <= 127. If p<0 then 128 <= b <= 255. This means that if bit 7 of b is 1 then b is negative, if 0 then b is positive. Negative values of m are handled the same way, except since w is a word, bit 15 of w is set to 1 to denote a negative number and b is adjusted to restrict positive values of m from 0 to 2^15 -1 = 32767. If m >=0 then 0 <= w <= 32767. If m < 0 then 32768 <= w <= 65535 This means that w has only 4 or 5 significant digits, but this is sufficient for most engineering calculations.
The attached code listing describes how to use the system with numerous comments. The code includes a demonstration of the system to calculate the unknown leg of a large right triangle given the hypotenuse and the other leg.
I'm new to PICAXE after spending time with the BASIC Stamp and then Arduino hardware. I was looking for a micro controller that was smaller and used less power than the Arduino boards so I though I would take a look at PICAXE. I wanted to use floating point, fp, math in a power meter project but quickly found that PICAXE BASIC doesn't do fp. I took the challenge and developed a system for doing fp math on a 08M2 chip. If you need to calculate with numbers from -32768e-128 to 32767e127 and don't need speed or a lot of program memory, then this system might be useful to someone.
The system does fp addition, subtraction, multiplication, division, square root, and factorial. More functions could be added but I had already used more than half of the chip's program memory and general purpose variables. I suspect that chip resource usage could be improved, but it's good enough for now.
Floating point numbers are expressed in scientific notation as m x 10^p. The power of 10, p, is stored in a byte variable, b, associated with an integer word, w, representing m. Therefore, a fp variable uses 3 bytes. The overflow and underflow of b around 0 is used to denote positive and negative powers. If p is positive then b = 0+p, but if b is negative then b = 0 - p = 256 - p. P is restricted to the range of -128 <= p < 128. If p >=0 then 0 <= b <= 127. If p<0 then 128 <= b <= 255. This means that if bit 7 of b is 1 then b is negative, if 0 then b is positive. Negative values of m are handled the same way, except since w is a word, bit 15 of w is set to 1 to denote a negative number and b is adjusted to restrict positive values of m from 0 to 2^15 -1 = 32767. If m >=0 then 0 <= w <= 32767. If m < 0 then 32768 <= w <= 65535 This means that w has only 4 or 5 significant digits, but this is sufficient for most engineering calculations.
The attached code listing describes how to use the system with numerous comments. The code includes a demonstration of the system to calculate the unknown leg of a large right triangle given the hypotenuse and the other leg.
Attachments
-
15.6 KB Views: 161