Multiple External Interrupts in PIC16F877A

We have already seen External Interrupt, Serial, Timer Interrupts in our previous tutorial. In this tutorial we are going to see How to use Multiple External Interrupts in PIC16F877A.

Interrupts in PIC16F877A

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.

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

As we  know already, we have only one external interrupt which is in PortB.0. For example in my project i need to use many external interrupts. How to use many since we have only one in PORTB.0. So In this tutorial we will see How to use Multiple External Interrupts in PIC16F877A.

PORTB Interrupt in PIC16F877A

In PIC16F877A we have one interrupt which is PORTB change interrupt. This Interrupt is used to identify the Port change in PORTB. This will monitor only the PORTB.4, PORTB.5,  PORTB.6 and PORTB.7. So first we will see this PORTB change interrupt. For this you have to familiar about the interrupt register available in PIC16F877A. We’ve already discussed about those registers. You can refresh here.

Steps to be followed

  1. Enable PORTB Change Interrupt.
  2. Connect external Interrupts pins to RB4, RB5, RB6 and RB7 (PORTB.4 – PORTB.7).
  3. Whenever anyone of the RB4, RB5, RB6, RB7 state changes, ISR will hit.
  4. In ISR you should read the PORTB register. Then only it will clears the PORTB interrupt.

Source Code

Whenever anyone of the RB4, RB5, RB6, RB7 state changes, Interrupt will happen. In ISR I am toggling the LED in PORTD.

Output

PORTB-Interrupt-in-PIC16F877A Multiple External Interrupts in PIC16F877A

Multiple External Interrupts in PIC16F877A

Steps to be followed

  1. Before Initializing the Interrupt first store the PORTB value into one variable.
  2. Enable PORTB Change Interrupt.
  3. Connect external Interrupts pins to RB4, RB5, RB6 and RB7 (PORTB.4 – PORTB.7).
  4. Whenever ISR hits, you need to read PORTB value and compare with old PORTB value using XOR method.
  5. Then you will get the new value and then using OR operator you can find the exact pin interrupted.

Source Code

In this source, whenever PORTB.4 – PORTB.7 changes, I’ve blink the respected LED connected in PORTD.

Output

Multiple-Externel-Interrupts-in-PIC16F877A Multiple External Interrupts in PIC16F877ASo now we have 5 external interrupts including PORTB.0 interrupt. You can play with it.

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!