28
RabbitCore RCM2100
tion of how Dynamic C handles multitasking with costatements and cofunctions, see
Chapter 5, “Multitasking with Dynamic C,” and Chapter 6, “The Virtual Driver,” in the
Dynamic C User’s Manual
.
3.3.4.3 TOGGLELED.C
One of Dynamic C’s unique and powerful aspects is its ability to efficiently multitask
using
cofunctions
and
costatements
. This simple application demonstrates how these pro-
gram elements work.
This sample program uses two costatements to set up and manage the two tasks. Costate-
ments must be contained in a loop that will “tap” each of them at regular intervals. This
program:
1. Initializes the pins of Port A as outputs.
2. Sets all the pins of Port A high, turning off the attached LEDs.
3. Sets the toggled LED status variable
vswitch
to 0 (LED off).
4. Starts an endless loop using a
while(1)
expression, and within that loop:
• Executes a costatement that flashes LED DS3;
• Executes a costatement that checks the state of switch S2 and toggles the state of
vswitch
if it is pressed;
• Turns LED DS2 on or off, according to the state of
vswitch
.
These steps repeat as long as the program is allowed to run.
The first costatement is a compressed version of
FLASHLED.c
, with slightly different
flash timing. It also uses the library function
DelayMs()
to deliver more accurate timing
than the simple delay loops of the previous program.
The second costatement does more than check the status of S2. Switch contacts often
“bounce” open and closed several times when the switch is actuated, and each bounce can
be interpreted by fast digital logic as an independent press. To clean up this input, the code
in the second costatement “debounces” the switch signal by waiting 50 milliseconds and
checking the state of the switch again. If it is detected as being closed both times, the pro-
gram considers it a valid switch press and toggles
vswitch
.
Unlike most C statements, the two costatements are not executed in their entirety on each
iteration of the
while(1)
loop. Instead, the list of statements within each costatement is
initiated on the first loop, and then executed one “slice” at a time on each successive inter-
ation. This mode of operation is known as a
state machine
, a powerful concept that per-
mits a single processor to efficiently handle a number of independent tasks.
The ability of Dynamic C to manage state machine programs enables you to create very
powerful and efficient embedded systems with much greater ease than other programming
methods.
More Information
See the entries for the
DelayMs()
function, as well as Section 5, “Multitasking with
Dynamic C,” in the
Dynamic C User’s Manual
.
Содержание RabbitCore RCM2100 Series
Страница 1: ...RabbitCore RCM2100 C Programmable Module with Ethernet Getting Started Manual 019 0093 050505 F ...
Страница 34: ...30 RabbitCore RCM2100 ...
Страница 52: ...48 RabbitCore RCM2100 ...
Страница 54: ...User s Manual 50 ...
Страница 56: ......