WAM v 3.0, Chapter 3 pg 85
Digital Input – Pushbuttons · Page 85
ME456: Mechatronics Systems Design
Chapters 3 to 5
Binary Fractions
9 Uncomment the PAUSE command by deleting the apostrophe, and try the
solution instead.
• Did multiply-by-two
you understand
this?
For precision, you can use the */ operator to multiply by a value with a fraction. The */
operator is not hard to use; here’s how:
Prof. Clark J. Radcliffe
Mechanical Engineering
Michigan State University
1)
Place the value or variable you want to multiply by a fractional value before the
*/ operator.
2)
Take the fractional value that you want to use and multiply it by 256.
3)
Round off to get rid of anything to the right of the decimal point.
4)
Place that value after the */ operator.
Example: Let’s say you want to multiply the timeCounter variable by 3.69.
1)
Start by placing timeCounter to the left of the */ operator:
timeCounter = timeCounter */
http://www.egr.msu.edu/classes/me456/radcliff
2)
Multiply your fractional value by 256: 3.69 x 256 = 944.64.
3)
Round off: 944.64 § 945.
4)
Place that value to the right of the */ operator:
timeCounter = timeCounter */ 945
' multiply by 3.69
Multiplying by 2 will scale a result of 14,000 to 28,000, which isn’t quite 30,000.
30,000 ÷ 14,000 § 2.14. To multiply by 2.14 with the */ operator for increased precision,
we need to figure out how many 256ths are in 2.14. So, 2.14 ! 256 = 547.84 § 548. You
can use this value and the */ operator to replace timecounter = timeCounter * 2.
9 Replace
timecounter = timeCounter *
timeCounter */ 548 and retest your program.
2 with timecounter
=
Your 30-second test with the original, unmodified program may yield a value that’s
slightly different from 14,000. If so, you can use the same procedure with your test
results to calculate a value for the */ operator to make your results even more precise.
9 Try it!
Fractions without Floating Point
(Chapters 3 and 5)
• There are many times you need to multiply by a
fraction
– How do you compute 1.610*35610 with the Basic Stamp????
– It only multiplies integers not fractions so how can I do that???
• Compute scaling factors
If a sensor measures = 691 and min=1 then how do I
scale those values to 1-500
scale = 500/691 = 0.724
• Answer… Understand number bases for
base 2, base 10, base 16 and base 256 values
What’s the Point?
The little dot tells us where the symbol
associated with Base0 power is …
Octal
Point
Hexadecimal
Point
19.6410 = 1x101+9x100+6x10-1+4x10-2
• Might be new…
11.102 = 1x21+1x20+1x2-1+0x2-2
= 210+110+0.510 = 3.510
3B.616 = 3x161+11x160+6x16-1
= 4810+1110+0.37510 = 59.37510
Multiplying base 2 with
fractions
1 12
Decimal
Point
All these symbols have the same value. Each
symbol in the number is interpreted using
their number base values based on position.
112 is the value 1x21 + 1x20=2+1 = 310
x1.12
1 12
1.12 = 1.48= 1.816= 1.510
Binary
Point
Number Bases – A Review
11.0 2
1 0 0.12
1.12 is the value 1x20 + 1x2-1=1+½ = 1.510
100.12 is the value 1x22 + 1x2-1=4+½ = 4.510
Which is precisely 310 x 1½10 = 4.510
Place Valued Arithmetic works in any base
1
A Simple Binary Multiply
• 1.510 = 1.12 and 810 = 1000.2
• What is the binary result of 1.510 x 810
1000.2
x 1.12
100.02
1000. 2
1100.02 = 810 + 410 = 1210
Multiplication Fact
The BS2 doesn’t track Points
• You must do that by keeping track where
the “point” is
• This would be difficult BUT base valued
numbers have an important property
• An N-symbol x M-symbol product
generates at most N+M symbols
112 x 1.12=100.12 (2+2=4) and
99.910 x 9910= 9890.110 (3+2=5)
The Word Length
Binary Fraction
• The largest digital value possible when multiplying (2) values
with 3 digits has 6 digits (sum of the digits)
999*999 = 998001
• 1.510 with a decimal point is 1x100 + 5x10-1
• The largest binary value possible when multiplying (2) values
with 16 binary digits (bits) has 32 bits (sum of the binary
digits)
– 1111111111111111*111111111111111=
11111111111111100000000000000001
• 1.12 with a binary point is 1x20 + 1x2-1
And 1x20 = 1x100 = 110
1x2-1 = 1x(1/2) = 0.510
•
bit is short for binary digit
Base 2 to Base 256
Written as two bytes with a binary point
as %00000001.10000000
Upper byte = %00000001 = 110 and
Lower byte = %10000000
=> .12 = .24 = .48 = .816 = .(128)256
=> 12810 in the book
I ran out of numeric symbols.
This means simply the 128th symbol
out of 0-255 required for base 256
• Binary values can be expressed with a binary point
• So 1.510 = 1.12
• Written as two bytes with a binary point between
%00000001.10000000
Upper byte = %00000001 = 110 and
Lower byte = %10000000 = 12810
Multiplying two Words
Multiplying Two words of Two bytes each
yields Four Bytes
[Byte].[Byte] x [Byte][Byte]. =
[Byte] [Byte][Byte].[Byte]
You must track the “binary point” yourself
2
Multiply Middle */ Operator
Multiply Middle operator (*/) multiplies
variables and/or constants, returning the
middle 16 bits of the 32-bit result. This has the
effect of multiplying a value by a whole
number and a fraction. The whole number is
the upper byte of the multiplier (0 to 255
whole units) and the fraction is the lower byte
of the multiplier (0 to 255 units of 1/256 each).
A Closer Look
0.101110012 =1x2-1+0x2-2+1x2-3+1x2-4+1x2-5+ …
1x2-1 = 0.510
1x2-3 = 0.12510
1x2-4 = 0.062510
1x2-5 = 0.0312510
1x2-8 = 0.0039062510
0.101110012 = 0.72265625 (approx 0.724)
Computing the Lower Byte
• We need 0.72410
• Trick: just compute 0.724*256 =185.344
18510
= 1x27+0x26+1x25+1x24+1x23+0x22+0x21+1x20
= 1x128+0x64 +1x32 +1x16 + 1x8 + 0x4 + 0x2 + 1x1
= 101110012
• So that 0.72410 => 00000000.(10111001)256
• BSII code is “A = 185” but means A=0.724
Our Scaling
0.724 x 691 = 0.101110012 x 10│10110011.2
(two 16 bit values)
= 1│11110011.010110112 (one 32 bit value)
Note that binary point is to the left of least significant byte…
Taking middle two bytes “truncates” result yielding the 16 bit result
1│1111│0011. 2 = 499.10 ≈ 500. 10 (w/roundoff)
I have used the | symbol to mark nibbles
It is easier than counting bits
Use your Windows Calculator set to Scientific & Binary
You must track the binary point location yourself just like
decimal multiplication with paper and pencil
A BS2 Example
•
•
•
•
'BS2 Floating (binary) point multiply
' {$STAMP BS2}
' {$PBASIC 2.5}
'Code to compute 0.724*691
•
•
•
•
•
•
•
•
•
•
'Symbols
F CON 185 '0.724*256=185.344 (Storage for Fraction)
A
VAR Word 'Storage for Integer
B
VAR Word
A=691
'Output Fraction and integer values as decimal
DEBUG ? F
DEBUG ? A
DEBUG "F (binary) = ", BIN16 F, CR
DEBUG "A (binary) = ", BIN16 A, CR
•
•
•
'Multiply and select Right Hand 2 bytes
B= F*A
DEBUG "RH two bytes of F*A =
•
•
•
'Multiply and select middle two bytes
B=F*/A
DEBUG "Middle two bytes of F*A = ",BIN16 B, " = ", DEC B, CR
•
•
•
'Multiply and select Left-hand two bytes
B=F**A
DEBUG "LH two bytes of F*A = ",BIN16 B, " = ", DEC B, CR
•
END
",BIN16 B, " = ", DEC B, CR
3
© Copyright 2026 Paperzz