Chris Byrne
/
TimerDebounce_Lab
main.cpp@0:6e4b35c64edd, 2020-08-07 (annotated)
- Committer:
- chbyrne79
- Date:
- Fri Aug 07 09:50:24 2020 +0000
- Revision:
- 0:6e4b35c64edd
- Child:
- 1:4cc86a20ec45
Timer_Debounce_Lab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chbyrne79 | 0:6e4b35c64edd | 1 | #include "mbed.h" |
chbyrne79 | 0:6e4b35c64edd | 2 | #include "C12832.h" |
chbyrne79 | 0:6e4b35c64edd | 3 | |
chbyrne79 | 0:6e4b35c64edd | 4 | C12832 lcd(p5, p7, p6, p8, p11); |
chbyrne79 | 0:6e4b35c64edd | 5 | InterruptIn joystickcenter(p14); |
chbyrne79 | 0:6e4b35c64edd | 6 | InterruptIn button(p9); |
chbyrne79 | 0:6e4b35c64edd | 7 | DigitalOut led(LED1); |
chbyrne79 | 0:6e4b35c64edd | 8 | DigitalOut flash(LED4); |
chbyrne79 | 0:6e4b35c64edd | 9 | Timer debounce; |
chbyrne79 | 0:6e4b35c64edd | 10 | int x; |
chbyrne79 | 0:6e4b35c64edd | 11 | |
chbyrne79 | 0:6e4b35c64edd | 12 | |
chbyrne79 | 0:6e4b35c64edd | 13 | void flip(){ |
chbyrne79 | 0:6e4b35c64edd | 14 | debounce.start(); |
chbyrne79 | 0:6e4b35c64edd | 15 | if (debounce.read_ms()>=5) { |
chbyrne79 | 0:6e4b35c64edd | 16 | led = !led; // toggles the led when the joystick button is pressed. |
chbyrne79 | 0:6e4b35c64edd | 17 | x=1; |
chbyrne79 | 0:6e4b35c64edd | 18 | debounce.reset(); |
chbyrne79 | 0:6e4b35c64edd | 19 | } |
chbyrne79 | 0:6e4b35c64edd | 20 | } |
chbyrne79 | 0:6e4b35c64edd | 21 | |
chbyrne79 | 0:6e4b35c64edd | 22 | int main() { |
chbyrne79 | 0:6e4b35c64edd | 23 | joystickcenter.rise(&flip); |
chbyrne79 | 0:6e4b35c64edd | 24 | // attach the function address to the rising edge button.mode(PullUp); |
chbyrne79 | 0:6e4b35c64edd | 25 | // With this, no external pullup resistor needed button.rise(&flip); |
chbyrne79 | 0:6e4b35c64edd | 26 | // attach the function address to the rising edge |
chbyrne79 | 0:6e4b35c64edd | 27 | while(1) { // wait around, interrupts will interrupt this! |
chbyrne79 | 0:6e4b35c64edd | 28 | flash = !flash; // turns LED4 on if off, off if on |
chbyrne79 | 0:6e4b35c64edd | 29 | if (x==1){ |
chbyrne79 | 0:6e4b35c64edd | 30 | lcd.printf("Timer Debounce"); |
chbyrne79 | 0:6e4b35c64edd | 31 | x=0; |
chbyrne79 | 0:6e4b35c64edd | 32 | } |
chbyrne79 | 0:6e4b35c64edd | 33 | wait(0.25); // the instruction to wait for a quarter-second |
chbyrne79 | 0:6e4b35c64edd | 34 | } |
chbyrne79 | 0:6e4b35c64edd | 35 | |
chbyrne79 | 0:6e4b35c64edd | 36 | } |