Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 10 months ago.
millisec counter
Is there a good way to get a millisecond counter or clock tick? mbed itself does not look like it has one by default. Does the mdot library have one? Thanks.
Question relating to:
3 Answers
8 years, 10 months ago.
You can create a Timer object and read that out. Internally they use 32-bit timers with microsecond resolution. You can also read it with ms function, but do remember that if you read it with us function it will gracefully roll over (nicely when it is done counting till 2^32), while with ms functions it gives an error if you want a continious count.
If you really want to directly access the counter, use us_ticker_read(). (Not 100% sure if this also initializes the timer, I think so, but not sure).
8 years, 10 months ago.
Anthony,
The mbed library does run a timer as a tick. I believe it fires every ms. This is how the wait() functions work as well as the Ticker and Timeout objects. I'm not sure if the IRQ for that tick is exposed, and making changes to it might break other functionality anyway. The Timer object gives you at least ms granularity and you can run as many of them as you need. I'd suggest using that.
The mDot library doesn't implement any timers or ticks because it is internally using what's provided in the mbed library.
Hope this helps!
-Mike
8 years, 10 months ago.
The mbed library runs a 1us timer in the background which is why all of the time functions have a resolution of 1us. If you want an event every 1ms then you can use a Ticker object:
#include "mbed.h" Ticker ms_tick; void onMillisecondTicker(void) { // this code will run every millisecond } main () { // turn on 1ms interrupt ms_tick.attach_us(onMillisecondTicker,1000); // turn off 1ms interrupt ms_tick.detach(); }
Thanks But is there a way to get the running millisecond count or running microsecond count, even if it rolls over? Doing this type of counter with an event quickly drifts in my experience so I was hoping there was an ongoing counter that I could access. Does something like that exists?
posted by 25 Feb 2016