Skip to content
Due to large volume of orders, all the orders placed/confirmed after 10am on 26th Oct will only be shipped on 27th Oct. We apologize for the inconvenience and appreciate your patience

8051 Programming With Keil: AT89S52 LED Blink Step by Step

8051 Programming With Keil: AT89S52 LED Blink Step by Step
-
Written By - Robocraze -
📅 Updated on 26 Oct 2026
✨ Summarize with AI
✅ Prompt copied

Summary

Learn 8051 programming with Keil µVision using the AT89S52 development board. Follow step-by-step instructions to create a project, write embedded C code, generate a HEX file, and flash it using a compatible USB ISP programmer. Start with an LED blink experiment, then explore timers, UART, and interrupts.

To program an 8051 microcontroller using Keil µVision, create a project for the AT89S52, write your C code, enable HEX file generation, and build the project. Then flash the HEX file to the board using a compatible ISP programmer. An LED blink program on Port 1 is a simple first experiment.

This tutorial walks through the Keil setup, project creation, C code, HEX file generation, USB ISP programming, and common troubleshooting steps using an AT89S52-based 8051 development board.

What You Need to Get Started

Before writing your first 8051 program, prepare the following hardware and software:

  • 8051 development board: A board based on the AT89S52 microcontroller.

  • Keil µVision IDE: Used to write, compile, and build the embedded C program.

  • Compatible USB ISP programmer: Used to transfer the compiled HEX file to the microcontroller.

  • USB cable: For the programmer or development board, depending on its design.

  • LED and current-limiting resistor: If you want to test an external LED. Some boards already include an LED connected to a port pin.

  • Computer: To run the IDE and the programmer's software.

The AT89S52 is an 8051-family microcontroller with on-chip Flash memory. Its programming method depends on the hardware interface exposed by your board. Make sure your programmer supports the AT89S52 specifically, rather than assuming every device marketed as an 8051 programmer will work with it.

Step 1: Create a New Keil µVision Project

Keil µVision is used to write and compile the embedded C program. The compiler creates the program output, and enabling HEX generation produces the file needed by the programmer. 

  1. Open Keil µVision.

  2. Select Project > New µVision Project.

  3. Create a folder for the project, such as 8051_LED_Blink, and save the project inside it.

  4. In the device selection window, locate the Atmel or Microchip device list and select AT89S52.

  5. If Keil asks whether to add the standard startup code to the project, follow the toolchain instructions for the selected device and project type. For this simple example, no custom startup code is required.

  6. Save the project.

Selecting the correct device matters because the compiler needs the right device definitions and memory configuration. If AT89S52 is missing from your device list, check whether your installed Keil edition and device database support the 8051 C51 toolchain.

Step 2: Write Your First 8051 C Program

This program toggles the state of Port 1, pin 0 (P1.0), repeatedly. If your development board has an LED connected to P1.0, the LED should blink. Otherwise, connect an LED with a suitable current-limiting resistor, following the board's pinout and electrical limits.

Create a new C source file in Keil and save it as main.c.

#include <REG52.H>

sbit LED = P1^0;

void delay_ms(unsigned int ms)
{
    unsigned int i;
    unsigned int j;

    for (i = 0; i < ms; i++)
    {
        for (j = 0; j < 500; j++)
        {
            /* Approximate software delay */
        }
    }
}

void main(void)
{
    while (1)
    {
        LED = 0;
        delay_ms(200);

        LED = 1;
        delay_ms(200);
    }
}

Understanding the code

  • #include <REG52.H> includes register definitions for the 8052-compatible device.

  • sbit LED = P1^0; gives the P1.0 pin a readable name.

  • delay_ms() runs a software loop to create a pause between LED changes.

  • LED = 0 drives the pin low, and LED = 1 drives it high.

  • while (1) repeats the sequence continuously.

Note: The delay is approximate, not a precise millisecond timer. Its duration depends on the oscillator frequency and compiler settings. If the onboard LED is wired as active-low, the LED will turn on when the pin is low. If it is wired differently, the visible on/off sequence may be reversed.

Step 3: Configure the Target and Generate the HEX File

The programmer generally needs an Intel HEX file rather than the C source code. Keil can generate this file automatically when the option is enabled.



 

  1. Open Project > Options for Target 'Target 1'.

  2. Under the Target tab, set the oscillator frequency to match your development board. For example, use 12.0 MHz only if the board actually uses a 12 MHz crystal.

  3. Open the Output tab.

  4. Enable Create HEX File.

  5. Confirm the settings and close the dialog.

  6. Select Project > Build Target, or press F7.

Check the build output window. Correct any compiler errors before proceeding. If the build succeeds, locate the generated .hex file in the configured output or project directory.

The HEX file contains the compiled program in a format accepted by compatible programming tools. It is the file you will load into the ISP programmer software.

Step 4: Flash the AT89S52 Using a USB ISP Programmer

The AT89S52 supports in-system programming through its serial programming interface. Its ISP signals include MOSI, MISO, and SCK, along with reset and the required power and clock connections. The exact connector and flashing procedure depend on your development board and programmer.



 

Follow this general procedure:

  1. Connect the programmer: Connect the compatible USB ISP programmer to your computer and to the development board's programming header. Align the connector using the board's pin labels or documentation.

  2. Power the board correctly: Check whether the programmer supplies board power or whether the board needs its own power source. Avoid connecting two power sources in a way that could cause back-feeding.

  3. Open the programmer software: Use the programming utility supplied or recommended for your specific programmer. Different USB ISP devices may require different software and drivers.

  4. Select the microcontroller: Choose AT89S52 from the device list.

  5. Load the HEX file: Browse to the .hex file generated by Keil.

  6. Program the device: Start the write or program operation and wait for it to complete.

  7. Verify the result: If the utility offers a verify operation, run it and confirm that programming succeeded.

  8. Run the program: Reset or power-cycle the board according to its instructions. The LED connected to P1.0 should begin blinking if the board's LED wiring matches the code.

Do not select a different microcontroller just because it belongs to the 8051 family. A programmer must support the exact device and programming protocol.

Next Experiments: LCD, UART, Timers and Interrupts

Once the LED blink program works, you can move on to more useful 8051 projects.

1. LCD interfacing

Connect a compatible character LCD to the microcontroller and display text, sensor values, or a counter. Check the LCD's logic voltage and the development board's pin assignments before wiring it.

2. UART serial communication

Use the AT89S52's UART to send text to a computer through a compatible USB-to-serial adapter. Configure the baud rate to match the clock frequency and timer settings. Remember that the microcontroller's UART pins use logic-level signals, not the voltage levels of a traditional RS-232 interface.

3. Timer-based LED blinking

Replace the software delay with a hardware timer interrupt. This provides more predictable timing and allows the microcontroller to perform other tasks between LED changes.

4. External interrupts

Connect a suitable switch or digital signal to an interrupt input and write code that responds to the event. Use appropriate pull-up or pull-down arrangements and handle switch bounce where necessary.

These experiments introduce the core skills needed for embedded systems: digital I/O, timing, serial communication, and interrupt-driven programming.

Troubleshooting Common 8051 Programming Problems

Keil does not generate a HEX file

Open Options for Target > Output and enable Create HEX File. Rebuild the project and check the build messages for errors. If compilation or linking fails, correct the reported problems before searching for the HEX file. 

The AT89S52 is not detected by the programmer

Check the USB driver, programmer compatibility, connector orientation, board power, and programming cable. Confirm that the programmer supports the AT89S52 and that the board's ISP signals are connected correctly.

The program flashes successfully, but the LED does not blink

Check whether the LED is connected to P1.0 or a different port pin. Verify the board's LED polarity, oscillator frequency, power supply, and reset circuit. If the LED is active-low, the visible on/off sequence may be opposite to your expectations.

The LED blinks much faster or slower than expected

The software delay is approximate and depends on the oscillator frequency and compiler output. Confirm the crystal frequency and adjust the delay loop, or use a hardware timer for more consistent timing.

The code builds but the programmer rejects the HEX file

Make sure you selected AT89S52 in the project, built the latest version, enabled HEX generation, and loaded the correct file. Check whether the programming utility requires a particular HEX format and follow the programmer's documentation.

Get an 8051 Development Board for Your First Project

The SmartElex Aryabhatta 8051 Development Board for AT89S52 is the target product for this tutorial. Before starting, check the product listing for its programming connector, onboard LED connections, clock frequency, power requirements, and supported ISP programmer.

For classroom or laboratory work, use the board's pinout and programming instructions as the reference for wiring and flashing. This helps avoid mistakes when moving from the basic LED experiment to LCDs, UART communication, timers, and external interrupts.

Conclusion

Programming an AT89S52 with Keil µVision involves four main tasks: creating the project, writing the embedded C code, generating the HEX file, and flashing the microcontroller with a compatible ISP programmer.

The LED blink program is a useful first test because it checks the basic software and hardware setup. Once it works, you can explore LCD interfacing, UART communication, timers, and interrupts to build more advanced 8051 projects.

For reliable results, select the correct device in Keil, match the target clock frequency to your board, verify the HEX file, and use a programmer that explicitly supports the AT89S52.

Excerpt

Write and flash your first 8051 program: install Keil µVision, create an AT89S52 project, generate a HEX file, and program the LED blink code using USB ISP.

Frequently Asked Questions

1. Is Keil µVision free for 8051 programming?

Keil's available features and code-size limits depend on the product edition and licence. Check the current Keil C51 licensing terms to determine whether your project fits within the available evaluation or licensed version.

2. How do I generate a HEX file in Keil?

Open Options for Target > Output, enable Create HEX File, and rebuild the project. If the build succeeds, Keil generates an Intel HEX file that can be loaded into a compatible programming utility.

3. Which software flashes the AT89S52 using USB ISP?

The software depends on the programmer. Some USB ISP programmers use ProgISP or a vendor-supplied utility, while other devices require different tools. Confirm the supported device list and software instructions for your specific programmer.

4. Can I program the AT89S52 in Arduino IDE?

The Arduino IDE does not provide the same standard workflow for the AT89S52 as it does for supported Arduino boards. A common approach is to write C code in Keil's 8051 toolchain, generate a HEX file, and flash it using a compatible programmer.

5. Why is my 8051 program not running?

Check the power supply, reset circuit, crystal or oscillator, programmer compatibility, and the selected microcontroller. Also confirm that the latest HEX file was programmed and that the LED or other output is connected to the port pin used in your code.

Prev Post
Next Post

Leave a comment

Please note, comments need to be approved before they are published.

Thanks for subscribing!

This email has been registered!

Shop the look

Choose Options

Edit Option
Back In Stock Notification
Compare
Product SKU Description Collection Availability Product Type Other Details

Choose Options

this is just a warning
Login
Shopping Cart
0 items
FREE SHIPPING!
₹100 OFF
₹200 OFF
₹999
₹2500
₹4900
WhatsApp Chat Chat