Friday, June 29, 2012

Blinking one LED with 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 :-

Using assembly

START: CPL P1.0
       ACALL WAIT  
       SJMP START

WAIT:  MOV R4,#05H
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
        DJNZ R3,WAIT2
        DJNZ R4,WAIT1
        RET

Using embedded C

#include<reg51.h>
void delay(unsigned int)
void main()
{
unsigned char a;
while(1)
{
p1^0=1;
delay(500);
p1^0=0;
delay(500);
}
void delay(unsigned int dtime) // generates  a delay of 1ms
{
unsigned int i,j;
for(i=0;i<dtime;i++)
for (j=0;j<1275;j++);
}
}
}

NOTE :-

Well it might look difficult with embedded C but making bigger programs are a lot more easier in embedded c then in assembly

No comments: