Ticker
Ticker 类层次结构
使用 Ticker 界面设置重复中断;它以指定的速率重复调用函数。
您可以创建任意数量的 Ticker 对象,同时允许多个未完成的中断。该函数可以是静态函数,特定对象的成员函数或 Callback 对象。
警告和说明
-
ISR 中没有阻塞代码:避免任何调用等待,无限循环或阻塞调用。
-
在 ISR 中没有 printf,malloc 或 new:避免调用庞大的库函数。特别是,某些库函数(例如 printf,malloc 和 new)不可重入,并且当从 ISR 调用时它们的行为可能会被破坏。
-
当事件附加到 Ticker 时,会阻止深度睡眠以保持准确的计时。如果您不需要微秒精度,请考虑使用 LowPowerTicker 类,因为这不会阻止深度睡眠模式。
Ticker 类参考
公共成员函数 | |
Ticker (const ticker_data_t *data) | |
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 () |
公共成员函数继承自 mbed::TimerEvent | |
TimerEvent (const ticker_data_t *data) | |
virtual | ~TimerEvent () |
受保护的成员函数 | |
void | setup (us_timestamp_t t) |
virtual void | handler () |
受保护的成员函数继承自 mbed::TimerEvent | |
void | insert (timestamp_t timestamp) |
void | insert_absolute (us_timestamp_t timestamp) |
void | remove () |
受保护的属性 | |
us_timestamp_t | _delay |
Callback< void()> | _function |
bool | _lock_deepsleep |
受保护的属性继承自 mbed::TimerEvent | |
ticker_event_t | event |
const ticker_data_t * | _ticker_data |
其他继承成员 | |
静态公共成员函数继承自 mbed::TimerEvent | |
static void | irq (uint32_t id) |
Ticker hello, world
尝试此程序设置 Ticker 以重复反转 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);
}
}
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);
}
}