6 years, 4 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:

Library for LoRa communication using MultiTech MDOT. Lora, mdot, multitech

3 Answers

6 years, 4 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).

Accepted Answer
6 years, 4 months ago.


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!


6 years, 4 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

// turn off 1ms interrupt

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 Anthony Huy 25 Feb 2016

volatile uint32_t mscount = 0;
void onMillisecondTicker(void)

Will give you a counter that goes up once a millisecond that you can use in your code.

posted by Andy A 25 Feb 2016