Using Interrupt with PIC Microcontroller

Using Interrupt with PIC Microcontroller


Interrupt is the one of the most powerful feature in embedded applications. Almost all the real time applications are implemented using Interrupts. So what is an Interrupt…??

As the name suggests Interrupts are special events that requires immediate attention, it stops a microcontroller/microprocessor from the running task and to serve a special task known as Interrupt Service Routine (ISR) or Interrupt Handler. Suppose you are at home, taking coffee. Suddenly your mobile phone rings. You stop taking coffee and answer the call. When you have finished the conversation, you will go back to take coffee. This process is similar to ISR execution. You can think the main service routine as taking coffee and the ringing of mobile phone is causes interrupt in taking coffee. This initiates your mobile phone conversation which is similar to executing ISR. When the mobile conversation is ended you will go back to the main service routine of taking coffee.

Need for Interrupts

Consider a MP3 player which build around microcontroller. It contains push button switches to select song, control volume etc. The microcontroller should be programmed to convert the data stored in MP3 files to electrical signal and to change controls according to the status of push button switches. Consider this application without using interrupt, the programmer wants to continuously do the following tasks.

  • Read the status of push button switches and change controls accordingly.
  • Read data from MP3 file.
  •  Convert it to electrical signal.

This processes of continuous monitoring is known as POLLING. This is not an efficient way of programming as it consumes all its processing time for monitoring. Consider if this problem is addressed using Interrupts. The microcontroller wants to respond only when an interrupt occurs.

Here is an analogy to understand the difference better. The method of polling is similar to a salesperson, who goes door-to-door to requesting to buy his product or service. This is similar to a microcontroller continuously monitoring the status of all devices attached to it. While the method of Interrupt is similar to  a shopkeeper. Whoever needs his products or services goes to him and buy it. This is similar to microcontroller responds only when an interrupt occurs.

Hardware and Software Interrupts

PIC Microcontroller consists of both Hardware and Software Interrupts. If the interrupts are generated by external hardware at certain pins of microcontroller, or by inbuilt devices like timer, they are called Hardware Interrupts. While Software Interrupts are generated by a piece of code in the program. Also known as External and Internal Interrupts.

Interrupts in PIC 16F877A

PIC 16F877A has the following 15 interrupt sources :

  • External
  • Timer 0
  • Timer 1
  • RB Port Change
  • Parallel Slave Port Read/Write
  • A/D Converter
  • USART Receive
  • USART Transmit
  • Synchronous Serial Port
  • CCP1 (Capture, Compare, PWM)
  • CCP2  (Capture, Compare, PWM)
  • TMR2 to PR2 Match
  • Comparator
  • EEPROM Write Operation
  • Bus Collision

The 5 registers that used to control the operation of Interrupts in PIC 16F877A Microcontroller :

  • PIE1
  • PIR1
  • PIE2
  • PIR2

This article deals with external interrupt. INTCON register is used to configure External Interrupts.

INTCON Register

INTCON Register PIC 16F877A to Configure Interrupts

INTCON Register PIC 16F877A

INTCON Register is a readable and writeable register which contains various enable and flag bits for External and Internal Interrupts.

GIE – Global Interrupt Enable

1 – Enables all unmasked interrupts
0 – Disables all interrupts

PEIE – Peripheral Interrupt Enable

1 – Enables all unmasked peripheral interrupts
0 – Disables all peripheral interrupts

TMR0IE – Timer 0 Overflow Interrupt Enable

1 – Enables the TMR0 interrupt
0 – Disables the TMR0 interrupt

INTE – RB0/INT External Interrupt Enable

1 – Enables the RB0/INT external interrupt
0 – Disables the RB0/INT external interrupt

RBIE –  RB Port Change Interrupt Enable

1 – Enables the RB port change interrupt
0 – Disables the RB port change interrupt

TMR0IF – Timer 0 Overflow Interrupt Flag

1 – TMR0 register has overflowed. It must be cleared in software.
0 – TMR0 register did not overflow

INTF – RB0/INT External Interrupt Flag

1 – The RB0/INT external interrupt occurred. It must be cleared in software.
0 – The RB0/INT external interrupt did not occur

RBIF – RB Port Change Interrupt Flag

1 – At least one of the RB7 – RB4 pins changed state, a mismatch condition will continue to set
the bit. Reading PORTB will end the mismatch condition and allow the bit to be cleared. It must be cleared in software.
0 – None of the RB7 – RB4 pins have changed state

INTEDG bit of OPTION_REG Register is the Interrupt Edge Select bit. When it is 1 interrupt is on rising edge of RB0/INT pin and when it is 0 interrupt is on falling edge of RB0/INT pin.

Circuit Diagram

Using Interrupt with PIC Microcontroller Circuit Diagram

Using Interrupts with PIC Microcontroller Circuit Diagram

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.


A push button switch is connected to the External Interrupt pin INT of the PIC Microcontroller. When this button is pressed, the microcontroller is interrupted and the ISR is executed. The ISR toggles the status of PORTC for 1 second.

MikroC Code

Interrupts can be easily handled by using reserved word ‘interrupt’. MikroC PRO for PIC Microcontrollers implicitly declares a function ‘interrupt’ to handle interrupts which cannot be redeclared.

void main()
  TRISC = 0; // To configure PORTC as output port
  OPTION_REG.INTEDG = 1; // Set Rising Edge Trigger for INT
  INTCON.GIE = 1; // Enable The Global Interrupt
  INTCON.INTE = 1; // Enable INT
    PORTC = 0x00; //Set some value at PortD

void interrupt() //  ISR
 INTCON.INTF=0; // Clear the interrupt 0 flag
 PORTC=~PORTC; // Invert (Toggle) the value at PortD
 Delay_ms(1000); // Delay for 1 sec

Download Here

You can download the hex file, MikroC source code, Proteus files etc here…

Share this post