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.
inc/Timer.h@1:ff38e3bad33a, 2018-11-15 (annotated)
- Committer:
- martwerl
- Date:
- Thu Nov 15 17:59:08 2018 +0000
- Revision:
- 1:ff38e3bad33a
- Parent:
- 0:afeca64a6543
Diplomarbeit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martwerl | 0:afeca64a6543 | 1 | /* mbed Microcontroller Library |
| martwerl | 0:afeca64a6543 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| martwerl | 0:afeca64a6543 | 3 | * |
| martwerl | 0:afeca64a6543 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| martwerl | 0:afeca64a6543 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| martwerl | 0:afeca64a6543 | 6 | * in the Software without restriction, including without limitation the rights |
| martwerl | 0:afeca64a6543 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| martwerl | 0:afeca64a6543 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| martwerl | 0:afeca64a6543 | 9 | * furnished to do so, subject to the following conditions: |
| martwerl | 0:afeca64a6543 | 10 | * |
| martwerl | 0:afeca64a6543 | 11 | * The above copyright notice and this permission notice shall be included in |
| martwerl | 0:afeca64a6543 | 12 | * all copies or substantial portions of the Software. |
| martwerl | 0:afeca64a6543 | 13 | * |
| martwerl | 0:afeca64a6543 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| martwerl | 0:afeca64a6543 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| martwerl | 0:afeca64a6543 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| martwerl | 0:afeca64a6543 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| martwerl | 0:afeca64a6543 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| martwerl | 0:afeca64a6543 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| martwerl | 0:afeca64a6543 | 20 | * SOFTWARE. |
| martwerl | 0:afeca64a6543 | 21 | */ |
| martwerl | 0:afeca64a6543 | 22 | #ifndef MBED_TIMER_H |
| martwerl | 0:afeca64a6543 | 23 | #define MBED_TIMER_H |
| martwerl | 0:afeca64a6543 | 24 | |
| martwerl | 0:afeca64a6543 | 25 | #include "platform.h" |
| martwerl | 0:afeca64a6543 | 26 | |
| martwerl | 0:afeca64a6543 | 27 | namespace mbed { |
| martwerl | 0:afeca64a6543 | 28 | |
| martwerl | 0:afeca64a6543 | 29 | /** A general purpose timer |
| martwerl | 0:afeca64a6543 | 30 | * |
| martwerl | 0:afeca64a6543 | 31 | * Example: |
| martwerl | 0:afeca64a6543 | 32 | * @code |
| martwerl | 0:afeca64a6543 | 33 | * // Count the time to toggle a LED |
| martwerl | 0:afeca64a6543 | 34 | * |
| martwerl | 0:afeca64a6543 | 35 | * #include "mbed.h" |
| martwerl | 0:afeca64a6543 | 36 | * |
| martwerl | 0:afeca64a6543 | 37 | * Timer timer; |
| martwerl | 0:afeca64a6543 | 38 | * DigitalOut led(LED1); |
| martwerl | 0:afeca64a6543 | 39 | * int begin, end; |
| martwerl | 0:afeca64a6543 | 40 | * |
| martwerl | 0:afeca64a6543 | 41 | * int main() { |
| martwerl | 0:afeca64a6543 | 42 | * timer.start(); |
| martwerl | 0:afeca64a6543 | 43 | * begin = timer.read_us(); |
| martwerl | 0:afeca64a6543 | 44 | * led = !led; |
| martwerl | 0:afeca64a6543 | 45 | * end = timer.read_us(); |
| martwerl | 0:afeca64a6543 | 46 | * printf("Toggle the led takes %d us", end - begin); |
| martwerl | 0:afeca64a6543 | 47 | * } |
| martwerl | 0:afeca64a6543 | 48 | * @endcode |
| martwerl | 0:afeca64a6543 | 49 | */ |
| martwerl | 0:afeca64a6543 | 50 | class Timer { |
| martwerl | 0:afeca64a6543 | 51 | |
| martwerl | 0:afeca64a6543 | 52 | public: |
| martwerl | 0:afeca64a6543 | 53 | Timer(); |
| martwerl | 0:afeca64a6543 | 54 | |
| martwerl | 0:afeca64a6543 | 55 | /** Start the timer |
| martwerl | 0:afeca64a6543 | 56 | */ |
| martwerl | 0:afeca64a6543 | 57 | void start(); |
| martwerl | 0:afeca64a6543 | 58 | |
| martwerl | 0:afeca64a6543 | 59 | /** Stop the timer |
| martwerl | 0:afeca64a6543 | 60 | */ |
| martwerl | 0:afeca64a6543 | 61 | void stop(); |
| martwerl | 0:afeca64a6543 | 62 | |
| martwerl | 0:afeca64a6543 | 63 | /** Reset the timer to 0. |
| martwerl | 0:afeca64a6543 | 64 | * |
| martwerl | 0:afeca64a6543 | 65 | * If it was already counting, it will continue |
| martwerl | 0:afeca64a6543 | 66 | */ |
| martwerl | 0:afeca64a6543 | 67 | void reset(); |
| martwerl | 0:afeca64a6543 | 68 | |
| martwerl | 0:afeca64a6543 | 69 | /** Get the time passed in seconds |
| martwerl | 0:afeca64a6543 | 70 | */ |
| martwerl | 0:afeca64a6543 | 71 | float read(); |
| martwerl | 0:afeca64a6543 | 72 | |
| martwerl | 0:afeca64a6543 | 73 | /** Get the time passed in mili-seconds |
| martwerl | 0:afeca64a6543 | 74 | */ |
| martwerl | 0:afeca64a6543 | 75 | int read_ms(); |
| martwerl | 0:afeca64a6543 | 76 | |
| martwerl | 0:afeca64a6543 | 77 | /** Get the time passed in micro-seconds |
| martwerl | 0:afeca64a6543 | 78 | */ |
| martwerl | 0:afeca64a6543 | 79 | int read_us(); |
| martwerl | 0:afeca64a6543 | 80 | |
| martwerl | 0:afeca64a6543 | 81 | #ifdef MBED_OPERATORS |
| martwerl | 0:afeca64a6543 | 82 | operator float(); |
| martwerl | 0:afeca64a6543 | 83 | #endif |
| martwerl | 0:afeca64a6543 | 84 | |
| martwerl | 0:afeca64a6543 | 85 | protected: |
| martwerl | 0:afeca64a6543 | 86 | int slicetime(); |
| martwerl | 0:afeca64a6543 | 87 | int _running; // whether the timer is running |
| martwerl | 0:afeca64a6543 | 88 | unsigned int _start; // the start time of the latest slice |
| martwerl | 0:afeca64a6543 | 89 | int _time; // any accumulated time from previous slices |
| martwerl | 0:afeca64a6543 | 90 | }; |
| martwerl | 0:afeca64a6543 | 91 | |
| martwerl | 0:afeca64a6543 | 92 | } // namespace mbed |
| martwerl | 0:afeca64a6543 | 93 | |
| martwerl | 0:afeca64a6543 | 94 | #endif |
| martwerl | 0:afeca64a6543 | 95 |