EE/CS: Keil C Programming Notes
Summer 2003
This handout is a tutorial on how to write a program for the University of Evansville 8051 controller board (Version 7) using Keil C uvision-2 and Philips WINISP. It assumes you have logged into a computer with these programs.
Keil C is launched by double-clicking on the icon on that says Keil uvision2. After it comes up, you should maximize the window if it is not full size. If there are already files open, go to the Project menu and select Close Project.
Every time you want to create a program and download it into
the controller board, you must create a project that tells the
compiler which files belong to your program, write the program, then
compile and link the program. This section will explain each of these
steps. The next section will explain how to download the program to
the controller board.
To set up a project, do the following steps:
http://csserver.evansville.edu/~lego101
under the Summer 2003 OPTIONS heading. Save robotics7opt.h and
robotics7opt.c to your project directory by right-clicking on
the link and choosing Save Target As (in Internet Explorer).
The LEGO robotics library is a collection of routines that give the UE controller board an API (Application Programmer's Interface) for using it as a robot controller. The interface is found in robotics7opt.h. The source code of these routines and some notes are found in robotics7opt.c. You are welcome to read these files, and if you want to know more about this code just ask an instructor.
A Select Device dialog box will appear. Double-click on Philips, then choose 89C51RD2. (This is processor chip is installed on the controller board. If this chip is not listed choose 89C51RD+.) Click OK.
Select the C source files you want to add. For this project you will always need the source file with your main program in it, example.c for this tutorial, and robotics7opt.c. (You do not add robotics7opt.h, but you need to have this file in your project directory.)
Click on Close to get rid of the Add Files dialog box.
You can open any of the files you have added to your project by double-clicking in the file list window or using File|Open. For this tutorial, you can double-click example.c. Here is the example program that you can type into the edit window.
/* ------------------------------------------------------------------------
File: example.c
This program demonstrates motors and buzzer
This program uses the OPTIONS 2003 robotics library for
Version 7 of the controller board.
Keil C headers are not guarded. If robotics library is used, its header
file should be the only included file.
---------------------------------------------------------------------------*/
#include "robotics7opt.h"
/* Constant declarations */
#define DELAY_TIME 20 /* about 1 sec */
void main (void)
{
/* Variable declarations */
unsigned char i;
/* Initialization */
InitializeMotors();
Delay (DELAY_TIME); /* Wait about 1 sec */
while (1) /* Repeat forever */
{
/* Forward */
SetMotor (1, 255);
SetMotor (2, -255);
Delay (DELAY_TIME*5);
AllStop();
/* Left Turn */
SetMotor (1, 255);
SetMotor (2, 255);
Delay (DELAY_TIME*2);
AllStop();
/* Reverse */
SetMotor (1, -255);
SetMotor (2, 255);
Delay (DELAY_TIME*5);
AllStop();
/* Right Turn */
SetMotor (1, -255);
SetMotor (2, -255);
Delay (DELAY_TIME*2);
AllStop();
Beep (2);
Delay (DELAY_TIME*2);
}
}
Once we have written a program, we need to build it. Go to the Project menu and choose Build Target. The window at the bottom of the screen should inform you of the progress being made.
First it will compile your source files. If you have any
syntax errors, the window will contain a list of errors and the line
numbers. Double-clicking on an error message will cause the
corresponding line in the source edit window to be highlighted.
Correct any errors and do Project|Build Target again.
If the compilation phase is successful, an object file will be
created (with .obj extension) for each source file. The
compiler also produces a listing file (with .lst extension)
for each source file. Then the system will link them into a combined
hex file (with .hex extension) suitable for downloading. In
this tutorial, example.obj, example.lst, and
example.hex are created. You may ignore the warnings it
gives about uncalled segments being ignored.
Once our program has been compiled into a HEX file, we need to download it into the controller board. For this step we need to use a different program called WINISP. It is probably easiest to minimize Keil C at this time.
To load our program into the controller board we need to following
steps:
If your vehicle does what is expected, great! Otherwise,
compare the behavior with your program, make changes using Keil C, and
repeat the build and load processes. (Sometimes the program is
``right'' and the hardware is wired ``wrong,'' so be sure to ask an
instructor if you do not understand why your vehicle behaves the way
it does.)
When you are done, exit WINISP by clicking on Close. In Keil
C, close your project by using Project|Close Project, then exit
using File|Exit.
To work on this program later, start up Keil C again. If you
didn't close your project previously, it will probably load your
project back. If not, go to the Project menu and choose Open Project.
Set the directory to your project directory, and select
example.prj. To open the files in the project, you can
double-click in the file list.