Manuel Campos
/
digitalInInterrupt_solution
solution interrupt program
Fork of digitalInInterrupt_sample by
main.cpp@5:dd7d8fb1ad75, 2018-02-02 (annotated)
- Committer:
- cvmanuel
- Date:
- Fri Feb 02 18:21:27 2018 +0000
- Revision:
- 5:dd7d8fb1ad75
- Parent:
- 4:5af35f086ddd
interrupts solution;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:a66a8cb0012c | 1 | #include "mbed.h" |
WilliamMarshQMUL | 0:a66a8cb0012c | 2 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 3 | // Labs 2: Example program for using an interrupt (or callback) |
WilliamMarshQMUL | 1:13e0c1956b54 | 4 | // ----------------------------------------------------------- |
WilliamMarshQMUL | 1:13e0c1956b54 | 5 | // A callback function (corresponding to an ISR) is called when a button |
WilliamMarshQMUL | 1:13e0c1956b54 | 6 | // is pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 7 | // The callback uses a shared variable to signal another thread |
WilliamMarshQMUL | 0:a66a8cb0012c | 8 | |
cvmanuel | 4:5af35f086ddd | 9 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 10 | InterruptIn button(PTD0); |
cvmanuel | 4:5af35f086ddd | 11 | DigitalOut led(LED_BLUE); |
cvmanuel | 4:5af35f086ddd | 12 | DigitalOut led2(LED_RED); |
cvmanuel | 4:5af35f086ddd | 13 | InterruptIn button2(PTD5); |
cvmanuel | 4:5af35f086ddd | 14 | |
cvmanuel | 4:5af35f086ddd | 15 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 16 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 17 | volatile int pressEvent = 0 ; |
cvmanuel | 4:5af35f086ddd | 18 | volatile int pressEvent2 = 0 ; |
cvmanuel | 4:5af35f086ddd | 19 | |
cvmanuel | 5:dd7d8fb1ad75 | 20 | volatile bool pressed1 = false; |
cvmanuel | 5:dd7d8fb1ad75 | 21 | volatile bool pressed2 = false; |
WilliamMarshQMUL | 0:a66a8cb0012c | 22 | |
WilliamMarshQMUL | 3:05b6a1431a6b | 23 | // This function is invoked when then interrupt occurs |
WilliamMarshQMUL | 3:05b6a1431a6b | 24 | // Signal that the button has been pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 25 | // Note: bounce may occur |
WilliamMarshQMUL | 1:13e0c1956b54 | 26 | void buttonCallback(){ |
WilliamMarshQMUL | 1:13e0c1956b54 | 27 | pressEvent = 1 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 28 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 29 | |
cvmanuel | 4:5af35f086ddd | 30 | void buttonCallback2(){ |
cvmanuel | 4:5af35f086ddd | 31 | pressEvent2 = 1 ; |
cvmanuel | 4:5af35f086ddd | 32 | } |
cvmanuel | 4:5af35f086ddd | 33 | |
WilliamMarshQMUL | 3:05b6a1431a6b | 34 | /* ---- Main function (default thread) ---- |
WilliamMarshQMUL | 3:05b6a1431a6b | 35 | Note that if this thread completes, nothing else works |
WilliamMarshQMUL | 3:05b6a1431a6b | 36 | */ |
WilliamMarshQMUL | 3:05b6a1431a6b | 37 | int main() { |
WilliamMarshQMUL | 3:05b6a1431a6b | 38 | button.mode(PullUp); // Ensure button i/p has pull up |
WilliamMarshQMUL | 3:05b6a1431a6b | 39 | button.fall(&buttonCallback) ; // Attach function to falling edge |
cvmanuel | 4:5af35f086ddd | 40 | |
cvmanuel | 4:5af35f086ddd | 41 | button2.mode(PullUp); // Ensure button i/p has pull up |
cvmanuel | 4:5af35f086ddd | 42 | button2.fall(&buttonCallback2) ; // Attach function to falling edge |
WilliamMarshQMUL | 3:05b6a1431a6b | 43 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 44 | while(true) { |
WilliamMarshQMUL | 3:05b6a1431a6b | 45 | // Toggle the LED every time the button is pressed |
WilliamMarshQMUL | 0:a66a8cb0012c | 46 | if (pressEvent) { |
cvmanuel | 4:5af35f086ddd | 47 | pressEvent = 0; // Clear the event variable |
cvmanuel | 4:5af35f086ddd | 48 | pressed1 = !pressed1; |
cvmanuel | 4:5af35f086ddd | 49 | } |
cvmanuel | 4:5af35f086ddd | 50 | if (pressEvent2) { |
cvmanuel | 4:5af35f086ddd | 51 | pressEvent2= 0 ; // Clear the event variable |
cvmanuel | 4:5af35f086ddd | 52 | pressed2= !pressed2; |
cvmanuel | 4:5af35f086ddd | 53 | } |
cvmanuel | 4:5af35f086ddd | 54 | if(!pressed1) |
cvmanuel | 4:5af35f086ddd | 55 | { |
WilliamMarshQMUL | 0:a66a8cb0012c | 56 | led = !led ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 57 | } |
cvmanuel | 4:5af35f086ddd | 58 | if(!pressed2) { |
cvmanuel | 4:5af35f086ddd | 59 | led2= !led2; |
cvmanuel | 4:5af35f086ddd | 60 | } |
cvmanuel | 5:dd7d8fb1ad75 | 61 | Thread::wait(500) ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 62 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 63 | } |