首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino上的Wire.onReceive()函数

Arduino上的Wire.onReceive()函数
EN

Stack Overflow用户
提问于 2017-10-13 21:12:17
回答 1查看 7.4K关注 0票数 0

我正在尝试以Arduino Wire库中的例子为例,并将其应用到我正在编写的程序中。

这是我的密码。Comm.NDP[]语句是未保存在该文件中的其他类实例,因此我相信您可以忽略它们。

代码语言:javascript
复制
**
* 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库中的示例代码

代码语言:javascript
复制
#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得到了这个错误。

错误:无效使用非静态成员函数。

代码语言:javascript
复制
Wire.onReceive(receiveEvent); // register event

                          ^

退出状态1非静态成员函数的无效使用

EN

回答 1

Stack Overflow用户

发布于 2017-10-13 21:34:15

在第一次使用之前,您缺少了receiveEvent声明。要么在begin之前移动它的定义,要么在那里添加:

代码语言:javascript
复制
void receiveEvent(int howMany);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46738101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档