AT89C51 Microcontroller Programming

A guide to a good way to learn AT89C51 Microcontroller Programming using C programming language

This is a guide for you to learn how to start learning and practicing AT89C51 microcontroller programming using C programing language. The usefulness of using learning AT89C51 microcontroller is that it is based on the first microcontroller called Intel 8051 microcontroller. Most of the newer microcontroller are based on this one.That is the other microcontrollers have evolved from this microcontroller directly or indirectly. So once you learn 8051 architecture and how to program it in C, it will be easier for you to learn other microcontrollers. Perhaps because of this, many computer and electronics engineering schools and colleges teaches this 8051 microcontroller. They have also development board for electronics engineering training. However most engineering colleges won’t teach you how to write program, teach about simulation of microcontroller etc. You will learn this here.

What you will learn and what you will need

This guide here is to teach you how to learn AT89C51 microcontroller programming in C via simulation. That is we are not going to build actual hardware but to learn writing codes for microcontrollers. This has the benefit that you will learn many concepts of C programming for microcontroller. In regards to hardware concept, you will learn how to connect the microcontroller to LEDs, crystals, resistors, capacitors and other external circuits such as LCD, motors, keypad etc. For the purpose of microcontroller simulation we will use a simulation software called Proteus. In the article Microcontroller Simulation Project with Proteus for beginners we showed simple example of microcontroller simulation using Proteus software. There we used assembly language and another microcontroller. You need Proteus to follow the guide here so you should download and install it.

The proteus software allows us to write C code for the microcontroller directly in its code editor. But to compile and build the C program we need 8051 compiler. Keil Software includes C51 C compiler for the 8051 based microcontrollers. And we can use this compiler from within the Proteus to compile the program codes. This Keil software will be used in this guide. But you can also any other 8051 C compiler.

Lastly, you should have the AT89C51 datasheet with you. You can just download it for free from the web. The datasheet becomes handy, often you want to know microcontroller ports, pins, registers etc.

How to install Keil uVision Software

Once you have download Keil IDE, you should install it. First you will see a window which ask you where or which drive you want to install the software. Then clicking on next, you will provide name and email address(you can put any name and address). Then the Keil uvision software will install. In the end click Finish. These steps is shown in the animation below.

Proteus and Keil C compiler integration

After you have installed the Keil software, you need to install Proteus software if you don’t already have it. Then you have to specify the C compiler o be used for the 8051 based microcontroller. If you installed Keil before installing Proteus then Proteus can automatically detect the compilers installed in your computer. But if not and if you installed the compiler after you installed Proteus, you can always specify the compiler from the compilers configuration window. To bring up the compilers configuration window, you have to open the source code editor which you can do from the toolbar. Then from the source code editor, you have to go to System > Compilers Configurations.

The following picture shows the Compilers Configurations window.

compilers in proteus

Once there scroll down until you find Keil for 8051 and then specify the path of the Keil software executable as shown in the picture above.

Now after doing this you can write the C program codes from within the Proteus software and compile it using the Keil C compiler.

Create AT89C51 LED blinking Schematic in Proteus

The next step is to create a schematic that uses AT89C51 microcontroller. In this basic demonstration guide, we will create a schematic with a single LED, oscillator and connection for the Reset and ~EA pin. We will turn on and off the LED via simulation and LED blinking program. Such LED blinking program in microcontroller world is often known as Hello World program.

Since this is guide to programming AT89C51 microcontroller, we will not dwell into how to create schematic, how to browse library and place the components onto the schematic. So we just show you the schematic used in this tutorial guide.

Writing C program code for AT89C51

Once you have created the schematic you have to provide the program to the AT89C51 microcontroller. Double click on the AT89C51 microcontroller and select Edit Firmware.

You should then see the Source Code editor. You will see Project source code panel on the left side and the code writing editor on the right. The code writing pane will initially have bare minimum code. You should rewrite the code for blinking LED connected to port 3 pin 0. The following shows the code editor and the LED blinking program in C for AT89C51.

LED blinking Program for AT89C51 microcontroller in C

The C program code is very simple. It uses the reg51.h header file where the different ports pins and other microcontroller related features are defined.This is specific to Keil C51 compiler.

Using the sbit we can rename the Port pin to user defined variable like LED in the above code. Whenever we want to refer to Port 3 pin 0 pin we can use the LED name. In Keil, the 8051 port pins are referred with the convention port_name^pin_number like P3^0.

After the LED definition, we have the declared the delay function prototype. This delay function will be used to create a time delay. The time delay is inserted whenever we want to create some time delay in our program. In this example we use the delay function to send 1 and 0 to the port3 pin 0 between the time interval defined by the delay function. This creates the LED blinking effect.

In the main function, we use the indefinite while loop while(1). Within this loop, we write 1 to the pin LED, insert time delay, write 0 to the same pin and again insert time delay.

Lastly outside the main function, we write the actual delay function. The time delay is created using simple for loops.

Compile and Simulation of AT89C51 microcontroller

The last step is to compile the above program and simulate the circuit. Right click on the project and select Build Project.

Doing this will compile the program and upload the program file into the AT89C51 microcontroller on the schematic. Going back to the schematic editor and double clicking on the AT89C51 microcontroller you should see that the program is loaded for the microcontroller as shown below.

Now close this window and run the simulation. You should see the LED blinking while running the simulation as shown in the following animation below.



What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

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

Telephone Transceiver for Microcontroller

Telephone Transceiver for Microcontroller

AT89C51 microcontroller Programming - Push Button

AT89C51 microcontroller Programming – Push Button