Hello community,
I'm trying to use I2C device on the 2nd hardware I2C avaible on this chip, on pin PB10 and PB11.
I found something on this forum that doesn't work for me. I guess it's because the topic concerned a fake STM32F103.
As i have a working screen on I2C1 (SD1306), I re-use some of the code of this topic to make an address scanner. It works (almost*) fine on I2C1 and it found all my devices.
When i plug the same devices on I2C2, it found nothing (Another screen and a MPU6050).
The full code :
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C /// on the screen's back, it's write 0x78 ans 0x7A, with the resistor on 0X78... but it works on 0x3C !?!?
TwoWire Wire2(PB11, PB10);
// TwoWire Wire2 (2, I2C_FAST_MODE); // generate error 'I2C_FAST_MODE' was not declared
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//const int MPU_ADDR = 0x68;
uint16_t tmp = 0;
void setup() {
//Wire2.setSCL(PB_10); // doesn't change anything
//Wire2.setSDA(PB_11);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.display();
}
void loop() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
if(tmp > 55) tmp = 0;
byte error, address;
int nDevices;
for(address = 1; address < 127; address++) {
Wire2.beginTransmission(address);
error = Wire2.endTransmission();
if (error == 0) {
display.setCursor(5, tmp); tmp += 9;
display.print("found at 0x");
if (address < 16)
display.print("0");
display.print(address, HEX);
nDevices++;
}
else if (error == 4) {}
}
display.setCursor(5, tmp); tmp += 9;
if (nDevices == 0)
display.print("nothing found");
else
Serial.println("done");
display.display();
delay(1000);
}
What's going wrong ?
Thank you very much
- it works almost fine because when i put a 2nd device on the same I2C than screen, i have glitterings on the screen and when try to use the MPU6050, it end crashing quickly after 2 or 3 requests. Don't understand why.
Oh, and serial don't works to debug. Guess i need a UART things or something ? (I use a STlinkV2)