lab2-part2
Fork of digitalInInterrupt_sample by
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 // Labs 2: Example program for using an interrupt (or callback) 00004 // ----------------------------------------------------------- 00005 // A callback function (corresponding to an ISR) is called when a button 00006 // is pressed 00007 // The callback uses a shared variable to signal another thread 00008 00009 InterruptIn buttonblue(PTD0); 00010 InterruptIn buttonred(PTD5);//need to change the address 00011 DigitalOut ledblue(LED_BLUE); 00012 DigitalOut ledred(LED_RED); 00013 00014 volatile int pressREvent = 0 ; 00015 volatile int pressBEvent = 0 ; 00016 //volatile bool switchledblue = true; //switch blue led 00017 //volatile bool switchledred = true; //switch red led 00018 00019 // This function is invoked when then interrupt occurs 00020 void buttonblueCallback() 00021 { 00022 pressBEvent = 1; 00023 } 00024 00025 void buttonredCallback() 00026 { 00027 pressREvent = 1; 00028 } 00029 00030 /* ---- Main function (default thread) ---- 00031 Note that if this thread completes, nothing else works 00032 */ 00033 int main() 00034 { 00035 00036 bool allow_flash_red = true; 00037 bool allow_flash_blue = true; 00038 00039 buttonblue.mode(PullUp); 00040 buttonred.mode(PullUp); // Ensure button i/p has pull up 00041 00042 buttonblue.fall(&buttonblueCallback) ; // Attach function to falling edge 00043 buttonred.fall(&buttonredCallback) ; 00044 while (true) 00045 { 00046 00047 if (pressREvent == 1) 00048 { 00049 allow_flash_red = !allow_flash_red; 00050 pressREvent =0; 00051 } 00052 00053 if (pressBEvent == 1) 00054 { 00055 00056 allow_flash_blue = !allow_flash_blue; 00057 pressBEvent = 0; 00058 } 00059 00060 if(allow_flash_red) 00061 { 00062 ledred = !ledred; 00063 } 00064 00065 if(allow_flash_blue) 00066 { 00067 ledblue = !ledblue; 00068 00069 } 00070 00071 Thread::wait(500); 00072 } 00073 00074 }
Generated on Sun Jul 17 2022 00:33:36 by
1.7.2
