Timer

Table of Contents

  1. Hello World!
  2. API

The Timer interface is used to create, start, stop and read a timer for measuring small times (between microseconds and seconds).

Any number of Timer objects can be created, and can be started and stopped independently

Hello World!

» Import this program

#include "mbed.h"
 
Timer t;
 
int main() {
    t.start();
    printf("Hello World!\n");
    t.stop();
    printf("The time taken was %f seconds\n", t.read());
}

API

API summary

» Import latest build into a program

Public Member Functions

void  start ()
  Start the timer.
void  stop ()
  Stop the timer.
void  reset ()
  Reset the timer to 0.
float  read ()
  Get the time passed in seconds.
int  read_ms ()
  Get the time passed in mili-seconds.
int  read_us ()
  Get the time passed in micro-seconds.

Warning

Note that timers are based on 32-bit int microsecond counters, so can only time up to a maximum of 2^31-1 microseconds i.e. 30 minutes. They are designed for times between microseconds and seconds. For longer times, you should consider the time()/Real time clock.

Implementation

The timer used to implement this functionality is:

  • On the LPC1768: Timer 3 (LPC_TIM3)
  • On the LPC11U24: 32-bit Counter/Timer B1 (LPC_CT32B1)



2 related questions:


11 comments:

23 Oct 2010

Can a future API please add a timer overflow callback? Thanks.

07 Nov 2010

I second the request for a timer overflow interrupt/callback. With that a real-time counter can be created.

24 Mar 2011

Yup, that would be nice.

11 May 2011

If you wanted a Timer overflow callback, wouldn't it not be hard to set up a Timeout function call that would trigger before the timer overflow?

28 Nov 2011

Using Timer usually doesn't impact the main programs. I'm using Timer to flash the 4 LEDS for 50 ms one after the other. Not perfect, close enough. At much slower time of 500 ms, the 4 LEDS do not flash in succession, more like random. Can someone explain Timer more fully and it's limits? Thanks, donde

15 Dec 2011

does any one know where to see the result of the timer code?.

09 Jan 2012

Well, the newest revisions of the mbed library doesn't work with timer. I have some software reading the timer output after it's been stopped, but it always returns 0... Tried to revert to 27, but this doesn't change anything.

04 Mar 2012

I also have problems with the timer lately

12 May 2012

user Christian Lerche wrote:

Well, the newest revisions of the mbed library doesn't work with timer. I have some software reading the timer output after it's been stopped, but it always returns 0... Tried to revert to 27, but this doesn't change anything.

It works, you just have to pay attention to the return type. The hello world above works because the return type of read() is float; if you want to use read_ms() or read_us(), you need to change the specifier in printf() from "f" to "d" because they both return integer type.

28 Jun 2012

Hi all.

I need a interface like "timer" but with nanosecond resolution (hundreds of nanoseconds). Where I can find information about LPC1768 timer register and interruption to create the interface?

thanks in advance for helps

Dario

29 Nov 2012

hi I want to design a controller to lower the speed of a motor if it exceeds a preset value. that means I have to know the know the current speed and compare with the set value. I tried to use an interruptin to count the number of pulses from a shaft encoder attached to the dc motor. but it wasn't working. can anyone help me with the cide that count the number of pulses in a second; hence determine the speed of the motor? .Thanks

Posting new comments for this page has been disabled