8051 switch input example

A very simple example that toggles a set of 8 LEDs based on whether a switch is pressed or not

The switch is connected to Port 3.0, the LEDs are connected to Port 2.0 to 7.

The code was written using Keil uVision2

Code

#include<reg52.h>

sbit SW1 = P3^0;

void main (void)
{
SW1 = 1;
while (1)
{
if(!SW1)
{
P2 = 0xAA;
}
else
{
P2 = 0x55;
}
}
}

Previous articleLED count down
Next articlemikroC Pro and 8051 development