ciao,
stavo provando a far comunicare l'arduino con un tiny tramite i2c ma niente da fare..
tra l'altro lato attiny manano i onRequest e onReceive che sono molto comodi..che mai non si riescono ad implementare?
limitazioni della USI?
uso questi due sketch:
se vedete degli errori o se percaso potete provarli e dirmi se a voi funzionano ve ne sarei veramente grato..
//lato attiny
#include <TinyWireM.h> // I2C Master lib for ATTinys which use USI
#define ARDUINO 0x01 // 7 bit I2C address for DS1621 temperature sensor
#define LED1_PIN 4 // ATtiny Pin 3
void setup(){
pinMode(LED1_PIN,OUTPUT);
Blink(LED1_PIN,2); // show it's alive
delay(1000);
TinyWireM.begin(); // initialize I2C lib
delay (3000);
}
void loop(){
TinyWireM.beginTransmission(ARDUINO);
TinyWireM.send('A'); // if one-shot, start conversions now
TinyWireM.endTransmission(); // Send 1 byte to the slave
while(TinyWireM.available()){
Blink(LED1_PIN,2); // blink 10's of temperature on LED 1
}
delay (1000);
}
void Blink(byte led, byte times){ // poor man's GUI
for (byte i=0; i< times; i++){
digitalWrite(led,HIGH);
delay (400);
digitalWrite(led,LOW);
delay (175);
}
}
//lato arduino
#include <Wire.h>
void setup()
{
Wire.begin(0x01); // join i2c bus (address optional for master)
}
void loop()
{
Wire.beginTransmission(0x02); // transmit to device #4
Wire.send('B'); // sends five bytes
Wire.endTransmission(); // stop transmitting
while(Wire.available()) // slave may send less than requested
{
char c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
L'Attiny è il master? Se si, il OnRequest lo devi usare sull'Arduino.
Dal codice sembra che anche l'Arduino si comporti come master è interroghi un'altra periferica.
Puoi spiegare meglio la logica che vuoi implementare?
Una cosa, però. Non mi torna che tu metta entrambi gli sketch dei 2 micro subito in trasmissione.
Prova ad alternare la cosa, ossia mettere tipo un pulsantino per attivare la trasmissione sul Tiny. A questo punto l'Arduino riceve il dato e risponde.
Ora è il Tiny a ricevere il dato ed a rispondere. E così via.
//lato arduino
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
}
void loop()
{
Wire.beginTransmission(0x02); // transmit to device #4
Wire.send(2); // sends five bytes
Wire.endTransmission(); // stop transmitting
digitalWrite(13,HIGH);
unsigned long mstx=millis();
while(millis()<(mstx+1000)){
while(Wire.available()) // slave may send less than requested
{
char c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
}
}
digitalWrite(13,LOW);
delay(2000);
}
#include "TinyWireS.h" // wrapper class for I2C slave routines
#define I2C_SLAVE_ADDR 0x02 // i2c slave address (38)
#define LED1_PIN 4 // ATtiny Pin 3
void setup(){
pinMode(LED1_PIN,OUTPUT); // for general DEBUG use
Blink(LED1_PIN,2); // show it's alive
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
}
void loop(){
byte byteRcvd = 0;
if (TinyWireS.available()){ // got I2C input!
byteRcvd = TinyWireS.receive(); // get the byte from master
Blink(LED1_PIN,byteRcvd); // master must wait for this to finish before calling Wire.requestFrom
// add 10 to what's received
TinyWireS.send('o'); // send it back to master
TinyWireS.send('k');
Blink(LED1_PIN,2); // show we transmitted
}
}
void Blink(byte led, byte times){ // poor man's display
for (byte i=0; i< times; i++){
digitalWrite(led,HIGH);
delay (250);
digitalWrite(led,LOW);
delay (175);
}
}
funziona, ma il problema è che dopo 16 tx si pianta, se tolgo i send funziona benissimo..
bisogna usare un comando per svuotare il buffer in uscita? al 32esimo byte inviato blocca l'esecuzione del programma e inoltre i dati non escono dal tiny