查看: 1409|回复: 0

[评测分享] 【ESP32-E Arduino开发板】-驱动OLED屏

[复制链接]
  • TA的每日心情
    奋斗
    昨天 21:59
  • 签到天数: 2299 天

    连续签到: 381 天

    [LV.Master]伴坛终老

    发表于 2021-7-11 18:53:24 | 显示全部楼层 |阅读模式
    分享到:
        说到OLED屏模块,我们知道常见的通讯接口有两种,一种是SPI,另一种是IIC。0.96寸OLED分辨率是128*64;即OLED显示是128行*64列,网上关于ssd1306的驱动实例诸多,这里不再一一列举。ESP32-E Arduino开发板上的IIC丝印很明显,具体的硬件连线如下图所示:
    接线.jpg
          关于Arduino开发软件中有现成的OLED屏驱动库,只不过库有针对SPI、IIC通信的,在库管理器中搜索Oled,即可找到需要的库,下载并安装。
    下载安装oled库.png
         安装好库文件后,选择“SSD130**mpleDemo”例程,然后稍作修改,编译上传。
    下载工程.png
         程序演示的效果如下:
    演示oled.gif
        代码如下:
    1. #define WiFi_Logo_width 60
    2. #define WiFi_Logo_height 36
    3. const uint8_t WiFi_Logo_bits[] PROGMEM = {
    4. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8,
    5. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00,
    6. 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
    7. 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00,
    8. 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
    9. 0xFF, 0x03, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
    10. 0x00, 0xFF, 0xFF, 0xFF, 0x07, 0xC0, 0x83, 0x01, 0x80, 0xFF, 0xFF, 0xFF,
    11. 0x01, 0x00, 0x07, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0x00,
    12. 0xC0, 0xFF, 0xFF, 0x7C, 0x00, 0x60, 0x0C, 0x00, 0xC0, 0x31, 0x46, 0x7C,
    13. 0xFC, 0x77, 0x08, 0x00, 0xE0, 0x23, 0xC6, 0x3C, 0xFC, 0x67, 0x18, 0x00,
    14. 0xE0, 0x23, 0xE4, 0x3F, 0x1C, 0x00, 0x18, 0x00, 0xE0, 0x23, 0x60, 0x3C,
    15. 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x03, 0x60, 0x3C, 0x1C, 0x70, 0x18, 0x00,
    16. 0xE0, 0x07, 0x60, 0x3C, 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C,
    17. 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00,
    18. 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x8F, 0x71, 0x3C,
    19. 0x1C, 0x70, 0x18, 0x00, 0xC0, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x08, 0x00,
    20. 0xC0, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x0C, 0x00, 0x80, 0xFF, 0xFF, 0x1F,
    21. 0x00, 0x00, 0x06, 0x00, 0x80, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x07, 0x00,
    22. 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0xF8, 0xFF, 0xFF,
    23. 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00,
    24. 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
    25. 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00,
    26. 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC,
    27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    28. };
    复制代码
    1. // Include the correct display library
    2. // For a connection via I2C using the Arduino Wire include:
    3. #include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
    4. #include "SSD1306Wire.h"        // legacy: #include "SSD1306.h"

    5. // Optionally include custom images
    6. #include "images.h"
    7. // Initialize the OLED display using Arduino Wire:
    8. SSD1306Wire display(0x3c, SDA, SCL);   // ADDRESS, SDA, SCL  -  SDA and SCL usually populate automatically based on your board's pins_arduino.h
    9. // SSD1306Wire display(0x3c, SDA, SCL, GEOMETRY_128_32);  // ADDRESS, SDA, SCL, OLEDDISPLAY_GEOMETRY  -  Extra param required for 128x32 displays.
    10. #define DEMO_DURATION 2000
    11. typedef void (*Demo)(void);

    12. int demoMode = 0;
    13. int counter = 1;

    14. void setup() {
    15.   Serial.begin(115200);
    16.   Serial.println();
    17.   Serial.println();

    18.   // Initialising the UI will init the display too.
    19.   display.init();

    20.   display.flipScreenVertically();
    21.   display.setFont(ArialMT_Plain_10);
    22. }

    23. void drawFontFaceDemo() {
    24.     // Font Demo1
    25.     // create more fonts at http://oleddisplay.squix.ch/
    26.     display.setTextAlignment(TEXT_ALIGN_LEFT);
    27.     display.setFont(ArialMT_Plain_10);
    28.     display.drawString(0, 0, "FireBeetle ESP32");
    29.     display.setFont(ArialMT_Plain_16);
    30.     display.drawString(0, 10, "FireBeetle ESP32");
    31.     display.setFont(ArialMT_Plain_24);
    32.     display.drawString(0, 26, "FireBeetle ESP32");
    33. }

    34. void drawTextFlowDemo() {
    35.     display.setFont(ArialMT_Plain_10);
    36.     display.setTextAlignment(TEXT_ALIGN_LEFT);
    37.     display.drawStringMaxWidth(0, 0, 128,
    38.       "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
    39. }

    40. void drawTextAlignmentDemo() {
    41.     // Text alignment demo
    42.   display.setFont(ArialMT_Plain_10);

    43.   // The coordinates define the left starting point of the text
    44.   display.setTextAlignment(TEXT_ALIGN_LEFT);
    45.   display.drawString(0, 10, "Left aligned (0,10)");

    46.   // The coordinates define the center of the text
    47.   display.setTextAlignment(TEXT_ALIGN_CENTER);
    48.   display.drawString(64, 22, "Center aligned (64,22)");

    49.   // The coordinates define the right end of the text
    50.   display.setTextAlignment(TEXT_ALIGN_RIGHT);
    51.   display.drawString(128, 33, "Right aligned (128,33)");
    52. }

    53. void drawRectDemo() {
    54.       // Draw a pixel at given position
    55.     for (int i = 0; i < 10; i++) {
    56.       display.setPixel(i, i);
    57.       display.setPixel(10 - i, i);
    58.     }
    59.     display.drawRect(12, 12, 20, 20);

    60.     // Fill the rectangle
    61.     display.fillRect(14, 14, 17, 17);

    62.     // Draw a line horizontally
    63.     display.drawHorizontalLine(0, 40, 40);

    64.     // Draw a line horizontally
    65.     display.drawVerticalLine(40, 0, 40);
    66. }

    67. void drawCircleDemo() {
    68.   for (int i=1; i < 8; i++) {
    69.     display.setColor(WHITE);
    70.     display.drawCircle(32, 32, i*3);
    71.     if (i % 2 == 0) {
    72.       display.setColor(BLACK);
    73.     }
    74.     display.fillCircle(96, 32, 32 - i* 3);
    75.   }
    76. }

    77. void drawProgressBarDemo() {
    78.   int progress = (counter / 5) % 100;
    79.   // draw the progress bar
    80.   display.drawProgressBar(0, 32, 120, 10, progress);

    81.   // draw the percentage as String
    82.   display.setTextAlignment(TEXT_ALIGN_CENTER);
    83.   display.drawString(64, 15, String(progress) + "%");
    84. }

    85. void drawImageDemo() {
    86.     // see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html
    87.     // on how to create xbm files
    88.     display.drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
    89. }

    90. Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
    91. int demoLength = (sizeof(demos) / sizeof(Demo));
    92. long timeSinceLastModeSwitch = 0;

    93. void loop() {
    94.   // clear the display
    95.   display.clear();
    96.   // draw the current demo method
    97.   demos[demoMode]();

    98.   display.setTextAlignment(TEXT_ALIGN_RIGHT);
    99.   display.drawString(10, 128, String(millis()));
    100.   // write the buffer to the display
    101.   display.display();

    102.   if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
    103.     demoMode = (demoMode + 1)  % demoLength;
    104.     timeSinceLastModeSwitch = millis();
    105.   }
    106.   counter++;
    107.   delay(10);
    108. }
    复制代码

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-11-23 09:38 , Processed in 0.145707 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.