CITY1082 Pushbutton debounce example
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(LED5); 00008 DigitalOut myled3(LED4); 00009 DigitalOut myled4(LED3); 00010 00011 PinDetect pb1(P0_4); 00012 //PinDetect pb2(p7); 00013 // SPST Pushbutton debounced count demo using interrupts and callback 00014 // no external PullUp resistor needed 00015 // Pushbutton from P8 to GND. 00016 // Second Pushbutton from P7 to GND. 00017 // A pb hit generates an interrupt and activates the callback function 00018 // after the switch is debounced 00019 00020 // Global count variable 00021 int volatile countit=0; 00022 00023 // Callback routine is interrupt activated by a debounced pb1 hit 00024 void pb1_hit_callback (void) { 00025 // printf("Count is %d\n", ++countit); 00026 countit--; 00027 myled4 = countit & 0x01; 00028 myled3 = (countit & 0x02)>>1; 00029 myled2 = (countit & 0x04)>>2; 00030 } 00031 // Callback routine is interrupt activated by a debounced pb2 hit 00032 /*void pb2_hit_callback (void) { 00033 count--; 00034 myled4 = count & 0x01; 00035 myled3 = (count & 0x02)>>1; 00036 myled2 = (count & 0x04)>>2; 00037 }*/ 00038 int main() { 00039 00040 // Use internal pullups for pushbutton 00041 pb1.mode(PullUp); 00042 // pb2.mode(PullUp); 00043 // Delay for initial pullup to take effect 00044 wait(.01); 00045 // Setup Interrupt callback functions for a pb hit 00046 pb1.attach_deasserted(&pb1_hit_callback); 00047 // pb2.attach_deasserted(&pb2_hit_callback); 00048 // Start sampling pb inputs using interrupts 00049 pb1.setSampleFrequency(); 00050 // pb2.setSampleFrequency(); 00051 //Blink myled in main routine forever while responding to pb changes 00052 // via interrupts that activate the callback counter function 00053 while (1) { 00054 myled = !myled; 00055 wait(.5); 00056 } 00057 00058 }
Generated on Fri Jul 29 2022 11:33:14 by
1.7.2