带有 Arduino Uno 的 Attiny85 用于 I2c 通信

Attiny85 with ArduinoUno for I2c comm

我正在为 I2C 通信开发 attiny85。我已经浏览过不同的库,例如 Wire.hTinyWire.htinyWireM.htinyWireS.h

一开始我想通过 I2C 通信发送一些字节的数据,并尝试用示波器测量引脚的范围,但没有给我适当的结果。在互联网上查看使attiny85与I2c一起工作的不同方法真的很无情,我无法完成任务。最后,我尝试将 attiny85 作为主设备,将 arduino Uno 作为从设备,因为它在我的盒子里是备用的。

我试图将 attiny85 设置为 master 并将数据发送到 arduino,并在串行监视器上查看输出,但它显示为零。

作为参考,主从代码已附上,我的任务只是简单地检查串口。

Attiny85 as Master

#include <TinyWireM.h>

void setup()
{
  TinyWireM.begin();
}
void loop()
{
  TinyWireM.begin();
  TinyWireM.beginTransmission(0x08);
  TinyWireM.send(0x99);  
  int Byte1 = TinyWireM.endTransmission();
  delay(1000);
}

Arduino 作为从机

#include <Wire.h>
const byte add = 0x08;
int byte1;
void setup()
{
  Wire.begin(add);               
  Wire.onReceive(receiveEvent); 
  Serial.begin(9600);         
}

void loop()
{
  Serial.println ("Data receiving");
  Serial.println(byte1);
  delay(1000);
}

void receiveEvent(int bytes)
{
  byte1 = Wire.read(); 
}

但我无法在 arduino 的串行监视器上获得输出。 我在这里做错了什么?

一段时间前,我使用 TinyWireS lib (https://github.com/nadavmatalon/TinyWireS) 将 Atiny85 作为从机使用,并且工作正常。 以下是引脚配置

带有 Arduino Uno A4 的 ATtiny85 引脚 5 和 带 Arduino Uno A5 的 ATtiny85 引脚 7

下面是我的代码

阿蒂尼。

#include "TinyWireS.h"                      

 const byte SLAVE_ADDR = 100;
 const byte NUM_BYTES = 4;

 volatile byte data = { 0, 1, 2, 3 };

 void setup() {
    TinyWireS.begin(SLAVE_ADDR);
    TinyWireS.onRequest(requestISR);
}

 void loop() {}

 void requestISR() {
    for (byte i=0; i<NUM_BYTES; i++) {
        TinyWireS.write(data[i]);
        data[i] += 1;
    }
}

欧诺

#include <Wire.h>

 const byte SLAVE_ADDR = 100;

 const byte NUM_BYTES = 4;

 byte data[NUM_BYTES] = { 0 };

 byte bytesReceived = 0;

 unsigned long timeNow = millis();

 void setup() {
    Serial.begin(9600);
    Wire.begin();
    Serial.print(F("\n\nSerial is Open\n\n"));
}

 void loop() {
    if (millis() - timeNow >= 750) {                                        // trigger every 750mS
        Wire.requestFrom(SLAVE_ADDR, NUM_BYTES);                            // request bytes from slave
        bytesReceived = Wire.available();                                   // count how many bytes received
        if (bytesReceived == NUM_BYTES) {                                   // if received correct number of bytes...
            for (byte i=0; i<NUM_BYTES; i++) data[i] = Wire.read();         // read and store each byte
            printData();                                                    // print the received data
        } else {                                                            // if received wrong number of bytes...
            Serial.print(F("\nRequested "));                                // print message with how many bytes received
            Serial.print(NUM_BYTES);
            Serial.print(F(" bytes, but got "));
            Serial.print(bytesReceived);
            Serial.print(F(" bytes\n"));
        }
        timeNow = millis();                                                 // mark preset time for next trigger
    }
}

 void printData() {
    Serial.print(F("\n"));
    for (byte i=0; i<NUM_BYTES; i++) {
          Serial.print(F("Byte["));
          Serial.print(i);
          Serial.print(F("]: "));
          Serial.print(data[i]);
          Serial.print(F("\t"));
    }
    Serial.print(F("\n"));    
}