Arduino functions millis, micros and eventually some more to come
Dependents: DigitalCamera_OV5642_WIZwiki-W7500 BMC F746NG_TestAll Prelude_OV5642_dev
Arduino basic compatibility millis(), micros(), evtl. more to come.
micros() and millis() are uint32_t and have a correct overflow at 2^32-1. micros() has a granularity/resolution of 50. This can be changed by a define. 1 microsecond is not realistic because every count needs an irq. A resolution of 50 microseconds means 20KHz irq. That should not take away more than 1 percent overall processing time.
millis.cpp@1:d1b960698e70, 2015-03-26 (annotated)
- Committer:
- eduardoG26
- Date:
- Thu Mar 26 14:21:36 2015 +0000
- Revision:
- 1:d1b960698e70
- Parent:
- 0:571ea8a1bbae
20150326
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eduardoG26 | 1:d1b960698e70 | 1 | /* |
eduardoG26 | 1:d1b960698e70 | 2 | The MIT License (MIT) |
eduardoG26 | 1:d1b960698e70 | 3 | |
eduardoG26 | 1:d1b960698e70 | 4 | Copyright (c) 2014 calima engineering |
eduardoG26 | 1:d1b960698e70 | 5 | |
eduardoG26 | 1:d1b960698e70 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
eduardoG26 | 1:d1b960698e70 | 7 | of this software and associated documentation files (the "Software"), to deal |
eduardoG26 | 1:d1b960698e70 | 8 | in the Software without restriction, including without limitation the rights |
eduardoG26 | 1:d1b960698e70 | 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
eduardoG26 | 1:d1b960698e70 | 10 | copies of the Software, and to permit persons to whom the Software is |
eduardoG26 | 1:d1b960698e70 | 11 | furnished to do so, subject to the following conditions: |
eduardoG26 | 1:d1b960698e70 | 12 | |
eduardoG26 | 1:d1b960698e70 | 13 | The above copyright notice and this permission notice shall be included in |
eduardoG26 | 1:d1b960698e70 | 14 | all copies or substantial portions of the Software. |
eduardoG26 | 1:d1b960698e70 | 15 | |
eduardoG26 | 1:d1b960698e70 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
eduardoG26 | 1:d1b960698e70 | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
eduardoG26 | 1:d1b960698e70 | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
eduardoG26 | 1:d1b960698e70 | 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
eduardoG26 | 1:d1b960698e70 | 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
eduardoG26 | 1:d1b960698e70 | 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
eduardoG26 | 1:d1b960698e70 | 22 | THE SOFTWARE. |
eduardoG26 | 1:d1b960698e70 | 23 | */ |
eduardoG26 | 0:571ea8a1bbae | 24 | // millis as in Arduino... |
eduardoG26 | 0:571ea8a1bbae | 25 | |
eduardoG26 | 0:571ea8a1bbae | 26 | #include "mbed.h" |
eduardoG26 | 0:571ea8a1bbae | 27 | #include "millis.h" |
eduardoG26 | 0:571ea8a1bbae | 28 | |
eduardoG26 | 1:d1b960698e70 | 29 | #ifdef DEBUG_MILLIS |
eduardoG26 | 1:d1b960698e70 | 30 | #if DEBUG_MILLIS |
eduardoG26 | 1:d1b960698e70 | 31 | DigitalOut DebugMillisPin(D2); |
eduardoG26 | 1:d1b960698e70 | 32 | #endif |
eduardoG26 | 1:d1b960698e70 | 33 | #endif |
eduardoG26 | 1:d1b960698e70 | 34 | |
eduardoG26 | 0:571ea8a1bbae | 35 | static volatile uint32_t MilliSeconds_u32; |
eduardoG26 | 0:571ea8a1bbae | 36 | |
eduardoG26 | 0:571ea8a1bbae | 37 | static Ticker MilliSecondsTicker; |
eduardoG26 | 0:571ea8a1bbae | 38 | |
eduardoG26 | 0:571ea8a1bbae | 39 | static void MilliSecondsTickerIRQ() |
eduardoG26 | 0:571ea8a1bbae | 40 | { |
eduardoG26 | 1:d1b960698e70 | 41 | #ifdef DEBUG_MILLIS |
eduardoG26 | 1:d1b960698e70 | 42 | #if DEBUG_MILLIS |
eduardoG26 | 1:d1b960698e70 | 43 | DebugMillisPin = not DebugMillisPin; |
eduardoG26 | 1:d1b960698e70 | 44 | #endif |
eduardoG26 | 1:d1b960698e70 | 45 | #endif |
eduardoG26 | 1:d1b960698e70 | 46 | MilliSeconds_u32++; |
eduardoG26 | 0:571ea8a1bbae | 47 | } |
eduardoG26 | 0:571ea8a1bbae | 48 | |
eduardoG26 | 0:571ea8a1bbae | 49 | uint32_t millis () |
eduardoG26 | 0:571ea8a1bbae | 50 | { |
eduardoG26 | 0:571ea8a1bbae | 51 | return MilliSeconds_u32; |
eduardoG26 | 0:571ea8a1bbae | 52 | } |
eduardoG26 | 0:571ea8a1bbae | 53 | |
eduardoG26 | 0:571ea8a1bbae | 54 | void StartMillis () |
eduardoG26 | 0:571ea8a1bbae | 55 | { |
eduardoG26 | 0:571ea8a1bbae | 56 | MilliSecondsTicker.attach_us (&MilliSecondsTickerIRQ, 1000); |
eduardoG26 | 0:571ea8a1bbae | 57 | } |
eduardoG26 | 0:571ea8a1bbae | 58 | |
eduardoG26 | 0:571ea8a1bbae | 59 | // End of file |