Limited memory & float data type
Archiver
Posts: 46,084
Hi,
My name is Bronson Alex.
I am new in BasicStamps (3 days).
I am doing a project. I found out that BS2 has very limited memory
size for variable (208 bits). Is there any optional memory expansion
for my BS2? Or maybe I should get other BS2 version?
Can BS2 deal with float-type of variable (eg. 23.56 or 1.6)?
Also, how can I convert array of byte (ASCII) to 'calculatable'
decimal value?
My name is Bronson Alex.
I am new in BasicStamps (3 days).
I am doing a project. I found out that BS2 has very limited memory
size for variable (208 bits). Is there any optional memory expansion
for my BS2? Or maybe I should get other BS2 version?
Can BS2 deal with float-type of variable (eg. 23.56 or 1.6)?
Also, how can I convert array of byte (ASCII) to 'calculatable'
decimal value?
Comments
wrote:
> I found out that BS2 has very limited memory size for variable
> (208 bits). Is there any optional memory expansion for my BS2?
> Or maybe I should get other BS2 version?
Some of the BS2 models have a little bit more RAM; still not much
though. You can add an external RAM chip using latches, shift
registers and such but it it kind of involved for someone just
starting.
> Can BS2 deal with float-type of variable (eg. 23.56 or 1.6)?
You may be able to do without floating point by using what is called
fixed point arithmetic. Basically, you use integers but you think of
them as having an implied decimal point. For example, 23.56 might be
represented as 2356. Adding and subtracting is straightforward -
just add or subtract as usual. When multiplying two fixe point
numbers, you have to divide out one of the scale factors. Since both
numbers have the scale factor, after multiplication you have the
scale factor squared so you divide one back out again. Division is
the opposite problem - you lose the scale factor so you have to
multiply it back in again again.
If this seems too complicated, you might consider a different
microcontroller, for example, the BX-24. It has floating point built
in, has lots more RAM, and has more program space.
Don't get me wrong, the BS2 is a fine chip. It just has well known
limitations that make it unsuitable for some applications. Lots of
people are using it to good effect.
> Also, how can I convert array of byte (ASCII) to 'calculatable'
> decimal value?
It's not clear exactly what you mean. If you mean that you have a
series of ASCII characters that are digits and you want to compute
the numeric value, the process is fairly simple. First initialize a
variable to zero, call this the "accumulator". Now fetch the most
significant digit character and compute its digit value (hint:
subtract 48 from the ASCII code). Now multiply the accumulator by 10
and add in the digit's value. Repeat this process using the next
most significant digit, etc. When you've processed the last digit,
the accumulator will hold the numeric value represented by the series
of digit characters.
The process is slightly more complicated if you need to handle real
number values (as opposed to integer values) but the process is
similar.
reply :-)
> I found out that BS2 has very limited memory size for variable (208
> bits). Is there any optional memory expansion for my BS2? Or maybe I
> should get other BS2 version?
The BS2/P has additional scratch pad memory which can be very helpful. Also,
some add on chips bring in additional storage of various kinds. Remember,
that for infrequently changing items, you can store data in program memory.
However, writing is not as fast and has a limited life cycle (Jon? 1 million
write cycles?).
> Can BS2 deal with float-type of variable (eg. 23.56 or 1.6)?
There are many ways you can often avoid floating point. If you can't,
consider the PAK series coprocessors. The PAK-I and II add floating point
math using an efficient protocol and have Stamp libraries. The PAK-IX is
like a PAK-II but adds several analog input channels. The PAK-XII is very
easy to use because it is like an RPN calculator. You simply use serial
commands to send things like:
4.1 3.995 + =
This also partially answers you next question. The above are just pure ASCII
strings (4.1, 3.995, etc.). More in a second. You can find the PAKs at
http://www.awce.com/pak12.htm (use the menu to find the others).
> Also, how can I convert array of byte (ASCII) to 'calculatable'
> decimal value?
If you are using integers from the serial port, the SERIN command has ways
to do this (the DEC specifier). Otherwise, the other poster's algorithm will
work. In assembly, multiplying by 10 is not as fast as doing y=(x<<3)+(x<<1)
-- I'm not sure if that's faster on the Stamp, but I'd bet it is (Tracy?).
If you are dealing with floats, the PAK-XII, as mentioned above, solves this
neatly.
Regards,
Al Williams
AWC
* Kits!
http://www.awce.com/kits.htm
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
Yahoo! Groups Links
>to do this (the DEC specifier). Otherwise, the other poster's algorithm will
>work. In assembly, multiplying by 10 is not as fast as doing y=(x<<3)+(x<<1)
>-- I'm not sure if that's faster on the Stamp, but I'd bet it is (Tracy?).
>If you are dealing with floats, the PAK-XII, as mentioned above, solves this
>neatly.
The speed of operations on the Stamp comes down mainly to how many
bits have to be transferred from the eeprom. So,
y = x * 10 ' about 320 microseconds
will be slower than
y=(x<<3)+(x<<1) ' about 700 microseconds
Allow about 190 microseconds for the "=", and an additional 130
microseconds for each math operator. It doesn't matter too much what
math operator it is, because the time to do the actual math is so
much less than the time to fetch the arguments and operators from the
eeprom. In the second equation, I think it counts for a little more
than 3 operators, because the operand "x" has to be fetched twice and
the intermediate result put on the operation stack. Division does
take noticeably longer (~180 to 200 microseconds), because division
is fact a long iterative algorithm.
As you pointed out, Al, the second method of multiplying by the
constant 10 is much faster than the general method *, because
shifting by 2 is lightning fast in machine language.
-- Tracy