Recent changes
SerialPC
Creating a program
Downloading a program
Setup guide
Exporting to Code Red
Exporting to uVision
Order
tag order
From the mbed microcontroller Handbook.  

Timer

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!

Simple timer example

#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

TimerA general purpose timer
Functions
startStart the timer
stopStop the timer
resetReset the timer to 0.
readGet the time passed in seconds
read_msGet the time passed in mili-seconds
read_usGet the time passed in micro-seconds
class Timer : public Base
A general purpose timer
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.




calendar Page history
Last modified 16 Oct 2010, by   user Simon Ford   tag No tags | 7 comments      

7 comments on Timer:

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?.

3 weeks, 4 days ago

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.

Please login to post comments.