Chris Byrne
/
TimerDebounce_Lab
main.cpp@1:4cc86a20ec45, 2020-08-07 (annotated)
- Committer:
- chbyrne79
- Date:
- Fri Aug 07 15:41:54 2020 +0000
- Revision:
- 1:4cc86a20ec45
- Parent:
- 0:6e4b35c64edd
TimerDebounce_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 | 1:4cc86a20ec45 | 9 | Timer debounce; // define debounce timer |
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 | 1:4cc86a20ec45 | 15 | if (debounce.read_ms()>=1000) { //by adjusting the timer up it |
chbyrne79 | 1:4cc86a20ec45 | 16 | // ensures the function isn’t called too often. |
chbyrne79 | 0:6e4b35c64edd | 17 | led = !led; // toggles the led when the joystick button is pressed. |
chbyrne79 | 0:6e4b35c64edd | 18 | x=1; |
chbyrne79 | 0:6e4b35c64edd | 19 | debounce.reset(); |
chbyrne79 | 0:6e4b35c64edd | 20 | } |
chbyrne79 | 0:6e4b35c64edd | 21 | } |
chbyrne79 | 0:6e4b35c64edd | 22 | |
chbyrne79 | 0:6e4b35c64edd | 23 | int main() { |
chbyrne79 | 0:6e4b35c64edd | 24 | joystickcenter.rise(&flip); |
chbyrne79 | 0:6e4b35c64edd | 25 | // attach the function address to the rising edge |
chbyrne79 | 0:6e4b35c64edd | 26 | while(1) { // wait around, interrupts will interrupt this! |
chbyrne79 | 0:6e4b35c64edd | 27 | flash = !flash; // turns LED4 on if off, off if on |
chbyrne79 | 0:6e4b35c64edd | 28 | if (x==1){ |
chbyrne79 | 0:6e4b35c64edd | 29 | lcd.printf("Timer Debounce"); |
chbyrne79 | 0:6e4b35c64edd | 30 | x=0; |
chbyrne79 | 0:6e4b35c64edd | 31 | } |
chbyrne79 | 0:6e4b35c64edd | 32 | wait(0.25); // the instruction to wait for a quarter-second |
chbyrne79 | 0:6e4b35c64edd | 33 | } |
chbyrne79 | 0:6e4b35c64edd | 34 | |
chbyrne79 | 0:6e4b35c64edd | 35 | } |