Mbed OS 文档翻译 之 参考(API(驱动(InterruptIn)))

InterruptIn

                                                                      

                                                                            InterruptIn 类层次结构

使用 InterruptIn 接口在数字输入引脚发生变化时触发事件。您可以在信号的上升沿(从 0 变为 1)或下降沿(从 1 变为 0)触发中断。

InterruptIn 类参考

mbed::InterruptIn 类参考

公共成员函数
 InterruptIn (PinName pin)
 InterruptIn (PinName pin, PinMode mode)
int read ()
 operator int ()
void rise (Callback< void()> func)
template<typename T , typename M >
void rise (T *obj, M method)
void fall (Callback< void()> func)
template<typename T , typename M >
void fall (T *obj, M method)
void mode (PinMode pull)
void enable_irq ()
void disable_irq ()
静态公共成员函数
static void _irq_handler (uint32_t id, gpio_irq_event event)
受保护的成员函数
void irq_init (PinName pin)
受保护的属性
gpio_t gpio
gpio_irq_t gpio_irq
Callback< void()> _rise
Callback< void()> _fall

警告:

  • ISR 中没有阻塞代码:避免任何调用等待,无限循环或阻塞调用。

  • 在 ISR 中没有 printf,malloc 或 new:避免调用庞大的库函数。特别是,某些库函数(例如 printf,malloc 和 new)是不可重入的,并且当从 ISR 调用时它们的行为可能会被破坏。

  • 对于来自中断上下文的 printfs,请改用 Event

要阅读输入,请参阅 DigitalIn

InterruptIn hello, world

main.cpp                                                                                                                                                导入到 Mbed IDE

/* 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"
 
InterruptIn button(SW2);
DigitalOut led(LED1);
DigitalOut flash(LED4);
 
void flip() {
    led = !led;
}
 
int main() {
    button.rise(&flip);  // attach the address of the flip function to the rising edge
    while(1) {           // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);
    }
}

InterruptIn 示例

尝试以下示例来计算引脚上的上升沿。

main.cpp                                                                                                                                                导入到 Mbed IDE

#include "mbed.h"

class Counter {
public:
    Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
        _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
    }

    void increment() {
        _count++;
    }

    int read() {
        return _count;
    }

private:
    InterruptIn _interrupt;
    volatile int _count;
};

Counter counter(SW2);

int main() {
    while(1) {
        printf("Count so far: %d\n", counter.read());
        wait(2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值