05-04-2020, 19:55
ESP8266 z I2C jest wyjątkowo cienki, bo ma to robione tylko programowo, jako master powinno działać. Dodatkowo działa różnie z różnymi wersjami core (czytałem na Elektrodzie). Pobaw się między AVR na początek i sprawdź czy działa jak chcesz.
Przykład odczytu slava:
Przykład odczytu slava:
Kod:
Master:
#include <Wire.h>
#define slave 8
const char str[]="1abcdefghijk";
char buf[32];
int dane;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
int ilosc = I2CRead(slave,buf,sizeof(buf)+1);
Serial.print("Odebrane dane: ");
Serial.println(buf);
Serial.println();
Serial.print("Odebrano bajtow: ");
Serial.println(ilosc);
delay(2000);
}
uint16_t I2CRead(uint8_t adres, void *data, int len) {
uint8_t receive;
byte *b;
Wire.beginTransmission(adres); //wyslanie danych konfiguracyjnych
Wire.write(15);
Wire.endTransmission();
Wire.requestFrom(adres, len);
for (receive = 0, b = (byte *) data; Wire.available() && receive< len; receive++, b++) {
*b = Wire.read();
}
return (receive);
}
Slave:
#include <Wire.h>
#define slave 8
const byte str[]="1fdgabcdfgdfgkkkl";
uint32_t c;
volatile uint8_t newreq=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin(slave);
Wire.onRequest(requestEvent);
//Wire.onReceive(receiveEvent);
}
void loop() {
// put your main code here, to run repeatedly:
if(newreq)
{
newreq=0;
Serial.print(c);
}
}
void requestEvent() {
I2CWrite(str,sizeof(str)+1);
}
void I2CWrite( void *data, int len) {
Wire.write((byte *) data, len);
}
Miło być decenianym https://buycoffee.to/kaczakat