Interrupt - modified version

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_blue(PTD0);
00010 DigitalOut led_blue(LED_BLUE);
00011 
00012 InterruptIn button_red(PTD4);
00013 DigitalOut led_red(LED_RED);
00014 
00015 volatile int pressEvent_blue = 0;
00016 volatile int pressEvent_red = 0;
00017 
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_blue()
00023 {
00024     pressEvent_blue = 1 ;
00025 }
00026 void buttonCallback_red()
00027 {
00028     pressEvent_red = 1 ;
00029 }
00030 
00031 /*  ---- Main function (default thread) ----
00032     Note that if this thread completes, nothing else works
00033  */
00034 int main()
00035 {
00036     led_blue = 0;
00037     led_red = 0;
00038     
00039     button_blue.mode(PullUp);             // Ensure button i/p has pull up
00040     button_blue.fall(&buttonCallback_blue) ;   // Attach function to falling edge
00041 
00042     button_red.mode(PullUp);
00043     button_red.fall(&buttonCallback_red);
00044 
00045     // active mode of each LED
00046     int active_blue = 1;
00047     int active_red = 1;
00048 
00049     // cycle counter of each LED
00050     int cycle_blue = 1;
00051     int cycle_red = 1;
00052 
00053     while(true) {
00054         // blue led
00055         if (pressEvent_blue) {
00056             active_blue = !active_blue;
00057             pressEvent_blue = 0 ; // Clear the event variable
00058             cycle_blue = 1;
00059         }
00060         if(active_blue) {
00061             if(cycle_blue == 5) {
00062                 led_blue = !led_blue ;
00063                 cycle_blue = 1;
00064             } else {
00065                 cycle_blue = cycle_blue+1;
00066             }
00067         }
00068 
00069         // red led
00070         if (pressEvent_red) {
00071             active_red = !active_red;
00072             pressEvent_red = 0 ; // Clear the event variable
00073             cycle_red = 1;
00074         }
00075         if(active_red) {
00076             if(cycle_red == 5) {
00077                 led_red = !led_red ;
00078                 cycle_red = 1;
00079             } else {
00080                 cycle_red = cycle_red+1;
00081             }
00082         }
00083 
00084         Thread::wait(100) ;
00085     }
00086 }