OLED测试程序

来自Microduino Wikipedia
502748957@qq.com讨论 | 贡献2018年6月4日 (一) 06:46的版本
跳转至: 导航搜索

MCookie - OLED测试程序


在下面的示例中,使用Core核心控制OLED模块显示不同图形与文字。

所需硬件


电路搭建

将Battery、Core、OLED堆叠在一起,或将屏幕通过传感器线接在SensorHub的IIC接口上。

代码

#include "U8glib.h"

// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The complete list of supported devices is here: http://code.google.com/p/u8glib/wiki/device


U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

void u8g_prepare(void) {
  u8g.setFont(u8g_font_6x10);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
}

void u8g_box_frame(uint8_t a) {
  u8g.drawStr( 0, 0, "drawBox");
  u8g.drawBox(5, 10, 20, 10);
  u8g.drawBox(10 + a, 15, 30, 7);
  u8g.drawStr( 0, 30, "drawFrame");
  u8g.drawFrame(5, 10 + 30, 20, 10);
  u8g.drawFrame(10 + a, 15 + 30, 30, 7);
}

void u8g_disc_circle(uint8_t a) {
  u8g.drawStr( 0, 0, "drawDisc");
  u8g.drawDisc(10, 18, 9);
  u8g.drawDisc(24 + a, 16, 7);
  u8g.drawStr( 0, 30, "drawCircle");
  u8g.drawCircle(10, 18 + 30, 9);
  u8g.drawCircle(24 + a, 16 + 30, 7);
}

void u8g_r_frame(uint8_t a) {
  u8g.drawStr( 0, 0, "drawRFrame/Box");
  u8g.drawRFrame(5, 10, 40, 30, a + 1);
  u8g.drawRBox(50, 10, 25, 40, a + 1);
}

void u8g_string(uint8_t a) {
  u8g.drawStr(30 + a, 31, " 0");
  u8g.drawStr90(30, 31 + a, " 90");
  u8g.drawStr180(30 - a, 31, " 180");
  u8g.drawStr270(30, 31 - a, " 270");
}

void u8g_line(uint8_t a) {
  u8g.drawStr( 0, 0, "drawLine");
  u8g.drawLine(7 + a, 10, 40, 55);
  u8g.drawLine(7 + a * 2, 10, 60, 55);
  u8g.drawLine(7 + a * 3, 10, 80, 55);
  u8g.drawLine(7 + a * 4, 10, 100, 55);
}

void u8g_ascii_1() {
  char s[2] = " ";
  uint8_t x, y;
  u8g.drawStr( 0, 0, "ASCII page 1");
  for ( y = 0; y < 6; y++ ) {
    for ( x = 0; x < 16; x++ ) {
      s[0] = y * 16 + x + 32;
      u8g.drawStr(x * 7, y * 10 + 10, s);
    }
  }
}

void u8g_ascii_2() {
  char s[2] = " ";
  uint8_t x, y;
  u8g.drawStr( 0, 0, "ASCII page 2");
  for ( y = 0; y < 6; y++ ) {
    for ( x = 0; x < 16; x++ ) {
      s[0] = y * 16 + x + 160;
      u8g.drawStr(x * 7, y * 10 + 10, s);
    }
  }
}


uint8_t draw_state = 0;

void draw(void) {
  u8g_prepare();
  switch (draw_state >> 3) {
    case 0: u8g_box_frame(draw_state & 7); break;
    case 1: u8g_disc_circle(draw_state & 7); break;
    case 2: u8g_r_frame(draw_state & 7); break;
    case 3: u8g_string(draw_state & 7); break;
    case 4: u8g_line(draw_state & 7); break;
    case 5: u8g_ascii_1(); break;
    case 6: u8g_ascii_2(); break;
  }
}

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

  u8g.setColorIndex(1);         // pixel on

  //u8g.setContrast(0x30);

  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

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

  // increase the state
  draw_state++;
  if ( draw_state >= 7 * 8 )
    draw_state = 0;

  // rebuild the picture after some delay
  delay(150);

}

下载成功后,观察OLED显示,无乱码等情况即可。

返回SD库语法手册