Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Interfacing LEDs and Switches



I use two push button switches to Port 1 and 8 LED to Port 2 of AT89C51 microcontroller. Then when i push button 1 ,the LED will blink and for push button 2 , the LED will run running light patter 2 by 2 .

Programming c++ for Keil

#include
sbit LED_pin = P2;  // Defining LED PIN
sbit Switch_pin1 = P1^0;  // Defining Switch PIN
sbit Switch_pin2 = P1^1;  // Defining Switch PIN
 char pattern[] = {0xFF,0xFC,0xF3,0xCF,0x3F}; // led will be blink two by two

void delay(int ms) // Delay 250us
{
int i,j;
for (i=0;i for (j=0;j}
void blink(void) //LED Blink
{
int i;
for (i=0;i {
P2 = ~P2 ; //LED reverse
delay(250);
}
}
void twobytwo(void) // LED run the light pattern
{
int i;
for (i=0;i {
P2 = pattern[i];
delay(250);
}
}
void main(void)
{
P2 = 0x00 ;
while(1)
{
if (Switch_pin1 == 0 && Switch_pin2 == 1) //if switch 1 pressed
{
P2 = 0xFF ;
blink(); // LED will blink
}
else if (Switch_pin1 == 1 && Switch_pin2 == 0) //if switch 2 pressed
{
P2 = 0xFF ;
twobytwo(); //LED run the light pattern
}
 else
{
P2 = 0xFF;
}
}
}5>2>250>


This post first appeared on Tutorial Blogger, please read the originial post: here

Share the post

Interfacing LEDs and Switches

×

Subscribe to Tutorial Blogger

Get updates delivered right to your inbox!

Thank you for your subscription

×