参考:http://www.geek-workshop.com/thread-10634-1-1.html
OLED有两种协议:SPI和I2C
/*SPI协议*/
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
/*I2C协议*/
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
四线:Vcc,GND,SCL,SDA
协议: IIC
A4–SDA
A5–SCL
首先安装一个外部库u8glib
helloworld.c
#include "U8glib.h"
/*I2C协议*/
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
void setup()
{
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(3); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
// u8g.setFont(u8g_font_unifont);
Serial.begin(9600);
u8g.setFont(u8g_font_6x10);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
}
void loop()
{
u8g.firstPage();
do {
u8g.drawStr(0,0,"hello world!");
} while( u8g.nextPage() );
delay(500);
}
测试程序:
#include "U8glib.h"
#include "utility/u8g.h"
//U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
const uint8_t bitmap_z [] U8G_PROGMEM ={
0x00,0x00,0x01,0x80,0x03,0x80,0x3F,0xBC,0x7F,0xBE,0x73,0xCE,0x63,0xCE,0x63,0xCE,
0x63,0xCE,0x7B,0x9E,0x7F,0xBE,0x3F,0xBC,0x1F,0xB8,0x03,0x80,0x03,0x80,0x01,0x80
};
const uint8_t bitmap_w [] U8G_PROGMEM ={
0x00,0x00,0x07,0xC0,0x3F,0xD8,0x7C,0x7E,0x7F,0xFE,0x00,0x00,0x1C,0x3C,0x1C,0x38,
0x0E,0x70,0x0F,0xF0,0x07,0xA0,0x0B,0xE0,0x3D,0xFC,0x7E,0xFE,0x7C,0x3E,0x30,0x00
};
void draw(void) {
u8g.setColorIndex(1);
u8g.drawBitmapP ( 0 , 0 , 2 , 16 , bitmap_z);
u8g.drawBitmapP ( 17 , 0 , 2 , 16 , bitmap_w );
}
void setup() {
}
void loop() {
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}