Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:caa17e8c0bc3, 2018-11-15 (annotated)
- Committer:
- Wizo
- Date:
- Thu Nov 15 18:05:48 2018 +0000
- Revision:
- 0:caa17e8c0bc3
TimerInt
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Wizo | 0:caa17e8c0bc3 | 1 | // switch blue LEDs LED1 .. LED4 with external interrupt functionality |
| Wizo | 0:caa17e8c0bc3 | 2 | /* |
| Wizo | 0:caa17e8c0bc3 | 3 | 3 classes |
| Wizo | 0:caa17e8c0bc3 | 4 | Timeout - Call a function after a specified delay |
| Wizo | 0:caa17e8c0bc3 | 5 | Ticker - Repeatedly call a function |
| Wizo | 0:caa17e8c0bc3 | 6 | Timer - Create, start, stop and read a timer |
| Wizo | 0:caa17e8c0bc3 | 7 | */ |
| Wizo | 0:caa17e8c0bc3 | 8 | |
| Wizo | 0:caa17e8c0bc3 | 9 | #include "mbed.h" |
| Wizo | 0:caa17e8c0bc3 | 10 | |
| Wizo | 0:caa17e8c0bc3 | 11 | Timeout toFlipper; |
| Wizo | 0:caa17e8c0bc3 | 12 | DigitalOut doLed1(LED1); |
| Wizo | 0:caa17e8c0bc3 | 13 | DigitalOut doLed2(LED2); |
| Wizo | 0:caa17e8c0bc3 | 14 | Ticker tickBlinker; |
| Wizo | 0:caa17e8c0bc3 | 15 | DigitalOut doLed3(LED3); |
| Wizo | 0:caa17e8c0bc3 | 16 | Timer tMyTimer; |
| Wizo | 0:caa17e8c0bc3 | 17 | Serial pc(USBTX, USBRX); // tx, rx |
| Wizo | 0:caa17e8c0bc3 | 18 | |
| Wizo | 0:caa17e8c0bc3 | 19 | // functions |
| Wizo | 0:caa17e8c0bc3 | 20 | void flip() { |
| Wizo | 0:caa17e8c0bc3 | 21 | doLed1 = !doLed1; |
| Wizo | 0:caa17e8c0bc3 | 22 | } |
| Wizo | 0:caa17e8c0bc3 | 23 | |
| Wizo | 0:caa17e8c0bc3 | 24 | void blink() { |
| Wizo | 0:caa17e8c0bc3 | 25 | doLed3 = !doLed3; |
| Wizo | 0:caa17e8c0bc3 | 26 | } |
| Wizo | 0:caa17e8c0bc3 | 27 | |
| Wizo | 0:caa17e8c0bc3 | 28 | int main() { |
| Wizo | 0:caa17e8c0bc3 | 29 | doLed2 = 1; |
| Wizo | 0:caa17e8c0bc3 | 30 | toFlipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds |
| Wizo | 0:caa17e8c0bc3 | 31 | tickBlinker.attach(&blink, 0.5); // setup blinker to call blink avery 500 msec |
| Wizo | 0:caa17e8c0bc3 | 32 | |
| Wizo | 0:caa17e8c0bc3 | 33 | // spin in a main loop. flipper will interrupt it to call flip |
| Wizo | 0:caa17e8c0bc3 | 34 | while(1) { |
| Wizo | 0:caa17e8c0bc3 | 35 | tMyTimer.reset(); |
| Wizo | 0:caa17e8c0bc3 | 36 | tMyTimer.start(); |
| Wizo | 0:caa17e8c0bc3 | 37 | pc.printf("Hello Timer-World! "); |
| Wizo | 0:caa17e8c0bc3 | 38 | doLed1 = !doLed1; |
| Wizo | 0:caa17e8c0bc3 | 39 | wait(1.0); |
| Wizo | 0:caa17e8c0bc3 | 40 | tMyTimer.stop(); |
| Wizo | 0:caa17e8c0bc3 | 41 | pc.printf("The time taken was %f seconds\r", tMyTimer.read()); |
| Wizo | 0:caa17e8c0bc3 | 42 | } |
| Wizo | 0:caa17e8c0bc3 | 43 | } |