<< Previous | Index | Next >>

3. Quick Tutorial

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 program Pong.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 name printf in the program and press <Ctrl-H>. This brings up a documentation box for the function printf. You can do this with all functions in the Dynamic C libraries, including libraries you write yourself.

Figure 1. Sample Program DEMO1.C

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:

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 of j advances when the statement j++ 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 the for (.. 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 variable k has been added along with a statement to increment k by the value of i each time around the endless loop. The statement


runwatch();

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 of k.

As an experiment, add another expression to the watch window:


k*5

Then press <Ctrl-U> several times to observe the watch expressions k and k*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.


main() {
int secs; // seconds counter
secs = 0; // initialize counter
(1) while (1) { // endless loop
// First task will print the seconds elapsed. (2) costate {
secs++; // increment counter
(3) waitfor( DelayMs(1000) ); // wait one second
printf("%d seconds\n", secs); // print elapsed seconds
(4) }
// Second task will check if any keys have been pressed. costate {
(5) if ( !kbhit() ) abort; // key been pressed?
printf(" key pressed = %c\n", getchar() );
}

(6) } // end of while loop
} // end of main

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. The while 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 the while loop. When a waitfor condition is encountered the first time, the current value of MS_TIMER is saved and then on each subsequent pass the saved value is compared to the current value. If a waitfor 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 the waitfor statement. Once 1000 ms has passed, the statement after the waitfor 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 the while 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 the abort 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 variable secs to the list of watch expressions, then press <Ctrl-U> repeatedly to observe as secs 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 or switch. 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 a runwatch() 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 and waitfor, 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