Arduino作为I2C从机与RPi对话

问题描述 投票:2回答:1

我使用Arduino作为I2C从器件来处理一些超声波传感器并将重要数据发送到Raspberry。在Arduino上运行的代码:

void setup() {
  // initialize i2c as slave
  Serial.begin(9600);
  Wire.begin(SLAVE_ADDRESS);
  // define callbacks for i2c communication
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);

  // useless
}

void loop() {
    // useless
} 

// callback for received data
void receiveData(int byteCount) {
  int i = 0;
  while (Wire.available()) {
    number[i] = (char) Wire.read();
    i++;
  }
  number[i] = '\0';
  Serial.println(atoi(number));
  if(atoi(number) != 0) { caseN = atoi(number); }
}  

int SonarSensor(int trigPin,int echoPin)
{
    // uselesss
}

// callback for sending data
void sendData() {
  if(caseN == 1) { Wire.write(distance1);}
  else if(caseN == 2) { Wire.write(distance2);}
  else if(caseN == 3) { Wire.write(distance3);}
  else if(caseN == 4) { Wire.write(distance4);}
  else if(caseN == 5)
  {
    if(state == 0)
    {
      state = 1;
      digitalWrite(ledPin, HIGH);
    }
    else
    {
      state = 0;
      digitalWrite(ledPin, LOW);
    }
  }
  else { Wire.write(0); }
}

我用Python做了第一个版本的“聊天”总线,效果非常好:

import smbus
import time

bus = smbus.SMBus(1)


address = 0x04

def writeNumber(value):
    bus.write_byte(address, value)
    return -1

def readNumber():
    number = bus.read_byte_data(address, 1)
    return number

while True:
    data = raw_input("Enter the data to be sent : ")
    data_list = list(data)
    for i in data_list:
        writeNumber(int(ord(i)))
        time.sleep(.1)

    writeNumber(int(0x0A))

我试图在C中做同样的事情,但看起来有点困难:

#include "i2c-dev.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <string.h>

int main() {
    const int adapter_nr = 1;
    char filename[20];
    snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
    const int file = open(filename, O_RDWR);
    if (file < 0) {
        printf("Unable to connect to Atmega, I2C plugged ? DC ok ?");
        exit(EXIT_FAILURE);
    }

    // Atmega address
    const int addr = 0x04;

    if(ioctl(file, I2C_SLAVE, addr) < 0) 
    {
        printf("Fail to reach Atmega");
        exit(EXIT_FAILURE);
    }

    const __u8 add = 5; // Ask to "distance 5"
    i2c_smbus_write_byte_data(file, 0x04, add); // What is the adress ?


    const __u8 reg = 0x0A;
    const __s32 result = i2c_smbus_read_byte_data(file, reg);
    if(result < 0) 
    {
        printf("Fail to reach Atmega reg");
        exit(EXIT_FAILURE);
    } 
    else 
    {
        printf("Distance %d cm \n", result);
    }

    close(file);

    return(EXIT_SUCCESS);
}

正如我在代码中提到的,我不知道我的Arduino奴隶有哪个寄存器地址。我可以在Arduino COM终端看到很多0而且只有0。

我希望你能理解我的问题。谢谢。

c arduino raspberry-pi i2c
1个回答
0
投票

为什么不定义从地址,以便您确切知道它是什么。此外,如果您将来使用超过1个arduino,这将允许您设置不同的地址。

在arduino上安装之前运行这行代码。只要在I2C连接上没有被其他系统使用,您就可以将地址更改为您想要的任何地址。

#define  SLAVE_ADDRESS           0x29  //slave address,any number from 0x01 to 0x7F

这是一个Tutorial讨论使用Arduino作为奴隶


0
投票

您需要知道Arduino正在使用的正确I2C从设备地址。

幸运的是,Raspberry Pi可以使用以下命令检测连接的任何I2C设备并显示其地址:

sudo i2cdetect -y 1

要么

sudo i2cdetect -y 0

注意:第一个命令适用于所有最新的Raspberry Pi3和Pi2(型号A,B,B +)和Pi Zero。第二个命令仅在您使用旧型号时才有效。

有关更多信息,您可以查看here(启用I2C)或here(配置I2C)。两者都相似,你可以跳到底部,解释如何使用上面的命令。

© www.soinside.com 2019 - 2024. All rights reserved.