How to Write First Program in KEIL IDE for 8051

In this article, we will learn to setup KEIL IDE to program the 8051 microcontroller. Hobbyists and developers use Atmel 8051 microcontrollers in IOT projects. These microcontrollers require KEIL IDE software for their programming. The program can be written in C or assembly language. Now we will discuss how to write the first program in the KEIL IDE software for the 8051 microcontroller and convert it to a hex file.

For downloading the software, visit the KEIL website by clicking here.

Download KEIL IDE Setup

Firstly, we will download the KEIL µVision 3 setup from the KEIL website. Then we will install it by following the guidelines provided by KEIL. The installation is quite simple, as the user manual and setup guide are included with the software package.

Steps to Use KEIL IDE

Start the KEIL µVision software and follow these steps.

Starting a New Project

  • For a new project, click on Project >> New µVision Project.
New µVision Project Keil
  • A new window will appear on screen, which will prompt for the project name and directory. Here, we will select the name and the path of our project (where we need to store the data). In our case, the project name is “First Program”.

Selecting Microcontroller

  • When we click on the Save button, a new window will appear with the title “Select Device for Target ‘Target 1′“. Here, we have to select the specific microcontroller we are going to use in our project. We are going to use 8051 for this tutorial, which belongs to the Atmel family, so we will double-click on Atmel. A list of all Atmel microcontrollers will be displayed. Select AT89C51 and then click “OK”.
New device selection in keil
  • When we click on OK, a window pops up with the title “Copy Standard 8051 Startup Code to Project Folder and Add File to Project?”. Click “Yes” to proceed further.
Keil add file to project
  • If we look on the left side of the screen, i.e., in the Project workspace, we can see the “STARTUP.A51” file. It is the file that contains the assembly language commands for the 8051 microcontroller. Leave it just as it is because we have to first set option values for Target 1.
Keil project workspace

Configuring the Microcontroller

  • To configure the option value of our microcontroller project. Select Target 1, click on Project >> “Option for Target ‘Target 1′”.
  • Select the Target tab to configure the microcontroller. Change the crystal value from 24 MHz to 12 MHz and go to the output tab.
microcontroller configuration
  • In the Output tab, check the “create HEX file” option and then click “OK”.
HEX file

Creating File

  • Now click on File >> New to create a new file. A 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 the file an extension of .c or.asm (according to the language used for the program) in the file name.
Header file

Right-click the target and select “Manage Components“. From the Project Component tab, select STARTUP.A51 >> click Add Files.

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

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

Writing Program – 8051 KEIL IDE

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

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

Proteus Schematic

In this schematic, we have connected an 8051 microcontroller to an LED. Firstly, we will attach An external crystal to pins 18 and 19 of the microcontroller. The frequency setting of the crystal oscillator needs to be 11.059 MHz. We have used PORT 2 as an output port, so 0 is given to that specific PORT pin. We have tested the code provided below on Proteus by making the following circuit:

LED blinking in keil with 8051 microcontroller

8051 Example Code in KEIL IDE

We have written a simple program to blink an LED. In this program, we toggle the LED’s output. This toggling LED will blink continuously, as we used a “while loop”. We can make the delay function separately, so whenever we call this function, it will create a delay of almost 1 second in the output. The sketch for the 8051 microcontroller is as follows:


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
    led = 0; // LED OFF

void Delay(void) // delay function
  int j;
  int i;

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

We will make the circuit according to the schematic above. Then we will burn the Hex file into the microcontroller. When the microcontroller connects with the power supply, the LED will start blinking with a delay of 1 sec. We can change this delay by setting the values in “for loops”.

Video Demonstration


In this article, we have discussed these topics:

  • KEIL IDE installation and setup.
  • Configuring the KEIL IDE for a project.
  • Writing program in KEIL IDE.
  • Schematic and Sketch.

Related Articles:

If you liked reading this article, check out these links below for similar content:

So this is all about how to use KEIL IDE for the 8051 microcontroller for the first time and how to write our first program using KEIL IDE for programming the 8051 chip.

2 thoughts on “How to Write First Program in KEIL IDE for 8051”

Leave a Comment