Interrupts are signals from a hardware part that can pause the current code and jump to execute a separate piece of code. An example: An interrupt from an IO ports can be used to turn an LED ON, instead of checking if the IO port is high or low. The interrupt executes the code and the user doesn’t have to worry about checking the IO port in the main code.
The ARM Interrupts:
The ARM interrupts are grouped into two
The Non maskable interrupt which are associated with the ARM core, once they are occurring ARM will vector to the relevant routine. Example of NMI is System tick.
The maskable interrupts which are associated with the External peripheral devices of ARM, which accepts interrupt requests only when they are enabled in the NVIC (Nested Vector Interrupt Controller). Each interrupt is associated with an interrupt number that is used in programming the NVIC.
Nested vector interrupt controller:
All interrupts, including core exceptions are managed by the NVIC core exceptions are always accepted. External interrupts are maskable, that is when an event occurs the peripheral will sent an interrupt request if the associated bit in the NVIC is enabled the interrupt will be accepted and forwarded to the associated vector and interrupt service routine. In the LPC1768/66/65/64, the NVIC supports 33 vectored interrupts. All interrupts are serviced in low latency since NVIC is closely associated with the core. NVIC also supports some advanced interrupt handling modes including Interrupt preemption, tail chaining, late arrival. These are the reasons why ARM has low latency and robust response. Regarding priority, every interrupt has associated an 8-bit priority level register. But not all bits are used to set priorities. LPC1768 has 32 priority levels, which means that 4 MSB bits are used to set priorities. The lower the priority number the highest priority of the interrupt. If needed, these bits can be split into two groups where you can create sub-priority level. Subpriority levels are handy when two or more same priority level interrupts occur. Then one with a higher subpriority will be handled first.
Handling external interrupts:
Each peripheral device has one interrupt line connected to the NVIC but may have several interrupt flags. Individual interrupts flags may also represent more than one interrupt source. Any pin on PORT0 and PORT2 (total of 42 pins) regardless of the selected function, can be programmed to generate an interrupt on a rising edge, a falling edge, or both. External interrupts are connected to NVIC through a special external interrupt controller (EXTI). It allows to map multiple GPIO lines to NVIC external interrupt inputs
Steps to setup an interrupt on a GPIO port:
We will be using pin P0.26 as the external interrupt pin. We must configure the microcontroller appropriately.
First modify the PINSEL0 register to change the function of pin P0.26 from general purpose IO (GPIO) to external interrupt 3 (EINT3).
Set the direction of the pin P0.26 as input.
Set internal pull-up or pull-low, according to the type of interrupt which is Rising or Falling.
Create a project using Keil uvision4 for LPC1768 Microcontroller:
In this section, we will start creating a project in Keil MDK we have already installed Keil µVision and Co-MDK Plug-in + CoLinkEx Drivers required for the CoLinkEx programming adapter. You can start by downloading the project files and kick start your practical experiment.
To setup and interrupt the command IOSetInterrupt(Portnum, Bitposition, Direction); is used. Portnum and Bitposition are for the IO port used. Direction is the direction of the interrupt. An interrupt can be rising edge or falling edge.
A correct instruction how to setup an falling edge interrupt on IO port 0.26 is:
IOSetInterrupt(0, 26, 0);
Dir 0 is for a falling edge.
To handle the interrupts the following code has to be placed under the main code:
void EINT3_IRQHandler (void)
Every time a GPIO interrupt happens, the code in the EINT3_IRQHandler() will be executed. The command IOClearInterrupt(); is needed, it will let the interrupt handler know that the interrupts are processed. If the interrupt is not cleared with the command IOClearInterrupt(), the code EINT3_IRQHandler will be in a loop forever.
If GPIO interrupts on multiple pins are used, the code in EINT3_IRQHandler will be executed for every interrupt. To check which pin generated an interrupt, the command IOCheckInterrupts(Portnum, Direction); is used. The command returns a 32 bit variable. If the return value is 1, then an interrupt occurred on the GPIO pin.
There are two ways to check if an interrupt occurred on a specific pin. This example uses pin P0.26. checkvalue = GPIOCheckInterrupts(0, 0);
if (checkvalue == (1 << 26))
//Code to execute if interrupt happened
The above code example is to check for an interrupt on port 0,26 and switches an LED on accordingly.
Fig. 1: Using External Interrupts in LPC1768 Prototype