Encoder (Ncd); Sine (Sin) - Parallax BASIC Stamp 2e Programming Manual

Table of Contents

Advertisement

2
2
e
E
: NCD
NCODER
2
2
e
S
: SIN
INE
2
2
e
4: BASIC Stamp Architecture – NCD, SIN
-- or --
2
2
Result
VAR
sx
p
Result = 99
DEBUG SDEC ? Result
Result = - Result
DEBUG SDEC ? Result
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
2
2
p
sx
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
Result = %1101
DEBUG ? NCD Result
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
2
2
circle with a radius of 1 unit (known as a unit circle), the sine is the y-
p
sx
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.
WORD
WORD
' Highest bit set is bit 3.
' Show the NCD of Result (4).
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 65
' Put -99 (2's complement format) into Result.
' Display it on the screen as a signed #.
' Negate the value
' Display it on the screen as a signed #.

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?

Table of Contents