lighting 8 common leds with a 8051 Microcontroller

lighting 8 common leds with a 8051 Microcontroller

Posted in: embedded by musiitwa joseph 2014-12-21 at 22:04:08

image for lighting 8 common leds with a 8051 Microcontroller The 8 leds are connect to port 2 of an 8051 Microcontroller. The leds are lit from the first to the last and then from the last to the first, just like creating a flash light from one led to another. This is repeated as along as the system is recieving power.

#include<REG51.H>
int i,j;

unsigned char order[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
/*
* WHERE THE ARRAY COMES FORM
*
* port 2 has 8 pins and each connected to a led which is active high(common cathode), so logic 1 turn on the led on.
*
* pin1 = 00000001 = 0x01;
* pin2 = 00000010 = 0x02;
* pin3 = 00000100 = 0x04;
* pin4 = 00001000 = 0x08;
* pin5 = 00010000 = 0x10;
* pin6 = 00100000 = 0x20;
* pin7 = 01000000 = 0x40;
* pin8 = 10000000 = 0x80;
*
*/

void delay(){
for(i=0; i<10000; i++);
}

void main(){

while(1){//infinite loop, it runs forever

for(j=0; j<=7; j++){
P2 = order[j];
delay(); //function creates a small delay before the next led is lighted
}

//reversing the array;
for(j=7; j>=0; j--){
P2 = order[j];
delay();
}

/*using these 2 for loops the 8 leds will be lit in the order
1234567887654321
and the order is repeated
*/
}

}


Tags:
c

Search

Leave a comment

Fields with * are required
Your email address will not be displayed
3 + 1 =