MSc Real Time Systems -C Programming Exercises

Prepared by Eric Goodyer

Room 4.10 James Went

For more information mail me at eg@dmu.ac.uk

OR eric.goodyer@ntlworld.com

To use the TURBO C compiler you must restart the PC in DOS mode. So switch off the PC, switch it on again, and during the boot up sequence select DOS mode. You will start up in the USER directory. Type TC to the DOS prompt to start up the DOS graphics interface environment.
Remember to bring copies of these lab programmes with you on a floppy disk
----------------------------------------------------------------------------

lab1.c
LAB EXERCISE 1 - LEARNING TO USE THE C COMPILER
This is the simplest programme that you can ever produce. It prints a line of text to the screen and simply waits for you to press a key. Once you have worked out how to compile and run it add some more code of your own. For example read the key into a variable
key = getch() ;
and then use the result to select different text strings to print.

key = getch();
switch(key)
{
case 'A':
printf("Letter A");
break ;
case 'B':
printf("Letter B");
break;
}

You should take up to two weeks to learn how to use the C compiler. Use the HELP facility to found out about other features. There are a range of basic I/O commands that you must learn, such as kbhit(), getch(), printf(). You should also learn about formated prints, such as
printf("%d",variable);
which will print the variable as a decimal number.

You can build up more complex strings such as
printf("The Answer to question number %d is %d",var1,var2);
The format commands (which always start with a % symbol) use the variables in order. So var1 is used as the source of data for the first %d, and var2 is used as the source of data for the second %d.

Once you are confident with using the basic I/O operations look at some different data types, and the ways that you can address them.
C defines the following types of data -
unsigned char integer in the range 0 to 255 (8 bits)
char signed integer in the range -128 to +127 (8 bits)
unsigned int integer in the range 0 to 65535 (16 bits)
int signed integer in the range -32768 to +32767 (16 bits)
float floating point number with signed 8 bit exponent and a signed 24 bit mantissa
double double precision float
The format command for a floating point output is %f

Finally you need to understand pointers. As well as declaring variables you can also declare a pointer to a variable. This is in fact the address of variable. For example consider the following code

int x,y;
int *point;
x = 27 ;
point = &y ;
*point = x ;
In line 1 we create two integers called 'x' and 'y'.
In line 2 we create a pointer to an integer calle 'point'.
In line 3 we assign the value 27 to the variable 'x'
In line 4 we set the pointer variable 'point' to the ADDRESS of variable 'y'. The & symbol in this instance means the address of the following variable.
Line 5 now set the variable POINTED BY 'POINT' to the current value of variable 'x'.
The variable 'y' also now holds the number 27

Pointers are a useful way to 'pass' the address of a variable from one function to another funtion.

At the end of two weeks you should be confident in the use of different variable types, formatted print statements and the use of pointers.
---------------------------------------------------------------------------------
lab2.c
LAB EXERCISE 2 - SOME SIMPLE GRAPHICS

This file contains some basic graphics commands. Learn how to use them, and then develop your own programme that draws a graph on the screen. You will find the pixel() function useful as well. For some hints on graphics look at my NEWP.EXE and SKINTEST.EXE programmes.

Take up to two weeks to learn how to use the Borland Graphics library. Th eHELP section is very useful, as it lists all the graphics commands and gives sample code.
As an exercise develop a programme that draws an XY graph on your screen, complete with axes and scales. Input numbers from the keyborad or a file, and draw the graph.
----------------------------------------------------------------------------------
lab3.c
LAB EXERCISE 3 - A REAL TIME EXECUTIVE KERNEL

This code includes all the building blocks that you need for a PC based real time executive.
You are strongly advised NOT TO ALTER IT, just add in your own code.

It is best to run it under DOS, because it can upset any version of Windows.

The core of this simple RTX is an interupt service routine (ISR) that occours every 50ms (20Hz). The ISR then derives a 10Hz, at 1Hz a 1 second and a ten second timing signals. For each of these time intervals a routine is called by the ISR - these are called the 'foreground' routines. The 10Hz routine
void foregroundhz10(void)
{ done++ ; // 10hz operations }
increments a counter called 'done'. In effect done is a running clock to an accuracy of 1/10th of a second. Whenever the background routine detects a change in 'done' in prints a new number on the screen. As long as the system is not overloaded the background routine will be able to keep up and will display the elapsed time on your screen. As a check the ISR also outputs the same clock, but by a much faster route as it writes data directly into the VGA display RAM.
The variable done is known a sa 'semaphore' as it is used to pass data between two concurrent routines. The ISR writes data into 'done' and the background routine reads data from 'done' in effect the ISR leaves a message for the background routine to pick up at a later time - it is for this reason that semaphores are also known as 'postboxes'. This is a very simple example of a semaphore, as data can only flow in one direction; more complex semaphores would allow data to be passed both ways, and would allow each routine to leave an acknowledgment to confirm that they have picked up the last message.
Take 1 week to study to understand how this programme works. Then add in your own 'real time' operations into the background routine - to achieve this you will have to create other lab4.c LAB EXERCISE 4 - USING THE FAN, HEATER & TEMPERATURE SENSOR

Last update 8 Jan 2002