Code for part 2 of lab 2

Fork of digitalInInterrupt_sample by William Marsh

Committer:
Tobden
Date:
Fri Feb 02 12:13:38 2018 +0000
Revision:
5:3e5e109868b6
Parent:
4:46bd7e19daae
Interrupt program for Mbed for switching between blinking light and pause state

Who changed what in which revision?

UserRevisionLine numberNew 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
Tobden 5:3e5e109868b6 9 InterruptIn button(PTD0);
Tobden 5:3e5e109868b6 10 InterruptIn button2(PTD2);
Tobden 4:46bd7e19daae 11 DigitalOut led(LED_RED);
Tobden 4:46bd7e19daae 12 DigitalOut led2(LED_BLUE);
WilliamMarshQMUL 0:a66a8cb0012c 13
Tobden 4:46bd7e19daae 14 volatile int change = 0;
Tobden 4:46bd7e19daae 15 volatile int change2 = 0;
WilliamMarshQMUL 1:13e0c1956b54 16 volatile int pressEvent = 0 ;
Tobden 4:46bd7e19daae 17 volatile int pressEvent2 = 0;
WilliamMarshQMUL 0:a66a8cb0012c 18
WilliamMarshQMUL 3:05b6a1431a6b 19 // This function is invoked when then interrupt occurs
WilliamMarshQMUL 3:05b6a1431a6b 20 // Signal that the button has been pressed
WilliamMarshQMUL 1:13e0c1956b54 21 // Note: bounce may occur
Tobden 5:3e5e109868b6 22 void buttonCallback(){ //Callback for the first button
WilliamMarshQMUL 1:13e0c1956b54 23 pressEvent = 1 ;
WilliamMarshQMUL 0:a66a8cb0012c 24 }
Tobden 5:3e5e109868b6 25 void button2Callback(){ //Callback for the second button
Tobden 4:46bd7e19daae 26 pressEvent2 = 1;
Tobden 4:46bd7e19daae 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 */
WilliamMarshQMUL 3:05b6a1431a6b 32 int main() {
WilliamMarshQMUL 3:05b6a1431a6b 33 button.mode(PullUp); // Ensure button i/p has pull up
WilliamMarshQMUL 3:05b6a1431a6b 34 button.fall(&buttonCallback) ; // Attach function to falling edge
Tobden 4:46bd7e19daae 35 button2.mode(PullUp);
Tobden 4:46bd7e19daae 36 button2.fall(&button2Callback);
Tobden 5:3e5e109868b6 37 //led2 = 1;
WilliamMarshQMUL 3:05b6a1431a6b 38
WilliamMarshQMUL 0:a66a8cb0012c 39 while(true) {
WilliamMarshQMUL 3:05b6a1431a6b 40 // Toggle the LED every time the button is pressed
WilliamMarshQMUL 0:a66a8cb0012c 41 if (pressEvent) {
Tobden 4:46bd7e19daae 42 change = !change;
Tobden 4:46bd7e19daae 43 if (change == 1){
Tobden 5:3e5e109868b6 44 led = led;}
WilliamMarshQMUL 1:13e0c1956b54 45 pressEvent = 0 ; // Clear the event variable
WilliamMarshQMUL 0:a66a8cb0012c 46 }
Tobden 5:3e5e109868b6 47 if (pressEvent2) {
Tobden 4:46bd7e19daae 48 change2=!change2;
Tobden 4:46bd7e19daae 49 if(change2 == 1){
Tobden 5:3e5e109868b6 50 led2 = led2;}
Tobden 4:46bd7e19daae 51 pressEvent2 = 0;
Tobden 4:46bd7e19daae 52 }
Tobden 4:46bd7e19daae 53 if (change == 0){
Tobden 4:46bd7e19daae 54 led = !led ;}
Tobden 5:3e5e109868b6 55 if (change2 == 0){
Tobden 4:46bd7e19daae 56 led2 = !led2;}
Tobden 4:46bd7e19daae 57 Thread::wait(500);
WilliamMarshQMUL 0:a66a8cb0012c 58 }
Tobden 5:3e5e109868b6 59 }