mbed library sources. Supersedes mbed-src.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
Timer.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_TIMER_H 00017 #define MBED_TIMER_H 00018 00019 #include "platform.h" 00020 #include "ticker_api.h" 00021 00022 namespace mbed { 00023 00024 /** A general purpose timer 00025 * 00026 * @Note Synchronization level: Interrupt safe 00027 * 00028 * Example: 00029 * @code 00030 * // Count the time to toggle a LED 00031 * 00032 * #include "mbed.h" 00033 * 00034 * Timer timer; 00035 * DigitalOut led(LED1); 00036 * int begin, end; 00037 * 00038 * int main() { 00039 * timer.start(); 00040 * begin = timer.read_us(); 00041 * led = !led; 00042 * end = timer.read_us(); 00043 * printf("Toggle the led takes %d us", end - begin); 00044 * } 00045 * @endcode 00046 */ 00047 class Timer { 00048 00049 public: 00050 Timer(); 00051 Timer(const ticker_data_t *data); 00052 00053 /** Start the timer 00054 */ 00055 void start(); 00056 00057 /** Stop the timer 00058 */ 00059 void stop(); 00060 00061 /** Reset the timer to 0. 00062 * 00063 * If it was already counting, it will continue 00064 */ 00065 void reset(); 00066 00067 /** Get the time passed in seconds 00068 */ 00069 float read(); 00070 00071 /** Get the time passed in mili-seconds 00072 */ 00073 int read_ms(); 00074 00075 /** Get the time passed in micro-seconds 00076 */ 00077 int read_us(); 00078 00079 /** An operator shorthand for read() 00080 */ 00081 operator float(); 00082 00083 protected: 00084 int slicetime(); 00085 int _running; // whether the timer is running 00086 unsigned int _start; // the start time of the latest slice 00087 int _time; // any accumulated time from previous slices 00088 const ticker_data_t *_ticker_data; 00089 }; 00090 00091 } // namespace mbed 00092 00093 #endif
Generated on Tue Jul 12 2022 16:04:26 by 1.7.2