Timer resolution in Mbed OS

Hi,
I noticed that the method to count the time duration “elapsed_time().count()” has the resolution of 1us and delay around 10us. Is there a way to have a more precise timer/counter?
In addition, how or where can I access the lower level library?
Thank you in advance!

Generally, to get better precision than microseconds, you will need to use a hardware timer on your microcontroller. What model of MCU are you using? I can try to point you to some resources.

Hi Jamie,
It’s LPC1768. Not sure if this is the correct link
Any example code is appreciated.

Oh now that’s a chip I have some experience with! Here’s a page that looks like a decent guide on how to configure TIM0 and TIM1: LPC1768: Timers - Tutorials . You can set them to run at a higher frequency than 1us, e.g. the processor’s core clock frequency (no prescaler).

Once you have configured a timer (say, TIM0), you can then use LPC_TIM0->TC to access the current count.

By default, Mbed OS uses TIM3 internally to implement the us ticker, leaving the other three timers available for your use.

2 Likes

Thanks for your tutorial link and swifty! I think that’s exactly what I’m looking for.
Since we talked about the hardware Timer, can you help me further if you know there’s any risk when I use external interrupts to start&stop the timer counting?

Risk? Of what?

A silly question from me: does the hardware timer stop if interrupt occurs?

No, that shouldn’t happen unless you specifically set the LPC_TIM0->MCR register to reset the timer when it hits one of the match registers.

Understood. Thanks for your great help.