Soheil Novinfard
/
digitalInInterrupt_modified
Interrupt - modified version
Fork of digitalInInterrupt_sample by
main.cpp@2:071f22412cdc, 2017-01-24 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Tue Jan 24 18:31:35 2017 +0000
- Revision:
- 2:071f22412cdc
- Parent:
- 1:13e0c1956b54
- Child:
- 3:05b6a1431a6b
Change to falling edge
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 | #include "rtos.h" |
WilliamMarshQMUL | 0:a66a8cb0012c | 3 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 4 | // Labs 2: Example program for using an interrupt (or callback) |
WilliamMarshQMUL | 1:13e0c1956b54 | 5 | // ----------------------------------------------------------- |
WilliamMarshQMUL | 1:13e0c1956b54 | 6 | // A callback function (corresponding to an ISR) is called when a button |
WilliamMarshQMUL | 1:13e0c1956b54 | 7 | // is pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 8 | // The callback uses a shared variable to signal another thread |
WilliamMarshQMUL | 0:a66a8cb0012c | 9 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 10 | InterruptIn button(PTD0); |
WilliamMarshQMUL | 0:a66a8cb0012c | 11 | DigitalOut led(LED_RED); |
WilliamMarshQMUL | 0:a66a8cb0012c | 12 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 13 | Thread flashT ; |
WilliamMarshQMUL | 1:13e0c1956b54 | 14 | volatile int pressEvent = 0 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 15 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 16 | // Signal when the button is pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 17 | // Note: bounce may occur |
WilliamMarshQMUL | 1:13e0c1956b54 | 18 | void buttonCallback(){ |
WilliamMarshQMUL | 1:13e0c1956b54 | 19 | pressEvent = 1 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 20 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 21 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 22 | // Toggle the LED every time the button is pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 23 | // Note: the LED could be toggled in the callback |
WilliamMarshQMUL | 0:a66a8cb0012c | 24 | void flash() { |
WilliamMarshQMUL | 0:a66a8cb0012c | 25 | while(true) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 26 | if (pressEvent) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 27 | led = !led ; |
WilliamMarshQMUL | 1:13e0c1956b54 | 28 | pressEvent = 0 ; // Clear the event variable |
WilliamMarshQMUL | 0:a66a8cb0012c | 29 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 30 | Thread::wait(100) ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 31 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 32 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 33 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 34 | int main() { |
WilliamMarshQMUL | 1:13e0c1956b54 | 35 | button.mode(PullUp); // Ensure button i/p has pull up |
WilliamMarshQMUL | 2:071f22412cdc | 36 | button.fall(&buttonCallback) ; // Attach function to falling edge |
WilliamMarshQMUL | 1:13e0c1956b54 | 37 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 38 | flashT.start(&flash) ; // Start the flashing thread running |
WilliamMarshQMUL | 0:a66a8cb0012c | 39 | } |