LAB 2 answer to interrupt
Dependencies: digitalInInterrupt_sample
Fork of digitalInInterrupt_sample by
Revision 4:e96ddcc3ded7, committed 2018-02-02
- Comitter:
- Bossman
- Date:
- Fri Feb 02 11:35:20 2018 +0000
- Parent:
- 3:05b6a1431a6b
- Commit message:
- Version 1
Changed in this revision
digitalInInterrupt_sample.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 05b6a1431a6b -r e96ddcc3ded7 digitalInInterrupt_sample.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/digitalInInterrupt_sample.lib Fri Feb 02 11:35:20 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/WilliamMarshQMUL/code/digitalInInterrupt_sample/#05b6a1431a6b
diff -r 05b6a1431a6b -r e96ddcc3ded7 main.cpp --- a/main.cpp Tue Jan 16 18:14:21 2018 +0000 +++ b/main.cpp Fri Feb 02 11:35:20 2018 +0000 @@ -2,35 +2,55 @@ // Labs 2: Example program for using an interrupt (or callback) // ----------------------------------------------------------- -// A callback function (corresponding to an ISR) is called when a button +// A callback function (corresponding to an ISR) is called when a button // is pressed // The callback uses a shared variable to signal another thread -InterruptIn button(PTD0); -DigitalOut led(LED_GREEN); +InterruptIn buttonred(PTD0); +InterruptIn buttonblue(PTD5); +DigitalOut ledred(LED_RED); +DigitalOut ledblue(LED_BLUE); -volatile int pressEvent = 0 ; - +volatile int pressEventred = 0 ; +volatile int pressEventblue = 0 ; +volatile int red; +volatile int blue; // This function is invoked when then interrupt occurs // Signal that the button has been pressed -// Note: bounce may occur -void buttonCallback(){ - pressEvent = 1 ; +// Note: bounce may occur +void buttonredCallback(){ + pressEventred = 1 ; } - +void buttonblueCallback(){ + pressEventblue = 1 ; +} /* ---- Main function (default thread) ---- Note that if this thread completes, nothing else works */ int main() { - button.mode(PullUp); // Ensure button i/p has pull up - button.fall(&buttonCallback) ; // Attach function to falling edge + buttonred.mode(PullUp); // Ensure button i/p has pull up + buttonred.fall(&buttonredCallback) ; // Attach function to falling edge + buttonblue.mode(PullUp); // Ensure button i/p has pull up + buttonblue.fall(&buttonblueCallback) ; // Attach function to falling edge while(true) { // Toggle the LED every time the button is pressed - if (pressEvent) { - led = !led ; - pressEvent = 0 ; // Clear the event variable + if (pressEventred){ + red=!red; + pressEventred = 0 ; // Clear the event variable } - Thread::wait(100) ; + if (red==0){ + ledred = !ledred; + } + if (pressEventblue){ + blue=!blue; + pressEventblue = 0 ; // Clear the event variable + } + if (blue==0){ + ledblue = !ledblue; + } + Thread::wait(500) ; } -} \ No newline at end of file + +} +