|
Revision 29, 1.8 kB
(checked in by emilmont, 6 months ago)
|
|
New Libraries 11.11
|
| Line | |
|---|
| 1 | /* mbed Microcontroller Library - Timer |
|---|
| 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | #ifndef MBED_TIMER_H |
|---|
| 6 | #define MBED_TIMER_H |
|---|
| 7 | |
|---|
| 8 | #include "platform.h" |
|---|
| 9 | #include "PinNames.h" |
|---|
| 10 | #include "PeripheralNames.h" |
|---|
| 11 | #include "Base.h" |
|---|
| 12 | |
|---|
| 13 | namespace mbed { |
|---|
| 14 | |
|---|
| 15 | /* Class: Timer |
|---|
| 16 | * A general purpose timer |
|---|
| 17 | * |
|---|
| 18 | * Example: |
|---|
| 19 | * > // Count the time to toggle a LED |
|---|
| 20 | * > |
|---|
| 21 | * > #include "mbed.h" |
|---|
| 22 | * > |
|---|
| 23 | * > Timer timer; |
|---|
| 24 | * > DigitalOut led(LED1); |
|---|
| 25 | * > int begin, end; |
|---|
| 26 | * > |
|---|
| 27 | * > int main() { |
|---|
| 28 | * > timer.start(); |
|---|
| 29 | * > begin = timer.read_us(); |
|---|
| 30 | * > led = !led; |
|---|
| 31 | * > end = timer.read_us(); |
|---|
| 32 | * > printf("Toggle the led takes %d us", end - begin); |
|---|
| 33 | * > } |
|---|
| 34 | */ |
|---|
| 35 | class Timer : public Base { |
|---|
| 36 | |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | Timer(const char *name = NULL); |
|---|
| 40 | |
|---|
| 41 | /* Function: start |
|---|
| 42 | * Start the timer |
|---|
| 43 | */ |
|---|
| 44 | void start(); |
|---|
| 45 | |
|---|
| 46 | /* Function: stop |
|---|
| 47 | * Stop the timer |
|---|
| 48 | */ |
|---|
| 49 | void stop(); |
|---|
| 50 | |
|---|
| 51 | /* Function: reset |
|---|
| 52 | * Reset the timer to 0. |
|---|
| 53 | * |
|---|
| 54 | * If it was already counting, it will continue |
|---|
| 55 | */ |
|---|
| 56 | void reset(); |
|---|
| 57 | |
|---|
| 58 | /* Function: read |
|---|
| 59 | * Get the time passed in seconds |
|---|
| 60 | */ |
|---|
| 61 | float read(); |
|---|
| 62 | |
|---|
| 63 | /* Function: read_ms |
|---|
| 64 | * Get the time passed in mili-seconds |
|---|
| 65 | */ |
|---|
| 66 | int read_ms(); |
|---|
| 67 | |
|---|
| 68 | /* Function: read_us |
|---|
| 69 | * Get the time passed in micro-seconds |
|---|
| 70 | */ |
|---|
| 71 | int read_us(); |
|---|
| 72 | |
|---|
| 73 | #ifdef MBED_OPERATORS |
|---|
| 74 | operator float(); |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | #ifdef MBED_RPC |
|---|
| 78 | virtual const struct rpc_method *get_rpc_methods(); |
|---|
| 79 | static struct rpc_class *get_rpc_class(); |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 82 | protected: |
|---|
| 83 | |
|---|
| 84 | int slicetime(); |
|---|
| 85 | int _running; // whether the timer is running |
|---|
| 86 | unsigned int _start; // the start time of the latest slice |
|---|
| 87 | int _time; // any accumulated time from previous slices |
|---|
| 88 | |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | } // namespace mbed |
|---|
| 92 | |
|---|
| 93 | #endif |
|---|
| 94 | |
|---|