Arduino上的Wire.onReceive()函数
Wire.onReceive() function on Arduino
我正在尝试采用Arduino Wire库给出的此示例中的内容,并将其应用于我正在编写的程序。
这是我的代码。Comm.NDP[]
语句是未保存在此文件中的其他类实例,因此我相信您可以忽略它们。
**
* I2C.cpp handles sending of event messages
* between the LORA and MEGA via I2C protocol.
*/
#include "I2C.h"
#include "DATA.h"
#include "Globals.h"
#include <Wire.h>
#include <Arduino.h>
/**
* Constructor used to reference all other variables & functions.
*/
I2C::I2C() {
}
/**
* Assigns the proper address to the current micro controller.
*/
void I2C::initialize() {
//Sets the address for the current micro controller.
// Mega - 0
// LoRa - 1
Wire.begin(0);
Wire.setClock(8000000);
//Registers recieveEvent as a interrupt.
Wire.onReceive(receiveEvent); // register event
}
/**
* Receives byte over I2C Connection.
*/
static void receiveEvent(int howmany) {
//Iterator used below.
int i = 0;
for(i=0;i<120;i++) {
Comm.NDP[i] = ' ';
}
//Resets iterator.
i = 0;
//Checks to see if serial port is empty.
while (1 < Wire.available()) {
//Reads in single character from serial port.
char character = Wire.read();
NDP[i] = character;
i++;
}
Serial.println(Comm.NDP);
}
来自Arduino的Wire.h库的示例代码
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
我从Arduino IDE收到此错误。
错误:非静态成员函数的使用无效
Wire.onReceive(receiveEvent); // register event
^
退出状态 1非静态成员函数的使用无效
您缺少首次使用前的receiveEvent
声明。要么在begin
之前移动它的定义,要么在那里添加:
void receiveEvent(int howMany);
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- Arduino上的Wire.onReceive()函数