Timer und Ticker

Dependencies:   mbed

Committer:
corsa1600
Date:
Mon Feb 04 16:58:24 2019 +0000
Revision:
0:1316a6f25ffc
Timer und Ticker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
corsa1600 0:1316a6f25ffc 1 // switch blue LEDs LED1 .. LED4 with external interrupt functionality
corsa1600 0:1316a6f25ffc 2 /*
corsa1600 0:1316a6f25ffc 3 3 classes
corsa1600 0:1316a6f25ffc 4 Timeout - Call a function after a specified delay
corsa1600 0:1316a6f25ffc 5 Ticker - Repeatedly call a function
corsa1600 0:1316a6f25ffc 6 Timer - Create, start, stop and read a timer
corsa1600 0:1316a6f25ffc 7 */
corsa1600 0:1316a6f25ffc 8
corsa1600 0:1316a6f25ffc 9 #include "mbed.h"
corsa1600 0:1316a6f25ffc 10
corsa1600 0:1316a6f25ffc 11 Timeout toFlipper;
corsa1600 0:1316a6f25ffc 12 DigitalOut doLed1(LED1);
corsa1600 0:1316a6f25ffc 13 DigitalOut doLed2(LED2);
corsa1600 0:1316a6f25ffc 14 Ticker tickBlinker;
corsa1600 0:1316a6f25ffc 15 DigitalOut doLed3(LED3);
corsa1600 0:1316a6f25ffc 16 DigitalOut doLed4(LED4);
corsa1600 0:1316a6f25ffc 17 Timer tMyTimer;
corsa1600 0:1316a6f25ffc 18 Serial pc(USBTX, USBRX); // tx, rx
corsa1600 0:1316a6f25ffc 19 int count =0;
corsa1600 0:1316a6f25ffc 20 int min=0;
corsa1600 0:1316a6f25ffc 21
corsa1600 0:1316a6f25ffc 22 // functions
corsa1600 0:1316a6f25ffc 23 void flip() {
corsa1600 0:1316a6f25ffc 24 doLed1 = !doLed1;
corsa1600 0:1316a6f25ffc 25 }
corsa1600 0:1316a6f25ffc 26
corsa1600 0:1316a6f25ffc 27 void blink() {
corsa1600 0:1316a6f25ffc 28 doLed3 = !doLed3;
corsa1600 0:1316a6f25ffc 29 count ++;
corsa1600 0:1316a6f25ffc 30 }
corsa1600 0:1316a6f25ffc 31
corsa1600 0:1316a6f25ffc 32 int main() {
corsa1600 0:1316a6f25ffc 33 doLed2 = 1;
corsa1600 0:1316a6f25ffc 34 toFlipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
corsa1600 0:1316a6f25ffc 35 tickBlinker.attach(&blink, 1.0); // setup blinker to call blink avery 500 msec
corsa1600 0:1316a6f25ffc 36
corsa1600 0:1316a6f25ffc 37 // spin in a main loop. flipper will interrupt it to call flip
corsa1600 0:1316a6f25ffc 38 while(1) {
corsa1600 0:1316a6f25ffc 39
corsa1600 0:1316a6f25ffc 40 if (count ==60)
corsa1600 0:1316a6f25ffc 41 {
corsa1600 0:1316a6f25ffc 42 min++;
corsa1600 0:1316a6f25ffc 43 count=0;
corsa1600 0:1316a6f25ffc 44 }
corsa1600 0:1316a6f25ffc 45 tMyTimer.reset();
corsa1600 0:1316a6f25ffc 46 tMyTimer.start();
corsa1600 0:1316a6f25ffc 47 //pc.printf("Hello Timer-World! ");
corsa1600 0:1316a6f25ffc 48 doLed4 = !doLed4;
corsa1600 0:1316a6f25ffc 49 wait(1.0);
corsa1600 0:1316a6f25ffc 50 tMyTimer.stop();
corsa1600 0:1316a6f25ffc 51 pc.printf("Uhr lauft seit %iMinuten & %i Sekunden\r",min,count);
corsa1600 0:1316a6f25ffc 52 //The time taken was %f seconds\r", tMyTimer.read());
corsa1600 0:1316a6f25ffc 53
corsa1600 0:1316a6f25ffc 54 }
corsa1600 0:1316a6f25ffc 55 }