Selamat datang ke BelajarBlogs...
Home » Interfacing LEDs and Switches

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<250 br="" j="">}
void blink(void) //LED Blink
{
int i;
for (i=0;i<2 blink="" br="" continous="" i="" it=""> {
P2 = ~P2 ; //LED reverse
delay(250);
}
}
void twobytwo(void) // LED run the light pattern
{
int i;
for (i=0;i<5 br="" 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;
}
}
}

2 ulasan: