BASIC stamp manual v2.2

4: BASIC Stamp Architecture – NCD, SIN
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 107
result VAR Word
result = 99 ' Put -99 into result
' ...(2's complement format)
DEBUG SDEC ? result ' Display as a signed #
result = -result ' Negate the value
DEBUG SDEC ? result ' Display 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 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 binary radian (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.
ENCODER: NCD
S
INE: SIN
All
2
All
2
All
2