Ticker

Use the Ticker interface to set up a recurring interrupt; it calls a function repeatedly and at a specified rate.

You can create any number of Ticker objects, allowing multiple outstanding interrupts at the same time. The function can be a static function, or a member function of a particular object.

API

Public Member Functions

void attach (Callback< void()> func, float t)
template<typename T , typename M >
void attach (T *obj, M method, float t)
void attach_us (Callback< void()> func, us_timestamp_t t)
template<typename T , typename M >
void attach_us (T *obj, M method, us_timestamp_t t)
void detach ()
- Public Member Functions inherited from mbed::TimerEvent
virtual ~TimerEvent ()

Protected Attributes

us_timestamp_t _delay
Callback< void()> _function

Additional Inherited Members

- Static Public Member Functions inherited from mbed::TimerEvent
static void irq (uint32_t id)

Hello World!

Try this program to set up a Ticker to repeatedly invert an LED:

 


/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
 
Ticker flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
void flip() {
    led2 = !led2;
}
 
int main() {
    led2 = 1;
    flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
 
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}

Warnings and notes

  • Timers are based on 32-bit int microsecond counters, so they can only time up to a maximum of 2^31-1 microseconds (30 minutes). They are designed for times between microseconds and seconds. For longer times, you should consider the time() real time clock.

  • No blocking code in ISR: avoid any call to wait, infinite while loop or blocking calls in general.

  • No printf, malloc or new in ISR: avoid any call to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR.

  • RTOS Timer: Consider using the mbed RTOS Timer instead of a Ticker. In this way your periodic function will not be executed in an ISR, giving you more freedom and safety in your code.

Examples

Use this example to attach a member function to a ticker:

 


#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);
Ticker t;
 
int main() {
    // the address of the object, member function, and interval
    t.attach(callback(&f, &Flipper::flip), 2.0); 
 
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        led1 = !led1;
        wait(0.2);
    }
}