Interrupt and Debounce program for Labs

Dependencies:   mbed C12832

Committer:
tonyk37
Date:
Thu Jun 25 10:13:46 2020 +0000
Revision:
0:49ecc494fc69
Interrupt and Debounce program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tonyk37 0:49ecc494fc69 1 #include "mbed.h"
tonyk37 0:49ecc494fc69 2 #include "C12832.h"
tonyk37 0:49ecc494fc69 3
tonyk37 0:49ecc494fc69 4 C12832 lcd(p5, p7, p6, p8, p11);
tonyk37 0:49ecc494fc69 5
tonyk37 0:49ecc494fc69 6 InterruptIn joystickcenter(p14);
tonyk37 0:49ecc494fc69 7 InterruptIn button(p9);
tonyk37 0:49ecc494fc69 8 Timer debounce;
tonyk37 0:49ecc494fc69 9
tonyk37 0:49ecc494fc69 10 DigitalOut led(LED1);
tonyk37 0:49ecc494fc69 11 DigitalOut flash(LED4);
tonyk37 0:49ecc494fc69 12
tonyk37 0:49ecc494fc69 13 int x ;
tonyk37 0:49ecc494fc69 14
tonyk37 0:49ecc494fc69 15 void flip() {
tonyk37 0:49ecc494fc69 16 led = !led; //toggles the led when the joystick is pressed.
tonyk37 0:49ecc494fc69 17 if (debounce.read_ms() > 3000 )
tonyk37 0:49ecc494fc69 18
tonyk37 0:49ecc494fc69 19 x = 1;
tonyk37 0:49ecc494fc69 20 debounce.reset();
tonyk37 0:49ecc494fc69 21 }
tonyk37 0:49ecc494fc69 22 int main() {
tonyk37 0:49ecc494fc69 23 debounce.start();
tonyk37 0:49ecc494fc69 24 joystickcenter.rise(&flip); // attach the function address to the rising edge
tonyk37 0:49ecc494fc69 25 button.mode(PullUp); // With this, no external pullup resistor needed
tonyk37 0:49ecc494fc69 26 button.rise(&flip); // attach the function address to the rising edge
tonyk37 0:49ecc494fc69 27
tonyk37 0:49ecc494fc69 28 while(1)
tonyk37 0:49ecc494fc69 29 { // wait around, interrupts will interrupt this!
tonyk37 0:49ecc494fc69 30 flash = !flash; // turns LED4 on if off, off if on
tonyk37 0:49ecc494fc69 31 if (x == 1)
tonyk37 0:49ecc494fc69 32 {
tonyk37 0:49ecc494fc69 33 wait (0.20);
tonyk37 0:49ecc494fc69 34 lcd.printf("high");
tonyk37 0:49ecc494fc69 35 x = 0;
tonyk37 0:49ecc494fc69 36 }
tonyk37 0:49ecc494fc69 37 wait(0.25); // the instruction to wait for a quarter-second
tonyk37 0:49ecc494fc69 38 }
tonyk37 0:49ecc494fc69 39 }