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.
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.
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.
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 is a readable and writeable register which contains various enable and flag
bits for External and Internal Interrupts.
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
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
}