Charles Tritt
/
21_TW_Ex_9_1_v5
Initial Mbed v. 5 version. Demonstrates ISR using two LED junctions, one blinking and one toggling.
main.cpp@109:e9badabfd93d, 2021-10-17 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Oct 17 15:50:50 2021 +0000
- Revision:
- 109:e9badabfd93d
- Parent:
- 108:5621bbcc10f5
Added circuit/wiring instructions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 107:61b9c99a4e27 | 1 | /* |
CSTritt | 108:5621bbcc10f5 | 2 | Project: 21_TW_Ex_9_1_v5 |
CSTritt | 108:5621bbcc10f5 | 3 | File: main.cpp |
CSTritt | 108:5621bbcc10f5 | 4 | |
CSTritt | 108:5621bbcc10f5 | 5 | An example similar to T&W example 9.1. Green junction will flash |
CSTritt | 108:5621bbcc10f5 | 6 | continuously. Blue junction will toggle in response to depressing the user |
CSTritt | 108:5621bbcc10f5 | 7 | button. |
CSTritt | 108:5621bbcc10f5 | 8 | |
CSTritt | 109:e9badabfd93d | 9 | Circuit (common cathode RGB LED needed) |
CSTritt | 109:e9badabfd93d | 10 | |
CSTritt | 109:e9badabfd93d | 11 | Connect green junction anode to D3. |
CSTritt | 109:e9badabfd93d | 12 | Connect blue junction anode to D4. |
CSTritt | 109:e9badabfd93d | 13 | Connect common cathode to common (ground). |
CSTritt | 109:e9badabfd93d | 14 | |
CSTritt | 109:e9badabfd93d | 15 | Created by Dr. C. S. Tritt |
CSTritt | 108:5621bbcc10f5 | 16 | Last revised: 10/17/21 (v. 1.0) |
CSTritt | 107:61b9c99a4e27 | 17 | */ |
Jonathan Austin |
0:2757d7abb7d9 | 18 | #include "mbed.h" |
CSTritt | 108:5621bbcc10f5 | 19 | |
CSTritt | 108:5621bbcc10f5 | 20 | InterruptIn myButton(USER_BUTTON); // Button is normally high. Goes low w/press. |
CSTritt | 108:5621bbcc10f5 | 21 | |
CSTritt | 108:5621bbcc10f5 | 22 | DigitalOut bluLED(D4); // Blue and green LED junctions. |
CSTritt | 108:5621bbcc10f5 | 23 | DigitalOut grnLED(D3); |
CSTritt | 108:5621bbcc10f5 | 24 | |
CSTritt | 108:5621bbcc10f5 | 25 | void myISR() { // Simple ISR toggles the blue LED junction when called. |
CSTritt | 108:5621bbcc10f5 | 26 | bluLED = !bluLED; // Toggle blue junction. |
CSTritt | 108:5621bbcc10f5 | 27 | } |
CSTritt | 108:5621bbcc10f5 | 28 | |
CSTritt | 108:5621bbcc10f5 | 29 | int main() { |
CSTritt | 108:5621bbcc10f5 | 30 | const int GRN_PERIOD = 500; |
CSTritt | 108:5621bbcc10f5 | 31 | bluLED = 0; // Turn blue & green off at start. |
CSTritt | 108:5621bbcc10f5 | 32 | grnLED = 0; |
CSTritt | 108:5621bbcc10f5 | 33 | |
CSTritt | 108:5621bbcc10f5 | 34 | myButton.fall(&myISR); // "Register" the ISR routine. Sets vector. |
CSTritt | 108:5621bbcc10f5 | 35 | |
CSTritt | 108:5621bbcc10f5 | 36 | while(true) { |
CSTritt | 108:5621bbcc10f5 | 37 | grnLED = !grnLED; // Toggle green junction. |
CSTritt | 108:5621bbcc10f5 | 38 | ThisThread::sleep_for(GRN_PERIOD); // Sleep for GRN_PERIOD mS. |
CSTritt | 108:5621bbcc10f5 | 39 | } |
CSTritt | 108:5621bbcc10f5 | 40 | } |