EmbeddedArtists AB
/
lpc812_exp_solution_interrupts
Solutions for the Interrupts experiments for LPC812 MAX
main.cpp@0:0614a4add2d3, 2013-11-24 (annotated)
- Committer:
- embeddedartists
- Date:
- Sun Nov 24 12:03:56 2013 +0000
- Revision:
- 0:0614a4add2d3
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:0614a4add2d3 | 1 | #include "mbed.h" |
embeddedartists | 0:0614a4add2d3 | 2 | |
embeddedartists | 0:0614a4add2d3 | 3 | InterruptIn event(D0); |
embeddedartists | 0:0614a4add2d3 | 4 | |
embeddedartists | 0:0614a4add2d3 | 5 | DigitalOut greenLED(LED_GREEN); |
embeddedartists | 0:0614a4add2d3 | 6 | DigitalOut redLED(LED_RED); |
embeddedartists | 0:0614a4add2d3 | 7 | DigitalOut blueLED(LED_BLUE); |
embeddedartists | 0:0614a4add2d3 | 8 | |
embeddedartists | 0:0614a4add2d3 | 9 | DigitalIn button(D0); |
embeddedartists | 0:0614a4add2d3 | 10 | |
embeddedartists | 0:0614a4add2d3 | 11 | Ticker timer; |
embeddedartists | 0:0614a4add2d3 | 12 | Ticker timerGreen; |
embeddedartists | 0:0614a4add2d3 | 13 | Ticker timerBlue; |
embeddedartists | 0:0614a4add2d3 | 14 | |
embeddedartists | 0:0614a4add2d3 | 15 | |
embeddedartists | 0:0614a4add2d3 | 16 | void trigger_alt1() { |
embeddedartists | 0:0614a4add2d3 | 17 | // add code to blink the LED |
embeddedartists | 0:0614a4add2d3 | 18 | redLED = 0; |
embeddedartists | 0:0614a4add2d3 | 19 | } |
embeddedartists | 0:0614a4add2d3 | 20 | |
embeddedartists | 0:0614a4add2d3 | 21 | static void experiment1_alt1() |
embeddedartists | 0:0614a4add2d3 | 22 | { |
embeddedartists | 0:0614a4add2d3 | 23 | redLED = greenLED = blueLED = 1; // Turn LEDs off |
embeddedartists | 0:0614a4add2d3 | 24 | |
embeddedartists | 0:0614a4add2d3 | 25 | // Register the handler for falling edges |
embeddedartists | 0:0614a4add2d3 | 26 | event.mode(PullUp); |
embeddedartists | 0:0614a4add2d3 | 27 | event.fall(&trigger_alt1); |
embeddedartists | 0:0614a4add2d3 | 28 | while(1) { |
embeddedartists | 0:0614a4add2d3 | 29 | //redLED = 1; |
embeddedartists | 0:0614a4add2d3 | 30 | } |
embeddedartists | 0:0614a4add2d3 | 31 | } |
embeddedartists | 0:0614a4add2d3 | 32 | |
embeddedartists | 0:0614a4add2d3 | 33 | void trigger_alt2() { |
embeddedartists | 0:0614a4add2d3 | 34 | greenLED = 0; |
embeddedartists | 0:0614a4add2d3 | 35 | while (!button) { |
embeddedartists | 0:0614a4add2d3 | 36 | } |
embeddedartists | 0:0614a4add2d3 | 37 | greenLED = 1; |
embeddedartists | 0:0614a4add2d3 | 38 | } |
embeddedartists | 0:0614a4add2d3 | 39 | |
embeddedartists | 0:0614a4add2d3 | 40 | static void experiment1_alt2() |
embeddedartists | 0:0614a4add2d3 | 41 | { |
embeddedartists | 0:0614a4add2d3 | 42 | // Register the handler for falling edges |
embeddedartists | 0:0614a4add2d3 | 43 | event.mode(PullUp); |
embeddedartists | 0:0614a4add2d3 | 44 | event.fall(&trigger_alt2); |
embeddedartists | 0:0614a4add2d3 | 45 | |
embeddedartists | 0:0614a4add2d3 | 46 | // Turn LEDs off |
embeddedartists | 0:0614a4add2d3 | 47 | greenLED = 1; |
embeddedartists | 0:0614a4add2d3 | 48 | redLED = 1; |
embeddedartists | 0:0614a4add2d3 | 49 | blueLED = 1; |
embeddedartists | 0:0614a4add2d3 | 50 | |
embeddedartists | 0:0614a4add2d3 | 51 | while(1) { |
embeddedartists | 0:0614a4add2d3 | 52 | redLED = !redLED; |
embeddedartists | 0:0614a4add2d3 | 53 | wait(0.1); |
embeddedartists | 0:0614a4add2d3 | 54 | } |
embeddedartists | 0:0614a4add2d3 | 55 | } |
embeddedartists | 0:0614a4add2d3 | 56 | |
embeddedartists | 0:0614a4add2d3 | 57 | |
embeddedartists | 0:0614a4add2d3 | 58 | static void ticker_event() |
embeddedartists | 0:0614a4add2d3 | 59 | { |
embeddedartists | 0:0614a4add2d3 | 60 | static bool on = false; |
embeddedartists | 0:0614a4add2d3 | 61 | on = !on; |
embeddedartists | 0:0614a4add2d3 | 62 | |
embeddedartists | 0:0614a4add2d3 | 63 | // add code to blink the LED |
embeddedartists | 0:0614a4add2d3 | 64 | redLED = (on ? 0 : 1); |
embeddedartists | 0:0614a4add2d3 | 65 | } |
embeddedartists | 0:0614a4add2d3 | 66 | |
embeddedartists | 0:0614a4add2d3 | 67 | static void experiment2_alt1() |
embeddedartists | 0:0614a4add2d3 | 68 | { |
embeddedartists | 0:0614a4add2d3 | 69 | redLED = greenLED = blueLED = 1; // Turn LEDs off |
embeddedartists | 0:0614a4add2d3 | 70 | |
embeddedartists | 0:0614a4add2d3 | 71 | // Request the timer function to be called every 5 seconds |
embeddedartists | 0:0614a4add2d3 | 72 | timer.attach(&ticker_event, 5); |
embeddedartists | 0:0614a4add2d3 | 73 | while(1) { |
embeddedartists | 0:0614a4add2d3 | 74 | ; |
embeddedartists | 0:0614a4add2d3 | 75 | } |
embeddedartists | 0:0614a4add2d3 | 76 | } |
embeddedartists | 0:0614a4add2d3 | 77 | |
embeddedartists | 0:0614a4add2d3 | 78 | static void ticker_event_green() |
embeddedartists | 0:0614a4add2d3 | 79 | { |
embeddedartists | 0:0614a4add2d3 | 80 | static bool on = false; |
embeddedartists | 0:0614a4add2d3 | 81 | on = !on; |
embeddedartists | 0:0614a4add2d3 | 82 | |
embeddedartists | 0:0614a4add2d3 | 83 | // add code to blink the LED |
embeddedartists | 0:0614a4add2d3 | 84 | greenLED = (on ? 0 : 1); |
embeddedartists | 0:0614a4add2d3 | 85 | } |
embeddedartists | 0:0614a4add2d3 | 86 | static void ticker_event_blue() |
embeddedartists | 0:0614a4add2d3 | 87 | { |
embeddedartists | 0:0614a4add2d3 | 88 | static bool on = true; |
embeddedartists | 0:0614a4add2d3 | 89 | on = !on; |
embeddedartists | 0:0614a4add2d3 | 90 | |
embeddedartists | 0:0614a4add2d3 | 91 | // add code to blink the LED |
embeddedartists | 0:0614a4add2d3 | 92 | blueLED = (on ? 0 : 1); |
embeddedartists | 0:0614a4add2d3 | 93 | } |
embeddedartists | 0:0614a4add2d3 | 94 | static void experiment2_alt2() |
embeddedartists | 0:0614a4add2d3 | 95 | { |
embeddedartists | 0:0614a4add2d3 | 96 | redLED = greenLED = blueLED = 1; // Turn LEDs off |
embeddedartists | 0:0614a4add2d3 | 97 | |
embeddedartists | 0:0614a4add2d3 | 98 | // Create one timer for each color. If the same timer is used then only |
embeddedartists | 0:0614a4add2d3 | 99 | // the last call to attach will work (previous calls are overwritten) |
embeddedartists | 0:0614a4add2d3 | 100 | timer.attach(&ticker_event, 0.4); |
embeddedartists | 0:0614a4add2d3 | 101 | timerGreen.attach(&ticker_event_green, 0.2); |
embeddedartists | 0:0614a4add2d3 | 102 | timerBlue.attach(&ticker_event_blue, 0.1); |
embeddedartists | 0:0614a4add2d3 | 103 | while(1) { |
embeddedartists | 0:0614a4add2d3 | 104 | ; |
embeddedartists | 0:0614a4add2d3 | 105 | } |
embeddedartists | 0:0614a4add2d3 | 106 | } |
embeddedartists | 0:0614a4add2d3 | 107 | |
embeddedartists | 0:0614a4add2d3 | 108 | |
embeddedartists | 0:0614a4add2d3 | 109 | int main() |
embeddedartists | 0:0614a4add2d3 | 110 | { |
embeddedartists | 0:0614a4add2d3 | 111 | //experiment1_alt1(); //simple trigger |
embeddedartists | 0:0614a4add2d3 | 112 | //experiment1_alt2(); //trigger which takes time to complete |
embeddedartists | 0:0614a4add2d3 | 113 | //experiment2_alt1(); //blinks the RED led every 5 seconds |
embeddedartists | 0:0614a4add2d3 | 114 | experiment2_alt2(); //blinks all three components of the RGB LED |
embeddedartists | 0:0614a4add2d3 | 115 | } |