前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OLED屏幕SSD1306芯片(杂文)

OLED屏幕SSD1306芯片(杂文)

作者头像
云深无际
发布2021-10-20 11:11:48
8870
发布2021-10-20 11:11:48
举报
文章被收录于专栏:云深之无迹云深之无迹
代码语言:javascript
复制
https://wiki.dfrobot.com.cn/_SKU_DFR0486_Gravity_I2C_OLED-2864%E6%98%BE%E7%A4%BA%E5%B1%8F

屏幕是Dfrobot家的,质量真好。

长这样

  • 工作电压:3.3V~5V
  • 显示颜色:蓝色
  • 像素个数:128列 × 64行
  • 接口方式:Gravity-I2C
  • 刷屏速率:60帧/秒
  • 亮度:60 (Typ) cd/m2
  • 全屏点亮功耗:约22.75mA
  • 工作温度:-30℃~+70℃
  • 显示面积:21.744 × 10.864(mm)
  • 模块尺寸:41.20 × 26.20 (mm)
  • 安装孔尺寸:35 x 20 (mm)
  • 模块重量:15克

128x64的分辨率

IIC的接口

  • VCC:3V3~5V电源输入
  • GND:电源地
  • SCL:I2C时钟线
  • SDA:I2C数据线

我们知道了,屏幕是128x64的分辨率,芯片是SD1306,3.3V~5V的电压都可以。那接下来就驱动它。

Arduino里面搜索关键词安装一下,lib是一代,2是二代

我这里全点了

找个demo跑一下

里面的函数

代码语言:javascript
复制
https://github.com/olikraus/u8g2

二代库的位置

第一点的事情是对于屏幕的初始化,SPI和I2C的连接。

代码语言:javascript
复制
https://github.com/olikraus/u8g2/wiki/u8x8setupcpp

初始化的构造方法在这里

代码语言:javascript
复制
#include <Arduino.h>
#include <SPI.h>
#include <U8x8lib.h>

/* Constructor */
U8X8_SSD1306_128X64_NONAME_4W_SW_SPI u8x8(/* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);

/* u8x8.begin() is required and will sent the setup/init sequence to the display */
void setup(void)
{
  u8x8.begin();
}

void loop(void)
{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.drawString(0,0,"Hello World!");
  delay(1000);
}

构造参数的意思,

下面看一代的库

代码语言:javascript
复制
https://github.com/olikraus/u8glib/wiki/device#ssd1306-128x64

选择I2C

如果使用硬件I2C,就不是IO口模拟的协议,还支持这两个

打开一个demo就是会显示这些,对应的连接芯片

代码语言:javascript
复制
#include "U8glib.h"

// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported
// devices with all constructor calls is here: https://github.com/olikraus/u8glib/wiki/device

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0); // I2C / TWI

void drawColorBox(void)
{
    u8g_uint_t w, h;
    u8g_uint_t r, g, b;

    w = u8g.getWidth() / 32;
    h = u8g.getHeight() / 8;
    for (b = 0; b < 4; b++)
        for (g = 0; g < 8; g++)
            for (r = 0; r < 8; r++)
            {
                u8g.setColorIndex((r << 5) | (g << 2) | b);
                u8g.drawBox(g * w + b * w * 8, r * h, w, h);
            }
}

void drawLogo(uint8_t d)
{
#ifdef MINI_LOGO
    u8g.setFont(u8g_font_gdr17r);
    u8g.drawStr(0 + d, 22 + d, "U");
    u8g.setFont(u8g_font_gdr20n);
    u8g.drawStr90(17 + d, 8 + d, "8");
    u8g.setFont(u8g_font_gdr17r);
    u8g.drawStr(39 + d, 22 + d, "g");

    u8g.drawHLine(2 + d, 25 + d, 34);
    u8g.drawVLine(32 + d, 22 + d, 12);
#else
    u8g.setFont(u8g_font_gdr25r);
    u8g.drawStr(0 + d, 30 + d, "U");
    u8g.setFont(u8g_font_gdr30n);
    u8g.drawStr90(23 + d, 10 + d, "8");
    u8g.setFont(u8g_font_gdr25r);
    u8g.drawStr(53 + d, 30 + d, "g");

    u8g.drawHLine(2 + d, 35 + d, 47);
    u8g.drawVLine(45 + d, 32 + d, 12);
#endif
}

void drawURL(void)
{
#ifndef MINI_LOGO
    u8g.setFont(u8g_font_4x6);
    if (u8g.getHeight() < 59)
    {
        u8g.drawStr(53, 9, "code.google.com");
        u8g.drawStr(77, 18, "/p/u8glib");
    }
    else
    {
        u8g.drawStr(1, 54, "code.google.com/p/u8glib");
    }
#endif
}

void draw(void)
{
    if (u8g.getMode() == U8G_MODE_R3G3B2)
    {
        drawColorBox();
    }
    u8g.setColorIndex(1);
    if (U8G_MODE_GET_BITS_PER_PIXEL(u8g.getMode()) > 1)
    {
        drawLogo(2);
        u8g.setColorIndex(2);
        drawLogo(1);
        u8g.setColorIndex(3);
    }
    drawLogo(0);
    drawURL();
}

void setup(void)
{
    // flip screen, if required
    //u8g.setRot180();
}

void loop(void)
{

    // picture loop
    u8g.firstPage();
    do
    {
        draw();
        u8g.setColorIndex(1);
    } while (u8g.nextPage());

    // rebuild the picture after some delay
    delay(200);
}

最后代码是这样的

在这里我也找到了上面初始化的意思

代码语言:javascript
复制
https://www.jianshu.com/p/35e185051aa7

这个是我看到的一个思维导图,地址在后面。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-10-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云深之无迹 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档