一、现象表征
前期测试mpu6050运行正常,昨天测试使用SSD1306Wire点亮oled。单独点亮是可以的,但是和陀螺仪同时运行时,显示屏不亮。
起初做了意见蠢事,把显示屏控制的文件村成了.c文件,造成提示说:
error: unknown type name ‘class‘
error: expected ‘=‘, ‘,‘, ‘;‘, ‘asm‘ or ‘__attribute__‘ before ‘{‘ token
好长时间没找到问题所在,最后把文件改成.cpp报错消失。
二、发现问题
oled显示屏和陀螺仪同时运行,显示器不亮。找到问题是通过注释代码,把陀螺仪的代码注释掉,显示屏就正常运行了。因此,发现问题是他们俩冲突。
三、解决问题
考虑应该是SSD1306Wire应该是有个默认的I2C通道,oled显示屏和陀螺仪使用了同一个通道发生冲突。后经研究SSD1306Wire.h,发现:
SSD1306Wire(uint8_t _address, int _sda = -1, int _scl = -1, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64, HW_I2C _i2cBus = I2C_ONE, int _frequency = 700000)其实有6个参数。参数介绍如下:
* @param1: _address I2C Display address
* @param2: _sda I2C SDA pin number, default to -1 to skip Wire begin call
* @param3: _scl I2C SCL pin number, default to -1 (only SDA = -1 is considered to skip Wire begin call)
* @param4: g display geometry dafault to generic GEOMETRY_128_64, see OLEDDISPLAY_GEOMETRY definition for other options
* @param5 _i2cBus on ESP32 with 2 I2C HW buses, I2C_ONE for 1st Bus, I2C_TWO fot 2nd bus, default I2C_ONE
* @param6: _frequency for Frequency by default Let's use ~700khz if ESP8266 is in 160Mhz mode, this will be limited to ~400khz if the ESP8266 in 80Mhz mode
其中第五个参数_i2cBus :on ESP32 with 2 I2C HW buses, I2C_ONE for 1st Bus, I2C_TWO fot 2nd bus, default I2C_ONE。
示例程序中SSD1306Wire只有三个参数:SSD1306Wire(0x3c, SDA, SCL);
于是,在我的程序中指定使用I2C_TWO:
SSD1306Wire display(0x3c, SDA, SCL,GEOMETRY_128_64,I2C_TWO,700000);
问题顺利解决。