8051 Led Blinking

8051 Ports

8051 has 32-gpio's grouped into 4-Ports namely P0-P3 as shown in the below table.
PORTNumber of PinsAlternative Function
P08 (P0.0-P0.7)AD0-AD7 (Address and Data bus)
P18 (P1.0-P1.7)None
P28 (P2.0-P2.7)A8-A15 (Higher Address Bus)
P38 (P3.0-P3.7)UART, Interrupts, (T0/T1)Counters

Hardware Connections

 

Program:

 


//Program by UdayaPrakashJayaraman

#include <reg51.h>

void delay(unsigned int Count)

{

unsigned int i,j;

for(i=0;i<Count;i++)

{

for(j=0;j<100;j++);

}

}

int main()

{
P2=0xff; //Default HIGH to configure pins in Negative switching

while(1)

{

P2 = 0x00; /* Turn ON all the leds connected to P2 */

delay(500);


P2 = 0x11; /* Turn OFF all the leds connected to P2 */

delay(500);

}

return (0);

}


Example 2

In this example, we will see how to access a single port pin to blink the LED.

//Program by UdayaPrakashJayaraman

#include <reg51.h>

sbit LED = P2^0; // Bling the single LED connected to P2.0


void delay(unsigned int Count)

{

unsigned int i,j;

for(i=0;i<Count;i++)

{

for(j=0;j<100;j++);

}

}


int main()

{

while(1)

{

LED = 1; /* Turn ON the LED connected to P2.0 */

delay(500);


LED = 0; /* Turn OFF the LED connected to P2.0 */

delay(500);

}

return (0);

}

Have an opinion, suggestion , question or feedback about the article let it out here!