Multiple Blinking LED using 8051 AT89S52 with video tutorial

For hardware details connections please refer blinking led tutorial.

Let us c the program

#include<at89x52.h>
int i,j;
void delay() {
for(i=0;i<1000;i++) {
for(j=0;j<100;j++) {
;
}
}
}
void main() {
P1 = 0;
delay();
while(1) {
P1_3 = 0;
P1_0 = 1;
delay();
P1_0 = 0;
P1_1 = 1;
delay();
P1_1 = 0;
P1_2 = 1;
delay();
P1_2 = 0;
P1_3 = 1;
delay();
}
}

Explanation:

delay() function:

This delay function's explanation.
For more details about delay function click here.

main() function:

Here we are just making the all pins in Port 1 to zero or low by assigning P1 = 0.Then we call delay function. Now infinite loop aka While(1) starts.

What is infinite loop???

Logic of this code

In infinite loop leave the first line P1_3 = 0, we may see why it is added at top later. Now P1_0 = 1 means first pin is high so first LED will glow  while delay function is executing.
Then P1_0 = 0 will make the 1st LED to be in off stage and 2 LED will be glown while delay function is executing by assigning P1_1 = 1.
Like wise,
P1_1 = 0;// 2nd led is off
P1_2 = 1;// 3rd led is on up to executing delay function. 
  
P1_2 = 0; // 3rd led is off
P1_3 = 0; // 4th led is on

Now you can understand why P1_3 = 0 is added before P1_0 = 1. Yes First time no problem but 2nd execution of infinite loop aka while (1) loop 4th led should be off. For this reason we added that line.

Tutorial and working video:



Cheers!!!!
-> Let Us Embed <-