采用I2C通信,必然涉及到引脚的定义,将I2C使用到的ESP32的引脚在程序中体现出来。采用Wire.h中的setPins() 或者begin()函数均可设置ESP32的具体引脚用于I2C通信。
//call setPins() first, so that begin() can be called without arguments from libraries
bool setPins(int sda, int scl);
bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus
// calling will attemp to recover hung bus
setPins() 和begin()函数的具体实现在Wire.cpp中可见:
bool TwoWire::setPins(int sdaPin, int sclPin)
{
if(i2c) {
log_e("can not set pins if begin was already called");
return false;
}
sda = sdaPin;
scl = sclPin;
return true;
}
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) {
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) {
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;
}
// Author: Nick
// Date:22 Sep,2021
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//ESP32 Arduino驱动0.91" OLED
//NODEMCU-32 V1.2
//Arduino V1.8.16
//参考链接:
//未说明SDA和SCL引脚设定:http://www.taichi-maker.com/homepage/reference-index/display-reference-index/arduino-oled-application/
//SDA和SCL在ware.begin()中定义:https://blog.csdn.net/qq_42805977/article/details/107765966
#include
// 引入驱动OLED0.91所需的库
#include
#include
#define SCREEN_WIDTH 128 // 设置OLED宽度,单位:像素
#define SCREEN_HEIGHT 32 // 设置OLED高度,单位:像素
// 自定义重置引脚,虽然教程未使用,但却是Adafruit_SSD1306库文件所必需的
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
Wire.begin(/*SDA*/17,/*SCL*/16);
// 初始化OLED并设置其IIC地址为 0x3C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop()
{
words_display();
display.display();
}
void words_display()
{
// 清除屏幕
display.clearDisplay();
// 设置字体颜色,白色可见
display.setTextColor(WHITE);
//设置字体大小
display.setTextSize(1.5);
//设置光标位置
display.setCursor(0, 0);
display.print("0.91 OLED");
display.setCursor(0, 10);
display.print("Run time: ");
//打印自开发板重置以来的秒数:
display.print(millis() / 1000);
display.print(" s");
display.setCursor(0, 20);
display.print("Author: ");
display.print("Nick");
}
1)说明整体实现思路及库添加注意事项,未说明SDA和SCL引脚设定:http://www.taichi-maker.com/homepage/reference-index/display-reference-index/arduino-oled-application/
2)说明SDA和SCL引脚设定,即SDA和SCL在Wire.begin()中定义:https://blog.csdn.net/qq_42805977/article/details/107765966
void setup()
{
Wire.begin(/*SDA*/12,/*SCL*/14);
}