Lab 2 Part 4 - Interrupt Modified

Fork of digitalInInterrupt_sample by William Marsh

Committer:
diviavad
Date:
Fri Feb 02 17:18:38 2018 +0000
Revision:
4:58cf361db287
Parent:
3:05b6a1431a6b
Version 1

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
WilliamMarshQMUL 1:13e0c1956b54 9 InterruptIn button(PTD0);
diviavad 4:58cf361db287 10 InterruptIn button2(PTD5);
diviavad 4:58cf361db287 11 DigitalOut led(LED_BLUE);
diviavad 4:58cf361db287 12 DigitalOut led1(LED_RED);
WilliamMarshQMUL 0:a66a8cb0012c 13
WilliamMarshQMUL 1:13e0c1956b54 14 volatile int pressEvent = 0 ;
diviavad 4:58cf361db287 15 volatile int pressEvent2 = 0;
diviavad 4:58cf361db287 16 volatile int x = 0;
diviavad 4:58cf361db287 17 volatile int y = 0;
WilliamMarshQMUL 3:05b6a1431a6b 18 // This function is invoked when then interrupt occurs
WilliamMarshQMUL 3:05b6a1431a6b 19 // Signal that the button has been pressed
WilliamMarshQMUL 1:13e0c1956b54 20 // Note: bounce may occur
WilliamMarshQMUL 1:13e0c1956b54 21 void buttonCallback(){
WilliamMarshQMUL 1:13e0c1956b54 22 pressEvent = 1 ;
WilliamMarshQMUL 0:a66a8cb0012c 23 }
WilliamMarshQMUL 0:a66a8cb0012c 24
diviavad 4:58cf361db287 25
diviavad 4:58cf361db287 26
diviavad 4:58cf361db287 27 // This function is invoked when then interrupt occurs
diviavad 4:58cf361db287 28 // Signal that the button has been pressed
diviavad 4:58cf361db287 29 // Note: bounce may occur
diviavad 4:58cf361db287 30 void button2Callback(){
diviavad 4:58cf361db287 31 pressEvent2 = 1 ;
diviavad 4:58cf361db287 32 }
diviavad 4:58cf361db287 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
diviavad 4:58cf361db287 40 button2.mode(PullUp);
diviavad 4:58cf361db287 41 button2.fall(&button2Callback);
diviavad 4:58cf361db287 42
diviavad 4:58cf361db287 43 while(true){
diviavad 4:58cf361db287 44 if(x==0){
diviavad 4:58cf361db287 45 led1=!led1;
diviavad 4:58cf361db287 46 }
diviavad 4:58cf361db287 47 else if (y==0){
diviavad 4:58cf361db287 48 led=!led;
diviavad 4:58cf361db287 49 }
WilliamMarshQMUL 3:05b6a1431a6b 50 // Toggle the LED every time the button is pressed
WilliamMarshQMUL 0:a66a8cb0012c 51 if (pressEvent) {
diviavad 4:58cf361db287 52 x=!x;
diviavad 4:58cf361db287 53 if (x==1){
diviavad 4:58cf361db287 54 led1 = !led1;
diviavad 4:58cf361db287 55 }
diviavad 4:58cf361db287 56
WilliamMarshQMUL 1:13e0c1956b54 57 pressEvent = 0 ; // Clear the event variable
diviavad 4:58cf361db287 58 }
diviavad 4:58cf361db287 59
diviavad 4:58cf361db287 60 else if(pressEvent2) {
diviavad 4:58cf361db287 61 y=!y;
diviavad 4:58cf361db287 62 if (y==1){
diviavad 4:58cf361db287 63 led = !led;
diviavad 4:58cf361db287 64 }
diviavad 4:58cf361db287 65 pressEvent2 = 0 ; // Clear the event variable
WilliamMarshQMUL 0:a66a8cb0012c 66 }
diviavad 4:58cf361db287 67 Thread::wait(500);
WilliamMarshQMUL 0:a66a8cb0012c 68 }
diviavad 4:58cf361db287 69 }