this is one of simplest project for beginner and a better way to get stated with an embedded system.In this article you are going to learn how to control led by separate combination of switches is shown in table below.
we are using Proteus as a tool for code simulation in this tutorial.By connecting two leds at pin P1.0 and P1.1 corresponding to switches at pin P2.0 and P2.1 respectively
PROTEUS DESIGN
assembly code
embedded C code
complete code
#include<reg51.h>
sbit sw1=P1^0;
sbit sw2=P1^1;
sbit led1=P2^0;
sbit led2=P2^1;
void main()
{
P2=0x00;
P1=0x00;
while(1)
{
if(sw1==0&sw2==0)
{
led1=0;
led2=0;
}
if(sw1==1&sw2==0)
{
led1=1;
led2=0;
}
if(sw1==0&sw2==1)
{
led1=0;
led2=1;
}
if(sw1==1&sw2==1)
{
led1=1;
led2=1;
}
}
}
Category: