4: BASIC Stamp Architecture – NCD, SIN
BASIC Stamp Programming Manual 2.0c
•
www.parallaxinc.com
•
Page 65
-- or --
Result VAR WORD
Result = 99 ' Put -99 (2's complement format) into Result.
DEBUG SDEC ? Result ' Display it on the screen as a signed #.
Result = - Result ' Negate the value
DEBUG SDEC ? Result ' Display it on the screen as a signed #.
The Encoder operator (NCD) is a "priority" encoder of a 16-bit value. NCD
takes a 16-bit value, finds the highest bit containing a 1 and returns the bit
position plus one (1 through 16). If no bit is set (the input value is 0) NCD
returns 0. NCD is a fast way to get an answer to the question “what is the
largest power of two that this value is greater than or equal to?” The
answer NCD returns will be that power, plus one. Example:
Result VAR WORD
Result = %1101 ' Highest bit set is bit 3.
DEBUG ? NCD Result ' Show the NCD of Result (4).
The Sine operator (SIN) returns the two’s complement, 16-bit sine of an
angle specified as an 8-bit (0 to 255) angle. To understand the SIN operator
more completely, let’s look at a typical sine function. By definition: given a
circle with a radius of 1 unit (known as a unit circle), the sine is the y-
coordinate distance from the center of the circle to its edge at a given
angle. Angles are measured relative to the 3-o'clock position on the circle,
increasing as you go around the circle counterclockwise.
At the origin point (0 degrees) the sine is 0, because that point has the
same y (vertical) coordinate as the circle center. At 45 degrees the sine is
0.707. At 90 degrees, sine is 1. At 180 degrees, sine is 0 again. At 270
degrees, sine is -1.
The BASIC Stamp SIN operator breaks the circle into 0 to 255 units instead
of 0 to 359 degrees. Some textbooks call this unit a binary radian or brad.
Each brad is equivalent to 1.406 degrees. And instead of a unit circle,
which results in fractional sine values between 0 and 1, BASIC Stamp SIN
is based on a 127-unit circle. Results are given in two’s complement form
in order to accommodate negative values. So, at the origin, SIN is 0. At 45
degrees (32 brads), sine is 90. At 90 degrees (64 brads), sine is 127. At 180
degrees (128 brads), sine is 0. At 270 degrees (192 brads), sine is -127.
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
E
NCODER
: NCD
S
INE
: SIN
Summary of Contents for BASIC Stamp 2e
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...