How to Write First Program in KEIL IDE for 8051 (original) (raw)

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

New µVision Project Keil

Selecting Microcontroller

New device selection in keil

Keil add file to project

Keil project workspace

Configuring the Microcontroller

microcontroller configuration

HEX file

Creating File

Header file

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

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.

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:

#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;
  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

Conclusion

In this article, we have discussed these topics:

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.