Table of Contents

  1. Hello World!
  2. API

Mbed OS 2 and Mbed OS 5

This is the handbook for Mbed OS 2. If you’re working with Mbed OS 5, please see the Mbed OS 5 documentation. For the latest Timer API, please see 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!

Import program

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


API summary

Import library

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 milli-seconds.
int  read_us ()
  Get the time passed in micro-seconds.
  operator float ()
  An operator shorthand for read()
us_timestamp_t   read_high_resolution_us ()
  Get in a high resolution type the time passed in micro-seconds.

Private Member Functions

  MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
  NonCopyable copy constructor.
  MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
  NonCopyable copy assignment operator.


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.


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)