You have no items in your shopping cart.

Subtotal: 0.00

Creating & Debugging a Project in KEIL

Introduction of KEIL

The Keil Software 89XX development tools listed below are programs you use to compile your C code, assemble your assembly source files, link and locate object modules and libraries, create HEX files, and debug your target program.

µVision for Windows™ is an Integrated Development Environment that combines project management, source code editing, and program debugging in one single, powerful environment.

The 8051 ANSI Optimizing C Compiler creates re locatable object modules from your C source code. The 8051 Macro Assembler creates re locatable object modules from your 89XX assembly source code. The Linker/Locator combines re-locatable object modules created by the Compiler and the Assembler into absolute object modules.

The Library Manager combines object modules into libraries that may be used by the linker.

The Object-HEX Converter creates Intel HEX files from absolute object modules.

Development Tools

The Keil development tools for 8051 offer numerous features and advantages that help you quickly and successfully develop embedded applications. They are easy to use and are guaranteed to help you achieve your design goals.

The µVision IDE and Debugger is the central part of the Keil 8051 development tools. µVision offers a Build Mode and a Debug Mode.

In the µVision Build Mode you maintain the project files and generate the application. µVision uses either the GNU or 8051 ADS/Real View™ development tools.

In the µVision Debug Mode you verify your program either with a powerful CPU and peripheral simulator that connects the debugger to the target system.

Getting Started

The µVision IDE from Keil combines project management, make facilities, source code editing, program debugging, and complete simulation in one powerful environment. The µVision development platform is easy-to-use and helping you quickly create embedded programs that work. The µVision editor and debugger are integrated in a single application that provides a seamless embedded project development environment.

The µVision IDE (Integrated Development Environment) is the easiest way for most developers to create embedded applications using the Keil development tools. To launch µVision, click on the icon on your desktop or select Keil µVision which version you are using from the Start Menu.

Create a Project

µVision includes a project manager which makes it easy to design applications for an 8051 based microcontroller. You need to perform the following steps to create a new project:

☞Start µVision and select the toolset

☞Create a project file and select a CPU from the device database.

☞Create a new source file and add this source file to the project.

☞Add and configure the startup code for the ARM.

☞Set tool options for target hardware.

☞Build project and create a HEX file for PROM programming.

Start µVision

µVision is a standard Windows application and started by clicking on the program icon.


Create a Project File

To create a new project file select from the µVision menu Project – New Project…. This opens a standard Windows dialog that asks you for the new project file name.

create a new project file

We suggest that you use a separate folder for each project. You can simply use the icon Create New Folder in this dialog to get a new empty folder. Then select this folder and enter the file name for the new project, i.e. Project1.

Project Workspace files

µVision creates a new project file with the name PROJECT1.UV2 which contains a default target and file group name. You can see these names in the Project Workspace – Files.

Select a Device

When you create a new project µVision asks you to select a CPU for your project. The Select Device dialog box shows the µVision device database. Just select the microcontroller you use.

Select device

Here we are using Philips P89V51RD2 controller. This selection sets necessary tool options for the P89V51RD2 device and simplifies in this way the tool configuration.



☞When you create a new project, µVision may automatically add the correct device specific CPU startup code for you.

☞On some devices, the µVision environment needs additional parameters that you have to enter manually. Please carefully read the information provided under Description in this dialog, since it might have additional instructions for the device configuration.

A window will open up asking for add up some startup files. Now we don’t want that, therefore Press NO.

Copy standard 8051 startup code

Set Tool Options for Target

µVision lets you set options for your target hardware. The dialog Options for Target opens via the toolbar icon or via the Project - Options for Target menu item.

µVision creates HEX files with each build process when Create HEX file under Options for Target – Output is enabled.


Select the Toolset

µVision uses the 8051 Real View compilation tools, the 8051 ADS compiler, the GNU GCC compiler, or the Keil C C51 compiler. When using the GNU GCC compiler or 8051 ADS it is required to install the toolset separately.

Select toolset

The toolset that you are actually using is selected in the µVision IDE under Project - Components, Environment, and Books.

☞Use Real View Compiler selects the 8051 development tools for this project. The setting Real View Folder specifies the path to the development tools. Examples for the various C51 ADS/Real View versions:

☞Real View Compiler for µVision: BIN\

☞ADS V1.2: C:\Program Files\C51\ADSv1_2\Bin

☞Real View Evaluation Version 2.1: C:\Program Files\C51\RVCT\Programs\2.1\350\eval2-sc\win_32-pentium

☞Use Keil C C51 Compiler selects the Keil C C51 Compiler, Keil AC51 Assembler and Keil LC51 Linker/Locater for this project.

☞Use GNU Compiler selects the GNU development tools for this project. The setting Cygnus Folder specifies the path to the GNU installation folder.

☞The GNU-Tool-Prefix allows using GNU tool chain variants. Examples for the various GNU versions:

☞GNU V3.22 with uclib: GNU-Tool-Prefix: arm-uclibc- Cygnus Folder: C:\Cygnus

☞GNUARM V4 with standard library: GNU-Tool-Prefix: arm-elf- Cygnus Folder: C:\Program Files\GNUC51\

☞The setting Keil Root Folder is the base folder of the Keil µVision/C51 installation. For the Keil C51 Tools the paths to the tool components is configured under Development Tool Folders.

Create New Source Files

You may create a new source file with the menu option File – New. This opens an empty editor window where you can enter your source code. µVision enables the C color syntax highlighting when you save your file with the dialog File – Save As… under a filename with the extension *.C. We are saving our example file under the name MAIN.C.


#define LED P0  

void delay(void);          voidled_left(void);

void led_right(void);

unsigned int j;

void led_left()


for (j=0x01; j<=0x80; j<<=1)


    LED = j;




void led_right()


  for (j=0x40; j>=0x01; j>>=1) 


    LED = j;                        




void delay(void)


     unsigned int i;

          for (i=0;i<10000;i++);


void main (void) 


LED = 0x00;                                       //Initialize to 0x00

          while (1)    







Once you have created your source file you can add this file to your project. µVision offers several ways to add source files to a project. For example, you can select the file group in the Project Workspace – Files page and click with the right mouse key to open a local menu.

The option Add Files opens the standard files dialog. Select the file MAIN.C you have just created.

add file to source group

Therefore you just creating a C file for add with the project. If you have already created C file then you just added that C file into Project file.

Source group

Now, you must rebuild the project file for generating HEX file. When you build the project then only the HEX file is generated by using the C Compiler.

Build the Project

Typical, the tool settings under Options – Target are all you need to start a new application. You may translate all source files and line the application with a click on the Build Target toolbar icon. When you build an application with syntax errors, µVision will display errors and warning messages in the Output Window – Build page. A double click on a message line opens the source file on the correct location in a µVision editor window.

Now you can modify existing source code or add new source files to the project. The Build Target toolbar button translates only modified or new source files and generates the executable file. µVision maintains a file dependency list and knows all include files used within a source file.

Even the tool options are saved in the file dependency list, so that µVision rebuilds files only when needed. With the Rebuild Target command, all source files are translated, regardless of modifications.

The final output will look like this...

Final output for creatting slicker

Test Programs with the µVision Debugger

It describes the Debug Mode of µVision and shows you how to use the user interface to test a sample program. Also discussed are simulation mode and the different options available for program debugging.

You can use µVision Debugger to test the applications you develop using the GNU or C51 ADS/Real View tool chain. The µVision Debugger offers two operating modes that are selected in the Options for Target – Debug dialog.

options for targetdebug

☞Use Simulator allows configuring the µVision Debugger as software-only product that simulates most features of an 8051 microcontroller without actually having target hardware.

☞You can test and debug your embedded application before the hardware is ready. µVision simulates a wide variety of peripherals including the serial port, external I/O, and timers. The peripheral set is selected when you select a CPU from the device database for your target.

CPU Simulation

The µVision Debugger simulates up to 4GB (Giga Bytes) of memory from which areas can be mapped for read, write, or code execution access. The µVision simulator traps and reports the illegal memory accesses.

In addition to memory mapping, the simulator also provides support for the integrated peripherals of the various 8051 based microcontroller devices. The on-chip peripherals of the CPU you have selected are configured From the Device Database selection you have made when you create your project target.

You may select and display the on-chip peripheral components using the Debug menu. You can also change the aspects of each peripheral using the controls in the dialog boxes.

Start Debug Mode

You can start the debug mode of µVision with the Debug – Start/Stop Debug Session command. Depending on the Options for Target – Debug configuration, µVision will load the application program and run the startup code.

Start debug mode

For information about the configuration of the µVision debugger refer to Set Debug Options. µVision saves the editor screen layout and restores the screen layout of the last debug session. If the program execution stops, µVision opens an editor window with the source text or shows CPU instructions in the disassembly window.

The next executable statement is marked with a yellow arrow.

During debugging, most editor features are still available. For example, you can use the find command or correct program errors. Program source text of your application is shown in the same windows.

Sample Programs

The sample programs are ready for you to run. You can use the sample programs to learn how to use our tools. Additionally, you can copy the code from KEIL samples for your own use.

The sample programs are found in ...\C51\Examples (for Keil toolset), \C51\GNU\Examples (for GNU), and...\ C51\ ADS\ Examples (for ADS).

Each sample program is stored in a separate folder along with project files that help you quickly build and evaluate each sample program.

HELLO: Your first C51 C Program

The HELLO sample program is located in C:\KEIL\C51\GNU\EXAMPLES\HELLO. HELLO does nothing more than print the text “Hello World” to the serial port. The entire program is contained in a single source file HELLO.C.

This small application helps you confirm that you can compile, link, and debug an application. You can perform these operations from the µVision for Windows using the provided project file.

The only on-chip peripheral used is the serial port. You do not actually need a target CPU because µVision lets you simulate the hardware required for this program.

HELLO Project File

In µVision, applications are maintained in a project file. A project file has been created for HELLO.

To load this project, select Open Project from the Project menu and open HELLO.UV2 from the folder …\C51\GNU\EXAMPLES\HELLO.


You can now edit HELLO.C. Double click on HELLO.C in the Files page of the Project Workspace. µVision loads and displays the contents of HELLO.C in an editor window.

Compiling and Linking HELLO

When you are ready to compile and link your project, use the Build Target command from the Project menu.

µVision begins to translate and link the source files and creates an absolute object module that you can load into the µVision debugger for testing. The status of the build process is listed in the Build page of the Output Window.

Build slicker

Testing HELLO

Once the HELLO program is compiled and linked, you can test it with the µVision debugger. In µVision, use the Start/Stop Debug Session command from the Debug menu or toolbar. µVision initializes the debugger and starts program execution till the main function.

open serial window

Open Serial Window #1 that displays the serial output of the application with the Serial Window #1 command from the View menu.

Run hello

Run HELLO with the Go command from the Debug menu or toolbar. The HELLO program executes and displays the text “Hello World” in the serial window.

Stop running

Stop Running HELLO with the Halt command from the Debug menu or the toolbar. You may also type ESC in the Command page of the Output window.

During debugging µVision will show the following output:

Debugging vision output

Single-Stepping and Breakpoints

insert remove breakpoints

Use the Insert/Remove Breakpoints command from the toolbar or the local editor menu that opens with a right mouse click and set a breakpoint at the beginning of the main function.


Use the Reset CPU command from the Debug menu or toolbar. If you have halted HELLO start program execution with Run. µVision will stop the program at the breakpoint.


You can single-step through the HELLO program using the Step buttons in the debug toolbar. The current instruction is marked with a yellow arrow. The arrow moves each time you step


Place the mouse cursor over a variable to view their value.

  Start stop debug session

You may stop debugging at any time with Start/Stop Debug Session command.

Using On-chip Peripherals

The on-chip peripherals of 805 microcontrollers are quite sophisticated and offer numerous configuration options. The code examples only present one or two methods of using each peripheral.

The status of the on-chip peripherals can be review in the µVision Debugger using the Peripherals menu. The menu items are device specific and configured based on information from the Device Database™.

It contains generic information about:

Startup Code: initialize the CPU and transfers control to the main function.

CPU Header Files: define the Peripheral Registers of the 8051 device in use.

Parallel Port I/O: usage of standard I/O ports.

Interrupts: explains the different interrupt variants on 8051 devices.

Vectored Interrupt Controller: explains the usage of a vectored interrupt.

General Purpose Timers: discusses the usage of standard timers.

Serial Interface: tells how to implement serial UART communication.

Watchdog Timer: usage of a watchdog timer for recovery from hardware or software failures.

A/D Converter: convert an analog input voltage to a digital value.