Introduction
The external interrupt input of the LPC1768 is a very important functionality of a software. It is one of the many functions of the system control block that are not related to a specific peripheral device. There are four pins that can be set to function as an external interrupt input (EINTn where n is 0-3). We will be learning how to initialize these pins in the external interrupt input mode and perform an LED switching routine.
Configuration
- PIN SELECTION: The four pins that are used for the external interrupt purpose is located from P2.10 to P2.13 and performs this functionality when the corresponding pins in the PINSEL4 register are set as 01.
- MODE: The external interrupt mode register is a 32-bit register that is used to select the level or edge sensitivity of the respective external interrupt pins.
- POLARITY: Based on level or edge sensitivity, setting the corresponding bits in the polarity register assumes different roles. In level sensitivity mode, the polarity register decides whether the pin is active high or active low. If the mode is edge sensitive, it decides whether the pin should be sensitive to the rising or falling edge.
Firmware
- DEFINITIONS: The definitions that will be used in this program are given below.
#define EINT_FUNC 0x01 #define EINT_CLR 0x11 #define EINT_0 20 #define EINT_1 22 #define EINT_2 24 #define EINT_3 26 #define LEVEL_SENSE 0 #define EDGE_SENSE 1 #define POL_LOWFALL 0 #define POL_HIGHRISE 1 #define EXTINT0 0 #define EXTINT1 1 #define EXTINT2 2 #define EXTINT3 3
- PIN SELECTION: We will be using external interrupt 0 for this tutorial.
LPC_PINCON->PINSEL4 &= (EINT_CLR << EINT_0); LPC_PINCON->PINSEL4 |= (EINT_FUNC << EINT_0);
- MODE: The mode of working that we are going to use is edge triggered external interrupt.
LPC_SC->EXTMODE |= EDGE_SENSE;
- POLARITY: The polarity for the edge-triggered mode that we are using is the falling edge.
LPC_SC->EXTPOLAR|= POL_LOWFALL;
- Finally, we must enable the interrupt control using the non vectored interrupt control function setting the EINT0_IRQn as the parameter for the function. The interrupt handler is called when an interrupt occurs and the LED control is set within the interrupt handler function. We will be using the 16th pin of PORT0 to toggle the led between the interrupts.
NOTE: An important point to note is that the external interrupt flag for the corresponding interrupt pin must be cleared during the interrupt handler function.void EINT0_IRQHandler (void) { static int LED = 0; LPC_SC->EXTINT |= (1<<EXTINT0); LED = ~LED; if(LED==1) LPC_GPIO0->FIOSET = (1<<16); else LPC_GPIO0->FIOCLR = (1<<16); }
Summary
We have learned to program a simple code to set up an external interrupt input and handle a pre-defined task. The sample code for this tutorial is available in the Code Library under the section ARM.