<< Previous | Index | Next >> | |
|
Sample programs are provided in the Dynamic C
Samples
folder similar to the one shown below.The subfolders contain sample programs that illustrate the use of the various Dynamic C libraries. The subfolder named Cofunc, for example, contains sample programs illustrating the use of
COFUNC.LIB
. The sample programPong.c
demonstrates output to the STDIO window. Each sample program has comments that describe its purpose and function.3.1 Run DEMO1.C
This sample program will be used to illustrate some of the functions of Dynamic C. Open the file
Samples/DEMO1.C
. The program will appear in a window, as shown in Figure 1 below (minus some comments). Use the mouse to place the cursor on the function nameprintf
in the program and press <Ctrl-H>. This brings up a documentation box for the functionprintf
. You can do this with all functions in the Dynamic C libraries, including libraries you write yourself.To run the program
DEMO1.C
, open it with the File menu, compile it using the Compile menu, and then run it by selecting Run in the Run menu. The value of the counter should be printed repeatedly to the STDIO window if everything went well. If this doesn't work, review the following points:
The target should be ready, indicated by the message "BIOS successfully compiled..." If you did not receive this message or you get a communication error, recompile the BIOS by typing <Ctrl-Y> or select Recompile BIOS from the Compile menu.
A message reports "No Rabbit Processor Detected" in cases where the wall transformer is not connected or not plugged in.
The programming cable must be connected to the controller. (The colored wire on the programming cable is closest to pin 1 on the programming header on the controller). The other end of the programming cable must be connected to the PC serial port. The COM port specified in the Dynamic C Options menu must be the same as the one the programming cable is connected to.
To check if you have the correct serial port, select Compile, then Compile BIOS, or press <Ctrl-Y>. If the "BIOS successfully compiled ..." message does not display, try a different serial port using the Dynamic C Options menu until you find the serial port you are plugged into. Don't change anything in this menu except the COM number. The baud rate should be 115,200 bps and the stop bits should be 1.
3.1.1 Single Stepping
Compile
DEMO1.C
by clicking the Compile button on the task bar. The program will compile and the screen will come up with a highlighted character (green) at the first executable statement of the program. Use the F8 key to single step. Each time the F8 key is pressed, the cursor will advance one statement. When you get to the statement:for(j=0, j< ...
, it becomes impractical to single step further because you would have to press F8 thousands of times. We will use this statement to illustrate watch expressions.3.1.2 Watch Expression
Press <Ctrl-W> or choose Add/Del Watch Expression in the Inspect menu. A box will come up. Type the lower case letter j and click on Add to top, then Close. Now continue single stepping by pressing F8. Each time you step, the watch expression (
j
) will be evaluated and printed in the watch window. Note how the value ofj
advances when the statementj++
is executed.3.1.3 Breakpoint
Move the cursor to the start of the statement:
for (j=0; j<20000; j++);
To set a breakpoint on this statement, press F2 or select Breakpoint from the Run menu. A red highlight appears on the first character of the statement. To get the program running at full speed, press F9 or select Run on the Run menu. The program will advance until it hits the breakpoint. The breakpoint will start flashing both red and green colors.
To remove the breakpoint, press F2 or select Toggle Breakpoint on the Run menu. To continue program execution, press F9 or select Run from the Run menu. Now the counter should be printing out regularly in the STDIO window.
You can set breakpoints while the program is running by positioning the cursor to a statement and using the F2 key. If the execution thread hits the breakpoint, a breakpoint will take place. You can toggle the breakpoint with the F2 key and continue execution with the F9 key.
3.1.4 Editing the Program
Click on the Edit box on the task bar. This will put Dynamic C into edit mode so that you can change the program. Use the Save as choice on the File menu to save the file with a new name so as not to change the demo program. Save the file as
MYTEST.C
. Now change the number 20000 in thefor (..
statement to 10000. Then use the F9 key to recompile and run the program. The counter displays twice as quickly as before because you reduced the value in the delay loop.3.2 Run DEMO2.C
Go back to edit mode and load the program
DEMO2.C
using the File menu Open command. This program is the same as the first program, except that a variablek
has been added along with a statement to incrementk
by the value ofi
each time around the endless loop. The statementrunwatch();
has been added as well. This is a debugging statement to view variables while the program is running. Use the F9 key to compile and run
DEMO2.C
.3.2.1 Watching Variables Dynamically
Press <Ctrl-W> to open the watch window and add the watch expression
k
to the top of the list of watch expressions. Now press <Ctrl-U>. Each time you press <Ctrl-U>, you will see the current value ofk
.As an experiment, add another expression to the watch window:
k*5
Then press <Ctrl-U> several times to observe the watch expressions
k
andk*5
.3.3 Run DEMO3.C
The example below, sample program
DEMO3.C
, uses costatements. A costatement is a way to perform a sequence of operations that involve pauses or waits for some external event to take place.3.3.1 Cooperative Multitasking
Cooperative multitasking is a way to perform several different tasks at virtually the same time. An example would be to step a machine through a sequence of tasks and at the same time carry on a dialog with the operator via a keyboard interface. Each separate task voluntarily surrenders its compute time when it does not need to perform any more immediate activity. In preemptive multitasking control is forcibly removed from the task via an interrupt.
Dynamic C has language extensions to support both types of multitasking. For cooperative multitasking the language extensions are costatements and cofunctions. Preemptive multitasking is accomplished with slicing or by using the µC/OS-II real-time kernel that comes with Dynamic C Premier.
Advantages of Cooperative Multitasking
Unlike preemptive multitasking, in cooperative multitasking variables can be shared between different tasks without taking elaborate precautions. Cooperative multitasking also takes advantage of the natural delays that occur in most tasks to more efficiently use the available processor time.
The
DEMO3.C
sample program has two independent tasks. The first task prints out a message to STDIO once per second. The second task watches to see if the keyboard has been pressed and prints out which key was entered.
The numbers in the left margin are reference indicators and not part of the code. Load and run the program. The elapsed time is printed to the STDIO window once per second. Push several keys and note how they are reported.
The elapsed time message is printed by the costatement starting at the line marked (2). Costatements need to be executed regularly, often at least every 25 ms. To accomplish this, the costatements are enclosed in a
while
loop. Thewhile
loop starts at (1) and ends at (6). The statement at (3) waits for a time delay, in this case 1000 ms (one second). The costatement executes each pass through thewhile
loop. When awaitfor
condition is encountered the first time, the current value ofMS_TIMER
is saved and then on each subsequent pass the saved value is compared to the current value. If awaitfor
condition is not encountered, then a jump is made to the end of the costatement (4), and on the next pass of the loop, when the execution thread reaches the beginning of the costatement, execution passes directly to thewaitfor
statement. Once 1000 ms has passed, the statement after thewaitfor
is executed. A costatement can wait for a long period of time, but not use a lot of execution time. Each costatement is a little program with its own statement pointer that advances in response to conditions. On each pass through thewhile
loop as few as one statement in the costatement executes, starting at the current position of the costatement's statement pointer. Consult Chapter 5 "Multitasking with Dynamic C" for more details.The second costatement in the program checks to see if a key has been pressed and, if one has, prints out that key. The
abort
statement is illustrated at (5). If theabort
statement is executed, the internal statement pointer is set back to the first statement in the costatement, and a jump is made to the closing brace of the costatement.To illustrate the use of snooping, use the watch window to observe
secs
while the program is running. Add the variablesecs
to the list of watch expressions, then press <Ctrl-U> repeatedly to observe assecs
increases.3.4 Summary of Features
This chapter provided a quick look at the intuitive interface of Dynamic C and some of the powerful options available for embedded systems programming.
Development Functions
When you load a program it appears in an edit window. You compile by clicking Compile on the task bar or from the Compile menu. The program is compiled into machine language and downloaded to the target over the serial port. The execution proceeds to the first statement of main, where it pauses, waiting to run. Press the F9 key or select Run on the Run menu. If want to compile and run the program with one keystroke, use F9, the run command; if the program is not already compiled, the run command compiles it.
Single Stepping
This is done with the F8 key. The F7 key can also be used for single stepping. If the F7 key is used, then descent into subroutines will take place. With the F8 key the subroutine is executed at full speed when the statement that calls it is stepped over.
Setting Breakpoints
The F2 key is used to toggle a breakpoint at the cursor position if the program has already been compiled. You can set a breakpoint if the program is paused at a breakpoint. You can also set a breakpoint in a program that is running at full speed. This will cause the program to break if the execution thread hits your breakpoint.
Watch Expressions
A watch expression is a C expression that is evaluated on command in the watch window. An expression is basically any type of C formula that can include operators, variables and function calls, but not statements that require multiple lines such as
for
orswitch
. You can have a list of watch expressions in the watch window. If you are single stepping, then they are all evaluated on each step. You can also command the watch expression to be evaluated by using the <Ctrl-U> command. When a watch expression is evaluated at a breakpoint, it is evaluated as if the statement was at the beginning of the function where you are single stepping. If your program is running you can also evaluate watch expressions with a <Ctrl-U> if your program has arunwatch()
command that is frequently executed. In this case, only expressions involving global variables can be evaluated, and the expression is evaluated as if it were in a separate function with no local variables.Costatements
A costatement is a Dynamic C extension that allows cooperative multitasking to be programmed by the user. Keywords, like
abort
andwaitfor
, are available to control multitasking operation from within costatements.
| |
<< Previous | Index | Next >> | |
Z-World, Inc. www.zworld.com Phone: 1.530.757.3737 Fax: 1.530.757.3792 |
Rabbit Semiconductor www.rabbitsemiconductor.com Phone: 1.530.757.8400 Fax: 1.530.757.8402 |