lab 2 interrupts
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 button(PTD0); 00010 InterruptIn button1(PTD2); 00011 DigitalOut led(LED_BLUE); 00012 DigitalOut led1(LED_RED); 00013 00014 volatile int pressEvent = 0 ; 00015 volatile int pressEvent1 = 0 ; 00016 00017 bool ledflashing = true; 00018 bool led1flashing = true; 00019 00020 enum blueledState { Flashing, NotFlashing }; 00021 enum redledState { Flashing1, NotFlashing1 }; 00022 00023 // This function is invoked when then interrupt occurs 00024 // Signal that the button has been pressed 00025 // Note: bounce may occur 00026 void buttonCallback(){ 00027 pressEvent = 1 ; 00028 } 00029 00030 void buttonCallback1(){ 00031 pressEvent1 = 1 ; 00032 } 00033 00034 /* ---- Main function (default thread) ---- 00035 Note that if this thread completes, nothing else works 00036 */ 00037 int main() { 00038 button.mode(PullUp); // Ensure button i/p has pull up 00039 button.fall(&buttonCallback) ; // Attach function to falling edge 00040 00041 button1.mode(PullUp); // Ensure button i/p has pull up 00042 button1.fall(&buttonCallback1) ; // Attach function to falling edge 00043 00044 led = 1; 00045 led1 = 1; 00046 00047 blueledState State1 = Flashing; 00048 redledState State2 = Flashing1; 00049 int count = 5; 00050 int count1 = 5; 00051 while(true) { 00052 wait(0.1); 00053 switch(State1){ 00054 case Flashing: 00055 if(pressEvent){ 00056 State1 = NotFlashing; 00057 pressEvent = 0; 00058 } 00059 else{ 00060 if(count!=0){ 00061 count-- ; 00062 } 00063 else if(count == 0){ 00064 led = !led ; 00065 count = 5; 00066 } 00067 } 00068 case NotFlashing: 00069 if(pressEvent){ 00070 State1 = Flashing; 00071 count = 5; 00072 pressEvent = 0; 00073 } 00074 } 00075 switch(State2){ 00076 case Flashing1: 00077 if(pressEvent1){ 00078 State2 = NotFlashing1; 00079 pressEvent1 = 0; 00080 } 00081 else{ 00082 if(count1!=0){ 00083 count1-- ; 00084 } 00085 else if(count1 == 0){ 00086 led1 = !led1 ; 00087 count1 = 5; 00088 } 00089 } 00090 break; 00091 case NotFlashing1: 00092 if(pressEvent1){ 00093 State2 = Flashing1; 00094 count1 = 5; 00095 pressEvent1 = 0; 00096 } 00097 break; 00098 } 00099 00100 } 00101 }
Generated on Wed Jul 13 2022 15:09:37 by
1.7.2