I need faster communication between the ESP and the ADC, so I found this https://github.com/pasko-zh/brzo_i2c.
Original wire Example
#include "Wire.h"
#define PCF8591 0x48
byte a0;
void setup() {
Wire.begin(13, 14);
Serial.begin(115200);
}
void loop() {
Wire.beginTransmission(PCF8591);
Wire.write(0x04);
Wire.endTransmission();
Wire.requestFrom(PCF8591, 5);
a0 = Wire.read();
a0 = Wire.read();
Serial.println(a0);
delay(1000);
}
Brzo I2C example
#include "brzo_i2c.h"
uint8_t buffer[10];
uint8_t error = 0;
float temp = 0.0;
uint8_t ICACHE_RAM_ATTR get_temp(float *t) {
uint8_t bcode = 0;
brzo_i2c_start_transaction(0x48, 400);
buffer[0] = 0x04;
brzo_i2c_write(buffer, 1, false);
brzo_i2c_read(buffer, 2, false);
bcode = brzo_i2c_end_transaction();
*t = ((buffer[0] << 8) | buffer[1]);
return bcode;
}
void setup() {
delay(500);
Serial.begin(115200);
brzo_i2c_setup(13, 14, 2000);
}
void loop() {
error = get_temp(&temp);
if (error == 0) {
Serial.print("Temp = ");
Serial.println(temp, 8);
} else {
Serial.print("Brzo error : ");
Serial.println(error);
}
delay(500);
}
But I get always the same error.
1 : Bus not free
I rewrote it in different ways, changing the frequency, pins and doing multiple transactions. The main reason I don't get it to work is the difference in the API.
There is no Wire.requestFrom();
in the brzo_i2c lib. So I need to read the value with brzo_i2c_read(buffer,2,false);
.
2 Answers 2
This line
brzo_i2c_start_transaction(0x48,400);
Sets the SCL frequency to 400kHz.
According to the Datasheet for the PCF8591 the maximum SCL clock frequency is 100kHz.
The other issue may be the "repeated_start" flag on brzo_i2c_write
.
If you look at the examples, and "apply" that to your code, your would do something like:
uint8_t ICACHE_RAM_ATTR get_temp(float *t){
uint8_t bcode=0;
brzo_i2c_start_transaction(0x48,100); // changed 400 to 100
buffer[0]=0x04;
brzo_i2c_write(buffer,1,true); // changed to true because
brzo_i2c_read(buffer,2,false);
bcode=brzo_i2c_end_transaction();
*t=((buffer[0]<<8)|buffer[1]);
return bcode;
}
My understanding of the code is that you would set repeated_start
to true in the write if you want to then immediately read - which in your case you want to.
I have no idea about Brzo but this looks wrong:
brzo_i2c_start_transaction(0x48,400);
buffer[0]=0x04;
brzo_i2c_write(buffer,1,false); // Write
brzo_i2c_read(buffer,2,false); // Then a read ??
bcode=brzo_i2c_end_transaction();
I think you end and read are the wrong way round, but like I say I've never done anything with this so I'm probably wrong.
-
buffer[0]=0x04; brzo_i2c_write(buffer,1,false); sets the chip to return the analog values. after that i need to read the chip the first byte is analog input0 2nd = AIN1...cocco– cocco2017年03月29日 12:33:22 +00:00Commented Mar 29, 2017 at 12:33
-
Wire.write(0x04); // control byte - read ADC0 then auto-incrementcocco– cocco2017年03月29日 12:36:09 +00:00Commented Mar 29, 2017 at 12:36
-
Like I said, I thought I might be wrong, I thought you had to end the transaction between writing and reading.Code Gorilla– Code Gorilla2017年03月29日 15:01:24 +00:00Commented Mar 29, 2017 at 15:01