Working code for external interrupt using EINT3 and two external interrupt GPIOs. Generating two signals on pins p29 & p30, and capturing that signal on pins p5(P0.9) & p6(P0.8) each change asociated with one of the four embedded LEDs.
main.cpp@0:a3cf97864ad5, 2016-02-09 (annotated)
- Committer:
- pistolero992000
- Date:
- Tue Feb 09 02:24:49 2016 +0000
- Revision:
- 0:a3cf97864ad5
Input Capture (External Interrupt) example using two signals and two GPIO for external interrupt.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pistolero992000 | 0:a3cf97864ad5 | 1 | /* |
pistolero992000 | 0:a3cf97864ad5 | 2 | ****************************************************************************************************** |
pistolero992000 | 0:a3cf97864ad5 | 3 | Name : Input Capture |
pistolero992000 | 0:a3cf97864ad5 | 4 | Author : Francisco Pizarro |
pistolero992000 | 0:a3cf97864ad5 | 5 | Version : 1.0 |
pistolero992000 | 0:a3cf97864ad5 | 6 | Copyright : Free |
pistolero992000 | 0:a3cf97864ad5 | 7 | Description : Input capture or External interrupt of two pins |
pistolero992000 | 0:a3cf97864ad5 | 8 | based on: http://anhnvnguyen.blogspot.cl/2010/04/lpc17xx-gpio-basic_05.html |
pistolero992000 | 0:a3cf97864ad5 | 9 | ****************************************************************************************************** |
pistolero992000 | 0:a3cf97864ad5 | 10 | */ |
pistolero992000 | 0:a3cf97864ad5 | 11 | |
pistolero992000 | 0:a3cf97864ad5 | 12 | #include "mbed.h" |
pistolero992000 | 0:a3cf97864ad5 | 13 | #include "LPC17xx.h" |
pistolero992000 | 0:a3cf97864ad5 | 14 | #define GPIO_INT8 1 << 8// GPIO interrupt->p0.9->PIN "p6" |
pistolero992000 | 0:a3cf97864ad5 | 15 | #define GPIO_INT9 1 << 9// GPIO interrupt->p0.9->PIN "p5" |
pistolero992000 | 0:a3cf97864ad5 | 16 | #define LED1 1 << 18// LED1->p1.18 |
pistolero992000 | 0:a3cf97864ad5 | 17 | #define LED2 1 << 20// LED2->p1.20 |
pistolero992000 | 0:a3cf97864ad5 | 18 | #define LED3 1 << 21// LED3->p1.21 |
pistolero992000 | 0:a3cf97864ad5 | 19 | #define LED4 1 << 23// LED4->p1.23 |
pistolero992000 | 0:a3cf97864ad5 | 20 | |
pistolero992000 | 0:a3cf97864ad5 | 21 | DigitalOut Signal1(p30); |
pistolero992000 | 0:a3cf97864ad5 | 22 | DigitalOut Signal2(p29); |
pistolero992000 | 0:a3cf97864ad5 | 23 | |
pistolero992000 | 0:a3cf97864ad5 | 24 | extern "C" { |
pistolero992000 | 0:a3cf97864ad5 | 25 | void EINT3_IRQHandler(void) |
pistolero992000 | 0:a3cf97864ad5 | 26 | { |
pistolero992000 | 0:a3cf97864ad5 | 27 | if (LPC_GPIOINT->IO0IntStatR & GPIO_INT9) { //Evaluates if p5 is interrupted |
pistolero992000 | 0:a3cf97864ad5 | 28 | LPC_GPIOINT->IO0IntClr=GPIO_INT9; // Clears interrupted pin p5 flag |
pistolero992000 | 0:a3cf97864ad5 | 29 | if (LPC_GPIO1->FIOPIN & LED1) { // Toggles LED1 between on & off |
pistolero992000 | 0:a3cf97864ad5 | 30 | LPC_GPIO1->FIOCLR |= LED1; |
pistolero992000 | 0:a3cf97864ad5 | 31 | } else { |
pistolero992000 | 0:a3cf97864ad5 | 32 | LPC_GPIO1->FIOSET |= LED1; |
pistolero992000 | 0:a3cf97864ad5 | 33 | } |
pistolero992000 | 0:a3cf97864ad5 | 34 | } |
pistolero992000 | 0:a3cf97864ad5 | 35 | |
pistolero992000 | 0:a3cf97864ad5 | 36 | if (LPC_GPIOINT->IO0IntStatR & GPIO_INT8) {//Evaluates if p6 is interrupted |
pistolero992000 | 0:a3cf97864ad5 | 37 | LPC_GPIOINT->IO0IntClr=GPIO_INT8; // Clears interrupted pin p5 flag |
pistolero992000 | 0:a3cf97864ad5 | 38 | if (LPC_GPIO1->FIOPIN & LED3) { // Toggles LED3 between on & off |
pistolero992000 | 0:a3cf97864ad5 | 39 | LPC_GPIO1->FIOCLR |= LED3; |
pistolero992000 | 0:a3cf97864ad5 | 40 | } else { |
pistolero992000 | 0:a3cf97864ad5 | 41 | LPC_GPIO1->FIOSET |= LED3; |
pistolero992000 | 0:a3cf97864ad5 | 42 | } |
pistolero992000 | 0:a3cf97864ad5 | 43 | } |
pistolero992000 | 0:a3cf97864ad5 | 44 | } |
pistolero992000 | 0:a3cf97864ad5 | 45 | } |
pistolero992000 | 0:a3cf97864ad5 | 46 | |
pistolero992000 | 0:a3cf97864ad5 | 47 | int main(void) |
pistolero992000 | 0:a3cf97864ad5 | 48 | { |
pistolero992000 | 0:a3cf97864ad5 | 49 | LPC_GPIO0->FIODIR &= ~GPIO_INT8 | ~GPIO_INT9; //set both p5 (p0.9) & p6 (p0.8) as inputs |
pistolero992000 | 0:a3cf97864ad5 | 50 | LPC_GPIO1->FIODIR |= LED1 | LED2 | LED3 | LED4; // set LED pins as outputs (p0.18+p0.20+p0.21+p0.23) |
pistolero992000 | 0:a3cf97864ad5 | 51 | |
pistolero992000 | 0:a3cf97864ad5 | 52 | LPC_GPIOINT->IO0IntEnR |=GPIO_INT8; // Enable external interrupt on p6 on rising edge trigger |
pistolero992000 | 0:a3cf97864ad5 | 53 | LPC_GPIOINT->IO0IntEnR |=GPIO_INT9; // Enable external interrupt on p5 on rising edge trigger |
pistolero992000 | 0:a3cf97864ad5 | 54 | NVIC_EnableIRQ(EINT3_IRQn); // Enable external NVIC EINT3 |
pistolero992000 | 0:a3cf97864ad5 | 55 | |
pistolero992000 | 0:a3cf97864ad5 | 56 | while(1) { // simple square signal generation to test the interrupts on pins p5 & p6 |
pistolero992000 | 0:a3cf97864ad5 | 57 | Signal1=1; LPC_GPIO1->FIOSET |= LED2; |
pistolero992000 | 0:a3cf97864ad5 | 58 | Signal2=1; LPC_GPIO1->FIOSET |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 59 | Signal2=0; LPC_GPIO1->FIOCLR |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 60 | Signal2=1; LPC_GPIO1->FIOSET |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 61 | Signal2=0; LPC_GPIO1->FIOCLR |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 62 | |
pistolero992000 | 0:a3cf97864ad5 | 63 | Signal1=0; LPC_GPIO1->FIOCLR |= LED2; |
pistolero992000 | 0:a3cf97864ad5 | 64 | Signal2=1; LPC_GPIO1->FIOSET |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 65 | Signal2=0; LPC_GPIO1->FIOCLR |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 66 | Signal2=1; LPC_GPIO1->FIOSET |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 67 | Signal2=0; LPC_GPIO1->FIOCLR |= LED4; wait_ms(100); |
pistolero992000 | 0:a3cf97864ad5 | 68 | } |
pistolero992000 | 0:a3cf97864ad5 | 69 | } |