EmbeddedArtists AB
/
lpc812_exp_solution_digital-in
Solutions for the Digital Input experiments for LPC812 MAX
main.cpp@2:697cd0c40fbf, 2013-11-22 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Nov 22 09:06:59 2013 +0000
- Revision:
- 2:697cd0c40fbf
- Parent:
- 1:5d12e995c311
Small fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:c81327076cac | 1 | #include "mbed.h" |
embeddedartists | 0:c81327076cac | 2 | |
embeddedartists | 0:c81327076cac | 3 | DigitalIn button1(D0); |
embeddedartists | 0:c81327076cac | 4 | DigitalIn button2(D1); |
embeddedartists | 0:c81327076cac | 5 | DigitalOut buzzer(D3); |
embeddedartists | 0:c81327076cac | 6 | DigitalOut led(LED_RED); |
embeddedartists | 0:c81327076cac | 7 | |
embeddedartists | 0:c81327076cac | 8 | static void experiment1() |
embeddedartists | 0:c81327076cac | 9 | { |
embeddedartists | 0:c81327076cac | 10 | led = 1; // Turn LED off |
embeddedartists | 0:c81327076cac | 11 | |
embeddedartists | 0:c81327076cac | 12 | button1.mode(PullUp); // Enable button |
embeddedartists | 0:c81327076cac | 13 | |
embeddedartists | 0:c81327076cac | 14 | // Enter forever loop |
embeddedartists | 0:c81327076cac | 15 | while(1) { |
embeddedartists | 0:c81327076cac | 16 | // Check if push-button is pressed (input is low) |
embeddedartists | 0:c81327076cac | 17 | if (button1.read() == 0) { |
embeddedartists | 0:c81327076cac | 18 | led = 0; // Button pressed so turn on LED |
embeddedartists | 0:c81327076cac | 19 | } else { |
embeddedartists | 0:c81327076cac | 20 | led = 1; // Button not pressed so turn off LED |
embeddedartists | 0:c81327076cac | 21 | } |
embeddedartists | 0:c81327076cac | 22 | } |
embeddedartists | 0:c81327076cac | 23 | } |
embeddedartists | 0:c81327076cac | 24 | |
embeddedartists | 0:c81327076cac | 25 | static void experiment2() |
embeddedartists | 0:c81327076cac | 26 | { |
embeddedartists | 0:c81327076cac | 27 | led = 1; // Turn LED off |
embeddedartists | 0:c81327076cac | 28 | |
embeddedartists | 0:c81327076cac | 29 | button1.mode(PullUp); // Enable button |
embeddedartists | 0:c81327076cac | 30 | button2.mode(PullUp); // Enable button |
embeddedartists | 0:c81327076cac | 31 | |
embeddedartists | 0:c81327076cac | 32 | // Enter forever loop |
embeddedartists | 0:c81327076cac | 33 | while(1) { |
embeddedartists | 0:c81327076cac | 34 | int state1 = button1.read(); |
embeddedartists | 0:c81327076cac | 35 | int state2 = button2.read(); |
embeddedartists | 0:c81327076cac | 36 | |
embeddedartists | 0:c81327076cac | 37 | // Check if push-button is pressed (input is low) |
embeddedartists | 0:c81327076cac | 38 | if (state1 == 0) { |
embeddedartists | 0:c81327076cac | 39 | if (state2 == 0) { |
embeddedartists | 0:c81327076cac | 40 | // Both pressed so turn off LED/buzzer |
embeddedartists | 0:c81327076cac | 41 | led = 1; |
embeddedartists | 2:697cd0c40fbf | 42 | buzzer = 1; |
embeddedartists | 0:c81327076cac | 43 | } else { |
embeddedartists | 0:c81327076cac | 44 | // Only one pressed so turn on LED/buzzer |
embeddedartists | 0:c81327076cac | 45 | led = 0; |
embeddedartists | 2:697cd0c40fbf | 46 | buzzer = 0; |
embeddedartists | 0:c81327076cac | 47 | } |
embeddedartists | 0:c81327076cac | 48 | } else { |
embeddedartists | 0:c81327076cac | 49 | if (state2 == 0) { |
embeddedartists | 0:c81327076cac | 50 | // Only one pressed so turn on LED/buzzer |
embeddedartists | 0:c81327076cac | 51 | led = 0; |
embeddedartists | 2:697cd0c40fbf | 52 | buzzer = 0; |
embeddedartists | 0:c81327076cac | 53 | } else { |
embeddedartists | 0:c81327076cac | 54 | // Both pressed so turn off LED/buzzer |
embeddedartists | 0:c81327076cac | 55 | led = 1; |
embeddedartists | 2:697cd0c40fbf | 56 | buzzer = 1; |
embeddedartists | 0:c81327076cac | 57 | } |
embeddedartists | 0:c81327076cac | 58 | } |
embeddedartists | 0:c81327076cac | 59 | } |
embeddedartists | 0:c81327076cac | 60 | } |
embeddedartists | 0:c81327076cac | 61 | |
embeddedartists | 0:c81327076cac | 62 | |
embeddedartists | 0:c81327076cac | 63 | static void experiment3() |
embeddedartists | 0:c81327076cac | 64 | { |
embeddedartists | 0:c81327076cac | 65 | bool ledOn = false; |
embeddedartists | 0:c81327076cac | 66 | |
embeddedartists | 0:c81327076cac | 67 | led = 1; // Turn LED off |
embeddedartists | 0:c81327076cac | 68 | |
embeddedartists | 0:c81327076cac | 69 | button1.mode(PullUp); // Enable button |
embeddedartists | 0:c81327076cac | 70 | |
embeddedartists | 0:c81327076cac | 71 | // Enter forever loop |
embeddedartists | 0:c81327076cac | 72 | while(1) { |
embeddedartists | 0:c81327076cac | 73 | |
embeddedartists | 0:c81327076cac | 74 | // Check if push-button is pressed (input is low) |
embeddedartists | 0:c81327076cac | 75 | if (!button1) { |
embeddedartists | 0:c81327076cac | 76 | // Toggle LED |
embeddedartists | 0:c81327076cac | 77 | ledOn = !ledOn; |
embeddedartists | 0:c81327076cac | 78 | if (ledOn) { |
embeddedartists | 0:c81327076cac | 79 | led = 0; |
embeddedartists | 0:c81327076cac | 80 | } else { |
embeddedartists | 0:c81327076cac | 81 | led = 1; |
embeddedartists | 0:c81327076cac | 82 | } |
embeddedartists | 0:c81327076cac | 83 | |
embeddedartists | 0:c81327076cac | 84 | // Wait until push-button is released |
embeddedartists | 0:c81327076cac | 85 | while (!button1) { |
embeddedartists | 0:c81327076cac | 86 | } |
embeddedartists | 0:c81327076cac | 87 | } |
embeddedartists | 0:c81327076cac | 88 | } |
embeddedartists | 0:c81327076cac | 89 | } |
embeddedartists | 0:c81327076cac | 90 | |
embeddedartists | 0:c81327076cac | 91 | static void experiment4() |
embeddedartists | 0:c81327076cac | 92 | { |
embeddedartists | 0:c81327076cac | 93 | bool ledOn = false; |
embeddedartists | 0:c81327076cac | 94 | |
embeddedartists | 0:c81327076cac | 95 | led = 1; // Turn LED off |
embeddedartists | 0:c81327076cac | 96 | |
embeddedartists | 0:c81327076cac | 97 | button1.mode(PullUp); // Enable button |
embeddedartists | 0:c81327076cac | 98 | |
embeddedartists | 0:c81327076cac | 99 | // Enter forever loop |
embeddedartists | 0:c81327076cac | 100 | while(1) { |
embeddedartists | 0:c81327076cac | 101 | // Delay a specified period of time (the sample period) |
embeddedartists | 1:5d12e995c311 | 102 | wait_ms(10); |
embeddedartists | 0:c81327076cac | 103 | |
embeddedartists | 0:c81327076cac | 104 | // Check if push-button is pressed (input is low) |
embeddedartists | 0:c81327076cac | 105 | if (!button1) { |
embeddedartists | 0:c81327076cac | 106 | // Toggle LED |
embeddedartists | 0:c81327076cac | 107 | ledOn = !ledOn; |
embeddedartists | 0:c81327076cac | 108 | if (ledOn) { |
embeddedartists | 0:c81327076cac | 109 | led = 0; |
embeddedartists | 0:c81327076cac | 110 | } else { |
embeddedartists | 0:c81327076cac | 111 | led = 1; |
embeddedartists | 0:c81327076cac | 112 | } |
embeddedartists | 0:c81327076cac | 113 | |
embeddedartists | 0:c81327076cac | 114 | // Wait until push-button is released |
embeddedartists | 0:c81327076cac | 115 | while (!button1) { |
embeddedartists | 0:c81327076cac | 116 | } |
embeddedartists | 0:c81327076cac | 117 | } |
embeddedartists | 0:c81327076cac | 118 | } |
embeddedartists | 0:c81327076cac | 119 | } |
embeddedartists | 0:c81327076cac | 120 | |
embeddedartists | 0:c81327076cac | 121 | int main() |
embeddedartists | 0:c81327076cac | 122 | { |
embeddedartists | 0:c81327076cac | 123 | //experiment1(); |
embeddedartists | 0:c81327076cac | 124 | //experiment2(); |
embeddedartists | 0:c81327076cac | 125 | //experiment3(); |
embeddedartists | 0:c81327076cac | 126 | experiment4(); |
embeddedartists | 0:c81327076cac | 127 | } |