Home PICPIC tutorials PIC16f877 and push button example

PIC16f877 and push button example

In this example we connect a push button switch to RD0 when the button is pressed we will light an LED(s), when the button is not pressed the LED(s) will not be lit. Since we are using a QL200 development board we are using the LEDs that are on the development board connected to PORT B.

By default RD0 is pulled up to 5v via the 10K resistor, when the button is pressed the RD0 pin will be pulled to ground

You can quite easily only connect one LED if you desire

Schematic

PIC16F877 and button

Code

 


void main()
{
TRISD.F0 = 1; //Configure 1st bit of PORTD as input
TRISB = 0; //Configure PORTB as output
PORTB = 0; //LED OFF
do
{

if(PORTD.F0 == 0) //If the switch is pressed
{
Delay_ms(100); //Switch Debounce
if(PORTD.F0 == 0)//If the switch is still pressed
{
PORTB = 0x00; //LED ON
}
}
else
{
PORTB = 0xFF; //LED OFF
}
}while(1);
}

Links
PIC16F877 20pcs

2pcs QL200 PIC16F877A-I/P PIC16F877 PIC 8-bit Development Board

You may also like