Using Interrupt with PIC Microcontroller

PIC16F877A

Using Interrupt with PIC Microcontroller

Introduction

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 :



  • INTCON
  • 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.

Working

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
  while(1)
  {
    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
}

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

Using Interrupt with PIC Microcontroller

Buy Here

  • Sale
    Crystal OscillatorCrystal Oscillator

    Crystal Oscillator

    0 out of 5

    A Crystal Oscillator is an electronic oscillator which is commonly used to provide clock signals to Microcontrollers and Microprocessors. It creates electrical signals with a particular frequency having very high stability. It takes the advantage of vibrating crystal of piezoelectric material to generate precise frequency of oscillations.

    10.00 8.00
  • -31%
    PIC 16F877APIC 16F877A

    PIC 16F877A Microcontroller

    0 out of 5

    This powerful (200 nanosecond instruction execution) yet easy-to-program (only 35 single word instructions) CMOS FLASH-based 8-bit microcontroller packs Microchip's powerful PIC architecture into an 40- or 44-pin package and is upwards compatible with the PIC16C5X, PIC12CXXX and PIC16C7X devices. The PIC16F877A features 256 bytes of EEPROM data memory, self programming, an ICD, 2 Comparators, 8 channels of 10-bit Analog-to-Digital (A/D) converter, 2 capture/compare/PWM functions, the synchronous serial port can be configured as either 3-wire Serial Peripheral Interface (SPI™) or the 2-wire Inter-Integrated Circuit (I²C™) bus and a Universal Asynchronous Receiver Transmitter (USART). All of these features make it ideal for more advanced level A/D applications in automotive, industrial, appliances and consumer applications.

    200.00 139.00
  • -21%
    Out of stock
    USB PIC ProgrammerPICKit2 Clone

    USB PIC Programmer PICkit 2

    0 out of 5

    The PICkit 2 programmer is a development tool with a easy to use interface for programming and debugging Microchip’s flash families of micro-controllers. The windows programming interface supports all commonly available PIC microcontrollers. With Microchip’s powerful MPLAB Integrated Development Environment (IDE) the PICkit 2 enables in circuit debugging on most PIC microcontrollers.

    1,170.00 929.00

Share this post

  • bandara

    Thanx man. This helps a lot!!!

  • Ammar

    I like the way you explained it

  • Guest

    please provide some examples in htc, microc ..

  • sujith thycattussery

    HTC code?

  • sujith thycattussery

    How to add more push buttons?

  • Will be published in future..

  • Interrupt is not intended to connect push button switches… It is used above to demonstrate the working of interrupt..

  • sujith thycattussery

    So, How we can manage button events?

  • You can connect push button to any pin of microcontroller which is configured as input pin…
    You can check the status of that pin through the program…
    Read the following article :
    http://electrosome.com/push-button-switch-pic-microcontroller/

  • PICMICRO

    Hi,George

    How can i count number of interrupt received by the PIC for a specific time?for example if i want to count the rising edge number for the period of 30sec.what will be code?

  • Try doing like this :
    unsigned int cnt;
    void main()
    {
    cnt = 0;


    }
    void interrupt()
    {

    ..
    cnt++;

    }

  • Ayoub AKIL

    Hi how can i use interrupt in the uart receiving to programme a circular buffer
    thx a lot for reply

  • Zoltan Lánczky

    Hi,I have tried to rebuild your project but did not succeded.

    Here is my code but doesnot work and I dont know why, any glue?
    (PIC16F887, 4MHz, the diargam is the same.)

    void interrupt(){

    // if (INTCON.RBIF==1) { // RB interrupt jött

    PORTD.F7 = ~PORTd.F7 ;

    INTCON.INTF = 0; // Clear the interrupt 0 flag

    delay_ms(1000);

    // INTCON.RBIF = 0; // RB interrupt jelző törlés

    // }

    }

    ////////////////////////////////////////////////////////////////

    void main() {

    TRISA = 0; // PORTA is digital, not analog

    TRISB = 0b1; // PORTB R0 és R1 INPUT

    TRISC = 0; // PORTC is Output

    TRISD = 0; // PORTD is output

    // WPUB = 0; // WEAK PULL UP 1 enabled

    INTCON.GIE = 1; //Enable global interrupt function – FŐ engedély

    INTCON.INTE = 1; //Enable RB0/INT externalnterrupt

    // INTCON.PEIE = 1; //Enable all unmasked peripheral interrupt

    // INTCON.RBIE = 1; //ENable RB int

    OPTION_REG.INTEDG = 1; //Interrupt on rising edge

    // IOCB = 0b11; // enable rb0 rb1 interrupt

    do {

    PORTc.F7 = ~PORTc.F7 ;

    delay_ms(1000);

    } while (1);

    }

  • For that you should enable UART Interrupt.. Check Interrupt Registers.

  • Hello, the above program is for PIC 16F877A not for 16F877

  • Zoltan Lánczky

    Hi, I know that, furtunately found the problem. 16F887 needs to clear ANSELH.
    Anyway thankx for the project, I learned a lot.

  • taha

    can u explain for pic18f452 and how i can choose one of three interrupt?

  • I will try to post a tutorial.

  • Cj92

    Hi, i’m new to PIC programming. Can’t find a clue to what this line is for in the article! Can you explain it?
    “OPTION_REG.INTEDG = 1; // Set Rising Edge Trigger for INT”

  • It is already explained in the article.
    The interrupt will be triggered during rising edge (LOW to HIGH transition) of RB0/INT interrupt pin..

  • seshu

    how to use PORTB Change Interrupts in the controller

  • PORTB on change interrupt is used in the following project :
    https://electrosome.com/hc-sr04-ultrasonic-sensor-pic/