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.
-
Open Keil µVision.
-
Select Project > New µVision Project.
-
Create a folder for the project, such as
8051_LED_Blink, and save the project inside it. -
In the device selection window, locate the Atmel or Microchip device list and select AT89S52.
-
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.
-
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 = 0drives the pin low, andLED = 1drives 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.
-
Open Project > Options for Target 'Target 1'.
-
Under the Target tab, set the oscillator frequency to match your development board. For example, use
12.0 MHzonly if the board actually uses a 12 MHz crystal. -
Open the Output tab.
-
Enable Create HEX File.
-
Confirm the settings and close the dialog.
-
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:
-
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.
-
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.
-
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.
-
Select the microcontroller: Choose AT89S52 from the device list.
-
Load the HEX file: Browse to the
.hexfile generated by Keil. -
Program the device: Start the write or program operation and wait for it to complete.
-
Verify the result: If the utility offers a verify operation, run it and confirm that programming succeeded.
-
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.