我正在为能量计数器创建一个库。我想通过 arduino 计算能量计数器产生的脉冲并将其转换为能量。为了捕捉任何脉冲,我想使用 arduino 中断。我希望它易于使用:
#include <EnergyCounter.h>
#define counterPin 2; // interrupt code 0 in Uno
EnergyCounter counter;
void setup(){
counter.begin(counterPin);
}
...
现在,在 begin 方法中,我想将中断附加到 counterPin:
void EnergyCounter::begin(byte pin){
pinMode(pin, INPUT);
attachInterrupt(0, countPulse, RISING);
}
void EnergyCounter::countPulse(){
display += increment_per_pulse;
}
当我尝试时,出现以下错误:
error: cannot convert 'EnergyCounter::countPulse' from type 'void (EnergyCounter::)()' to type 'void (*)()'
attachInterrupt(0, countPulse, RISING);
^
Erro ao compilar.
这是否意味着我不能将方法用作中断服务程序 (ISR)? 我怎样才能解决这个问题,同时拥有一个易于使用的库界面?
最佳答案
increment_per_pulse
需要是一个静态函数(见签名)。
因此,将其标记为static
:
static void increment_per_pulse();
关于c++ - 中断创建arduino库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627745/