An interrupt is the way that the outside world attracts the attention of a processor. In a PC interrupts are caused by many external devices, for example keyboards, sound cards, network cards and hard disks.
When the interrupt occurs the processor will stop the foreground program and divert to an interrupt handler which is a piece of code which will deal with the particular event. When the event has been dealt with the interrupt handler will obey an instruction which returns execution to the foreground program.
The PIC microcontroller has only a single interrupt, whereas other computer processors may have several. Interrupts can be generated from a number of sources:
-
External interrupt signal (on PORTB bit 0)
-
Timer Overflow
-
Change on inputs (on PORTB bits 4-7)
-
EEPROM write complete
We will use some of these interrupts in the practical exercises. When any of the above interrupts occurs the PIC microcontroller branches to a particular address. In the case of BoostC a call is made to a function with the special name of interrupt. The function must then decide which of the four items above caused the interrupt and then invoke an appropriate handler.
On the right you can see the bit assignments in the INTCON register and how they are used.
INTCON - INTerrupt CONtrol Register
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
GIE |
PEIE |
TMR0IE |
INTE |
IOCIE |
TMR0IF |
INTF |
IOCIF |
These are the bits which are used to control interrupts in the PICmicro. You can read or write these bits, so that you can read back settings which you have made.
If the name ends in E this means that the bit is an enable bit which must be set to enable that particular interrupt source. If the name ends in an F this means that the bit is a flag bit and indicates the source of an interrupt. Note that the flag bits are set by the hardware irrespective of whether or not the interrupts are enabled.
GIE - Global Interrupt Enable
If this bit is clear the PICmicro will not respond to interrupts. Setting this bit "turns interrupts on". After reset interrupts are turned off. Note that even if the interrupts are turned off the flag bits still function as below and can be read and reset by software.
PEIE - Peripheral Interrupt Enable
If this bit is set the PICmicro will generate an interrupt when one of the peripherals enabled by the PIE enable mask fires an event.
TMR0IE - Timer Overflow Interrupt Enable
If this bit is set the PICmicro will be interrupted each time the timer "wraps round" from 255 to 0 (see the timer section for more details on this)
INTE - External INTerrupt Flag
If this bit is set the PICmicro will be interrupted each time the pin transitions round" from 1 to 0 or a 0 to a 1 (see the RB0/INT section for more details on this)
IOCIE - Interrupt On Change Enable
You can configure the PICmicro so that a change in input bits 4-7 on PORTB can trigger an interrupt. This is useful if you want to jump to a routine to read buttons or inputs when they change. It saves having to poll the inputs looking for a change. In an alternative configuration bit 0 of PORTB can be used as an interrupt source. In either case this bit must be set for interrupts from PORTB to be acted on.
TMR0IF - Timer Overflow Interrupt Flag
This flag is set by the PICmicro when the timer overflows. This makes it possible for the interrupt handler to decide that the timer was the cause of the interrupt and act accordingly. Note that to get the next interrupt from the timer you must clear this flag in your interrupt handler to allow the interrupt to occur again.
INTF - External INTerrupt Flag
This bit is set to indicate that an external interrupt signal on bit 0 of PORTB has caused the interrupt. To allow another interrupt from this source you must clear the bit in software.
IOCIF - Interrupt On Change Flag
If this bit is set it indicates that the interrupt was caused by a change in the input bits 0-4 on PORTB.