27220 Parallax Inc, 27220 Datasheet - Page 53

BOOK STAMPWORKS

27220

Manufacturer Part Number
27220
Description
BOOK STAMPWORKS
Manufacturer
Parallax Inc
Datasheet

Specifications of 27220

Accessory Type
Booklet
Product
Microcontroller Accessories
Lead Free Status / RoHS Status
Not applicable / Not applicable
For Use With/related Products
StampWorks
Lead Free Status / RoHS Status
Lead free / RoHS Compliant, Not applicable / Not applicable
Behind the Scenes
Now we’re getting into it – this program, while short, is a bit on the sophisticated
side as it allows us to enter raw readings from the potentiometer and the program
will take care of the rest.
After initializing the outputs (P0 – P7) to drive LEDs, the program reads the 10K
potentiometer with the RCTIME function. Using DEBUG to display the raw value, it
was determined that RCTIME returned a low value of 10 and a high value of 746.
Since grafVal is a byte-sized variable, rawVal must be scaled down to fit into
eight bits.
To scale the raw value to fit into grafVal we’ll want to divide it by 2.73 (695 / 255).
The problem for us is that division in PBASIC is integer-only, so we’d end up with
troublesome rounding errors. Since division is the same as multiplying by a value’s
reciprocal, we can multiply rawVal by 0.366906.
multiply and divide to approximate the fractional value, but this is not possible
because the 16-bit (final) values used in PBASIC may cause high bit truncation.
This is where the */ (star-slash) operator comes in: this operator allows us to
multiply a value by another with a resolution of 1/256. The way this works is that */
does a multiplication of two values, then takes the middle two bytes of the 32-bit
result – the net effect is that we’re multiplying then immediately dividing by 256
(hence the resolution of 1/256). If the fractional value is going to be a constant we
can calculate the*/ parameter in advance by multiplying the fractional value by 256.
In our case this would be:
As it turns out we can very easily calculate the value of Scale by dividing $FFFF
(maximum 16-bit value) by the pot span (difference between high and low readings).
Better yet, we can embed this calculation in a constant definition – this saves us
valuable variable space. At the top of the listing we have:
0. 366906 x 256 = 93.92 (round up to 94)
LoScale
HiScale
Span
Scale
CON
CON
CON
CON
10
695
HiScale - LoScale
$FFFF / Span
In some cases we can do a
' raw low reading
' raw high reading
' between lo-to-hi
' scale factor 0..255

Related parts for 27220