Program For Led Blink Microcontroller

Since we have just compiled a small LED blinking program, the memory summary shows that we have just consumed 0.5% of the available program space and 1.4% of Data space. Memory of the PIC16F877 microcontroller is basically divided into 3 types: Program Memory: This memory contains the program (which we had written), after we’ve burned it.

This project/tutorial is about getting started with 8051 microcontrollers and keil ide. In this post i am going to explain a simple code on How to blink an Led using 8051(89c51,89c52) series microcontrollers. I am going to write code in c language. The tutorial is helpful for those that are going to practice 89c51 microcontroller programming in lab. I consider that you have previous academic knowledge of 8051(89c51,89c52) microcontrollers their ports, gpio pins, and registers etc. Let's begin and try to blink an led at random delay using 89c51 microcontroller and keil uvision ide.
You can program 8051 microcontrollers in assembly and c. I recommend to first know about the assembly instructions then move on to the c instructions. Assembly instructions gives in depth knowledge of each register and its individual bits functionality. While in c programming most of the configurations are hidden and they are cascaded with the code during compiling and linking process. However in c language we can use inline assembly code with c. This technique is utilized to properly configure and debug the code in case of unknown errors.
  • 89c51 or 89c52 microcontroller
  • One LED(Light Emitting Diode)
  • Crystal(11.0592 MHz)
  • Resistor 1k t0 4.7k ohm
  • Two 33-pf Capacitors
  • Power Supply
  1. Let’s Blink LED with LPC1768 Microcontroller before we proceed, keep in mind that we have upto 70 general purpose I/O pins in LPC1768 Microcontroller. GPIO or General Purpose Input Output, is the easiest way for you to interact with basic peripherals like LED, Switches and other basic components. The GPIO ports can be used as input or output.
  2. LED BLINKING WITH PIC16f877A MICROCONTROLLER,to interface an LED with the Microcontroller, is the simplest and most commonly used example for the beginners. It acts as a stepping stone for microcontroller development.
The circuit of the project is pretty straight forward. We are going to blink an led connected to pin#1 of 89c51 microcontroller. Pin#1 corresponds to microcontroller Port-1 pin#0. The pin is initialized as output pin in the code. External clock source ceramic crystal is connected to Pins 18 & 19 of 89c51 microcontroller. Crystal is connected to microcontroller in parallel to two 33-pf capacitors. 11.0592Mhz crystal is used in the project. Apply Vcc(+5v) to Pins 31 & 40 of 89c51. Ground pin#20. Now we are done all the necessary connections are made. Circuit Diagram of blinking led with 89c51 microcontroller is given at the right side.
Led polarity is anode facing the power supply and cathode is connected to port#1 pin#0.

Led is connected to port-1 pin#1. Led Anode is connected to an external +5v series with 510 ohm resistor. Resistor is used to limit the amount of current led is consuming. Led cathode is connected to 89c51 microcontroller pin directly. So when the microcontroller sources some voltage the led turns off and when it grounds the led turns on. Opposite polarity functionally by microcontroller pin.

Project Code

Microcontroller led projects

Download the project code from the links given at the bottom of the Post.

In the code above I first included the header file reg51.h. This header file is very important. When ever we are using 89c51 microcontroller in our project we have to include it. If we are using 89c52 than the file name changes to reg52.h. This library contains default function definitions and configuration for 8051 series microcontrollers.
Note: I am using keil software to write my code to blink an led. If you are using any other software to write code for 8051 microcontroller. Then first check which library that software made compulsory to be included for writing code of 8051 series microcontrollers. Keil made it compulsory to include the reg51.h header file in the codes that are written for 8051 series microcontrollers. Actually the compiler that is working with keil needs this header file to compile the code and generate Hex code for you.
Then the statement sbit Led=P1^0; is initializing port#1 pin#0. Now we can use Port-1 Pin#0 with the name of Led. Next delay(value) function here is actually generating some arbitrary delay for us. If Led switches on, we want it to remain in this state for some time so we give some delay before executing the off instruction. You can see the two for loops in the delay function. These two for loops are providing us random delay. The first loop runs number of times for the value which is given to it, and second loop runs 5 times for each iteration of the first loop. We usually pass some big number as parameter to the function and this huge number makes the for loop runs for some seconds to minutes depending on our given integer value. You can also use the internal timers of the 8051 for delays but since this tutorial is for the beginners so you should adopt the easy way.
In the main() function the instruction P1=0x00; is initializing our Port-1 as output. It is necessary to initialize each port as input our output before using it in the program. Since my Led is connected to Port-1 Pin#0 that's why I am initializing Port-1 as output.
0x00; is a hexadecimal command caring 8-bit instruction. If we translate it in binary it becomes P1=00000000. Since Port-1 of microcontroller consists of 8-Pins. This 8-bit instruction is written to each single pin of microcontroller. If 0 is written on Pin it initializes the pin as output. If 1 is written it initializes the pin as input. can easily be written to the microcontroller port and each bit represent a particular port pin of micro controller.
Initializing Port-1 of 8051(89c51,89c52) microcontroller.
While(1) Loop is continuously running the logic present in it. Led Blinking Logic is Present in While(1) Loop. The whole cycle is below.
  • First I made Led=1 which switches on my led.
  • Then Some Delay() Keeps Led in on state.
  • After Delay() I made Led=0 which switches off the led.
  • Then Some Delay() keeps led in switch off state.
Led
After the above cycle is completed while loop starts again and this makes the led to seem like blinking.
Download the Project files, Code(Hex,C). Simulation is also present in the folder. Code is written using C language. Keil software is used to write and compile code. Simulation is made in Proteaus 8.0.

Blinking 1 LED using 8051

This is the first project regarding 8051 and of course one of the simplest, blinking LED using 8051. The microcontroller used here is AT89S51 In the circuit, push button switch S1, capacitor C3 and resistor R3 forms the reset circuitry. When S1 is pressed, voltage at the reset pin (pin9) goes high and this resets the chip. C1, C2 and X1 are related to the on chip oscillator which produces the required clock frequency. P1.0 (pin1) is selected as the output pin. When P1.o goes high the transistor Q1 is forward biased and LED goes ON. When P1.0 goes low the transistor goes to cut off and the LED extinguishes. The transistor driver circuit for the LED can be avoided and the LED can be connected directly to the P1.0 pin with a series current limiting resistor(~1K). The time for which P1.o goes high and low (time period of the LED) is determined by the program. The circuit diagram for blinking 1 LED is shown below.

Program For Led Fan

Program.

Blinking 2 LED alternatively using 8051.

Program For Led Blink Microcontroller Software

This circuit can blink two LEDs alternatively. P1.0 and P1.1 are assigned as the outputs. When P1.0 goes high P1.0 goes low and vice versa and the LEDs follow the state of the corresponding port to which it is connected. Here there is no driver stage for the LEDs and they are connected directly to the corresponding ports through series current limiting resistors (R1 & R2). Circuit diagram is shown below.

Blinking 2 LED alternatively using 8051


Program.

Program For Led Blink Microcontroller Light

Program For Led Blink Microcontroller

Testing images and videos.