[?] Subscribe To This Site
A Hardware Interrupt Tutorial.
An interrupt is a signal that stops the current program forcing it to execute another program immediately. The interrupt does this without waiting for the current program to finish. It is unconditional and immediate which is why it is called an interrupt.
The whole point of a interrupt is that the main program can perform a task without worrying about an external event.
For example if you want to read a push button connected to one pin of an input port you could read it in one of two ways either by polling it or by using interrupts.
Polling is simply reading the button input regularly. Usually you need to do some other tasks as well e.g. read an RS232 input so there will be a delay between reads of the button. As long as the delays are small compared to the speed of the input change then no button presses will be missed.
If however you had to do some long calculation then a keyboard press could be missed while the processor is busy. With polling you have to be more aware of the processor activity so that you allow enough time for each essential activity.
By using a hardware interrupt driven button reader the calculation could proceed with all button presses captured. With the interrupt running in the background you would not have to alter the calculation function to give up processing time for button reading.
The interrupt routine obviously takes processor time – but you do not have to worry about it while constructing the calculation function.
You do have to keep the interrupt routine small compared to the processing time of the other functions - its no good putting tons of operations into the ISR. Interrupt routines should be kept short and sweet so that the main part of the program executes correctly e.g. for lots of interrupts you need the routine to finish quickly ready for the next one.
Hardware Interrupt or polling ?
The benefit of the hardware interrupt is that processor time is used efficiently and not wasted polling input ports. The benefit of polling is that it is easy to do.
Another benefit of using interrupts is that in some processors you can use a wake-from-sleep interrupt. This lets the processor go into a low power mode, where only the interrupt hardware is active, which is useful if the system is running on batteries.
Hardware interrupt Common terms
Terms you might hear associated with hardware interrupts are ISR, interrupt mask, non maskable interrupt, an asynchronous event, interrupt vector and context switching.
An ISR is simply another program function. It is no different to any other function except that it does context switching (saving/restoring processor registers) and at the end of the function it re-enables interrupts.
After the ISR finishes program execution returns to the original program and it continues exactly from where it was interrupted. The orriginal program will have no idea that this has happened.
Hardware Interrupt vector
This is a fixed address that contains the location of your ISR – for a PIC micro it is usually address 4. In other micros there may be an interrupt vector for each vector – you have to make an interrupt routine for each one that you want to use. For PIC micros you have just one interrupt and you have to detect which interrupt triggered by examining the interrupt flag register(s).
You program the interrupt address with the address of your interrupt routine. Whenever the interrupt is triggered (and if the interrupt is unmasked) program operation jumps to the location of your interrupt routine.
Note: high level language compilers take care of all of this for you - in 'C' you just declare the function using the keyword interrupt (as the type returned from the function). It then puts the address of this routine in the interrupt vector.
The NMI is exactly the same as a normal interrupt except that you can not control whether or not it is active - it's always active. It is more commonly found on larger (non-microcontroller) processors as a dedicated input pin. In this case it is more than likely fed with a brown-out (PSU voltage dip) detector circuit.
You don't need it in a microcontroller as you can achieve exactly the same functionality using a programmable interrupt and many microcontrollers have built in BODs.
This is simply an event that is not synchronised to the processor clock. It is an event that the processor can not predict e.g. A button press.
This means that the register state is preserved at the start of an ISR and restored at the end of an ISR. It ensures that the function that was interrupted is not affected by the ISR.
A Basic Interrupt System.
This is a general description of an interrupt system (biased slightly to PIC micros) which is true for any interrupt module and it is useful for understanding how to control and use interrupts.
These are signals that start an interrupt. They can be external pins (where a rising or falling edge of a input triggers the interrupt) or internal peripheral interrupts e.g. ADC completed, Serial Rx data received, timer overflow etc. (The 16F877 has 15 interrupt sources).
Note: you can ususlly control which edge (rising or falling) is used by setting bits in yet another register!
Each hardware interrupt source has an associated interrupt flag - whenever the interrupt is triggered the corresponding interrupt flag is set. These flags are usually stored as bits within an interrupt register.
The processor can read from and write to the interrupt register, reading from it to find out which interrupts occurred and writing to it to clear the interrupt flags.
The interrupt mask has a set of bits identical to those in the interrupt register. Setting any bit (or unmasking) lets the corresponding signal source generate an interrupt - causing the processor to execute the ISR.
Note: When a bit in the mask is clear (masked) the ISR is not activated for that signal source but the interrupt register is still set by the signal source. So you can still detect what the hardware is doing by polling the interrupt register.
Hardware interrupt vector
The interrupt vector is a location in memory that you program with the address of your interrupt service routine (ISR). Whenever an unmasked interrupt occurs program execution starts from the address contained in the interrupt vector.
For PlC micros the hardware interrupt vector address is usually 0004.
Jump from Hardware Interrupt to:
Best-microcontroller-projects home page.