我正在为I2C通信开发attiny85。我已经遍历了像Wire.h
,TinyWire.h
,tinyWireM.h
,tinyWireS.h
这样的不同库。
首先,我想通过I2C通讯发送一些字节的数据,并试图用示波器确定引脚的范围,但它没有给我适当的结果。在互联网上寻找使attiny85与I2c配合使用的不同方法实在是无情的,我无法完成任务。最终,我尝试将attiny85用作主机,将arduino Uno用作从机,因为它在我的包装盒中是备用的。
我试图以attiny85作为主机,并将数据发送到arduino,并在串行监视器上查看输出,但显示为零。
作为参考,附加了主代码和从代码,我的任务很容易检查串行。
Attiny85作为管理员
#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的串行监视器上获得输出。 我在这里做什么错了?
答案 0 :(得分:0)
一段时间以前,我已经使用TinyWireS lib(https://github.com/nadavmatalon/TinyWireS)将Atiny85用作从属设备,并且工作正常。 下面是引脚配置
ATtiny85引脚5与Arduino Uno A4和 带有Arduino Uno A5的ATtiny85引脚7
下面是我的代码
Atiny。
#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;
}
}
Uno。
#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"));
}