Arduino驱动OLED

Arduino驱动OLED

我的OLED屏为IIC通信,128X32像素

使用了Arduino的库文件

原理

向OLED发送一个128*32的数组表示相应像素点的状态(黑,白)

流程图+接线

Arduino可以是Uno或nano(已测试)

其他版本可自行修改尝试

ArduinoOLED
GNDGND
5V5V
A4SDA
A5SCl
包含库
定义变量及宏定义
调用类
初始化IIC通信及对OLED寄存器写值使能OLED
clearDisplay清除缓冲区
write向缓冲区写入数据
display显示出你想要的图形

#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

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

初始化代码

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();

基本显示语句

  display.clearDisplay();//每次需要先清除再写入,最后显示
  display.drawCircle(display.width()/2, display.height()/2, 5, WHITE);//要显示的命令(以⚪为例)
  display.display();

显示语句

静态字符串

  display.clearDisplay();
  
  display.cp437(true);//若声明则使用CP437字符集(默认关闭)
  display.setTextSize(1);             // 字体大小
  display.setTextColor(WHITE);        // 字体颜色
  display.setCursor(20,8);             // 字体位置(未声明将顺序向下)
  display.println(F("Hello, world!"));

  display.setTextColor(BLACK, WHITE); // 反转文本
  display.println("3.141592");
  display.display();
  delay(500);

  display.setTextSize(1);   
  display.setCursor(7,0);          
  display.setTextColor(WHITE);
  display.print(F("0x")); 
  display.println(0xDEADBEEF, HEX);

  display.display();

注意2进制、8进制与16进制的区别

上面都是ASCII码

显示ASCII码

  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0,0);          
  display.setTextColor(WHITE);
  for(int i=48;i<68;i++){
    display.write(i);  //显示ASCII码代表的字符
  }
  display.display();

动态字符串

  display.clearDisplay();
  display.setCursor(0,0);
  
  display.println("SB");display.write(3);
  display.display();
  display.startscrollright(0x00, 0x0F);//持续右移
  delay(100);
  display.stopscroll();
  delay(200);
  display.startscrollleft(0x00, 0x0F);//持续左移
  delay(2000);
  display.stopscroll();
  delay(200);
  display.startscrolldiagright(0x00, 0x0F);//持续左上移
  delay(8000);
  display.startscrolldiagleft(0x00, 0x07);//持续左上移
  delay(2000);
  display.stopscroll();
  delay(200);

显示图形

线段

  display.drawLine(x0,y0,x1,y1,color);//(线段A点坐标,B点坐标,颜色)

  //display.drawCircle(display.width()/2, display.height()/2, 5, WHITE);//圆O圆心坐标,半径,颜色
  //display.drawRoundRect(2, 2, display.width()-2*2, display.height()-2*2,display.height()/4, WHITE);//圆角矩形,多一个参数未圆角半径

矩形

  display.drawRect(1, 1, display.width()-2, display.height()-2, BLACK);//(矩形ABCD的A点坐标,C点坐标,颜色)*/
  display.fillRect(8, 8, display.width()-16, display.height()-16, INVERSE);//简单地用一种颜色填充一个矩形(INVERSE表示反转像素)注:按命名可以此类推

三角形

  display.drawTriangle(
      display.width()/2  , display.height()/2-i,
      display.width()/2-i, display.height()/2+i,
      display.width()/2+i, display.height()/2+i, WHITE);//三角形三点坐标

一些应用

背景全为白
  for(int i=0;i<=32;i++){
    display.drawLine(0,i,display.width(),i,WHITE);//背景全变为白
  }
填充图形
display.fillRect(8, 8, display.width()-16, display.height()-16, INVERSE);//简单地用一种颜色填充一个矩形(INVERSE表示反转像素)注:按命名可以此类推

特殊

动态雪花图实例

雪花数组
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };//一个雪花对应的像素数组

这个数组表示雪花形状以表示OLED上的像素

宏定义
#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

#define XPOS   0 // Indexes into the 'icons' array in function below
#define YPOS   1
#define DELTAY 2
#define NUMFLAKES 10
显示单个雪花
  display.drawBitmap(
    (display.width()  - LOGO_WIDTH ) / 2,
    (display.height() - LOGO_HEIGHT) / 2,
    logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);

drawBitmap函数说明

  • /*
  • @Brief使用指定的前景色(未设置的位是透明的),在指定的(x,y)位置绘制一个程序驻留的1位图像。
  • @参数x左上角x坐标
  • @参数Y左上角Y坐标
  • @带单色位图的参数位图字节数组
  • @位图的参数宽度(像素)
  • @以像素为单位的位图高度
  • @参数颜色16位5-6-5要绘制的颜色
  • */
重要的函数
void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  int8_t f, icons[NUMFLAKES][3];

  // 生成随机数,让图像随机出现
  for(f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
    icons[f][YPOS]   = -LOGO_HEIGHT;
    icons[f][DELTAY] = random(1, 6);
  }

  for(;;) { // Loop forever...
    display.clearDisplay(); // Clear the display buffer

    // Draw each snowflake:
    for(f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
    }

    display.display(); // Show the display buffer on the screen
    delay(200);        // Pause for 1/10 second

    // Then update coordinates of each flake...
    for(f=0; f< NUMFLAKES; f++) {
      icons[f][YPOS] += icons[f][DELTAY];
      // If snowflake is off the bottom of the screen...
      if (icons[f][YPOS] >= display.height()) {
        // Reinitialize to a random position, just off the top
        icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
        icons[f][YPOS]   = -LOGO_HEIGHT;
        icons[f][DELTAY] = random(1, 6);
      }
    }
  }
}

完整代码

/* OLED例程
 * 
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

TwoWire *wire2;


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };//一个雪花对应的像素数组

const  uint8_t k[20][32] = {1};

void setup() {
  // put your setup code here, to run once:
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();//每次需要先清除再写入,最后显示
  
  display.display();
  delay(500);

  /*display.clearDisplay();
  
  display.cp437(true);
  display.setTextSize(1);             // 字体大小
  display.setTextColor(WHITE);        // 字体颜色
  display.setCursor(20,8);             // 字体位置(未声明将顺序向下)
  display.println(F("Hello, world!"));

  display.setTextColor(BLACK, WHITE); // 反转文本
  display.println("3.141592");
  display.display();
  delay(500);

  display.setTextSize(1);   
  display.setCursor(7,0);          
  display.setTextColor(WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(500);

  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0,0);          
  display.setTextColor(WHITE);
  for(int i=48;i<68;i++){
    display.write(i);  //显示ASCII码代表的字符
  }
  display.display();


  display.clearDisplay();
  display.setCursor(0,0);
  
  display.println("SB");display.write(3);
  display.display();
  display.startscrollright(0x00, 0x0F);//持续右移
  delay(100);
  display.stopscroll();
  delay(200);
  display.startscrollleft(0x00, 0x0F);//持续左移
  delay(2000);
  display.stopscroll();
  delay(200);
  display.startscrolldiagright(0x00, 0x0F);//持续左上移
  delay(8000);
  display.startscrolldiagleft(0x00, 0x07);//持续左上移
  delay(2000);
  display.stopscroll();
  delay(200);*/
}

void loop() {
  // put your main code here, to run repeatedly:
  /*显示爱心*/
  display.clearDisplay();
  display.setTextSize(4);
  display.setCursor(0,0);          
  display.setTextColor(WHITE);
  display.write(3);
  display.display();
  display.startscrolldiagright(0x00, 0x0F);
  delay(5000);
  display.startscrolldiagleft(0x00, 0x0F);
  delay(5000);
  
  /*display.clearDisplay();
  display.drawCircle(display.width()/2, display.height()/2, 5, WHITE);//圆O圆心坐标,半径,颜色
  display.drawRoundRect(2, 2, display.width()-2*2, display.height()-2*2,display.height()/4, WHITE);//圆角矩形,多一个参数未圆角半径
  int i =20;
  display.drawBitmap(
    (display.width()  - LOGO_WIDTH ) / 2,
    (display.height() - LOGO_HEIGHT) / 2,
    logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);*/
  
  
  /*display.drawTriangle(
      display.width()/2  , display.height()/2-i,
      display.width()/2-i, display.height()/2+i,
      display.width()/2+i, display.height()/2+i, WHITE);//三角形三点坐标*/
  /*for(int i=0;i<=32;i++){
    display.drawLine(0,i,display.width(),i,WHITE);//背景全变为白
  }
  display.drawRect(1, 1, display.width()-2, display.height()-2, BLACK);//(矩形ABCD的A点坐标,C点坐标,颜色)*/
  //display.fillRect(8, 8, display.width()-16, display.height()-16, INVERSE);//简单地用一种颜色填充一个矩形(INVERSE表示反转像素)注:按命名可以此类推
  
  //display.height()//获取当前高度,考虑当前旋转(width-i*2是因为向下移动了i行)
  //display.drawLine(x0,y0,x1,y1,color);//(线段A点坐标,B点坐标,颜色)
  /*display.drawLine(0, 0, 7, display.height()-1, WHITE);
  display.drawLine(0, display.height()-1, 2, 0, WHITE);
  display.drawLine(0, 0, display.width()-1, 6, WHITE);*/
  /*display.display();
  testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT);//动态图,参数:数组,长度,宽度*/
}

#define XPOS   0 // Indexes into the 'icons' array in function below
#define YPOS   1
#define DELTAY 2
#define NUMFLAKES 10

void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  int8_t f, icons[NUMFLAKES][3];

  // 生成随机数,让图像随机出现
  for(f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
    icons[f][YPOS]   = -LOGO_HEIGHT;
    icons[f][DELTAY] = random(1, 6);
  }

  for(;;) { // Loop forever...
    display.clearDisplay(); // Clear the display buffer

    // Draw each snowflake:
    for(f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
    }

    display.display(); // Show the display buffer on the screen
    delay(200);        // Pause for 1/10 second

    // Then update coordinates of each flake...
    for(f=0; f< NUMFLAKES; f++) {
      icons[f][YPOS] += icons[f][DELTAY];
      // If snowflake is off the bottom of the screen...
      if (icons[f][YPOS] >= display.height()) {
        // Reinitialize to a random position, just off the top
        icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
        icons[f][YPOS]   = -LOGO_HEIGHT;
        icons[f][DELTAY] = random(1, 6);
      }
    }
  }
}

1/10 second

// Then update coordinates of each flake...
for(f=0; f< NUMFLAKES; f++) {
  icons[f][YPOS] += icons[f][DELTAY];
  // If snowflake is off the bottom of the screen...
  if (icons[f][YPOS] >= display.height()) {
    // Reinitialize to a random position, just off the top
    icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
    icons[f][YPOS]   = -LOGO_HEIGHT;
    icons[f][DELTAY] = random(1, 6);
  }
}

}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值