How to handle interrupts in PIC 16F877A

Microcontroller Topics
Post Reply
User avatar
Shehani
Lieutenant
Lieutenant
Posts: 61
Joined: Mon Aug 19, 2013 2:11 pm

How to handle interrupts in PIC 16F877A

Post by Shehani » Fri Aug 23, 2013 2:51 pm

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.

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
- PIR
- PIE2
- PIR2

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

INTCON Register
1.png
1.png (5.89 KiB) Viewed 4477 times
INTCON Register PIC 16F877A


INTCON Register is a readable register which contains various enable and flag bite 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
2.png
2.png (212.55 KiB) Viewed 4477 times
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.

Code: Select all

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
}
Article courtesy of electrosome.com
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How to handling interrupting PIC 16F877A

Post by Nandika » Fri Aug 23, 2013 11:06 pm

Really good post. :biggrin:
Thank you.

I like to add more useful things to your small sample program according to my knowledge.
In this ISR waiting for only RB0 interrupts for Toggle PORTC LEDs. Some time another interrupts can occur .Because Global interrupts are enable.(GIE=1)Therefore,In ISR must check for RB0 interrupts.

Code: Select all

void interrupt() //  ISR
{
if(INTCON.INTF){
	INTCON.INTF=0; // Clear the interrupt 0 flag(Data sheet said "it must be cleared by software")
	PORTC=~PORTC; // Invert (Toggle) the value at PortD
	Delay_ms(1000); // Delay for 1 sec
}
}
I think this is useful usage of Interrupt Flag bit.
And,You had some typing mistake.There is no PIR register in PIC16F877A,It must be PIR1. ;)
Post Reply

Return to “Microcontrollers”