LAB#1: InterruptIn

Dependencies:   mbed

Committer:
hulmpants
Date:
Thu Aug 15 16:56:39 2019 +0000
Revision:
1:cd0e7975714c
Embedded_LAB1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hulmpants 1:cd0e7975714c 1 // IT Tralee Mechatronics: Embedded Systems LAB#1
hulmpants 1:cd0e7975714c 2 // Revised code to include a simple software solution to mechanical bounce experienced by switch
hulmpants 1:cd0e7975714c 3
hulmpants 1:cd0e7975714c 4
hulmpants 1:cd0e7975714c 5 #include "mbed.h"
hulmpants 1:cd0e7975714c 6
hulmpants 1:cd0e7975714c 7 InterruptIn d(p12); // down
hulmpants 1:cd0e7975714c 8 InterruptIn l(p13); // left
hulmpants 1:cd0e7975714c 9 InterruptIn c(p14); // centre
hulmpants 1:cd0e7975714c 10 InterruptIn u(p15); // up
hulmpants 1:cd0e7975714c 11 InterruptIn r(p16); // right
hulmpants 1:cd0e7975714c 12
hulmpants 1:cd0e7975714c 13
hulmpants 1:cd0e7975714c 14 DigitalOut flash(LED1);
hulmpants 1:cd0e7975714c 15 float debounce(0.5); // 0.5s delay: software sln for mechanical bounce
hulmpants 1:cd0e7975714c 16
hulmpants 1:cd0e7975714c 17 void left() {
hulmpants 1:cd0e7975714c 18 printf("Left \n \r"); // print
hulmpants 1:cd0e7975714c 19 wait(debounce);
hulmpants 1:cd0e7975714c 20 }
hulmpants 1:cd0e7975714c 21
hulmpants 1:cd0e7975714c 22 void right() {
hulmpants 1:cd0e7975714c 23 printf("Right \n \r");
hulmpants 1:cd0e7975714c 24 wait(debounce);
hulmpants 1:cd0e7975714c 25 }
hulmpants 1:cd0e7975714c 26
hulmpants 1:cd0e7975714c 27 void up() {
hulmpants 1:cd0e7975714c 28 printf("Up \n \r");
hulmpants 1:cd0e7975714c 29 wait(debounce);
hulmpants 1:cd0e7975714c 30 }
hulmpants 1:cd0e7975714c 31
hulmpants 1:cd0e7975714c 32 void down() {
hulmpants 1:cd0e7975714c 33 printf("Down \n \r");
hulmpants 1:cd0e7975714c 34 wait(debounce);
hulmpants 1:cd0e7975714c 35 }
hulmpants 1:cd0e7975714c 36
hulmpants 1:cd0e7975714c 37 void centre() {
hulmpants 1:cd0e7975714c 38 printf("Centre \n \r");
hulmpants 1:cd0e7975714c 39 wait(debounce);
hulmpants 1:cd0e7975714c 40 }
hulmpants 1:cd0e7975714c 41
hulmpants 1:cd0e7975714c 42 int main() {
hulmpants 1:cd0e7975714c 43 l.rise(&left); // attach the address of the (left in this line) functions to the interrupt rising edge. 0->1
hulmpants 1:cd0e7975714c 44 r.rise(&right);
hulmpants 1:cd0e7975714c 45 u.rise(&up);
hulmpants 1:cd0e7975714c 46 d.rise(&down);
hulmpants 1:cd0e7975714c 47 c.rise(&centre);
hulmpants 1:cd0e7975714c 48
hulmpants 1:cd0e7975714c 49 while(1) { // flash led1 until interrupt to show that program is stopping while interrupt is active.
hulmpants 1:cd0e7975714c 50 flash = !flash;
hulmpants 1:cd0e7975714c 51 wait(0.2);
hulmpants 1:cd0e7975714c 52 }
hulmpants 1:cd0e7975714c 53 }