Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 1 month ago.
Is there solution for defining of interrupt
I have to processing a lot of interrupts. How can I define current interrupt that happens in this example?
#include "mbed.h" InterruptIn event1(p16); InterruptIn event2(p15); InterruptIn event3(p14); DigitalOut led(LED1); void trigger() { printf("triggered!\n"); } int main() { event1.rise(&trigger); event2.rise(&trigger); event3.rise(&trigger); while(1) { led = !led; wait(0.25); } }
1 Answer
6 years, 1 month ago.
Hello Igor,
We can try to use lambda expressions for that. But because the InterruptIn
doesn't provide pin name, first we have to derive our own class. For example as below:
#include "mbed.h" class MyInterruptIn : public InterruptIn { PinName _pin; public: MyInterruptIn(PinName pin) : InterruptIn(pin), _pin(pin) {} PinName pin() { return _pin; } }; MyInterruptIn myPins[] = { MyInterruptIn(p14), MyInterruptIn(p15), MyInterruptIn(p16) }; DigitalOut led(LED1); int main() { for (int i = 0; i < (sizeof(myPins) / sizeof(*myPins)); i++) myPins[i].rise([i]() { printf("Pin %d triggered!\r\n", myPins[i].pin()); }); while(1) { led = !led; wait(0.25); } }
If position in array is sufficient for us then:
#include "mbed.h" InterruptIn myPins[] = { InterruptIn(p14), InterruptIn(p15), InterruptIn(p16) }; DigitalOut led(LED1); int main() { for (int i = 0; i < (sizeof(myPins) / sizeof(*myPins)); i++) myPins[i].rise([i]() { printf("Pin %d triggered!\r\n", i); }); while(1) { led = !led; wait(0.25); } }
Hi, I have tried your last example, but the compiler shows:
Error: Expected an expression in "main.cpp", Line: 13, Col: 25
line:
myPins[i].rise([i]() { printf("Pin %d triggered!\r\n", i); });
Sorry, I compiled the program offline with the Arm GCC toolchain. The online compiler is using the Keil Arm toolchain and it seems that that one does not support C++11 : (
posted by 03 Oct 2018oh, it's a bad news I guess... Can you please ansew for another my question, I tried to use 4 fall interrupts, but calls only 2 last added. I have no any idea why, maybe there is online compiler problem?
posted by 03 Oct 2018The number of available external interrupt lines is usually less than the number of GPIO pins. That's why the GPIO pins are grouped and each group is connected to different interrupt line. If it happens that you select two pins that are in the same group (connected to the same interrupt line) then only the latter will work. I'd recommend to read the user manual of your MCU to see how the GPIO are grouped and connected to the individual interrupt lines.
Have a look also at
https://os.mbed.com/questions/78709/What-are-mbed-files-that-contain-pin-ext
https://os.mbed.com/questions/78961/Interrupts-help/#answer13323