jim hamblen
/
Pushbutton_Debounce_Interrupt
Pushbutton counter demo with debounce and a callback using interrupts
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "PinDetect.h" 00003 // must import Cookbook PinDetct library into project 00004 // URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw 00005 00006 DigitalOut myled(LED1); 00007 DigitalOut myled2(LED2); 00008 DigitalOut myled3(LED3); 00009 DigitalOut myled4(LED4); 00010 00011 PinDetect pb(p8); 00012 // SPST Pushbutton debounced count demo using interrupts and callback 00013 // no external PullUp resistor needed 00014 // Pushbutton from P8 to GND. 00015 // A pb hit generates an interrupt and activates the callback function 00016 // after the switch is debounced 00017 00018 // Global count variable 00019 int volatile count=0; 00020 00021 // Callback routine is interrupt activated by a debounced pb hit 00022 void pb_hit_callback (void) { 00023 count++; 00024 myled4 = count & 0x01; 00025 myled3 = (count & 0x02)>>1; 00026 myled2 = (count & 0x04)>>2; 00027 } 00028 int main() { 00029 00030 // Use internal pullup for pushbutton 00031 pb.mode(PullUp); 00032 // Delay for initial pullup to take effect 00033 wait(.01); 00034 // Setup Interrupt callback function for a pb hit 00035 pb.attach_deasserted(&pb_hit_callback); 00036 // Start sampling pb input using interrupts 00037 pb.setSampleFrequency(); 00038 00039 //Blink myled in main routine forever while responding to pb changes 00040 // via interrupts that activate the callback counter function 00041 while (1) { 00042 myled = !myled; 00043 wait(.5); 00044 } 00045 00046 }
Generated on Sun Jul 17 2022 06:08:04 by 1.7.2