HOW TO WRITE FIRST PROGRAM IN KEIL FOR 8051



HOW TO WRITE FIRST PROGRAM IN KEIL FOR 8051: This article deals with the programming related 8051 microcontroller. Atmel 8051 microcontroller needs Keil software for its programming. Programming can be done in C language or assembly language. Now we will discuss here that how to write first program in Keil software for 8051 microcontroller and converting it in hex file.



Steps to use Keil 

Firstly we will download Keil µVision 3 setup and install it by following the guidelines of installation. That is not a big issue, since user manual guide is available with the software package. Start the KeilµVision software and follow these steps.

  • For any new project, click on Project>> New µVision Project.New µVision Project Keil
  • A new window will appear on screen which will prompt for project name and directory.Give a name to your project or also change the directory to specific folder where you want to collect all your work. Here, I have given the project name as “First Program”.
  • When you click on save button, a new window will appear naming “Select Device for Target ‘Target 1’”. Now we have to select specific microcontroller. Here, we have to use 8051, which belong to Atmel family, so double click on Atmel. All Atmel microcontrollers will get available. Select AT89C51 and then click on OK.New device selection in keil
  • When you click on OK, another window will appear which will ask for “Copy Standard 8051 Startup Code to Project Folder and Add File to Project?” Click on YES to proceed further.Keil add file to project
  • If we see on left side of screen i-e, in ‘Project workspace’ you can see ‘STARTUP.A51’ file. It is the file which contains the assembly language commands of 8051 microcontroller. Leave it just as it is, because we have to first set option values for target1.Keil project workspace
  • To configure option value of our microcontroller project. Select the Target 1, click on Project>>Option for target ‘target 1’”.
  • Select Target tab to configure microcontroller.Change the crystal value from 24 MHz to 12 MHz and go to output tab.microcontroller configuration in Keil
  • In Output tab, check the “create HEX file” option and then click OK.HEX file in Keil
  • Now click on File << New.Or simply create new file. Blank text file will open and we have to write our code in it. After writing the code, save this file and the most important thing is to give extension of .c or .asm (according to the language used for program) in file name.Header file in Keil

Right click the target and select manage components.From the Project component tab >> select STARTUP.A51 >> click add files.



  • Add saved file and close the components window by clicking OK.
  • Now we are again on the code. Just click on the “Rebuild all target files” from tool bars. We can also choose Project << Rebuild all target files. By clicking this button our program will start compilation and give the compilation results at bottom output window. If our code is correct then there will be no errors.

Now the hex file is created in the specified folder with “.hex” extension. We can burn it in microcontroller through Fly pro software and burner circuit.

WRITING PROGRAM in Keil for 8051

We have to first add the header file to our program. This will help us to access registers related peripherals. To place the correct header file just right click on the text file screen and insert header file in it.All the four ports P0, P1, P2 and P3 of 8051 microcontroller have 8 pins which make them 8-bit ports. All the ports upon RESET are configured as inputs.

  • When the PORT is to be used as output port, 0 is given to that port.
  • To configure PORT as an input, 1 must be sent to thatport.

write your first code in Keil 

Here in this example, I have written a simple program showing blinking of an LED using 8051 microcontroller. An external crystal is attached to the pin 18 and 19 of microcontroller. The frequency of oscillator crystal should be set to 11.059MHz. I have used PORT 2 as an output port, so 0 is given to that specific PORT pin. After that, led output is toggled. This LED will blink continuously when we use a “while loop”. We can make the delay function separately, so whenever this function is called, it will create a delay of almost 1 second in the output.

WOKING of LED blinking with circuit and code

Code is checked on proteus by making a small circuit. Hex file is burned into microcontroller. When the microcontroller is given proper supply voltages, led will start blinking with a delay of 1 sec. Delay can be changed by setting the values in “for loops”.

Video lecture on how to use Keil for 8051

PROTEUS SIMULATION of LED Blinking

LED blinking in keil with 8051 microcontroller

CODE for 8051 in Keil

#include<reg51.h>

sbit led=P2^0;                   // led at PORT 2 pin 0

void Delay(void);              // Delay function declaration




void main ()                        // main function

{

led=0;                                   //output PORT pin




while(1)                               // infinite loop

    {

led = 1;                 // LED ON

Delay();

led = 0;                 // LED OFF

Delay();

    }

}

void Delay(void)               // delay function

{

int j;

inti;

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

    {

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

        {

        }

    }

}

So this all about how to use Keil for 8051 microcontroller first time and how to write your first program for 8051 microcontroller using keil programming software.



Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,395 other subscribers