Sunday, 5 February 2012

Tutorial for kiel comipler







Download links:


for keil:   from kiel(official website)

               from rapidshare
 
1. Introduction to kiel    

Machines can not understand human-languages and this give rise to the requirement of compilers. The format which can be directly understood by 8051 is the HEX format. So if you won’t your 8051 to do something you must have a Hex file. But what about humans? Humans also don’t understand language of machines. So we humans write the program in some higher level languages such as C, JAVA etc. we can be understood by us then we use a compiler and convert it into a machine language.  Other option is we can write our program in assembly language of 8051 (which is also human understandable) and then by using an assembler we can turn it into the machine language.

What is keil??
     Original name of the software is keil micro vision.  Kiel is Germany based software Development Company which provides many tools. Kiel provide following tools for 8051 development:
1.     C51 Optimizing C Cross Compiler,
2.     A51 Macro Assembler,
3.     8051 Utilities (linker, object file converter, library manager),
4.     Source-Level Debugger/Simulator,
5.     µVision for Windows Integrated Development Environment.
But the question still continues what is kiel??So kiel is basically a cross compiler .so let’s understand compiler and cross compiler.

Compilers:
        Compilers are programs used to convert a High Level Language to object code (machine code). Desktop compilers produce an output object code for the underlying microprocessor, but not for other microprocessors. I.e. the programs written in one of the HLL like ‘C’ will compile the code to run on the system for a particular processor like x86 (the processor which is inside our computer). For example TURBO C is very widely used compiler for c language.Diffrent programming languages have different compiler. 
But keep in mind that a compiler refers to a higher level languages such as C,pythone,pascal,JAVA etc. where  as assembler refers to the assembly language which is a low level language(not machine language).

Cross compiler:
        So the definition of cross compiler is a compiler that runs on one computer (x86 processor) but produces object code for a different type of computer (8051 and  derivatives). Cross compilers are used to generate software that can run on computers with a new architecture or on special-purpose devices that cannot host their own compilers. Cross compilers are very popular for embedded development, where the target probably couldn't run a compiler. Typically an embedded platform has restricted RAM, no hard disk, and limited I/O capability. Code can be edited and compiled on a fast host machine (such as a PC or Unix workstation) and the resulting executable code can then be downloaded to the target to be tested. Cross compilers are beneficial whenever the host machine has more resources (memory, disk, I/O etc) than the target.  Keil C Compiler is one such compiler that supports a huge number of host and target (including 8051, avr, pic, arm and many more) combinations.

2. Starting a new project in kiel:  
  1)   So, before starting with kiel I recommend you to make a folder in which you gonna  store your entire work because working with kiel will generate number of files like object file, BAK file etc. Now here we will see how to write the program in C and assembly, how to generate HEX file. In this case we are making a folder called ‘blink’. Because our program is about blinking the LEDs connected to port 0.

  
2)  Now open keil software by clicking on start menu then program and then select keil3 (or any other version). I am using kiel3. Following window will appear on your screen. If you have just installed kiel then you will find hello program on your screen.
  3)  Now to begin our blinking LED project. Click on new micro vision project.
 
 4)  And then you will be asked to provide a folder for project and project name .We will use ‘blink’ folder which we have made before and will  use ‘first’ as project name.
 5) Now you will be asked to select chip and chip manufacturer .here I am using AT89S52 so. first select ATMEL and then click on (+) pulse sign near to it so it will expand and then select AT89S52.

 6) Now you will asked to add 8051 startup code to your project. .I doesn’t know about this 8051 startup code but if you click ‘no’ it won’t make any difference. And if you are using assembly language for programming you must click ‘no’ if you won’t to avoid some warnings. But to match up with many other tutorials on net we will click ‘yes’.

    
 7) Now you will get following screen on your computer. Click on the pulse (+) sign near to Target1 so it will expand. By double clicking on STARTUP.A51 you can read that file.  

3. Creating a source file in C
  8) Click on file ->new to create new file. Now you will have a blank screen in front of you. You can start writing from here. 


9)  Now write the program in the blank window. Consider we are using C language here.

program: 

// Program to blink the LEDs. LEDs are connected to port 0.

#include<reg51.h>

void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}

void main()
{
while(1)
{
P0=0x00;
delay(50);
P0=0xff;
delay(50);
}
}

10) After completing your program click on file->save as to save your project. Write ‘first.c’ in file name. We are using .c extension. In assembly you can use .a51 or .asm .now you will find coloring text on screen.




   
4. Adding source file to the project

11)      Now we have created a file called “first.c” .But still we have to add this file to our project. To add this file to our project, expand the Target1 tree menu by clicking on + sign. Right  click on ‘Source group 1’ and select  ‘Add Files to Group’ .Then give file name ‘first.c’ and close the window.Remeber it is not necessary that you write entire code and then add file to project you can just add a blank file to project and then start writing the program it won’t matter .


      12)  Now you will find first.c under ‘Source group 1’ means “first.c” is added to our project.  
                                                                   
13)   Now it’s time to debug our program .so click on the” Build Target” from project menu or click on Build Target button .You will find some activity in “output window”. At the end of building process you must get message “0 Errors (s), 0 Warnings”. If your programs have any error kiel will give “Target not created”. You have to find all errors and correct it. You can avoid warnings but you must have proper reason why to avoid them. After this every time you edit your program you can just click on ” rebuild all Target files”





5. Generating HEX file from C file
       14) OK every thing went fine but where is HEX file. We do not have HEX file. To create HEX file click directly on “option for target” button or from the project menu select “option for target ’target1’ ”.You will find the screen as shown bellow .All you have do is in the ‘output’ bar select “create HEX file” check box and in the “Target” bar fill Xtal speed for your controller. I am using 12 MHz. Not necessary to do more. Click OK.




    15) Now click on “rebuild all target files” button. You will find message “creating HEX file from ‘first’.” In the output window.

      16) That’s all. Go back to the Blink folder you will find “first.HEX” file there open it .This is what your controller understand. Our HEX file is ready to be burned in our controller.



Same thing with assembly language
      Create a new kiel project as described above and then write the code below(note:in assembly when asked about 8051 startup code please select “NO” if you want avoid number of warnings):

Program
 ;MAIN PROGRAME
MOV PSW,#00H
L1:MOV P0,#00H
ACALL DELAY
MOV P0,#0FFH
ACALL DELAY
SJMP L1

;DELAY SUBROUTINE
DELAY:MOV R0,#200  ;PROVIDING DELAY IN MILLISECONDS
BACK:MOV DPTR,#1275
     MOV R1,DPL
L2:  DJNZ R1,L2
     MOV R1,DPH
L3:  DJNZ R1,L3
     DJNZ R0,BACK
     RET
     END  

Store the file as “first.a51” or “first.asm”. Then add file to project and the remaining thing is as same as C.


7. Is it over?
            
              No it is not over. We have seen very basic operation of kiel. But Kiel is very good stimulator too. Using kiel we can analyze resistors of controller, what output we get when we give specific input, we can monitor specific variable in our program by adding watches, also by using logic analyzer we can see the original signals. For all this please visit our blog. 
 









3 comments: