1.前言
在这里了解一下Adafruit_SSD1306和Adafruit_GFX这两个库
Adafruti_SSD1306是SSD1306 OLED屏幕的专用显示库
-
Adafruit_GFX库是LCD和OLED屏幕的通用父类,这是一个父类的图形库
-
Adafruit_GFX要与屏幕的专用显示库一同使用,子类库依赖父类库
例如:
- SSD1306 OLED显示器:Adafruit_GFX和Adafruit_SSD1306
- ST7735 1.8英寸彩色LCD显示器:Adafruit_GFX,Adafruit_ZeroDMA 和 Adafruit_ST7735
2.SSD1306显示器介绍
OLED显示器可以抽象为像素点,SSD1306显示器就是128x64像素点阵。
OLED显示屏有四个引脚:
SDA:数据线
SCK:时钟线
VDD:电源线(3.3V)
GND:地线
SSD1306驱动的IIC接口的显示屏的默认通信地址为0x3c/0x3d,具体的可通过如下程序查询:
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop(){
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++ ){
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}else if (error == 4){
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
打开串口监视器可查看通信地址
3.库方法介绍
3.1.初始化构造器 (声明OLED显示器)
Adafruit_ssd1306初始化构造器
SSD1306包括IIC和SPI两个版本,本篇现只介绍IIC版本,后续会更新SPI版本
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi=&Wire, int8_t rst_pin=-1,uint32_t clkDuring=400000UL, uint32_t clkAfter=100000UL);
参数介绍:
w:屏幕宽度像素
h:屏幕高度像素
twi:IIC总线实例,默认为&wrie
rst_pin:复位引脚,没有则填-1
clkDuring:SSD1306库调用期间的传输速率,默认为400000(400KHz)
clkAfter:SSD1306库非调用期间的传输速率,为了兼容IIC总线上其它设备,默认为100000(100KHz)
使用代码实例
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//屏幕分辨率
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Adafruit_SSD1306 display(128, 64, &Wire, -1);
3.2 OLED初始化方法
3.2.1 begin方法
boolean Adafruit_SSD1306::begin(
uint8_t switchvcc=SSD1306_SWITCHCAPVCC,
uint8_t i2caddr=0,
boolean reset=true,
boolean periphBegin=true)
参数介绍:
switchvcc: OLED的电压
i2caddr: OLED的通信地址
使用代码实例:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
3.2.2 clearDisplay ------清屏
- 此方法只清楚单片机缓存,不会显示在屏幕上,需配合display清屏
void Adafruit_SSD1306::clearDisplay(void)
使用代码实例
display.clearDisplay(); // clears the screen and buffer
3.2.3 display ------ 显示内容
- display方法才会把内容推到显示屏上
使用代码实例
display.diaplay();
3.3绘制类方法
因为Adafruit_SSD1306是继承的Adafruit_GFX,所以绘制类方法分为Adafruit_GFX的父类方法和Adafruit_SSD1306的子类方法:
Adafruit_GFX的父类方法:
- drawCircle——绘制空心圆
- fillCircle——绘制实心圆
- drawTriangle——绘制空心三角形
- fillTriangle——绘制实心三角形
- drawRoundRect——绘制空心圆角方形
- fillRoundRect——绘制实心圆角方形
- drawBitmap——绘制Bitmap图像
- drawXBitmap——绘制XBitmap图像
- drawGrayscaleBitmap
- drawRGBBitmap
- drawChar——绘制字符
- getTextBounds——绘制单个字母
- setTextSize——设置字号
- setFont——设置字体
- setCursor——设置光标起始坐标
- setTextColor——设置字体颜色
- setTextWrap
Adafrui