Posts tagged ‘Microcontroller Tutorial’

Led blinking program with 8051 Microcontroller and Keil uVision4

The first “Hello World!” project I prefer for Microcontroller is LED Blinking. I have used ATMEL’s 89C51 (40-pins DIP) 8051 architecture microcontroller which is ideal for first time learning MCU Chip.  The program is very simple and straight forward, that uses a delay procedure  loop based software delay.

Source Code

Here,i have  written code  in C using Keil uVision4.

#include <REGX51.H>
void msdelay(unsigned int );
void main(){

P2=0x00;  //all pin of PORT2 declared as output
//infinite loop

P2=0xFF;   //all pin high
msdelay(250);    //delay
P2=0x00;   //all pin low
msdelay(250); //delay


//delay function
void msdelay(unsigned int value){

unsigned int x,y;

In C programs you cannot be sure of delay, cause  it depends on compiler how it optimize the loops as soon as you make changes in the options the delay changes.

you will have to use Timers for making exact delays….. it does not matters whether u need delays of ms , us or even seconds…

below is a function of 1 second using timers…..

// the idea is to make a 50ms delay and run it 20 times as 20x50ms= 1000ms= 1sec

delay_1s() // timer of 1 sec
int d;
TR0=1; // start timer.
while(TF0==0); // run until TF turns to 1
TR0=0; // stop timer
TF0=0; // reset the flag

Circuit Diagram

Led blinking with 8051 microcontroller