interrupt for lab2

Fork of digitalInInterrupt_sample by William Marsh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 s_button(PTD2);
00011 DigitalOut led(LED_BLUE);
00012 DigitalOut s_led(LED_RED);
00013 volatile int time1;
00014 volatile int time2;
00015 
00016 volatile int pressEvent = 0 ;
00017 volatile int s_pressEvent = 0 ;
00018 
00019 // This function is invoked when then interrupt occurs
00020 //   Signal that the button has been pressed
00021 //   Note: bounce may occur 
00022 void buttonCallback(){
00023     pressEvent = 1 ;   
00024 }
00025 
00026 void s_buttonCallback(){
00027     s_pressEvent = 1 ;
00028     
00029 }
00030 
00031 /*  ---- Main function (default thread) ----
00032     Note that if this thread completes, nothing else works
00033  */
00034 int main() {
00035     //S_led=1;
00036     button.mode(PullUp);             // Ensure button i/p has pull up
00037     button.fall(&buttonCallback) ;   // Attach function to falling edge
00038    
00039     //second button
00040     
00041     s_button.mode(PullUp);             
00042     s_button.fall(&s_buttonCallback) ;
00043     //s_led=1; 
00044     
00045     while(true) {
00046         // Toggle the LED every time the button is pressed
00047         
00048         // control button 1 (blue)
00049            
00050         if(time1==0){
00051             
00052         led=!led;
00053         }
00054         
00055         if(time2==0){
00056         s_led=!s_led;
00057         
00058         }
00059      
00060         //press event_1 coltrol button 1 (Blue)
00061         
00062         if (pressEvent) { 
00063            time1 = !time1;
00064             if(time1==1){    
00065             led = !led ;
00066             }
00067             pressEvent = 0 ; // Clear the event variable    
00068                  
00069         }
00070         
00071         //press even_2 coltrol button 2 (RED)
00072         if (s_pressEvent) {
00073             time2 = !time2;
00074             if(time2==1){
00075             s_led = !s_led ;
00076             }
00077             s_pressEvent = 0 ; // Clear the event variable    
00078              
00079         }
00080         Thread::wait(500) ;
00081     }
00082 }