mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbotkinl 0:2cc6bb4d7fea 1 /* mbed Microcontroller Library
mbotkinl 0:2cc6bb4d7fea 2 * Copyright (c) 2006-2013 ARM Limited
mbotkinl 0:2cc6bb4d7fea 3 *
mbotkinl 0:2cc6bb4d7fea 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbotkinl 0:2cc6bb4d7fea 5 * you may not use this file except in compliance with the License.
mbotkinl 0:2cc6bb4d7fea 6 * You may obtain a copy of the License at
mbotkinl 0:2cc6bb4d7fea 7 *
mbotkinl 0:2cc6bb4d7fea 8 * http://www.apache.org/licenses/LICENSE-2.0
mbotkinl 0:2cc6bb4d7fea 9 *
mbotkinl 0:2cc6bb4d7fea 10 * Unless required by applicable law or agreed to in writing, software
mbotkinl 0:2cc6bb4d7fea 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbotkinl 0:2cc6bb4d7fea 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbotkinl 0:2cc6bb4d7fea 13 * See the License for the specific language governing permissions and
mbotkinl 0:2cc6bb4d7fea 14 * limitations under the License.
mbotkinl 0:2cc6bb4d7fea 15 */
mbotkinl 0:2cc6bb4d7fea 16 #ifndef MBED_TIMER_H
mbotkinl 0:2cc6bb4d7fea 17 #define MBED_TIMER_H
mbotkinl 0:2cc6bb4d7fea 18
mbotkinl 0:2cc6bb4d7fea 19 #include "platform.h"
mbotkinl 0:2cc6bb4d7fea 20
mbotkinl 0:2cc6bb4d7fea 21 namespace mbed {
mbotkinl 0:2cc6bb4d7fea 22
mbotkinl 0:2cc6bb4d7fea 23 /** A general purpose timer
mbotkinl 0:2cc6bb4d7fea 24 *
mbotkinl 0:2cc6bb4d7fea 25 * Example:
mbotkinl 0:2cc6bb4d7fea 26 * @code
mbotkinl 0:2cc6bb4d7fea 27 * // Count the time to toggle a LED
mbotkinl 0:2cc6bb4d7fea 28 *
mbotkinl 0:2cc6bb4d7fea 29 * #include "mbed.h"
mbotkinl 0:2cc6bb4d7fea 30 *
mbotkinl 0:2cc6bb4d7fea 31 * Timer timer;
mbotkinl 0:2cc6bb4d7fea 32 * DigitalOut led(LED1);
mbotkinl 0:2cc6bb4d7fea 33 * int begin, end;
mbotkinl 0:2cc6bb4d7fea 34 *
mbotkinl 0:2cc6bb4d7fea 35 * int main() {
mbotkinl 0:2cc6bb4d7fea 36 * timer.start();
mbotkinl 0:2cc6bb4d7fea 37 * begin = timer.read_us();
mbotkinl 0:2cc6bb4d7fea 38 * led = !led;
mbotkinl 0:2cc6bb4d7fea 39 * end = timer.read_us();
mbotkinl 0:2cc6bb4d7fea 40 * printf("Toggle the led takes %d us", end - begin);
mbotkinl 0:2cc6bb4d7fea 41 * }
mbotkinl 0:2cc6bb4d7fea 42 * @endcode
mbotkinl 0:2cc6bb4d7fea 43 */
mbotkinl 0:2cc6bb4d7fea 44 class Timer {
mbotkinl 0:2cc6bb4d7fea 45
mbotkinl 0:2cc6bb4d7fea 46 public:
mbotkinl 0:2cc6bb4d7fea 47 Timer();
mbotkinl 0:2cc6bb4d7fea 48
mbotkinl 0:2cc6bb4d7fea 49 /** Start the timer
mbotkinl 0:2cc6bb4d7fea 50 */
mbotkinl 0:2cc6bb4d7fea 51 void start();
mbotkinl 0:2cc6bb4d7fea 52
mbotkinl 0:2cc6bb4d7fea 53 /** Stop the timer
mbotkinl 0:2cc6bb4d7fea 54 */
mbotkinl 0:2cc6bb4d7fea 55 void stop();
mbotkinl 0:2cc6bb4d7fea 56
mbotkinl 0:2cc6bb4d7fea 57 /** Reset the timer to 0.
mbotkinl 0:2cc6bb4d7fea 58 *
mbotkinl 0:2cc6bb4d7fea 59 * If it was already counting, it will continue
mbotkinl 0:2cc6bb4d7fea 60 */
mbotkinl 0:2cc6bb4d7fea 61 void reset();
mbotkinl 0:2cc6bb4d7fea 62
mbotkinl 0:2cc6bb4d7fea 63 /** Get the time passed in seconds
mbotkinl 0:2cc6bb4d7fea 64 */
mbotkinl 0:2cc6bb4d7fea 65 float read();
mbotkinl 0:2cc6bb4d7fea 66
mbotkinl 0:2cc6bb4d7fea 67 /** Get the time passed in mili-seconds
mbotkinl 0:2cc6bb4d7fea 68 */
mbotkinl 0:2cc6bb4d7fea 69 int read_ms();
mbotkinl 0:2cc6bb4d7fea 70
mbotkinl 0:2cc6bb4d7fea 71 /** Get the time passed in micro-seconds
mbotkinl 0:2cc6bb4d7fea 72 */
mbotkinl 0:2cc6bb4d7fea 73 int read_us();
mbotkinl 0:2cc6bb4d7fea 74
mbotkinl 0:2cc6bb4d7fea 75 #ifdef MBED_OPERATORS
mbotkinl 0:2cc6bb4d7fea 76 operator float();
mbotkinl 0:2cc6bb4d7fea 77 #endif
mbotkinl 0:2cc6bb4d7fea 78
mbotkinl 0:2cc6bb4d7fea 79 protected:
mbotkinl 0:2cc6bb4d7fea 80 int slicetime();
mbotkinl 0:2cc6bb4d7fea 81 int _running; // whether the timer is running
mbotkinl 0:2cc6bb4d7fea 82 unsigned int _start; // the start time of the latest slice
mbotkinl 0:2cc6bb4d7fea 83 int _time; // any accumulated time from previous slices
mbotkinl 0:2cc6bb4d7fea 84 };
mbotkinl 0:2cc6bb4d7fea 85
mbotkinl 0:2cc6bb4d7fea 86 } // namespace mbed
mbotkinl 0:2cc6bb4d7fea 87
mbotkinl 0:2cc6bb4d7fea 88 #endif