Sei sulla pagina 1di 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 Program When its time to write the program that controls your project, the options include using machine code, assembly language, or a higher-level language. Which programming language 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 Downloader The 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 have to 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. Now the microcontroller runs as you programmed. The device used for downloading the hex file is called the downloader or programmer. Assembly language One step removed from machine code is assembly language, where abbreviations called mnemonics (memory aids) substitute for the machine codes. The mnemonics are easier 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 desktop computer and translates the mnemonics into machine code. Most assemblers provide other 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 languages A 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

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 to another. 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 many different 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 code immediately 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 machine code 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 language also 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 also available. 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 Debugging 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. The process of ferreting out and correcting mistakes is called debugging. Easy debugging and troubleshooting can make 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 EPROM One 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.

Development systems Another 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 of the 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 critical spots in your program. At any breakpoint, you can view or change the contents of memory or perform other tests. Simulators Another 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 program location 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. Emulators An 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 at each 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-IDE development system The 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 KeilIDE system. The keil contains the A51 assembler, C51 compiler and BL51 linker to translate the assembly or C source code into INTEL hex file.

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 hex file, 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 only the 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 tools suite 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 project manger. 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 powerful debugger 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.

(II)How to Start with Keil


To start with keil we have the only option write a source code in assembly or C and convert 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 when writing 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 Project 2. Navigate to the Project file location 3. For Example; C:\Keil\C51\Examples\Hello.UV2 4. Select Hello.UV2 and Click open 5. Select options for target and select output option 6. Select the create hex option and click ok 7. To activate Shortcut, Go to View and enable the build target toolbar 8. 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 option 11. Selecting memory model for the target 12. 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

Double click to open project

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.

Opening the Existing Project Using the Project Manager

Fig 2.2 Opening an existing project from project manager

Navigating to the Project file location After clicking the open project from the project manager, the navigation toolbar for 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.

Navigate to the Project File location

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 example program 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 keil folder. 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

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 MCBx51 evaluation board. The DES project is the implementation of Cryptographic algorithm in 8 bit 8051 microcontroller. The CSAMPLE program implements a simple addition and subtraction calculator. 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

Select Keil Folder

Fig 2.4 Selecting the keil Folder

Opening a Hello Project from the keil examples: Selecting and opening the Hello project from the keil example projects. It is not necessary 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.

Select Hello.UV2

Open Hello.UV2

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.

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.

Options for the target

Fig 2.6 Selecting options for the target

Click Ok to enable hex File Generation

Select Create hex file

Fig 2.7 Enable Create hex file option In the listing option we can enable the list file and assembly file generation. We can 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.

Options for the Source Group

Fig 2.8 Selecting options for the Source Group.

Click OK to Select all the Options

Set Options such as generate Assembler SRC file, Assemble SRC file and other Options

Fig 2.9 Setting options for the Source group

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.

Select device for the target

Fig 2.10 Select device for the target

Select Manufacturer And the device

Click OK to make the Atmel AT89C52 selected

Fig 2.11 Selecting 89C52 Controller from Atmel

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 Manufacturer and 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 terms build, rebuild and Compile means the same, making an Intel hex file.

Rebuild all target files From Shortcut Button

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 option and 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

option. Rebuilding the targets from the project manager and the project workspace window are depicted in the figure 2.13.

Rebuild all target files From project manager

Fig 2.13 Rebuilding the target from project manager

Rebuild target from Project Workspace

Fig 2.14 Rebuilding the target from project workspace

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 model In 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 because the 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.

Small variables in data Ensures very efficient memory Usage for the microcontroller

Fig 2.14 Selecting small memory model Compact Model Using 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 registers R0 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.

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 registers and 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) is provided 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.

Selecting Compact variables In PDATA

Fig 2.15 Selecting Compact memory model

STARTUP.A51 The 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 memory 2. 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

7. Initializes the 8051 hardware stack pointer 8. Transfers control to the main C function Large Model In 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.

Selecting Large Memory Model

Click Ok to Select and Close the window

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.

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 determines whether 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.

Selecting Code ROM size Small program, Compact or Large

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.

Setting up the operating system: If RTX-51 (RTOS for 8051) operating system is used it can be selected. It can be either 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.

Selecting Operating System. Select none if no Operating system is used

Fig 2.18 Selecting the Operating System

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 target device 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 generated 9. 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 Project dialog 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 command in the Project menu. Enter the name of the target to add in the Target to Add box. Click the Add button.

3. Remove a Target from a Project Open the Targets and Groups dialog box using the Targets and Groups command in 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 command in 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 command in 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 command in 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.

(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 may be 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 File options. 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.

4. Set Options for a File 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 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-chip peripherals 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 to enter 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.

Select New Project for Creating a new Project

Fig 3.1 Selecting New Project Option Creating a Directory:

Create a new directory

Fig 3.2 Creating a New Directory

Naming the directory as Hello

Name the Directory As Hello

Fig 3.3 Naming the Directory as Hello

Creating a Project File into Hello Directory

After naming the project as Hello Click save Button Name the Project as Hello

Navigating into Hello Directory

Fig 3.4 Naming the project as Hello

Select device for the Target

Fig 3.5 Selecting the Target Device Creating a Source File After 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.

Creating a new Source file:

Shortcut for creating a new source file

Creating a new Source File from file menu

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 the Source 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.

Source Group added to The Target by default Save the Source File as C

Target to which the Project is developed

Click Save for Saving the file

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 area location for that registers. For the target selected in our example the header file is #include <AT89X52.H>. It is common for C series and S series Controllers from Atmel. That is why it is represented by X in the header file name. We can also use the Common header file for 8051 series Controller as #include <reg51.H>. The selection of the header file for the target device is quite simple. Just right Click in the Source file and select the header file name associated with the target. This is shown in the fig 3.8.

Header File Selection:

Right Click on the Source File Main.c and select the header

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); } }

Void inituart() { TMOD=0x20; // 8 bit auto reload mode for timer 0 SCON=0x50; // REN enabled, 8 bit TX/RX mode TH1=0xfd; TR1 = 1; TI=1; } // 9600 baud rate // Run the Timer // Set TI flag for Transmission to be started

Add the stdio.h file for the printf function

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 the Source 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 file Click 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.

Right Click on Source Group1

Add files to the Source Group1

Remove file from Source Group1

Fig 3.10 Add files to the source group

Select main.c file from the directory

Click add to add the file Close the Add files Window Fig 3.11 Adding main.c to the source group

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 in the Fig 3.12.

Build the Target to create the Hex file

Missing Semicolon

Main.c Added To the Source Group

Fig 3.12. Main.c added to the Source Group

Correct the Semicolon

Build the Target to create the Hex file

Fig 3.13 Syntax Error due to missing semicolon

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 as shown in fig 3.13. This is because, in the printf statement, the semicolon is missing. Correct the error and build it again.

Missing Function Prototype Error

No Function prototype

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 the functions. 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

Function prototype For inituart () function

Fig 3.15 Function Prototype declaration for inituart ()

Select Create Hex file

Click OK to Confirm the option

Fig 3.16 Selecting create hex file

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 select create 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. This is illustrated in fig 3.17

Hex File Hello.hex is Created

Fig 3.17 View hex file being created Until now we have seen adding a single source file to the Source group. We can add 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 an example 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

Missing function prototype for printf occurs. This is due to missing stdio.h header. These errors are shown in the fig 3.18

Adding name.c file

Calling printname ( ) function in name.c

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 as prototype. This is similar to adding the header file in the main file.

Stdio.h header added to name.c

Fig 3.19 Adding stdio.h header to name.c

Printname() function prototype

Fig 3.20 Adding printname () prototype declaration in main.c

Adding sub files to main files as func header

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 be added 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

Right Click Target 1 and Select Target, Groups

Fig 3.22 Target Group Selection

Click ADD to add files to group Type a Source group name as Document or some name

Fig 3.23 Add a group to the target

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 is the group available in the target.

The Group Document is already created

Click OK to Close this Window now

The Group name used here is document1

Fig 3.24 Add a group Document1 to the target

Right Click on the group Document and Select Add files to the Group Document

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 the document group.

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 as hello.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 Select the file Hello.txt

Click Add to add the File to group Document

Fig 3.26 Add a file hello.txt to the Group Document

Documentation about The project

Fig 3.27 Creation of documentation file related to the project

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 you enable 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.

To Start With Start/ Debug Session: Build the project with the source files for no errors and warnings. Open the Start/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 are explained in fig 4.4 and fig 4.5.

Start / Stop Debug Session

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 debugging process 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

Select Debug Toolbar

Watch and Call Stack Window selection Serial Window selection

Fig 4.2 Selecting view options

Go for Run (F5) - Shortcut Step Into (F11) - Shortcut

Step Over (F10) - Shortcut Go for Run (F5) - Shortcut

Fig 4.3 Selecting debug options

Choose Serial Window From peripheral

Fig 4.4 Serial Window peripheral

Choose Timer1 From peripheral

Fig 4.5 Timer peripheral

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.

Step into shortcut

Register Window Serial Channel Timer1

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 the user 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 the address 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.

Timer1 8 bit auto reload

Debug Pointer position

SCON=0x50 As given in program

TH1=0xfd As given in program

TMOD=0x20 As given in program

Fig 4.7 Debug Windows and peripherals (b)

Disassembly window Selection

Serial window Selection Debug point in printf line

Fig 4.8 Serial and disassembly windows selection

Disassembly window Debug tool pointer pointing printname Function

Register Values in r1

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. So what 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.

Serial Window 1

Hello World and My name printing in the serial window

Watch and Call Stack Window

Fig 4.10 Serial window and watch and call Stack window.

Potrebbero piacerti anche