Question 1 for Embed Lab

Dependencies:   mbed

Committer:
t00214916
Date:
Sat Aug 21 22:33:05 2021 +0000
Revision:
2:c82fdcdbcab2
Parent:
1:780300dcee8d
InterruptIn (FINAL_COMMIT)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
t00214916 1:780300dcee8d 1 // My first program did not have an interrupt and as a result sometime it would
t00214916 1:780300dcee8d 2 // display the button twice. this was resolved by the use on an interrupt.
t00214916 2:c82fdcdbcab2 3 // A hardware option to control debouncing would be the use of a capacitor
t00214916 2:c82fdcdbcab2 4 // this will allow for a smooth switch. For a software solution an interrupt is
t00214916 2:c82fdcdbcab2 5 // used for debouncing.
t00214916 1:780300dcee8d 6
t00214916 1:780300dcee8d 7 #include "mbed.h"
t00214916 1:780300dcee8d 8 Serial pc (USBTX, USBRX);
t00214916 0:0cbd2a7185e7 9
t00214916 1:780300dcee8d 10 InterruptIn center(p14);
t00214916 1:780300dcee8d 11 InterruptIn left(p13);
t00214916 1:780300dcee8d 12 InterruptIn right(p16);
t00214916 1:780300dcee8d 13 InterruptIn up(p15);
t00214916 1:780300dcee8d 14 InterruptIn down(p12);
t00214916 0:0cbd2a7185e7 15
t00214916 1:780300dcee8d 16 void center_released() { wait (0.25);
t00214916 1:780300dcee8d 17 }
t00214916 1:780300dcee8d 18 void center_pressed() {
t00214916 1:780300dcee8d 19 pc.printf("center\n\r");
t00214916 1:780300dcee8d 20 }
t00214916 1:780300dcee8d 21 void left_released() { wait (0.25);
t00214916 1:780300dcee8d 22 }
t00214916 1:780300dcee8d 23 void left_pressed() {
t00214916 1:780300dcee8d 24 pc.printf("left\n\r");
t00214916 1:780300dcee8d 25 }
t00214916 1:780300dcee8d 26 void right_released() { wait (0.25);
t00214916 1:780300dcee8d 27 }
t00214916 1:780300dcee8d 28 void right_pressed() {
t00214916 1:780300dcee8d 29 pc.printf("right\n\r");
t00214916 1:780300dcee8d 30 }
t00214916 1:780300dcee8d 31 void up_released() { wait (0.25);
t00214916 1:780300dcee8d 32 }
t00214916 1:780300dcee8d 33 void up_pressed() {
t00214916 1:780300dcee8d 34 pc.printf("up\n\r");
t00214916 1:780300dcee8d 35 }
t00214916 1:780300dcee8d 36 void down_released() { wait (0.25);
t00214916 1:780300dcee8d 37 }
t00214916 1:780300dcee8d 38 void down_pressed() {
t00214916 1:780300dcee8d 39 pc.printf("down\n\r");
t00214916 1:780300dcee8d 40 }
t00214916 1:780300dcee8d 41 int main() {
t00214916 1:780300dcee8d 42
t00214916 1:780300dcee8d 43 // Both rise and fall edges generate an interrupt
t00214916 1:780300dcee8d 44 center.fall(&center_released);
t00214916 1:780300dcee8d 45 center.rise(&center_pressed);
t00214916 1:780300dcee8d 46 left.fall(&left_released);
t00214916 1:780300dcee8d 47 left.rise(&left_pressed);
t00214916 1:780300dcee8d 48 right.fall(&right_released);
t00214916 1:780300dcee8d 49 right.rise(&right_pressed);
t00214916 1:780300dcee8d 50 up.fall(&up_released);
t00214916 1:780300dcee8d 51 up.rise(&up_pressed);
t00214916 1:780300dcee8d 52 down.fall(&down_released);
t00214916 1:780300dcee8d 53 down.rise(&down_pressed);
t00214916 1:780300dcee8d 54
t00214916 1:780300dcee8d 55 left.mode(PullDown);
t00214916 1:780300dcee8d 56 right.mode(PullDown);
t00214916 1:780300dcee8d 57 up.mode(PullDown);
t00214916 1:780300dcee8d 58 down.mode(PullDown);
t00214916 1:780300dcee8d 59
t00214916 1:780300dcee8d 60 while (1) {
t00214916 0:0cbd2a7185e7 61 }
t00214916 1:780300dcee8d 62 }