0

我编写了以下基于 MBED 的 C++ 程序,作为我正在为我的 Nucleoboard 微控制器开发的更详细项目的实验:

#include "mbed.h"

DigitalOut greenLed(PA_5);

#include "mbed.h"
class TimedLED
{
    public:
        TimedLED()
        {
            Ticker t;
            t.attach_us(this, &TimedLED::flip, 1000000);
        }

        void flip(void)
        {
           static int count = 0;
           greenLed.write(count%2); //-- toggle greenLed
           count++;
        }
};

int main()
{
    TimedLED flash;
    while (1);
}

我查看的所有参考资料似乎都表明 t.attach_us(this, &TimedLED::flip, 1000000) 应该调用该方法,每秒“翻转”一次,从而导致 LED 开关打开和关闭。然而,这并没有发生。我看不出问题出在哪里。我希望有人能帮我解决这个问题。

我收到以下警告消息,表明此格式已被弃用,但文档链接已损坏,因此我无法获得更多详细信息:

Function "mbed::Ticker::attach_us(T *, M, us_timestamp_t) [with T=TimedLED, M=void(TimedLED::*)()]" (declared at /extras/mbed_fd96258d940d/drivers/Ticker.h:122) was declared "deprecated" "t.attach_us(this, &TimedLED::flip, 1000000);"

即使它已被弃用,它仍然应该工作,不是吗?此外,大概如果弃用消息是正确的,那么有一种更新的方法可以做同样的事情。尽管在任何地方,我都找不到对替代方法的参考。

4

1 回答 1

2

您在堆栈上的构造函数中声明Ticker t;,当构造函数退出时,它将清除对象,因此代码将不会运行。

在你的类中声明变量,它将按预期运行:

class TimedLED
{
    public:
        TimedLED()
        {
            t.attach(callback(this, &TimedLED::flip), 1.0f);
        }

        void flip(void)
        {
            static int count = 0;
            greenLed.write(count%2); //-- toggle greenLed
            count++;
        }

    private:
        Ticker t;
};

另请注意构造函数中的更改,这是在 mbed OS 5 中附加回调的首选(非弃用)方式。

于 2017-09-21T07:51:37.127 回答