jim hamblen
/
Pushbutton_NoBounce_Demo
Pushbutton counter demo with debounce
main.cpp@0:ca31694551ed, 2011-09-20 (annotated)
- Committer:
- 4180_1
- Date:
- Tue Sep 20 14:03:10 2011 +0000
- Revision:
- 0:ca31694551ed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:ca31694551ed | 1 | #include "mbed.h" |
4180_1 | 0:ca31694551ed | 2 | #include "DebounceIn.h" |
4180_1 | 0:ca31694551ed | 3 | // must import Cookbook Debounce library into project |
4180_1 | 0:ca31694551ed | 4 | // URL: http://mbed.org/users/AjK/libraries/DebounceIn/lky9pc |
4180_1 | 0:ca31694551ed | 5 | |
4180_1 | 0:ca31694551ed | 6 | DigitalOut myled(LED1); |
4180_1 | 0:ca31694551ed | 7 | DigitalOut myled2(LED2); |
4180_1 | 0:ca31694551ed | 8 | DigitalOut myled3(LED3); |
4180_1 | 0:ca31694551ed | 9 | DigitalOut myled4(LED4); |
4180_1 | 0:ca31694551ed | 10 | |
4180_1 | 0:ca31694551ed | 11 | DebounceIn pb(p8); |
4180_1 | 0:ca31694551ed | 12 | // SPST Pushbutton count demo using internal PullUp function |
4180_1 | 0:ca31694551ed | 13 | // no external PullUp resistor needed |
4180_1 | 0:ca31694551ed | 14 | // Pushbutton from P8 to GND. |
4180_1 | 0:ca31694551ed | 15 | // Demonstrates need for debounce - will not count more than once per button hit |
4180_1 | 0:ca31694551ed | 16 | // This ocuurs on all switches due to mechanical contact bounce |
4180_1 | 0:ca31694551ed | 17 | int main() |
4180_1 | 0:ca31694551ed | 18 | { |
4180_1 | 0:ca31694551ed | 19 | int count=0; |
4180_1 | 0:ca31694551ed | 20 | int old_pb=0; |
4180_1 | 0:ca31694551ed | 21 | int new_pb; |
4180_1 | 0:ca31694551ed | 22 | // Use internal pullup for pushbutton |
4180_1 | 0:ca31694551ed | 23 | pb.mode(PullUp); |
4180_1 | 0:ca31694551ed | 24 | // Delay for initial pullup to take effect |
4180_1 | 0:ca31694551ed | 25 | wait(.001); |
4180_1 | 0:ca31694551ed | 26 | while(1) { |
4180_1 | 0:ca31694551ed | 27 | new_pb = pb; |
4180_1 | 0:ca31694551ed | 28 | if ((new_pb==0) && (old_pb==1)) count++; |
4180_1 | 0:ca31694551ed | 29 | myled4 = count & 0x01; |
4180_1 | 0:ca31694551ed | 30 | myled3 = (count & 0x02)>>1; |
4180_1 | 0:ca31694551ed | 31 | myled2 = (count & 0x04)>>2; |
4180_1 | 0:ca31694551ed | 32 | myled = (count & 0x08)>>3; |
4180_1 | 0:ca31694551ed | 33 | old_pb = new_pb; |
4180_1 | 0:ca31694551ed | 34 | } |
4180_1 | 0:ca31694551ed | 35 | } |