exploring keil
TRANSCRIPT
-
8/3/2019 Exploring Keil
1/50
CHAPTER1: INTRODUCTION
[This chapter discusses -Intel Hex file, Assembly language, C language, Embedded
Development Tools-Assemblers, Interpreters, Compilers, simulators, emulators,
debuggers and IDE for the development of 8051 and other Embedded Systems]
-----------------------------------------------------------------------------------------------------------
Writing the Control ProgramWhen its time to write the program that controls your project, the options include
using machine code, assembly language, or a higher-level language. Which programminglanguage you use depends on things like desired execution speed, program length, and
convenience, as well as whats available in your price range.
Machine code, INTEL HEX file and DownloaderThe most fundamental program form is machine code, the binary instructions that
cause the CPU to perform the operations you desire. Representing the machine code is of
many types usually binary. Combining this binary data into 8 bit, 16 bit and 32 bits
makes it easily understandable. Hexadecimal format combines BCD data which is a
direct representation of binary. For easier representation of the binary is the hexadecimal
format. Therefore microprocessor or microcontroller development makes use of hex file
format. This is called as Intel hex file. Assembler or Compiler whatever we use to
develop code for microprocessors or controllers hex file is the ultimate target. If an
assignment is given by you to make a led blink using your microcontroller then you haveto develop code in assembly or high level language and create the hex file for that
program. This hex file should be downloaded to the microcontroller code memory. Nowthe microcontroller runs as you programmed. The device used for downloading the hex
file is called the downloader or programmer.
Assembly languageOne step removed from machine code is assembly language, where abbreviations
called mnemonics (memory aids) substitute for the machine codes. The mnemonics areeasier to remember than the machine codes they stand for. For example, in the 8051s
assembly language, the mnemonic CLR C means clear the carry bit, and is easier to
remember than its binary code (11000011).
Since machine code is ultimately the only language that a CPU understands, you need
some way of translating assembly-language programs into machine code. For very short
programs, you can hand assemble, or translate the mnemonics yourself by looking up the
machine codes for each abbreviation.
Another option is to use an assembler, which is software that runs on a desktopcomputer and translates the mnemonics into machine code. Most assemblers provideother features, such as formatting the program code and creating a listing that shows both
the machine-code and assembly-language versions of a program side -by-side.
Higher-level languagesA disadvantage to assembly language is that each device family has its own set of
mnemonics, so you have to learn a new vocabulary for each family you Work with. To
-
8/3/2019 Exploring Keil
2/50
get around this problem, higher-level languages like C, Pascal, FORTRAN, Forth, and
BASIC follow a standard syntax so that programs are more portable from one device toanother. The idea is that with minor changes, you can use a language like BASIC to write
programs for many different devices. In reality, each language tends to develop manydifferent dialects, depending on the chip and the preferences of the languages vendor, so
porting a program to a different device isnt always effortless. But there are many
similarities among the dialects of a single language, so, as with spoken language, a new
dialect is easier to learn than a whole new language.Higher-level languages also simplify
programming by allowing you to do in one or a few lines what would require many lines
of assembly code to accomplish.
Interpreters and compilers:These are two forms of higher-level languages. An interpreter translates a program
into machine code each time the program runs, while a compiler translates only once,
creating a new, executable file that the computer runs directly, without re-translating.As a rule, interpreters are very convenient for shorter programs where execution
speed isnt critical. With an interpreted language, you can run your program codeimmediately after you write it, without a separate compile or assembly step. A compiler
is a good choice when a program is long or has to execute quickly. A single language like
BASIC may be available in both interpreted and compiled versions. Each device family
requires its own interpreter or compiler to translate the higher-level code into the machinecode for that device. In other words, you cant use Quick BASIC for IBM
PCs to program an 8051 microcontrolleryou need a compiler that generates
program code for the 8051.Compared to an equivalent program written in assembly
language, a compiled program usually is larger and slower, so assembly language is the
way to go if a program must be as fast or as small as possible. A higher-level languagealso may not offer all of the abilities of assembly code, though you can get around this by
calling subroutines in assembly language when necessary.
BASIC-52 is an interpreted language, but BASIC compilers for the 8052 are alsoavailable. In fact, you can have the best of both worlds by testing your programs with the
BASIC-52 interpreter, and compiling the finished product for faster execution and other
benefits of the compiled version.
Testing and DebuggingAfter youve written a program, or a section of one, its time to test it and as
necessary, find and correct mistakes to get it working properly. The process of ferreting
out and correcting mistakes is called debugging. Easy debugging and troubleshooting canmake a big difference in how long it takes to get a system up and running. As with
programming, you have several options here as well.
Testing in EPROMOne way is to burn your program into EPROM, install the EPROM in your system,
run the program, and observe the results. If problems occur (as they usually Will) you
modify the program, erase and reburn the EPROM, and try again, repeating as many
times as necessary until the system is operating properly.
-
8/3/2019 Exploring Keil
3/50
Development systemsAnother option is to use a development system. A typical development System
consists of a monitor program, which is a program stored in EPROM or other memory in
the microcontroller system, and a serial link to a personal computer. Using the abilities ofthe monitor program, you can load your program from a personal computer into RAM
(instead of the more permanent EPROM) on the microcontroller system, then run the
program, modify it, and retry as often as necessary until the program is working properly.
Most development systems also allow single-stepping, setting breakpoints, and
viewing and changing the data in memory. In single-stepping, you run the program one
step at time, pausing after each step, so you can more easily monitor what the circuits and
program are doing at each step. A breakpoint is a program location where the program
stops executing and waits for a command to continue. You can set breakpoints at criticalspots in your program. At any breakpoint, you can view or change the contents of
memory or perform other tests.
SimulatorsAnother development tool is a simulator, which is software that runs on a desktop
computer and uses the video display to demonstrate what would happen if a specific
microprocessor or microcontroller were to run a particular program.
You can look inside the simulated chip, observe the contents of internal memory,
and single-step or set breakpoints to stop program execution at a desired programlocation or condition. In this way, you can get a program working properly before you
commit it to EPROM. One drawback to simulators is that they cant mimic all features of
the chip of interest, especially interrupt-response and timing characteristics.
EmulatorsAn in-circuit emulator (ICE) is hardware that replaces the microprocessor in question
by plugging into the microprocessors socket on the device you want to test. Like a
simulator, an emulator lets you control program execution and monitor what happens ateach program step. Microprocessor emulators typically are expensive. A ROM emulator
is a lower-cost option that simulates an EPROM (using RAM, for example) for program
storage, and usually provides the abilities of a development system as well.
The Keil-IDEdevelopment systemThe Keil-IDE system and a personal computer form a complete development system
for writing, testing, and storing programs. The personal computers keyboard and screen
make it easy to write and run programs and view the results.Keil-IDE has many built-in debugging features that make it easy to test programs.
You can run a program immediately after writing it, without having to assemble, compile,
or program an EPROM. You can use a STOP statement and CONT (continue) command
to set breakpoints and resume executing your program. You can use PRINT statements to
display variables as the program runs. And, if you wish, you can use your personal
computer for writing programs off-line and uploading and downloading them to the Keil-
IDE system. The keil contains the A51 assembler, C51 compiler and BL51 linker to
translate the assembly or C source code into INTEL hex file.
-
8/3/2019 Exploring Keil
4/50
CHAPTER2:BUILDING AN APPLICATION IN KEIL
[This chapter discusses Keil uVision2 IDE features, Start with Keil, Building an
existing application using the keil uVision]
-----------------------------------------------------------------------------------------------------------
(I)What is uVision2?
uVision2 is an IDE (Integrated Development Environment) that helps you write, compile,
and debug embedded programs. It encapsulates the following components:
A project manager. A make facility. Tool configuration. Editor. A powerful debugger.
A Project manager is used to add all the source code files and document files together
to make it as a complete project file. It Links all the Source files together and helps to
generate a single hex file output if the source files are compiled.
The term make facility refers to compiling the source file to create or make the hexfile, assembly file, object file, linker file, listing file etc. But the requirement for a
programmer is the hex file. Because the microcontroller or processor can understand onlythe hex file which is the machine level language. The hex file will be downloaded to the
microcontroller using the downloader.
Tools configuration refers to hardware and software tools that can be used along with
the keil - IDE as add on-board or add on software. But we have to configure the toolssuite the keil.
Editor is the source code editor used to write the assembly language and C language.
Then the file can be saved as C or assembly file and it can be added to the projectmanger.
After youve written a program, or a section of one, its time to test it and as
necessary, find and correct mistakes to get it working properly. Keil contains a powerfuldebugger in built in it.
The above said options are available to the user. Other tools such as assembler,
compiler, linker and object file converter are available in the IDE but are not for the
usage by the software developer. It can be commented as For Office Use Only but
essential to create the hex file.
-
8/3/2019 Exploring Keil
5/50
(II)How to Start with Keil
To start with keil we have the only option write a source code in assembly or C andconvert into Intel hex file. This is for keil is used. First of all we can open an existing
project or we can create a new project on our own.
The concept explained below helps you in opening an example application already
available with the keil and to develop project software to display Hello World by the
microcontroller to the serial port of the PC using the UART of the microcontroller.
What is the language you prefer using keil. Choosing an assembly is not worthy whenwriting a large program. Also many assemblers are available in the market for developing
assembly programming with a cheaper price. Choosing a keil for C development might
be the better choice and cost effective. For beginners in Embedded C and 8051 keil is a
gift.
(III)Building an Application in uVision2
To build (compile, assemble, and link) an application in uVision2, you must:
1. Select Project - Open Project2. Navigate to the Project file location3. For Example; C:\Keil\C51\Examples\Hello.UV2 4. Select Hello.UV2 and Click open5. Select options for target and select output option6. Select the create hex option and click ok7. To activate Shortcut, Go to View and enable the build target toolbar8. Now the project window opens. Go to build target and Click build target.9. Select Project - Rebuild all target files or Build target.10.Target device can be changed using select device for the target option11.Selecting memory model for the target12.Selecting the code ROM size and operating system.
Opening an Existing Project:
If the application or the project has been created already that can be opened by
directly going to the project location and clicking the project file or it can be loaded from
the project manager window.
Opening a project in Keil uVision2 is shown in fig 2.1
-
8/3/2019 Exploring Keil
6/50
Fig 2.1 Opening an existing project Hello
For opening a project already exists in some location of the PC, Go to Project and
Click Open Project as shown in the diagram.
Fig 2.2 Opening an existing project from project manager
Opening the Existing Project
Using the Project Manager
Double click to open project
-
8/3/2019 Exploring Keil
7/50
Navigating to the Project file location
After clicking the open project from the project manager, the navigation toolbarfor the project source file location appears.
Navigate and select the example project from keil folder in the installed location
or open the project created by you.
Fig 2.3 Navigating to the Project file location
Navigation refers to normal choosing a location in the PC which you are familiar
already. The example here depicts you opening a project Hello which is an exampleprogram which is in the keil software.
Many example programs come along with the keil software. These are available
to the user as soon as keil has been installed. Fig 2.3 and 2.4 explains navigation to keilfolder.
These examples are available in the keil/Examples folder in C or D colon where
keil has been installed. Some of the example programs available in keil uVision2 are
Navigate to the Project
File location
-
8/3/2019 Exploring Keil
8/50
Hello which prints a string Hello world to the serial port of the PC using the
microcontroller?
Traffic is a program that controls a traffic light
The MEASURE program uses the analog inputs of the C515 to simulate a data
logger.
The BLINKY project does nothing more than blink the LEDs on the MCBx51evaluation board.
The DES project is the implementation of Cryptographic algorithm in 8 bit 8051
microcontroller.
The CSAMPLE program implements a simple addition and subtractioncalculator. Numbers and operators are entered via serial I/O.
ASAMPLE is an assembly program that writes text to the serial port of the 8052
microcontroller.
Navigating to the Project file location
Fig 2.4 Selecting the keil Folder
Select Keil
Folder
-
8/3/2019 Exploring Keil
9/50
Opening a Hello Project from the keil examples:
Selecting and opening the Hello project from the keil example projects. It is notnecessary that the project should be the example.
You can open your own project which you create already if you have saved in
your PC. Fig 2.5 depicts opening the Hello Project file.
Fig 2.5 Opening the Hello.UV2 Project file
Setting the options for the target device
Make sure that the output Intel hex file creation is selected. If it is not enabled.Make it enabled. This is available in the options for the target. Fig 2.6 depicts how to
select the options for the target. Right Click on the target and Select options for the target.
Now the option for the target window opens as shown in the fig2.7.Select the
output option and enable create hex file option. If device is to be changed, change it to a
desired one using the target window like the output window.
No need to change the other option for A51, C51, BL51 and other option. If
hardware debugger keil monitor 51 is used change the option of the debugger. Otherwise
select the simulator option in the debug window. Default condition is simulator selected.
Select Hello.UV2
Open Hello.UV2
-
8/3/2019 Exploring Keil
10/50
A51 is the assembler available in the keil IDE which converts the assembly
language into object file.
C51 is the Cross Compiler available in the keil IDE which converts the C
language into assembly language.
The object files for every source files either an assembly file or a C file will be
created. These object files should be linked together to create the hex file. The purpose of
the linker is that. The linker available in keil is BL51. The target options Contains all
these options to be configured if necessary by the user.
Fig 2.6 Selecting options for the target
Options for the target
-
8/3/2019 Exploring Keil
11/50
Fig 2.7 Enable Create hex file option
In the listing option we can enable the list file and assembly file generation. Wecan also select include files and Symbols options.
If you enable all these things the Compiler generates the Assembly files related to
the C file we are using in our project.
Similarly we can select the options for the Source group by right Clicking on the
Source group and selecting options for the Source group. Fig 2.8 depicts the Selection of
the options for the source group.
Enable generate Assembler SRC file. Assemble Always build and other options in
the Options for the source group window as shown in fig 2.9.
Select Create hex fileClick Ok to enable hex
File Generation
-
8/3/2019 Exploring Keil
12/50
Fig 2.8 Selecting options for the Source Group.
Fig 2.9 Setting options for the Source group
Options for the Source Group
Set Options such as generate
Assembler SRC file, Assemble
SRC file and other Options
Click OK to Selectall the Options
-
8/3/2019 Exploring Keil
13/50
Selecting or changing the target device
For changing or selecting the device for the target right Click on the target(Simulator) and Select the Option Select Device for the Target Simulator.
Fig 2.10 Select device for the target
Fig 2.11 Selecting 89C52 Controller from Atmel
Select device for the target
Select Manufacturer
And the device
Click OK to make the
Atmel AT89C52 selected
-
8/3/2019 Exploring Keil
14/50
Fig 2.10 shows right clicking and selecting the Select device for the target. Now the
Select device for the Target window opens. Now Navigate to the ATMEL Manufacturerand select the device 89C52 for using the Chip AT89C52 microcontroller.
If we are using AT89s52 Controller also we can select the device At89c52. After
selecting the device click ok to close the device selection window. This is shown in the
fig 2.11.
Rebuild target from the shortcut for rebuild option.
The Project has been opened. Now build the application using the rebuild option.
All the source files have been already created and added to the target project manager.
So its time to convert the project sources to hex file. For that the project targets or
source files should be builded, rebuild or compiled to make the Intel hex file. The termsbuild, rebuild and Compile means the same, making an Intel hex file.
Fig 2.12 Rebuilding the target using the shortcut
If not the shortcut is available to the user in the keil software GUI just go the view optionand select tool bar. This is shown in fig 2.12.The user can build the target files also by the
project manager window or the project workspace available. Similarly if project work
space is not available to the user in shortcut, go to view and select the project window
Rebuild all target files
From Shortcut Button
-
8/3/2019 Exploring Keil
15/50
option. Rebuilding the targets from the project manager and the project workspace
window are depicted in the figure 2.13.
Fig 2.13 Rebuilding the target from project manager
Fig 2.14 Rebuilding the target from project workspace
Rebuild all target files
From project manager
Rebuild target from
Project Workspace
-
8/3/2019 Exploring Keil
16/50
Memory models:The memory model determines the default memory type to use for function
arguments, automatic variables, and declarations with no explicit memory type specifier.
You specify the memory model on the Cx51 compiler command line using the SMALL,
COMPACT and LARGE memory model option from the target option.
Small modelIn this model, all variables, by default, reside in the internal data memory of the
8051 system. (This is the same as if they were declared explicitly using the data memory
type specifier.) In this memory model, variable access is very efficient. However, all
objects, as well as the stack must fit into the internal RAM. Stack size is critical becausethe real stack size depends upon the nesting depth of the various functions. Typically, if
the linker/locator is configured to overlay variables in the internal data memory, the small
model is the best model to use. Fig 2.14 shows the small memory model selection.
Fig 2.14 Selecting small memory model
Compact ModelUsing the compact model, all variables, by default, reside in one page of external
data memory. (This is as if they were explicitly declared using the pdata memory type
specifier.) This memory model can accommodate a maximum of 256 Bytes of variables.The limitation is due to the addressing scheme used, which is indirect through registersR0 and R1 (@R0, @R1). This memory model is not as efficient as the small model;
therefore, variable access is not as fast. However, the compact model is faster than the
large model.
Small variables in data
Ensures very efficient memory
Usage for the microcontroller
-
8/3/2019 Exploring Keil
17/50
When using the compact model, the Cx51 compiler accesses external memory
with instructions that utilize the @R0 and @R1 operands. R0 and R1 are byte registersand provide only the low-order byte of the address. If the compact model is used with
more than 256 bytes of external memory, the high-order address byte (or page) isprovided by Port 2 on the 8051
. In this case, you must initialize Port 2 with the proper external memory page to
use. This can be done in the startup code. You must also specify the starting address for
PDATA to the linker. Fig 2.15 shows the selection of compact memory model.
Fig 2.15 Selecting Compact memory model
STARTUP.A51The STARTUP.A51 file contains the startup code for a Cx51 target program.This source file is located in the LIB directory. Include a copy of this file in each 8051
project that needs custom startup code. The startup code is executed immediately upon
reset of the target system and optionally performs the following operations, in order:
1. Clears internal data memory2. Clears external data memory
3. Clears paged external data memory
4. Initializes the small model reentrant stack and pointer
5. Initializes the large model reentrant stack and pointer
6. Initializes the compact model reentrant stack and pointer
Selecting Compact variables
In PDATA
-
8/3/2019 Exploring Keil
18/50
7. Initializes the 8051 hardware stack pointer
8. Transfers control to the main C function
Large ModelIn the large model, all variables, by default, reside in external data memory (up to
64 KBytes). (This is the same as if they were explicitly declared using the xdata memory
type specifier.) The data pointer (DPTR) is used for addressing. Memory access through
this data pointer is inefficient, especially on variables with a length of two or more bytes.
This type of data access mechanism generates more code than the small or compact
models.fig 2.16 shows the large memory model selection.
Fig 2.16 Selecting Large memory model
Selecting the Code Rom Size and Operating System:
SMALL PROGRAM 2K or LESS:CALL and JMP instructions are coded as ACALL and AJMP. The maximum
program size may be 2 KBytes. The entire program must be allocated within the 2 Kbyte
program memory space.
Selecting Large Memory Model
Click Ok to Select andClose the window
-
8/3/2019 Exploring Keil
19/50
COMPACT 2K FUNCTIONS AND 64K PROGRAM:CALL instructions are coded as LCALL. JMP instructions are coded as AJMP
within a function. The size of a function must not exceed 2 KBytes. The entire program
may, however, comprise a maximum of 64 KBytes. The type of application determineswhether or not ROM (COMPACT) is more advantageous than ROM (LARGE). Any
code space saving advantages in using ROM (COMPACT) must be empirically
determined.
LARGE 64K PROGRAM:CALL and JMP instructions are coded as LCALL and LJMP. This allows you
to use the entire address space without any restrictions. Program size is limited to 64
KBytes. Function size is also limited to 64 KBytes.
Fig 2.17 Selecting Code Rom Size
Its better to choose 64K large program size. Since it contains no restrictions. Keil
evaluation version does not allow programming than 2k.
Full version keil contains the option to use large program size. Only the option
small can be used in evaluation version.
Fig 1.17 Shows the Selection of the Code ROM Size.
Selecting Code ROM size
Small pro ram, Compact or Lar e
-
8/3/2019 Exploring Keil
20/50
Setting up the operating system:
If RTX-51 (RTOS for 8051) operating system is used it can be selected. It can beeither full or RTX- TINY depending on the application we are going to develop.
If no operating system is used then choose the option none. This is the default
condition. Standalone development in 8051 requires no operating system. SO click none
in your target option. Fig 2.18 shows the selection of the operating system.
Fig 2.18 Selecting the Operating System
Selecting Operating System. Selectnone if no O eratin s stem is used
-
8/3/2019 Exploring Keil
21/50
CHAPTER3: CREATING AN APPLICATION IN KEIL
[This chapter discusses Creating a new project in Keil, Adding source files to target,
creating a Source code and Building the target]
------------------------------------------------------------------------------------------------------------
(I) Creating your own application in uVision2:
To create a new project in uVision2, you must:
1. Select Project - New Project.2. Select a directory and enter the name of the project file.3. Select Project - Select Device and select an 8051, 251, or C16x/ST10 device from
the Device Database.
4. Create source files to add to the project.5. Select Project - Targets, Groups, Files. Add/Files, select Source Group1, and add
the source files to the project.
6. Select Project - Options and set the tool options. Note when you select the targetdevice from the Device Database all special options are set automatically. You
typically only need to configure the memory map of your target hardware. Default
memory model settings are optimal for most applications.
7.
Select Project - Rebuild all target files or Build target.8. After building the target make sure the hex file is being generated9. For Selecting the Hex file generation option, Go to Options for the target, Output
and Select Create Hex file Option and then Click OK.
(II) Creating and Editing a Project:
1. Create a New Project:
Select the New Project command from the Project menu to open the New Projectdialog box.
Enter the name of the project in the Project Name box. Click browse to select the folder for the project. Click OK.
2. Add Targets to a Project
Open the Targets and Groups dialog box using the Targets and Groups commandin the Project menu.
Enter the name of the target to add in the Target to Add box. Click the Add button.
-
8/3/2019 Exploring Keil
22/50
3. Remove a Target from a Project
Open the Targets and Groups dialog box using the Targets and Groups commandin the Project menu.
Click on the desired target in the Available Targets box. Click the Remove Target button.
4. Select a Project Target
Open the Targets and Groups dialog box using the Targets and Groups commandin the Project menu.
Click on the desired target in the Available Targets box. Click the Set as Current Target button.
5. Add File Groups to a Project
Open the Targets and Groups dialog box using the Targets and Groups commandin the Project menu.
Click on the Groups tab. Enter the name of the new group in the Group to Add box. Click the Add button.
6. Remove a File Group from a Project
Open the Targets and Groups dialog box using the Targets and Groups commandin the Project menu.
Click on the Groups tab. Click on the desired group in the Available Groups box. Click the Remove Group button.
7. Add Source Files to a Project
To add source files from the Project window in the Project window, right-click on the
group to add source files to.
Select Add Files to Group from the drop-down menu that appears.To add source files from the Project menu, open the Targets and Groups dialog box using
the Targets and Groups command in the Project menu.
Click on the Groups tab. Click on the desired group in the Available Groups box. Click the Add Files to Group button.
-
8/3/2019 Exploring Keil
23/50
(III) Setting Project Options:
1. Set File Extensions for Source Files
Select the File Extensions command from the Project menu. Enter file extensions for each file type.
Each file extension specification must be prefixed with (*.). Multiple file extensions maybe separated with a semicolon (';'). For example: *.c; *.cpp.
2. Set Options for a Target
Projects support three levels of toolset options: Target options, Group options, and File
options. You set options for each level using the Project window.
To set target options from the Project window
In the Project window, right-click on the target to change. Select Options for Target from the drop-down menu that appears. To set target options from the Project menu In the Project window, click on the target to change.
Select the Options command from the Project menu.
Options for a target may be overridden by group or file options. A toolset include an assembler, compiler, linker, HEX converter, debugger, and
the other associated tools for a particular device family like the 8051, 251, or 166.
All of the tools or programs in a toolset are dedicated to generating target code for
a specific family of chips.
3. Set Options for a Group
Projects support three levels of toolset options: Target options, Group options, and Fileoptions. You set options for each level using the Project window.
To set group options from the Project window
In the Project window, right-click on the group to change. Select Options for Target from the drop-down menu that appears.
To set group options from the Project menu
In the Project window, click on the group to change. Select the Options command from the Project menu.
-
8/3/2019 Exploring Keil
24/50
4. Set Options for a File
Projects support three levels of toolset options: Target options, Group options, and Fileoptions. You set options for each level using the Project window.
To set file options from the Project window
In the Project window, right-click on the file to change. Select Options for File from the drop-down menu that appears.
To set file options from the Project menu
In the Project window, click on the file to change. Select the Options command from the Project menu.
(IV) Peripheral Simulation:
The uVision2 Debugger enables you to simulate the CPU and the on-chip peripherals of
most embedded devices. However, with more than 400 device derivatives, it is
impossible for the debugger and simulator to support all aspects and all on-chipperipherals of every device.
To discover which peripherals of a device are supported, in uVision2 select the Simulated
Peripherals item from the Help menu. You may also use the web-based Device Database.
We are constantly adding simulation support for on-chip peripherals so be sure to check
the on-line Device Database to find out if a particular device's peripherals are fully
simulated.
To create a new project:
Open keil uVision, Go to Project and Select new project. Fig 3.1 shows the
selection of the new project tool bar from the Project Wizard.
Fig 3.2 and Fig 3.3 shows how to create a project directory. Fig 3.4 shows how toenter the name of the project file.
Here the project file name is Hello. The project directory name is also Hello.After Saving the Project with the name Hello into the directory Hello, the software will
prompt for the selection of the device or the target we are going to use.
Fig 3.5 shows how to select the device for the target. In the example explained
below we are using AT89C52 microcontroller from the manufacturer ATMEL. After
selecting this device click ok.
-
8/3/2019 Exploring Keil
25/50
Fig 3.1 Selecting New Project Option
Creating a Directory:
Fig 3.2 Creating a New Directory
Select New Project for
Creatin a new Pro ect
Create a new directory
-
8/3/2019 Exploring Keil
26/50
Naming the directory as Hello
Fig 3.3 Naming the Directory as Hello
Creating a Project File into Hello Directory
Fig 3.4 Naming the project as Hello
Name the DirectoryAs Hello
Navigating into
Hello Directory
Name the Project as
Hello
After naming the project as
Hello Click save Button
-
8/3/2019 Exploring Keil
27/50
Fig 3.5 Selecting the Target Device
Creating a Source FileAfter creating a project file. The Project Work Space with Source Group appears
as shown in Fig 3.7.
Now we should create a new Source file in C and save it with a file name with
extension C or asm.
Asm is for assembly source file and Filename.c for C source file.
Fig 3.6 shows how to create a new Source file. Go to file option and select new.
An untitled file with the name Text1 will appear. Save that file as C file. Similarly the
new file can be crated by selecting the shortcut as shown in the fig 3.6.
Select device for theTar et
-
8/3/2019 Exploring Keil
28/50
Creating a new Source file:
Fig 3.6 Creating a new Source file
Saving the Source file as C file:
Fig 3.7 Shows the Project work space window. It contains the Target1 and theSource Group added with the target.
Now the Source file created by us should be saved with the extension C and then
it should be added to the Source group for creating the Source code for the Controller in
C language.
Creating a new Source
File from file menu
Shortcut for creating
a new source file
-
8/3/2019 Exploring Keil
29/50
Fig 3.7 Saving the Source file as C file
Writing the Source code for the Controller operation in C:
The first step in writing the Source code in C is adding the header file for the
target device selected. The header file contains the declarations for that particular
controller.
The SFR and other registers associated with the controller and the memory arealocation for that registers.
For the target selected in our example the header file is #include .
It is common for C series and S series Controllers from Atmel. That is why it isrepresented by X in the header file name.
We can also use the Common header file for 8051 series Controller as #include
.
The selection of the header file for the target device is quite simple. Just rightClick in the Source file and select the header file name associated with the target. This is
shown in the fig 3.8.
Source Group added to
The Tar et b default
Target to which the
Pro ect is developed
Save the Source
File as C
Click Save for
Savin the file
-
8/3/2019 Exploring Keil
30/50
Header File Selection:
Fig 3.8 Selection of the header File
Writing the Source code to print Hello World to the Serial Port:
The example Source code is to print the String Hello World to the Serial Port of
the PC by the microcontroller UART.
The printf function used in the source code is declared in the stdio.h header. So
include the header.
Then it is necessary to write a function to initialize the uart of the Controller for
serial communication. That is done in the inituart () function.
We need to set the timer 1 in 8 bit auto reload mode, Serial Control register in 8
bit receive and REN enabled mode.
Set 9600 baud rate and Run the timer flag. Make sure the TI and RI bit set to 1
for using the Printf statement.
Void main()
{
inituart ();
while (1){
printf (Hello World\n);
}
}
Right Click on the SourceFile Main.c and select the header
-
8/3/2019 Exploring Keil
31/50
Void inituart()
{
TMOD=0x20; // 8 bit auto reload mode for timer 0SCON=0x50; // REN enabled, 8 bit TX/RX mode
TH1=0xfd; // 9600 baud rate
TR1 = 1; // Run the Timer
TI=1; // Set TI flag for Transmission to be started
}
Fig 3.9 Source code to Print Hello World.
Adding/ Removing the C file from the source group:
After Writing the Source code for the Controller, then add the main.c source file
to the Source group.
Right Click on the Source group in the Project window and select Add files to theSource Group. Now Select the directory in which the Source file is present and select the
file we want to add.
Here main.c is the file to be added to the source code. After selecting the fileClick add and Close the Add files to the Source group window. To remove a file select
remove file from the source group.
These are clearly illustrated in the fig 3.10 and fig 3.11.
Add the stdio.h file for the
printf function
-
8/3/2019 Exploring Keil
32/50
Fig 3.10 Add files to the source group
Fig 3.11 Adding main.c to the source group
Add files to the Source Group1
Right Click on Source Group1
Select main.c file from
the directory
Click add to add the file
Close the Add files Window
Remove file from Source Group1
-
8/3/2019 Exploring Keil
33/50
Fig 3.12 shows that the Project file main.c added to the source group1. After
Adding the Source file to the Group. Build the target using the Build option as shown inthe Fig 3.12.
Fig 3.12. Main.c added to the Source Group
Fig 3.13 Syntax Error due to missing semicolon
Main.c Added To the
Source Grou
Build the Target to
create the Hex file
Missing Semicolon
Build the Target tocreate the Hex fileCorrect the Semicolon
-
8/3/2019 Exploring Keil
34/50
The Build output shows the errors and warnings if any in the source code. In the
example taken here if you compile the source code, the output has the syntax error asshown in fig 3.13. This is because, in the printf statement, the semicolon is missing.
Correct the error and build it again.
Fig 3.14 Missing Function Prototype
Once again the error missing function prototype for inituart() function occurs. We
have to declare the function inituart if the function is called below the main function.
Now the target should be build again. This is shown in fig 3.14
In C if any function is written below the main function it is required to declare the
function prototype below the header file declaration.
If the functions are written above the main file it is not necessary to declare thefunctions. Here the inituart () function is called as a void function below the main
function. So it is to be declared as shown in the fig 3.15
No Functionprototype
Missing Function
Prototype Error
-
8/3/2019 Exploring Keil
35/50
Fig 3.15 Function Prototype declaration for inituart ()
Fig 3.16 Selecting create hex file
Function prototypeFor inituart function
Select Create Hex file
Click OK to Confirm
the option
-
8/3/2019 Exploring Keil
36/50
After adding the source file, make sure the hex file generation is selected. If it is
not selected, Go to Target and right click on the target.
Select the options for the target and Click output. In the output window selectcreate hex file and click ok. This is illustrated in the Fig 13.16.
After Selecting create hex file option, correcting the source errors, compile the
source code again. Now you will see the hex file being generated by the Software.
Hello.hex is the hex file generated here. The hex file is usually the Project file name. Thisis illustrated in fig 3.17
Fig 3.17 View hex file being created
Until now we have seen adding a single source file to the Source group. We canadd one or more source files to the group and we call functions in another source file
from some other source file or the main source file.
For that we should create another c file and add to the source group. Here anexample is illustrated with name.c as the file name. We are calling a function printname()
in the name.c file from main.c
In the main function printname() is called which is a function written in another
source file. But the function prototype declaration for printname is not given. Also
Hex File Hello.hex isCreated
-
8/3/2019 Exploring Keil
37/50
Missing function prototype for printf occurs. This is due to missing stdio.h header. These
errors are shown in the fig 3.18
Fig 3.18 Adding name.c and printname function
Now write a function printname in name.c as shown in the fig3.19. Here printf is
used, so it is necessary to add the stdio.h header to the file name.c as shown
Then it is also necessary to add the function prototype for the function
printname() in the main.c file as shown in fig 3.20.
This is one way of linking more than one c file together. Other way is adding only
the main.c file to the source group and calling other files using the function prototype
declaration such as #include name.c as shown in the fig 3.21.
We can add n number of source files by declaring the file in the main file asprototype. This is similar to adding the header file in the main file.
Calling printname ( )
function in name.cAdding name.c file
-
8/3/2019 Exploring Keil
38/50
Fig 3.19 Adding stdio.h header to name.c
Fig 3.20 Adding printname () prototype declaration in main.c
Stdio.h header
added to name.c
Printname()
function rotot e
-
8/3/2019 Exploring Keil
39/50
Fig 3.21 Adding printname () prototype declaration in main.c
With this ends our project creation and compilation. Then it is very important
what our source represents? What are the functions and variables used in the sources?
What are the source files used in the project? What are all the hardware used and how it
is linked with the software? What our application is?
These can be made easily understandable to the other developers of your project
only by perfect documentation. We can also add documentation to the Source group.
The document file is usually the text file. So it can be saved as txt. It cannot beadded to the source group1 because source group1 completely involves C and assembly
files, i.e the source code files. In which is you add text file it affects the compilation and
so error occurs.
So we can create another source group to the target 1. Here we can name it as
documentation and we can add the text file document to the source group.
Right Click on the target 1 and select the target, Groups and files option as shown
in fig 3.22
Adding sub files to main
files as func header
-
8/3/2019 Exploring Keil
40/50
Fig 3.22 Target Group Selection
Fig 3.23 Add a group to the target
Right Click Target 1 and
Select Target, Groups
Type a Source group name
as Document or some name
Click ADD to add
files to group
-
8/3/2019 Exploring Keil
41/50
As shown in fig 3.23 and fig 3.24 we can add a group to the target. Here we are
adding the group document and document 1 to the target. Already the Source group1 isthe group available in the target.
Fig 3.24 Add a group Document1 to the target
Fig 3.25 Add a file to the Group Document
Now the Group named document and document1 are added to the target along
with the source group1. Now the developer can add the document file hello.txt to thedocument group.
The Group name used here
is document1
The Group Document
is already created
Click OK to Close this
Window now
Right Click on the group
Document and Select Add
files to the Group Document
-
8/3/2019 Exploring Keil
42/50
The document file is any text file which can be in any directory location. It is
wise to have the text file in the Project directory itself. The project directory is hello here.
For that navigate to the Project directory, create a new text file, and Rename it ashello.txt. Type the Documentation part in that file and close the file.
After doing all these things add the hello.txt to the document group as shown in
fig 3.25 and fig 3.26. Right Click on the Document group and select Add files to the
Group Document. This is illustrated in the fig 3.25.
Fig 3.27 Shows How to type the Document Content in the Text File
Fig 3.26 Add a file hello.txt to the Group Document
Fig 3.27 Creation of documentation file related to the project
Select the file Hello.txt
Click Add to add the
File to group Document
Documentation about
The pro ect
-
8/3/2019 Exploring Keil
43/50
CHAPTER4: DEBUGGING AN APPLICATION IN KEIL
[This chapter discusses Debugging a Project using the debugger in keil, Viewing the
output using the simulator and source code development]
------------------------------------------------------------------------------------------
Debugging an Application in uVision2
To debug an application created using uVision2, you must:
1. Select Debug - Start/Stop Debug Session.2. Use the Step toolbar buttons to single-step through your program. You may enter
G, main in the Output Window to execute to the main C function.3. Open the peripherals such as timer and serial window from the peripherals menu.4. Open the Serial Window using the Serial #1 button on the toolbar.5. Debug your program using standard options like Step, Go, Break, and so on.
Debugging
Preparing Targets for Debugging
The debugger integrated into uVision2 supports symbolic, source-level debugging. The
compiler and assembler include debug information in the generated object files when youenable debug information.
Enable Debug Information
To include debug information from the Project window
Right-click on the desired target in the Project window. Select Options for Target from the drop-down menu that appears. Select the Output tab in the Options dialog box. Select the Debug Information check box.
To include debug information from the Project menu
Click on the desired target in the Project window. Select the Options for Target command from the Project menu. Select the Output tab in the Options dialog box. Select the Debug Information check box.
-
8/3/2019 Exploring Keil
44/50
To Start With Start/ Debug Session:
Build the project with the source files for no errors and warnings. Open theStart/Debug Session from the Debug menu. Ctrl F5 is the shortcut for starting or stopping
the debug session. After Starting the debug session, open all the peripherals related to the
project such as the Serial Window, I/O ports and the Timers from the peripherals menu.
The peripheral menu also contains the reset CPU option. The reset CPU option
can be used when the user need to start the debug process from the first line of the
program. Opening the debug session is shown in the fig 4.1.Peripherals options areexplained in fig 4.4 and fig 4.5.
Fig 4.1 Selecting and opening a debug session
If the Shortcut for debug window is not selected, select it from the view menu.
The serial Window to view the output to the serial port, Watch and Call stack window,Memory window, Disassembly window and other Windows can be selected from the
view menu. The status bar, Build tool bar, Project window, Output window also can be
selected from the view menu.
These are explained clearly in the fig 4.2 shown below. Then the debuggingprocess can be started by single step execution using STEP INTO option in the debug
window. In case the user wants to debug a function and not required to debug the
program line by line, they can use the step over function. The Program can be run with
the Go function. These options for debug can be selected from the shortcut also incase the
debug toolbar is enabled. These are explained in fig4.3
Start / Stop Debug Session
-
8/3/2019 Exploring Keil
45/50
Fig 4.2 Selecting view options
Fig 4.3 Selecting debug options
Select Debug Toolbar
Step Into
F11 - Shortcut
Step Over
F10 - Shortcut
Go for Run
F5 - Shortcut
Go for Run
F5 - Shortcut
Watch and Call Stack
Window selectionSerial Window selection
-
8/3/2019 Exploring Keil
46/50
Fig 4.4 Serial Window peripheral
Fig 4.5 Timer peripheral
Choose Timer1
From peripheral
Choose Serial
Window From peripheral
-
8/3/2019 Exploring Keil
47/50
Fig 4.6 shows the serial channel, Timer/ Counter1, Step into shortcut option in the debug
window, register Window and the other debug windows facilitating the debug option.
Fig 4.6 Debug Windows and peripherals (a)
Fig 4.7 shows the values moving into the Timer Control registers, Serial Control
register, Serial buffer register, TI and RI flags related to the serial communication. Timer
mode selection and the debug pointer position movement step by step.
The output of the printf function is printing a string in the serial window, so theuser needs to open the serial window1 and then click on the Go function from the debug
menu.
Fig 4.8 shows the selection of the serial window and disassembly selection
window. The disassembly window is the window showing the assembly code and theaddress where the opcodes related to the C source code developed by the user.
The disassembly window and the register window are shown in the fig 4.9 clearly.
The user can view the data moving into the registers as shown in the figure 4.9.
Register Window
Timer1Serial Channel
Step into shortcut
-
8/3/2019 Exploring Keil
48/50
Fig 4.7 Debug Windows and peripherals (b)
Fig 4.8 Serial and disassembly windows selection
Timer18 bit auto reload
TH1=0xfd
As iven in pro ramTMOD=0x20
As iven in pro ram
SCON=0x50
As iven in pro ram
Debug Pointer position
Debug point in printf line
Disassembly window
Selection
Serial window
Selection
-
8/3/2019 Exploring Keil
49/50
Fig 4.9 Disassembly window and register window
The figure 4.10 shows the Serial window, Hello World and the My name String
being print in the serial port. It is being simulated to the user in the serial window.
The serial Window is the window representing the serial port of the PC. It is the
exact replacement of the Hyperterminal in the Windows OS.
Hyperterminal is the Communication terminal of the COM ports in the PC. Sowhat ever data sent to the serial port of the PC can be simulated in the Serial Window of
the Keil debugger.
The fig 4.10 also Shows the Watch and Call Stack window. It shows the values of
the Local variables and the values moved to the variables if a function is called. At any
cost the global variables cannot be viewed in the Watch and Call Stack Window.
Disassembly window Debug tool
pointer pointing printname
Function
Register Values in r1
-
8/3/2019 Exploring Keil
50/50
Fig 4.10 Serial window and watch and call Stack window.
Hello World and My namerintin in the serial window
Watch and CallStack Window
Serial Window 1