Recent changes
Order
tag order
RTOS
Help
mbed NXP LPC1768
Firmware
Homepage
From the mbed microcontroller Handbook.  

Timeout

The Timeout interface is used to setup an interrupt to call a function after a specified delay.

Any number of Timeout objects can be created, allowing multiple outstanding interrupts at the same time.

Hello World!

A simple program to setup a Timeout to invert an LED after a given timeout...

Code

#include "mbed.h"

Timeout flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);

void flip() {
    led2 = !led2;
}

int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds

    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}

API

API summary

TimeoutA Timeout is used to call a function at a point in the future
Functions
attachAttach a function to be called by the Timeout, specifiying the delay in seconds
attachAttach a member function to be called by the Timeout, specifiying the delay in seconds
attach_usAttach a function to be called by the Timeout, specifiying the delay in micro-seconds
attach_usAttach a member function to be called by the Timeout, specifiying the delay in micro-seconds
detachDetach the function
class Timeout : public Ticker
A Timeout is used to call a function at a point in the future
void attach(void (*fptr)(void),
float t)
Attach a function to be called by the Timeout, specifiying the delay in seconds
void attach_us(void (*fptr)(void),
unsigned int t)
Attach a function to be called by the Timeout, specifiying the delay in micro-seconds
void detach()
Detach the function

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.

Examples

Attaching a member function

Code

#include "mbed.h"

// A class for flip()-ing a DigitalOut 
class Flipper {
public:
    Flipper(PinName pin) : _pin(pin) {
        _pin = 0;
    }
    void flip() {
        _pin = !_pin;
    }
private:
    DigitalOut _pin;
};

DigitalOut led1(LED1);
Flipper f(LED2);
Timeout t;

int main() {
    t.attach(&f, &Flipper::flip, 2.0); // the address of the object, member function, and interval

    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}



calendar Page history
Last modified 04 Mar 2012, by   user Simon Ford   tag No tags | 10 comments  

10 comments on Timeout:

17 Oct 2010

That Demo code is not working on my mbed board. The Led 2 is not flashing every 2 seconds as it should do.

17 Oct 2010

my fault, confusion about timeout and ticker.

29 Nov 2010

Is there an example of using the overloaded method with the: template<typename T> void attach(T * tptr, void (T::*mptr)(void),float t). I am trying to call Timeout inside of a class method but can't figure out the syntax. Here is what I am trying to do: _timeout.attach_us(myClass, myClass::&attimeout, delay); Timeout _timeout is property of the class and attimeout is the method of the same class

29 Nov 2010

Hi Istvan,

Assuming you are trying to attach this in a method within a class, to the instance of itself, try:

Code

_timeout.attach_us(this, &myClass::attimeout, delay);

The format is basically (address of object, address of function within object, delay);

30 Nov 2010

thanks, works perfect

13 Apr 2011

I'm posting this to warn anyone from making the same silly mistake I did. Be careful to check the scope on your Timeout object. If you declare your Timeout object within another function it's possible that the function will resolve and the timeout object will be deleted before your timeout completes.

For instance in this case your blink function will never be called.

Code

if (1){    
        Timeout myTimeout;           //we should declare this object outside of the if statement.
        myTimeout.attach(&blink,2);  //The function blink would be called in two seconds but myTimeout is deleted before then

    }
23 Sep 2011

Hello;

  1. include "mbed.h"

Timeout flipper; DigitalOut led1(LED1); DigitalOut led2(LED2);

void flip() { led2 = !led2; }

int main() { led2 = 1; flipper.attach(&flip, 2.0); setup flipper to call flip after 2 seconds

spin in a main loop. flipper will interrupt it to call flip while(1) { led1 = !led1; wait(0.2); } }

I compiled this code but it didn't work for me. I want to make interruptions to execute functions.

THANKS.

23 Sep 2011

Hello; I had a confusion between Ticker and timeout. Now Every thing works fine for me.

:-)

26 Nov 2011

This page should note that the mbed compiler optimises global variables aggressively. So if you use a global variable to communicate between base code and Timeout interrupt it must be marked volatile:

Code

volatile int running = 1;

led1 = DigitalOut(LED1);

void isr_code(void)
{
    running = 0;
}

int main(void)
{
   Timeout.attach(&isr_code, 10.0);
   led1 = 1
   while (running);
   led1 = 0;
   while (1) {
      wait(1);
      led1 = 1 - led1;
   }
}

in this example if running is not volatile the first loop will run forever!

07 Mar 2012

Hello,

a short question:

Is it Ok to call attach() a second/third... time before the timeout has fired or must I call detach() before and attach again?

Michael

Please login to post comments.