Pantech.AI

“How to write the program for 8051 using KEIL”

Introduction

In this guide, we’ll cover the essentials of getting started with the 8051 microcontroller using the Keil µVision IDE.

The 8051 microcontroller can be programmed in two main languages:

  1. Assembly Language
  2. C Language

Popular development environments for the 8051 microcontroller include the MCU 8051 IDE and Keil µVision, which is widely used for creating and debugging code.

The Keil µVision IDE includes several key components:

  • C Compiler: C51.Exe
  • Assembler: A51.Exe
  • Linker/Locator: BL51.Exe
  • Librarian: LIB51.Exe
  • Hex Converter: OH51.Exe

Each component plays a crucial role in translating and optimizing your code for the 8051 microcontroller.

Let’s create a simple LED blinking program using the Keil µVision IDE and the C51 compiler. For this example, we’ll use the AT89S52 microcontroller from the 8051 family.

  1. Download and Install Keil µVision
  • First, download the Keil µVision IDE from the official website and complete the installation.
  1. Launch Keil µVision
  • After installation, open the Keil µVision IDE to start setting up your project.

In the following steps, we’ll configure the project and write code to blink an LED using the AT89S52 microcontroller.

Go to the Project menu and select New µVision Project

The Create New Project window will appear. Enter a name for your project, choose a location to save it, and click Save.

The Select Device for Target window will appear. Choose your device from the list (in this case, select AT89S52).

The µVision window will prompt you to copy STARTUP.A51 to the project folder and add it to the project. This step is not necessary for our example, so select No.

Next, go to the File menu and select New File. Enter your program code (in this case, the LED blinking program).

LED Blinking Code for AT89S52 Microcontroller

#include<reg52.h> /* Include header file */

void delay(k)  /* Delay for msec. (here Xtal freq. is 11.0592 MHz) */
{
	int i,j;
	for (i=0;i<k;i++)
		for (j=0;j<112;j++);
}

void main()
{
	while(1)
		{
			P1 = 0x00;  /* Make Port 1 (P1) Low for 200 msec.*/
			delay(200);
			P1 = 0xFF;  /* Make Port 1 (P1) High for 200 msec.*/
			delay(200);
		}
}

Save the program code with a .c extension. (If you’re using assembly language, save the code with a .asm extension instead)

Right-click on the Source Group 1 folder under Target 1, then select Add Existing Files to Group ‘Source Group 1’.

Select the program file saved with the .c or .asm extension (if you’re using assembly language) and add it. After that, close the window. You should see the added file listed under the Source Group 1 folder in the left project window.

Next, go to the Project menu and click on Build Target. This action will build the project and display the status in the Build Output window, including any errors or warnings encountered during the process.

To create a hex file, right-click on Target 1 and select Options for Target ‘Target 1’.

The target options window will appear. Enter the Xtal (MHz) frequency (in this case, we use 11.0592 MHz) and check the box next to Use On-chip ROM.

Within the same window, select the Output tab, check the box next to Create Hex File, and then click OK.

Now, select Build Target from the Project menu again, or simply press the F7 shortcut key. This will build the target and create a hex file. You can see the progress of the hex file creation in the Build Output window.

The created hex file is programmed into the microcontroller’s flash memory using various methods, depending on the programmer or the specific manufacturer’s tool.

For example, Flash Magic is used exclusively for NXP Philips microcontrollers and is developed by NXP. Other manufacturers may utilize serial programmers, such as an ISP programmer, to flash their controllers.

Now, load the generated hex file into any available programmer and flash it onto your 8051 microcontroller.

Below is a snapshot of the hex file.

LED Blinking using AT89S52 Microcontroller Output

Leave a Comment

Your email address will not be published. Required fields are marked *