Skip to content

Instantly share code, notes, and snippets.

@technoburst
Last active December 16, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technoburst/5364280 to your computer and use it in GitHub Desktop.
Save technoburst/5364280 to your computer and use it in GitHub Desktop.
A sample program to learn pin change interrupt of PIC16F877A microcontroller
//Pin change interrupt example program written for
//PIC programming tutorial.
//Target Device : PIC16F877A
//Compiler : HITECH C
//Author : Anil C S
//Website : www.technoburst.net
#include <htc.h>
__CONFIG(0x3f7a); //Setting the configuration bits
#define _XTAL_FREQ 20000000 //setting the crystal frequency to 20MHz
void toggleLED();
void main()
{
INTCON = 0b10001000; //Sets GIE,RBIE bits of INTCON register
TRISB = 0xff; //Sets port B as input port
TRISD = 0x00; //Setting port D as output port
PORTD = 0xff; //Initializing port D
while(1) continue;
}
//Interrupt routine
void interrupt pin_chg_interrupt(void)
{
unsigned char data;
INTCON &= 0b01110111; //Clears GIE,RIE for disabling interrupts while ISR is running
if(INTCON&0b00000001) //Checking whether the source of interrupt was a pin status change at PORTB
{
toggleLED();
data = PORTB; //Reading the PORTB. This will clear the mismatch condition
INTCON&=0b11111110; //Clearing the interrupt flag RBIF
}
INTCON |= 0b10001000; //Re enabling the interrupt
}
//Function to toggle LED connected to PORTD
void toggleLED()
{
static unsigned char i = 0;
if(i)
{
PORTD = 0x00;
i=0;
}
else
{
PORTD = 0xff;
i=1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment