ESP32 Arduino 两路IIC使用方法

ESP32 Arduino 两路IIC使用方法


由于项目需要两路IIC,但是我不想复用IIC接口。现做如下更改

ESP32 Arduino版本

ESP32 选用模板为ESP32 Dev Module
Arduino 版本为1.8.19

库文件

pins_arduino.h Wire.cpp

pins_arduino.h

修改你所需要配置成IIC的引脚
static const uint8_t SDA = 26;
static const uint8_t SCL = 25;
static const uint8_t SDA1 = 21;
static const uint8_t SCL1 = 22;

Wire.cpp

  1. 在Wire.cpp代码中最下面查看是否有如下代码
    TwoWire Wire = TwoWire(0);
    TwoWire Wire1 = TwoWire(1);

  2. 修改函数 bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency),修改下方代码加粗部分
    bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
    {
    if(sdaPin < 0) { // default param passed
    if(num == 0) {
    if(sda==-1) {
    sdaPin = SDA; //use Default Pin
    } else {
    sdaPin = sda; // reuse prior pin
    }
    } else {
    if(sda==-1) {
    sdaPin = SDA1;
    // log_e(“no Default SDA Pin for Second Peripheral”);
    // return false; //no Default pin for Second Peripheral

    } else {
    sdaPin = sda; // reuse prior pin
    }
    }
    }

    if(sclPin < 0) { // default param passed
    if(num == 0) {
    if(scl == -1) {
    sclPin = SCL; // use Default pin
    } else {
    sclPin = scl; // reuse prior pin
    }
    } else {
    if(scl == -1) {
    sclPin = SCL1;
    // log_e(“no Default SCL Pin for Second Peripheral”);
    // return false; //no Default pin for Second Peripheral

    } else {
    sclPin = scl; // reuse prior pin
    }
    }
    }

    sda = sdaPin;
    scl = sclPin;
    i2c = i2cInit(num, sdaPin, sclPin, frequency);
    if(!i2c) {
    return false;
    }

    flush();
    return true;

}

调用

这路IIC用来驱动0.96寸OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET);

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
中景园电子0.96OLED显示屏_arduino_IIC_例程+SPI_例程工程源码: void Adafruit_SSD1306::startscrolldiagleft(uint8_t start, uint8_t stop){ ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA); ssd1306_command(0X00); ssd1306_command(SSD1306_LCDHEIGHT); ssd1306_command(SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL); ssd1306_command(0X00); ssd1306_command(start); ssd1306_command(0X00); ssd1306_command(stop); ssd1306_command(0X01); ssd1306_command(SSD1306_ACTIVATE_SCROLL); } void Adafruit_SSD1306::stopscroll(void){ ssd1306_command(SSD1306_DEACTIVATE_SCROLL); } void Adafruit_SSD1306::ssd1306_data(uint8_t c) { if (sid != -1) { // SPI //digitalWrite(cs, HIGH); *csport |= cspinmask; //digitalWrite(dc, HIGH); *dcport |= dcpinmask; //digitalWrite(cs, LOW); *csport &= ~cspinmask; fastSPIwrite(c); //digitalWrite(cs, HIGH); *csport |= cspinmask; } else { // I2C uint8_t control = 0x40; // Co = 0, D/C = 1 Wire.beginTransmission(_i2caddr); Wire.write(control); Wire.write(c); Wire.endTransmission(); } } void Adafruit_SSD1306::display(void) { ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 if (sid != -1) { // SPI *csport |= cspinmask; *dcport |= dcpinmask; *csport &= ~cspinmask; for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) { fastSPIwrite(buffer[i]); //ssd1306_data(buffer[i]); } // i wonder why we have to do this (check datasheet) if (SSD1306_LCDHEIGHT == 32) { for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) { //ssd1306_data(0); fastSPIwrite(0); } } *csport |= cspinmask; } else { // save I2C bitrate uint8_t twbrbackup = TWBR; TWBR = 12; // upgrade to 400KHz! //Serial.println(TWBR, DEC); //Serial.println(TWSR & 0x3, DEC); // I2C
ESP32 Arduino库中提供了IIC(Inter-Integrated Circuit)接口的支持。IIC是一种串行通信协议,用于在芯片之间进行数据传输。在ESP32上,IIC接口可以作为主机使用。\[3\]你可以使用SPI库中的相关函数来进行IIC通信。例如,你可以使用SPI.transfer()函数来发送和接收数据。示例代码如下: ```cpp #include <Wire.h> void setup() { Wire.begin(); // 初始化IIC接口 Serial.begin(115200); } void loop() { Wire.beginTransmission(0x50); // 设置要通信的设备地址 Wire.write(0x01); // 发送要写入的数据 Wire.endTransmission(); // 结束传输 Wire.requestFrom(0x50, 1); // 请求从设备读取数据 if (Wire.available()) { byte data = Wire.read(); // 读取数据 Serial.println(data); // 打印数据 } delay(1000); } ``` 在这个例子中,我们使用Wire库来进行IIC通信。首先,我们使用Wire.begin()函数初始化IIC接口。然后,在循环中,我们使用Wire.beginTransmission()函数设置要通信的设备地址,使用Wire.write()函数发送要写入的数据,最后使用Wire.endTransmission()函数结束传输。接着,我们使用Wire.requestFrom()函数请求从设备读取数据,并使用Wire.available()函数检查是否有数据可用。如果有数据可用,我们使用Wire.read()函数读取数据,并使用Serial.println()函数打印数据。最后,我们使用delay()函数延迟一段时间。 这是一个简单的ESP32 Arduino库中使用IIC接口的示例。你可以根据自己的需求进行修改和扩展。 #### 引用[.reference_title] - *1* *2* [玩转 ESP32 + Arduino (六) 硬件定时器, IIC, SPI](https://blog.csdn.net/finedayforu/article/details/108464949)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ESP32 Arduino (八) IIc和SPI](https://blog.csdn.net/DOF526570/article/details/128910827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值