Chasing LEDs using Maths

This is a simple program using a mathematical formula for 8051 MUC for chasing effect of 8 LEDs connected to PORT1 from P1^7 to P1^0.
Code written by: Yash Vidyasagar

#include <reg51.h>
int i,j=1, k=2;

// User defined code for delay in the program
void delay(unsigned int msec)
{
  int a, b;
	
   for(a=0;a<msec;a++)
   {
      for(b=0;b<128;b++);
   }
}

int main()
{
   while(1)
   {
     for(i=0;i<10;i++)
     {
        P1=j;
        delay(1000);
        j=j+k; // incrementing the value of "j" by "k"
        k=k+k; // incrementing the value of "k" by "k"
			
     if(j=255) // when j=255, i.e. all LEDs are ON, we must make j=1, k=2
     {
       delay(1000); // this delay ensures that last 8th LED will glow for 1 sec.
       j=1;
       k=2;
     }
     }	
  }
}

Check other oustanding projects using 8051 microcontroller.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.