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;
}
}
}

Related posts

8051 and RGB led example using the SiLabs IDE

C8051F340 flashing led example

8051 7 Segment display program written in Basic