Interrupts in PIC16F877A Pic microcontroller with Push button Example using Proteus

Before going to this tutorial, you are recommended to see

1) PIC16F877A GPIO configuration
2) Blinking LED
3) Multiple Blinking LED

What is Interrupt ?

Interrupts are used to interrupt the processor, to perform some other task. After completing the task, processor will continue it's previous task.

Interrupts in PIC16F877A Pic microcontroller

PIC16F877A has 15 interrupts which has one GPIO interrupt i.e., RB0/INT.

How to configure this GPIO as a interrupt ?

1) Configure the RB0 pin as a input
2) Enable the Global interrupt.(GIE)
3) Enable Peripheral interrupt.(PEIE)
4) Enable RB0 interrupt.(INTE)
5) Write the interrupt service routine using interrupt keyword before the function.

Hardware Proteus Circuit

Embedded C Program

#include <xc.h>
//interrupt service routine
void interrupt ISR() {
    //Check if the interrupt is because of
    //RBO GPIO pin
    if(INTCONbits.INTF == 1) {
        //Clear the interrupt
        INTCONbits.INTF = 0;
        //If the LED is on
        if(PORTBbits.RB1 == 1) {
            //turn off the LED
            PORTBbits.RB1 = 0;
        //If the LED is off
        else {
            //turn on the LED
            PORTBbits.RB1 = 1;
void main() {
    //RB0 as a input
    TRISBbits.TRISB0 = 1;
    //RB1 as a output
    TRISBbits.TRISB1 = 0;
    //RB1 high so LED On
    PORTBbits.RB1 = 1;
    //Enable the Global interrupt
    INTCONbits.GIE = 1;
    //Peripheral Interrupt Enable
    INTCONbits.PEIE =1;
    //enable RB0 interrupt
    INTCONbits.INTE = 1;
    //Loop forever

Proteus Push button Output

Video Tutorial


Also See 

Push button Example using 8051

No comments:

Post a comment