TA的每日心情 | 奋斗 昨天 21:59 |
---|
签到天数: 2299 天 连续签到: 381 天 [LV.Master]伴坛终老
|
说到OLED屏模块,我们知道常见的通讯接口有两种,一种是SPI,另一种是IIC。0.96寸OLED分辨率是128*64;即OLED显示是128行*64列,网上关于ssd1306的驱动实例诸多,这里不再一一列举。ESP32-E Arduino开发板上的IIC丝印很明显,具体的硬件连线如下图所示:
关于Arduino开发软件中有现成的OLED屏驱动库,只不过库有针对SPI、IIC通信的,在库管理器中搜索Oled,即可找到需要的库,下载并安装。
安装好库文件后,选择“SSD130**mpleDemo”例程,然后稍作修改,编译上传。
程序演示的效果如下:
代码如下:
- #define WiFi_Logo_width 60
- #define WiFi_Logo_height 36
- const uint8_t WiFi_Logo_bits[] PROGMEM = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
- 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
- 0xFF, 0x03, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
- 0x00, 0xFF, 0xFF, 0xFF, 0x07, 0xC0, 0x83, 0x01, 0x80, 0xFF, 0xFF, 0xFF,
- 0x01, 0x00, 0x07, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0x00,
- 0xC0, 0xFF, 0xFF, 0x7C, 0x00, 0x60, 0x0C, 0x00, 0xC0, 0x31, 0x46, 0x7C,
- 0xFC, 0x77, 0x08, 0x00, 0xE0, 0x23, 0xC6, 0x3C, 0xFC, 0x67, 0x18, 0x00,
- 0xE0, 0x23, 0xE4, 0x3F, 0x1C, 0x00, 0x18, 0x00, 0xE0, 0x23, 0x60, 0x3C,
- 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x03, 0x60, 0x3C, 0x1C, 0x70, 0x18, 0x00,
- 0xE0, 0x07, 0x60, 0x3C, 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C,
- 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00,
- 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x8F, 0x71, 0x3C,
- 0x1C, 0x70, 0x18, 0x00, 0xC0, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x08, 0x00,
- 0xC0, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x0C, 0x00, 0x80, 0xFF, 0xFF, 0x1F,
- 0x00, 0x00, 0x06, 0x00, 0x80, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x07, 0x00,
- 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0xF8, 0xFF, 0xFF,
- 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
- 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
复制代码- // Include the correct display library
- // For a connection via I2C using the Arduino Wire include:
- #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
- #include "SSD1306Wire.h" // legacy: #include "SSD1306.h"
- // Optionally include custom images
- #include "images.h"
- // Initialize the OLED display using Arduino Wire:
- SSD1306Wire display(0x3c, SDA, SCL); // ADDRESS, SDA, SCL - SDA and SCL usually populate automatically based on your board's pins_arduino.h
- // SSD1306Wire display(0x3c, SDA, SCL, GEOMETRY_128_32); // ADDRESS, SDA, SCL, OLEDDISPLAY_GEOMETRY - Extra param required for 128x32 displays.
- #define DEMO_DURATION 2000
- typedef void (*Demo)(void);
- int demoMode = 0;
- int counter = 1;
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.println();
- // Initialising the UI will init the display too.
- display.init();
- display.flipScreenVertically();
- display.setFont(ArialMT_Plain_10);
- }
- void drawFontFaceDemo() {
- // Font Demo1
- // create more fonts at http://oleddisplay.squix.ch/
- display.setTextAlignment(TEXT_ALIGN_LEFT);
- display.setFont(ArialMT_Plain_10);
- display.drawString(0, 0, "FireBeetle ESP32");
- display.setFont(ArialMT_Plain_16);
- display.drawString(0, 10, "FireBeetle ESP32");
- display.setFont(ArialMT_Plain_24);
- display.drawString(0, 26, "FireBeetle ESP32");
- }
- void drawTextFlowDemo() {
- display.setFont(ArialMT_Plain_10);
- display.setTextAlignment(TEXT_ALIGN_LEFT);
- display.drawStringMaxWidth(0, 0, 128,
- "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
- }
- void drawTextAlignmentDemo() {
- // Text alignment demo
- display.setFont(ArialMT_Plain_10);
- // The coordinates define the left starting point of the text
- display.setTextAlignment(TEXT_ALIGN_LEFT);
- display.drawString(0, 10, "Left aligned (0,10)");
- // The coordinates define the center of the text
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.drawString(64, 22, "Center aligned (64,22)");
- // The coordinates define the right end of the text
- display.setTextAlignment(TEXT_ALIGN_RIGHT);
- display.drawString(128, 33, "Right aligned (128,33)");
- }
- void drawRectDemo() {
- // Draw a pixel at given position
- for (int i = 0; i < 10; i++) {
- display.setPixel(i, i);
- display.setPixel(10 - i, i);
- }
- display.drawRect(12, 12, 20, 20);
- // Fill the rectangle
- display.fillRect(14, 14, 17, 17);
- // Draw a line horizontally
- display.drawHorizontalLine(0, 40, 40);
- // Draw a line horizontally
- display.drawVerticalLine(40, 0, 40);
- }
- void drawCircleDemo() {
- for (int i=1; i < 8; i++) {
- display.setColor(WHITE);
- display.drawCircle(32, 32, i*3);
- if (i % 2 == 0) {
- display.setColor(BLACK);
- }
- display.fillCircle(96, 32, 32 - i* 3);
- }
- }
- void drawProgressBarDemo() {
- int progress = (counter / 5) % 100;
- // draw the progress bar
- display.drawProgressBar(0, 32, 120, 10, progress);
- // draw the percentage as String
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.drawString(64, 15, String(progress) + "%");
- }
- void drawImageDemo() {
- // see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html
- // on how to create xbm files
- display.drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
- }
- Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
- int demoLength = (sizeof(demos) / sizeof(Demo));
- long timeSinceLastModeSwitch = 0;
- void loop() {
- // clear the display
- display.clear();
- // draw the current demo method
- demos[demoMode]();
- display.setTextAlignment(TEXT_ALIGN_RIGHT);
- display.drawString(10, 128, String(millis()));
- // write the buffer to the display
- display.display();
- if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
- demoMode = (demoMode + 1) % demoLength;
- timeSinceLastModeSwitch = millis();
- }
- counter++;
- delay(10);
- }
复制代码
|
|