Lab-2 Digital Interrupt by Edwin Kadavy
Fork of digitalInInterrupt_sample by
main.cpp@4:54ff81b0e148, 2018-02-02 (annotated)
- Committer:
- edwinkad
- Date:
- Fri Feb 02 21:29:00 2018 +0000
- Revision:
- 4:54ff81b0e148
- Parent:
- 3:05b6a1431a6b
Lab_2 Digital Interrupt by Edwin Kadavy
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 | // ----------------------------------------------------------- |
edwinkad | 4:54ff81b0e148 | 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 | |
edwinkad | 4:54ff81b0e148 | 9 | InterruptIn button1(PTD0); |
edwinkad | 4:54ff81b0e148 | 10 | InterruptIn button2(PTD2); |
edwinkad | 4:54ff81b0e148 | 11 | DigitalOut led1(LED_RED); |
edwinkad | 4:54ff81b0e148 | 12 | DigitalOut led2(LED_BLUE); |
WilliamMarshQMUL | 0:a66a8cb0012c | 13 | |
edwinkad | 4:54ff81b0e148 | 14 | volatile int pressEvent1 = 0 ; |
edwinkad | 4:54ff81b0e148 | 15 | volatile int pressEvent2 = 0 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 16 | |
WilliamMarshQMUL | 3:05b6a1431a6b | 17 | // This function is invoked when then interrupt occurs |
WilliamMarshQMUL | 3:05b6a1431a6b | 18 | // Signal that the button has been pressed |
edwinkad | 4:54ff81b0e148 | 19 | // Note: bounce may occur |
edwinkad | 4:54ff81b0e148 | 20 | void buttonCallback1() |
edwinkad | 4:54ff81b0e148 | 21 | { |
edwinkad | 4:54ff81b0e148 | 22 | pressEvent1 = 1 ; |
edwinkad | 4:54ff81b0e148 | 23 | } |
edwinkad | 4:54ff81b0e148 | 24 | void buttonCallback2() |
edwinkad | 4:54ff81b0e148 | 25 | { |
edwinkad | 4:54ff81b0e148 | 26 | pressEvent2 = 1 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 27 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 28 | |
WilliamMarshQMUL | 3:05b6a1431a6b | 29 | /* ---- Main function (default thread) ---- |
WilliamMarshQMUL | 3:05b6a1431a6b | 30 | Note that if this thread completes, nothing else works |
WilliamMarshQMUL | 3:05b6a1431a6b | 31 | */ |
edwinkad | 4:54ff81b0e148 | 32 | int main() |
edwinkad | 4:54ff81b0e148 | 33 | { |
edwinkad | 4:54ff81b0e148 | 34 | button1.mode(PullUp); // Ensure button i/p has pull up |
edwinkad | 4:54ff81b0e148 | 35 | button2.mode(PullUp); // Ensure button i/p has pull up |
edwinkad | 4:54ff81b0e148 | 36 | button1.fall(&buttonCallback1) ; // Attach function to falling edge |
edwinkad | 4:54ff81b0e148 | 37 | button2.fall(&buttonCallback2) ; // Attach function to falling edge |
WilliamMarshQMUL | 3:05b6a1431a6b | 38 | |
edwinkad | 4:54ff81b0e148 | 39 | bool change1=true; |
edwinkad | 4:54ff81b0e148 | 40 | bool change2=true; |
WilliamMarshQMUL | 0:a66a8cb0012c | 41 | while(true) { |
WilliamMarshQMUL | 3:05b6a1431a6b | 42 | // Toggle the LED every time the button is pressed |
edwinkad | 4:54ff81b0e148 | 43 | if (pressEvent1) { |
edwinkad | 4:54ff81b0e148 | 44 | change1=!change1; |
edwinkad | 4:54ff81b0e148 | 45 | pressEvent1 = 0 ; // Clear the event variable |
edwinkad | 4:54ff81b0e148 | 46 | } |
edwinkad | 4:54ff81b0e148 | 47 | if (change1==true) { |
edwinkad | 4:54ff81b0e148 | 48 | led1 = !led1 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 49 | } |
edwinkad | 4:54ff81b0e148 | 50 | if (pressEvent2) { |
edwinkad | 4:54ff81b0e148 | 51 | change2=!change2; |
edwinkad | 4:54ff81b0e148 | 52 | pressEvent2 = 0 ; // Clear the event variable |
edwinkad | 4:54ff81b0e148 | 53 | } |
edwinkad | 4:54ff81b0e148 | 54 | if (change2==true) { |
edwinkad | 4:54ff81b0e148 | 55 | led2 = !led2 ; |
edwinkad | 4:54ff81b0e148 | 56 | } |
edwinkad | 4:54ff81b0e148 | 57 | |
edwinkad | 4:54ff81b0e148 | 58 | Thread::wait(500) ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 59 | } |
edwinkad | 4:54ff81b0e148 | 60 | } |