首先下载SSD1306的库到Arduino,有两种方式,任选其一:
1.在Github下载开源库 Adafruit_SSD1306 https://github.com/adafruit/Adafruit_SSD1306,下载解压后放到Arduino的库文件夹即可。
2.直接使用Arduino IDE下载 Adafruit_SSD1306库(推荐用这个方法安装类库)。
打开Tools菜单里的Mnge Libraries选项。 搜索ssd1306关键词,安装下面选中的这个Adafruit_SSD1306库(点击Install按钮进行安装) 安装时提示是否安装一些其它相关库,点击Install all按钮,全部安装。 安装好后重启一下Arduino IDE, 打开Adafruit_SSD1306库自带的示例代码把示例代码上传到Arduino,下面连一下OLED的线。
Adafruit_SSD1306库的示例代码中包含了图形文字以及动画的代码,写的很清楚。接下来,新建一个空的工程,引用库,显示一串文字试试。
#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 32 // 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);
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer 这里如果不清理buffer,默认的显示内容为Adafruit类库的LOGO
display.clearDisplay();
ShowText();
}
void loop() {
// put your main code here, to run repeatedly:
}
void ShowText(void) {
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text 2倍字体
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 10); //显示的坐标位置
display.println(F("Happy day"));
display.display(); // Show text
}
虽然Adafruit_SSD1306这个库功能很强大,但是显示中文不是很方便,需要先用软件把文字转换成点位数据,当图片来显示。
先下载取模软件PCtoLCD2002,打开后先配置如下。然后输入汉字内容,点击生成字模按钮。转换好的输入会显示在下面。复制到代码中定义成数组。
下面写段简单的测试代码,显示一行中文。
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// 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);
static const unsigned char PROGMEM cnWord_shi[] =
{
0x02,0x20,0x12,0x20,0x12,0x20,0x12,0x20,0x12,0x20,0xFF,0xFE,0x12,0x20,0x12,0x20,
0x12,0x20,0x12,0x20,0x13,0xE0,0x10,0x00,0x10,0x00,0x10,0x00,0x1F,0xFC,0x00,0x00,//世
};
static const unsigned char PROGMEM cnWord_jie[] =
{
0x00,0x00,0x1F,0xF0,0x11,0x10,0x11,0x10,0x1F,0xF0,0x11,0x10,0x11,0x10,0x1F,0xF0,
0x02,0x80,0x0C,0x60,0x34,0x58,0xC4,0x46,0x04,0x40,0x08,0x40,0x08,0x40,0x10,0x40,//界
};
static const unsigned char PROGMEM cnWord_jia[] =
{
0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x7C,0xFE,0x44,0x12,0x44,0x12,0x44,0x12,0x44,
0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x22,0x44,0x22,0x7C,0x4A,0x44,0x84,0x00,//加
};
static const unsigned char PROGMEM cnWord_you[] =
{
0x00,0x40,0x20,0x40,0x10,0x40,0x10,0x40,0x87,0xFC,0x44,0x44,0x44,0x44,0x14,0x44,
0x14,0x44,0x27,0xFC,0xE4,0x44,0x24,0x44,0x24,0x44,0x24,0x44,0x27,0xFC,0x04,0x04,//油
};
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer 这里如果不清理buffer,默认的显示内容为Adafruit类库的LOGO
display.clearDisplay();
ShowCnText();
}
void loop() {
// put your main code here, to run repeatedly:
}
void ShowCnText()
{
// display.clearDisplay(); // clears the screen and buffer
// display.drawBitmap(10,10,cnWord_shi,16,16, 1);
// display.display();
display.clearDisplay();
display.drawBitmap(10,10, cnWord_shi, 16,16, 1);
display.drawBitmap(30,10, cnWord_jie, 16,16, 1);
display.drawBitmap(50,10, cnWord_jia, 16,16, 1);
display.drawBitmap(70,10, cnWord_you, 16,16, 1);
display.display();
delay(1000);
}
网友评论