Thursday, October 27, 2011

Understanding Interrupts

Description :
Interrupts are powerful concept embedded systems for controlling events in time-critical environment .In a typical embedded system,the embedded processor (microcontroller) is responsible for doing more than one task (but can do only one at a time ).For example let's say a programmable digital room thermostat,the microcontroller is assigned to monitor the room temperature,turn the AC or heater ON znd OFF,control the LCD display,and respond to any new temperature setting from the user.Out of these the first three taskes are non-time-critical and are executed continuously in sequence one after the other,within the main loop.But when the user presses any button on the setting panel,the microcontroller should be able to read it before the user releases the button.So this is a time-critical event and microcontroller should stop whatever it is doing and responding to this higher priority event.This is possible through the use of interrupts.This tutorial first describes the interrupt system in general and then illustrates how it is handled in PIC Microcontrollers.
Theory :
As mentioned above,interrupts form the basis for separating the time-critical events from the others and execute them in a prioritized manner.An interrupt tells the microcontroller to drop whatever it is doing and execute another program (the Interrupt Service Routine or ISR)stored at a predefined place in the program memory. interrupt requests are asynchronous events which means that an interrupt request can occur at any at any time during the execution of a program.But whenever it occurs,the CPU finishes the current instruction and stores the address for the next instruction (which is in the program counter,or pc) in to the stack memory so that the current program can continue once the interrupt service ends.The CPU them jumps to a certain program memory location (0x0004 in the PIC),where the ISR  begins.At the end of the ISR,execution of the return instruction loads the address at the top of the stack back into the program counter,and execution proceeds from the same point where the processor was interrupted.
A microcontroller has several sources of interrupts,which can be external or internal.The most common internal interrupt sources are timers,EEPROM,ADCmodule,and comparator,External interrupts originate in peripherals and reach the microcontroller through one of its pins and associated ports.
Interrupts in medium-end PIC microcontrollers are fixed,maskable interrupts.This means that interrupts can be globally enabled or disabled,as well as each interrupt source can be individually enabled or disabled. We will consider the PIC 16f688 microcontroller for today's discussion on interrupts.
The PIC 16f688 has the following sources of interrupt :
  • External interrupt at RA2/INT pin
  • TMR0 Overflow Interrrupt
  • portA change Interrupts
  • 2 Comparator Interrupts
  • A/D Interrupt
  • Timer1 Overflow Interrupt
  • EEPROM Data Write Interrupt
  • FAIL-Safe Clock Monitor Interrupt
  • EUSART Receive and Transmit interrupts
Each of these interrupts can be enabled or disabled individually,and more than one interrupt can be active at the same time.When an interrupt is requested,the microcontroller finishes executing the current instruction,stores the value of the program counter in the stack,and jumps to address 0004h in program memory,where the ISR is located.As all interrupt sources make the program to jump to the same place(0004h),the programmer,when writing the ISR,must first find the sepcial function registers,INTCON and PIR1,associated with the interrupt system.The figure below shows the bits in the interrupt control register (INTCON).
Te details for each bit are describehd here :
GIE (global interrupt enable) : This bit is the master switch for all interrupts.Turn it off (GIE=0) and no interrupts are enabled (regardless of the state of their individual enable bits).Turn it on (GIE=1) and interrupts whose individual enable bits are set will be enabled.When an interrupt is requested,this bit is automatically set to 0,thus any further interrupts. The return instruction at the end of the interrupt service subroutine sets bit GIE back to 1,thus enabling the global interrupt system.
PEIE(peripheral interrupt enable) : Is a mini-master switch for a group of interrupts which are know as 'peripheral interrupts'.They have thier own enable bits in the PIE1 register (discussed later).This bit must be set to enable any enabled peripheral interrupts.
T0IE,T0IF : These bits are related to timer0 overflow interrupt.When T0IE=1,the timer0 interrupt is enabled.T0IF is set to 1 whenever TMR0 overflows from 255 to 0.This discussed in PIC Timers and Counters article too.
INTE,INTF : These bits are related to the external interrupt which depends on the state of the RA2/INT  pin of PIC 16f688 .Bit INTE  enables this interrupt. The interrupt can be set to trigger on the rising edge or falling edge of the signal on INT pin.This is done using the bit 6 (INTEDG) of the OPTION register (shown below).Bit INTF is a flag that indicates the detection of an edge (rising or falling) in the pin INT .
RAIE,RAIF : These bits are related to the interrupt due to change of logic level at PORTA pins.RAIE=1 enables this interrupt and RBIF is a flag that indicates a change in one of the pins of PORTA.In order to select which PORTA pin should be able to trigger the interrupt,the corresponding bit of INTERRUPT-ON-CHANGE PORTA (IOCA)register must be set.
The individual bits of OPTION,PIE1 and IOCA registers are shown in the figures below .

Please read datasheet of 16f688 for the detail description of these registers.In the folowing section,we will discuss how to program the interrupt that originates in the RA2/INT line of 16f688.
In order to intialize the RA2/INT interrupt,the following operations must take place :

  1. PortA ,line2(RA2) must be intialized for input
  2. The interrupt source must be set to take place either on the falling or the rising edge of the signal using INTEDG bit of OPTION-REG.
  3. The external interrupt flag (INTF in the INTCON Register) must be initially cleared
  4. Global interrupts must be enabled by setting the GIE bit in the INTCON Register
  5. The External Interrupt on  RA2/INT must be enabled by setting the INTE bit in the INTCON Regester.
A sample program using RA2/INT interrupt is developed later in this articale

Circuit Diagram :
In the circuit diagram shown below,a push button switch is wired to the RA2 port of 16f688.This switch produces the interrupt when pressed.Four red color LEDs are connected to RC0 trough RC3 port and an yellow led is wired to port RA0.The main program is a 4-bit binary up counter that increments at rate of approximatley one second.The value of the counter is sent to PORT-C and displayed on the four red LEDs.The yellow LED is toggled on and off when the pushbutton switch is pressed .The switch is active low and therefore,the interrupt is programmed on the falling edge of the signal(INTEDG=0).The 16f688 runs at 4MHz clock derived from the internal source.
Software :
The Interrupt Service Routine (ISR) depends on the specific application.However,certain steps are applicable to all cases.For example,the ISR should begin by checking which particular event triggered the interrupt(if more than one input is enabled).In this case,the INTF bit of INTCON regidter must be checked to verify that the cource of interrupt is from RA2/int pin.Similary,the interrupt flag(INTF bit in INTCON register)lust be cleared by the ISR before returning to the main program otherwise an endless series of interrupts will occur.This is because on return the CPU finds the interrupt flag still set and assumes it as another interrupt and jumps back to ISR.
In PIC microcontrollers,the processor automaticlly clears the GIE bit in the INTCON register when an interrrupt occurs.This means that no interrupt can take place in the ISR,otherwise you can imagine the havoc that would take place if it should not be the case.When the processor executes the return statement in the ISR,the GIE bit is set and the interrupts are enabled again.
The interrupt programming for this experiment is written in mikroc pro for pic.it is a C compiler for PIC from mikroElectronika .In mikroc,the ISR is written just like another subroutine but with a reseved name,interrupt.Within the ISR,appropriate flag bits are checked to identify the source of interrupt.The complete program for this experiment is given below.

/*
Lab 16: Understanding Interrupts
MCU: PIC16F688
Internal Clock @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
by charaf zouhair
oct 27, 2011
*/

sbit LED at RA0_bit;  // Interrupt LED
unsigned short count;

// Interrupt service routine
void interrupt(){
 if(INTCON.INTF == 1) LED = ~LED;
 delay_ms(200);   // Key debounce time
 INTCON.INTF = 0;
}

void main() {
 ANSEL = 0b00000000;    // All I/O pins are configured as digital
 CMCON0 = 0x07 ;        // Disbale comparators
 TRISC = 0b00010000;    // PORTC all outputs except RC4
 TRISA = 0b00000100;    // PORTA all outputs except RA2
 INTCON = 0b10010000;   // Set GIE and INTE bits
 OPTION_REG.INTEDG = 0; // External interrupt on falling edge of RA2/INT pin
 count = 0;
 LED = 0;
 PORTC = count;
 do {
  count ++;
  if (count ==16) count =0;
  PORTC = count;
  delay_ms(1000);
 }while (1); // infinite loop
}
Output:
You will see the microcontroller will keep counting from 0 to 15 and roll over to 0 again and keep it continue.Whenever you press the interrupt switch,the processor will respond to it by changing the status of the yellow LED and then will resume the counting  task

Summary :
  • An interrupt is an event that requires the CPU to stop normal program execution and perform some service (the ISR) related to the event.
  • An interrupt can be generated internally (inside the chip by ADC,EEPROM,TIMERS,etc) or externally (outside the chip through the INT pin).
  • proper use of interrupts allows more efficient utilization of the CPU time.
  • Interrupts are very helpful to perform time-critical applications.Many emergent events,such as power failure and process control,require the CPU to take action immediately.
The interrupt mechanism provides a way to force the CPU to divert from normal program execution and take immediate actions.
References:
PIC MICROCONTROLLER


for more information contact me : charaf_zohair@hotmail.com


No comments: