butOn 发表于 2015-3-29 11:51:05

驱动OLED,没用第三方库,处女作

本帖最后由 butOn 于 2015-3-29 12:01 编辑

第一次写驱动!


用MCU驱动OLED屏幕,

我没有用网上现成的程序,没有用第三方库,
而是自己根据厂家的数据手册,分析初始化流程以及串行通讯时序,写出一套自己的驱动。好像程序员都喜欢“重新制造轮子”。

由于Linux没有图形取模软件,所以只好自己一个格子一个格子弄点阵……那可是128*64=8192个啊!弄完还要一个一个转成16进制的,一共128*8=1024个……我的妈呀,死一大片脑细胞不说,打完手都要断啦……

所以只做了两个字母“b” “u”
自行脑补后面的t0n吧。

bitmap做完,显存总是没法写入,后来才发现二维数组定义后要立即赋值,还有串口不知道哪抽了,烧个程序平均要插个五六遍才有反应。

下面是源码:
/*
* OLED_Arduino.ino
*
* Created by Jack, March 28, 2015.
*
* FUNCTION: This is a driver program to display some pics or words(传说中的中式英语)
*                  on the OLED screen.
*
* OLED INFORMATION:
*             96`    128*64,
*      driver chip: SSD1306,所罗门的驱动IC,这款IC还有用于驱动E-ink电子纸!
*
* INTERFACE: 4-wire SPI.
*
*/
#define SDIN 9
#define SCLK 10
#define DC 11
#define RST 13
#define CS 12
void OLED_INIT ();
void w_cmd (uint8_t c);
void w_data (uint8_t d);
void refresh_GRAM ();
void draw_point (uint8_t x, uint8_t y, uint8_t t);
void draw_lineX (uint8_t x1, uint8_t y1, uint8_t L);
void clear_display ();
uint8_t OLED_GRAM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xF8, 0x04, 0x02, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x04, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1F, 0x20, 0x40, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x40, 0x20, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,


0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0xFF, 0x04, 0x02, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0xFC,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x7F, 0x20, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x20, 0x1F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1F, 0x20, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x20, 0x1F, 0x20, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,


};

void setup (){
      OLED_INIT ();
      }

void loop (){
//clear_display ();


      refresh_GRAM ();
delay (100);
while (1){}//不停的游荡~

      }

      void OLED_INIT (){
      pinMode (SDIN, OUTPUT);
      pinMode (SCLK, OUTPUT);
      pinMode (DC, OUTPUT);
      pinMode (RST, OUTPUT);
      pinMode (CS, OUTPUT);
// You can findInitial Sequence on the datasheet
      digitalWrite (RST, HIGH);// Set RES# as High
      delay (1);                           // 3us Delay Recommended
      digitalWrite (RST, LOW);// Reset
      delay(10);
      digitalWrite (RST, HIGH);
      w_cmd (0xAE);// 1.Set Display Off
      w_cmd (0xD5); // 2.Set display Clock Divide Ratio/Oscillator Frequency
      w_cmd (0x80);
      w_cmd (0xA8); // 3.Set Multiplex Ratio
      w_cmd (0x3F);
      w_cmd (0xD3); // 4.Set Display Offset
      w_cmd (0x00);
      w_cmd (0x40); // 5.Set Display Start Line
      // 6.Set Segment Re-Map   0xA1   ***
// 7.Set COM Output Scan Direction   0xC8      ***
// 8.Set COM Pins Hardware Configurartion   0xDA, 0x12      ***
// 9.Set Contrast Control   0x81, 0x66      **^
// 10.Set Pre-Charge Period   0xD9, 0xF1       ***
// 11.Set VCOMH Deselect Level      0xDB, 0x30      **^
// 12.Set Entire Display On/Off      0xA4      ***
// 13.Set Normal/Inverse Display   0xA6    ***
// Clear Screen
//                  w_cmd (0x8D);// 14.Set Charge Pump
//                  w_cmd (0x14);
      // 15.Set Display On    0xAF   ***
      w_cmd (0x20);
      w_cmd (0x10); //0x00
      w_cmd (0xA1); // 6.Set Segment Re-Map
      w_cmd (0xC8); // 7.Set COM Output Scan Direction
      w_cmd (0xDA); // 8.Set COM Pins Hardware Configurartion
      w_cmd (0x12);
      w_cmd (0x81); // 9.Set Contrast Control   0x66
      w_cmd (0x66); // 0xCF
      w_cmd (0xD9); // 10.Set Pre-Charge Period
      w_cmd (0xF1);
      w_cmd (0xDB); // 11.Set VCOMH Deselect Level0x30
      w_cmd (0x30); // 0x40
      w_cmd (0xA4); // 12.Set Entire Display On/Off
      w_cmd (0xA6); // 13.Set Normal/Inverse Display
      w_cmd (0x8D);// 14.Set Charge Pump
      w_cmd (0x14);
      w_cmd (0xAF); // 15.Set Display On
      }
      void w_cmd (uint8_t c){
      digitalWrite (CS, HIGH);
      digitalWrite (DC, LOW); // Command
      digitalWrite (CS, LOW);
      shiftOut (SDIN, SCLK, MSBFIRST, c);
      digitalWrite (CS, HIGH);
      }
      void w_data (uint8_t d){
      digitalWrite (CS, HIGH);
      digitalWrite (DC, HIGH); // Data
      digitalWrite (CS, LOW);
      shiftOut (SDIN, SCLK, MSBFIRST, d);
      digitalWrite (CS, HIGH);
      }
      void refresh_GRAM (){
      uint8_t x, y; // x = SEG    y = PAGE
      for (y = 0; y < 8; y++){
      w_cmd (0xB0 + y);// Set Page Number
      w_cmd (0x00);//Set Low(4bit)
      w_cmd (0x10);//Set High (4bit)
      for (x = 0; x<128; x++)
      w_data (OLED_GRAM);
      }
      }
      void clear_display (){
      memset (OLED_GRAM, 0x00, sizeof (OLED_GRAM));
      }
我会在Github上更新,如果感兴趣可以移步http://github.com/but0n

suoma 发表于 2015-3-29 16:42:10

    显示结果是什么?

butOn 发表于 2015-3-29 22:21:07

suoma 发表于 2015-3-29 16:42 static/image/common/back.gif
显示结果是什么?

发不了图。。。。。。。

布列松 发表于 2015-4-7 22:31:38


能够显示了,但是不是这样

butOn 发表于 2015-4-8 00:32:10

布列松 发表于 2015-4-7 22:31 static/image/common/back.gif
能够显示了,但是不是这样

对就是这样
代码我更新了,移植了字库,视频:http://v.youku.com/v_show/id_XOTI4NTExMTky.html?xhttp://v.youku.com/v_show/id_XOTI4NTExMTky.html?x

布列松 发表于 2015-4-8 09:19:15

butOn 发表于 2015-4-8 00:32 static/image/common/back.gif
对就是这样
代码我更新了,移植了字库,视频:http://v.youku.com/v_show/id_XOTI4NTExMTky.html?x

代码在那里?

butOn 发表于 2015-4-8 10:43:45

布列松 发表于 2015-4-8 09:19 static/image/common/back.gif
代码在那里?

这是3.0版的,我会在我的Github更新
/*
* OLED_Arduino-3.0.ino
*
* Created by Jack, April 7, 2015.
*
* FUNCTION: This is a driver program to display some pics or words
*                  on the OLED screen.
*
* OLED INFORMATION:
*             96`    128*64,
*      driver chip: SSD1306,
*
* INTERFACE: 4-wire SPI.
*
*/
#define SDIN 9//D1
#define SCLK 10//D0
#define DC 11
#define RST 13
#define CS 12

typedef struct insert {
uint8_t page;
uint8_t start;
}insert;
insert in = {0, 0};
void OLED_INIT ();
void w_cmd (uint8_t c);
void w_data (uint8_t d);
void clear_display ();
void oprint (uint8_t w, insert *in);
void oled_printf (char text[]);
void prtpage (char c1[], char c2[], char c3[], char c4[]);

//ASCII-buff


uint8_t buff_0 = {
0xF0, 0xF8, 0x0C, 0xC4, 0x0C, 0xF8, 0xF0, 0x00,
0x03, 0x07, 0x0C, 0x08, 0x0C, 0x07, 0x03, 0x00
};
uint8_t buff_1 = {
0x00, 0x10, 0x18, 0xFC, 0xFC, 0x00, 0x00, 0x00,
0x00, 0x08, 0x08, 0x0F, 0x0F, 0x08, 0x08, 0x00
};
uint8_t buff_2 = {
0x08, 0x0C, 0x84, 0xC4, 0x64, 0x3C, 0x18, 0x00,
0x0E, 0x0F, 0x09, 0x08, 0x08, 0x0C, 0x0C, 0x00
};
uint8_t buff_3 = {
0x08, 0x0C, 0x44, 0x44, 0x44, 0xFC, 0xB8, 0x00,
0x04, 0x0C, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_4 = {
0xC0, 0xE0, 0xB0, 0x98, 0xFC, 0xFC, 0x80, 0x00,
0x00, 0x00, 0x00, 0x08, 0x0F, 0x0F, 0x08, 0x00
};
uint8_t buff_5 = {
0x7C, 0x7C, 0x44, 0x44, 0x44, 0xC4, 0x84, 0x00,
0x04, 0x0C, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_6 = {
0xF0, 0xF8, 0x4C, 0x44, 0x44, 0xC0, 0x80, 0x00,
0x07, 0x07, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_7 = {
0x0C, 0x0C, 0x04, 0x84, 0xC4, 0x7C, 0x3C, 0x00,
0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00
};
uint8_t buff_8 = {
0xB8, 0xFC, 0x44, 0x44, 0x44, 0xFC, 0xB8, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_9 = {
0x38, 0x7C, 0x44, 0x44, 0x44, 0xFC, 0xF8, 0x00,
0x00, 0x08, 0x08, 0x08, 0x0C, 0x07, 0x03, 0x00
};

uint8_t buff_a = {
0x00, 0xA0, 0xA0, 0xA0, 0xE0, 0xC0, 0x00, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x07, 0x0F, 0x08, 0x00
};
uint8_t buff_b = {
0x04, 0xFC, 0xFC, 0x20, 0x60, 0xC0, 0x80, 0x00,
0x00, 0x0F, 0x0F, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_c = {
0xC0, 0xE0, 0x20, 0x20, 0x20, 0x60, 0x40, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0C, 0x04, 0x00
};
uint8_t buff_d = {
0x80, 0xC0, 0x60, 0x24, 0xFC, 0xFC, 0x00, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x07, 0x0F, 0x08, 0x00
};
uint8_t buff_e = {
0xC0, 0xE0, 0xA0, 0xA0, 0xA0, 0xE0, 0xC0, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0C, 0x04, 0x00
};
uint8_t buff_f = {
0x40, 0xF8, 0xFC, 0x44, 0x0C, 0x18, 0x00, 0x00,
0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00
};
uint8_t buff_g = {
0xC0, 0xE0, 0x20, 0x20, 0xC0, 0xE0, 0x20, 0x00,
0x27, 0x6F, 0x48, 0x48, 0x7F, 0x3F, 0x00, 0x00
};
uint8_t buff_h = {
0x04, 0xFC, 0xFC, 0x40, 0x20, 0xE0, 0xC0, 0x00,
0x08, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x00
};
uint8_t buff_i = {
0x00, 0x00, 0x20, 0xEC, 0xEC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00
};
uint8_t buff_j = {
0x00, 0x00, 0x00, 0x00, 0x20, 0xEC, 0xEC, 0x00,
0x00, 0x30, 0x70, 0x40, 0x40, 0x7F, 0x3F, 0x00
};
uint8_t buff_k = {
0x04, 0xFC, 0xFC, 0x80, 0xC0, 0x60, 0x20, 0x00,
0x08, 0x0F, 0x0F, 0x01, 0x03, 0x0E, 0x0C, 0x00
};
uint8_t buff_l = {
0x00, 0x00, 0x04, 0xFC, 0xFC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00
};
uint8_t buff_m = {
0xE0, 0xE0, 0x60, 0xC0, 0x60, 0xE0, 0xC0, 0x00,
0x0F, 0x0F, 0x00, 0x07, 0x00, 0x0F, 0x0F, 0x00
};
uint8_t buff_n = {
0x20, 0xE0, 0xC0, 0x20, 0x20, 0xE0, 0xC0, 0x00,
0x00, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x00
};
uint8_t buff_o = {
0xC0, 0xE0, 0x20, 0x20, 0x20, 0xE0, 0xC0, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_p = {
0x20, 0xE0, 0xC0, 0x20, 0x20, 0xE0, 0xC0, 0x00,
0x40, 0x7F, 0x7F, 0x48, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_q = {
0xC0, 0xE0, 0x20, 0x20, 0xC0, 0xE0, 0x20, 0x00,
0x07, 0x0F, 0x08, 0x48, 0x7F, 0x7F, 0x40, 0x00
};
uint8_t buff_r = {
0x20, 0xE0, 0xC0, 0x60, 0x20, 0xE0, 0xC0, 0x00,
0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00
};
uint8_t buff_s = {
0x40, 0xE0, 0xA0, 0x20, 0x20, 0x60, 0x40, 0x00,
0x04, 0x0C, 0x09, 0x09, 0x0B, 0x0E, 0x04, 0x00
};
uint8_t buff_t = {
0x20, 0x20, 0xF8, 0xFC, 0x20, 0x20, 0x00, 0x00,
0x00, 0x00, 0x07, 0x0F, 0x08, 0x0C, 0x04, 0x00
};
uint8_t buff_u = {
0xE0, 0xE0, 0x00, 0x00, 0xE0, 0xE0, 0x00, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x07, 0x0F, 0x08, 0x00
};
uint8_t buff_v = {
0x00, 0xE0, 0xE0, 0x00, 0x00, 0xE0, 0xE0, 0x00,
0x00, 0x03, 0x07, 0x0C, 0x0C, 0x07, 0x03, 0x00
};
uint8_t buff_w = {
0xE0, 0xE0, 0x00, 0x80, 0x00, 0xE0, 0xE0, 0x00,
0x07, 0x0F, 0x0C, 0x07, 0x0C, 0x0F, 0x07, 0x00
};
uint8_t buff_x = {
0x20, 0x60, 0xC0, 0x80, 0xC0, 0x60, 0x20, 0x00,
0x08, 0x0C, 0x07, 0x03, 0x07, 0x0C, 0x08, 0x00
};
uint8_t buff_y = {
0xE0, 0xE0, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0x00,
0x47, 0x4F, 0x48, 0x48, 0x68, 0x3F, 0x1F, 0x00
};
uint8_t buff_z = {
0x60, 0x60, 0x20, 0xA0, 0xE0, 0x60, 0x20, 0x00,
0x0C, 0x0E, 0x0B, 0x09, 0x08, 0x0C, 0x0C, 0x00
};

    void setup (){
      
      OLED_INIT ();
      clear_display ();//一切好像总是等待那里,指向着同样的事情...
    }

    void loop (){
prtpage ("", "      but0n", "", "");
prtpage ("", "    knowledge", "       is", "   power");
prtpage ("       qq", "", "    32700567", "");
//while (1){}
    }

          void OLED_INIT (){
                  pinMode (SDIN, OUTPUT);
                  pinMode (SCLK, OUTPUT);   
                  pinMode (DC, OUTPUT);
                  pinMode (RST, OUTPUT);
                  pinMode (CS, OUTPUT);
// You can findInitial Sequence on the datasheet
                  
                  digitalWrite (RST, HIGH);// Set RES# as High
                  delay (1);                           // 3us Delay Recommended
                  
                  digitalWrite (RST, LOW);// Reset
                  delay(10);
                  
                  digitalWrite (RST, HIGH);
                  
                  w_cmd (0xAE);// 1.Set Display Off
                  
                  w_cmd (0xD5); // 2.Set display Clock Divide Ratio/Oscillator Frequency
                  w_cmd (0x80);
                  
                  w_cmd (0xA8); // 3.Set Multiplex Ratio
                  w_cmd (0x3F);
                  
                  w_cmd (0xD3); // 4.Set Display Offset
                  w_cmd (0x00);
                  
                  w_cmd (0x40); // 5.Set Display Start Line
                  
// 6.Set Segment Re-Map   0xA1   ***
// 7.Set COM Output Scan Direction   0xC8      ***
// 8.Set COM Pins Hardware Configurartion   0xDA, 0x12      ***
// 9.Set Contrast Control   0x81, 0x66      **^
// 10.Set Pre-Charge Period   0xD9, 0xF1       ***
// 11.Set VCOMH Deselect Level      0xDB, 0x30      **^
// 12.Set Entire Display On/Off      0xA4      ***
// 13.Set Normal/Inverse Display   0xA6    ***
// Clear Screen

//                  w_cmd (0x8D);// 14.Set Charge Pump
//                  w_cmd (0x14);
                  
// 15.Set Display On    0xAF   ***
                  w_cmd (0x20);
                  w_cmd (0x10); //0x00
                  
                  w_cmd (0xA1); // 6.Set Segment Re-Map
                  
                  w_cmd (0xC8); // 7.Set COM Output Scan Direction
                  
                  w_cmd (0xDA); // 8.Set COM Pins Hardware Configurartion
                  w_cmd (0x12);
                  
                  w_cmd (0x81); // 9.Set Contrast Control   0x66
                  w_cmd (0x66); // 0xCF
                  
                  w_cmd (0xD9); // 10.Set Pre-Charge Period
                  w_cmd (0xF1);
                  
                  w_cmd (0xDB); // 11.Set VCOMH Deselect Level0x30
                  w_cmd (0x30); // 0x40
                  
                  w_cmd (0xA4); // 12.Set Entire Display On/Off
                  
                  w_cmd (0xA6); // 13.Set Normal/Inverse Display
                  
                  w_cmd (0x8D);// 14.Set Charge Pump
                  w_cmd (0x14);
                  
                  w_cmd (0xAF); // 15.Set Display On
                  
                  
          }
            void w_cmd (uint8_t c){
                digitalWrite (CS, HIGH);
                digitalWrite (DC, LOW); // Command
                digitalWrite (CS, LOW);
                  shiftOut (SDIN, SCLK, MSBFIRST, c);
                digitalWrite (CS, HIGH);
            }
            
            void w_data (uint8_t d){
                digitalWrite (CS, HIGH);
                digitalWrite (DC, HIGH); // Data
                digitalWrite (CS, LOW);
                  shiftOut (SDIN, SCLK, MSBFIRST, d);
                digitalWrite (CS, HIGH);
            }
                  
                  void clear_display (){
                            w_cmd (0xAE);
                            in = {0, 0};
                            uint8_t x, y; // x = SEG    y = PAGE
                            for (y = 0; y < 8; y++){
                                    w_cmd (0xB0 + y);// Set Page Number
                                    w_cmd (0x00);//Set Low(4bit)
                                    w_cmd (0x10);//Set High (4bit)
                                    for (x = 0; x<128; x++)
                                          w_data (0x00);
                           
                            }
w_cmd (0xAF);
                  }
                  
                  
                  
         
    void oprint (uint8_t w, struct insert *in){
                  if (in->page > 7 ){
                        *in = {0, 0};
                        delay (500);
                        clear_display ();
                  }
                uint8_t t, n;
                for (t = 0; t < 2; t++){
                            w_cmd (0xB0 + in->page + t);
                            w_cmd (in->start & 0x0F);
                            w_cmd (((in->start >> 4) & 0x0F) | 0x10);
                            for (n = 0; n < 8; n++){
                                          switch (w){
                                             
                                              case '0':
                                              w_data (buff_0);
                                              break;
                                              case '1':
                                              w_data (buff_1);
                                              break;
                                              case '2':
                                              w_data (buff_2);
                                              break;
                                              case '3':
                                              w_data (buff_3);
                                              break;
                                              case '4':
                                              w_data (buff_4);
                                              break;
                                              case '5':
                                              w_data (buff_5);
                                              break;
                                              case '6':
                                              w_data (buff_6);
                                              break;
                                              case '7':
                                              w_data (buff_7);
                                              break;
                                              case '8':
                                              w_data (buff_8);
                                              break;
                                              case '9':
                                              w_data (buff_9);
                                              break;
                                              case 'a':
                                              w_data (buff_a);
                                              break;
                                              case 'b':
                                              w_data (buff_b);
                                              break;
                                              case 'c':
                                              w_data (buff_c);
                                              break;
                                              case 'd':
                                              w_data (buff_d);
                                              break;
                                              case 'e':
                                              w_data (buff_e);
                                              break;
                                              case 'f':
                                              w_data (buff_f);
                                              break;
                                              case 'g':
                                              w_data (buff_g);
                                              break;
                                              case 'h':
                                              w_data (buff_h);
                                              break;
                                              case 'i':
                                              w_data (buff_i);
                                              break;
                                              case 'j':
                                              w_data (buff_j);
                                              break;
                                              case 'k':
                                              w_data (buff_k);
                                              break;
                                              case 'l':
                                              w_data (buff_l);
                                              break;
                                              case 'm':
                                              w_data (buff_m);
                                              break;
                                              case 'n':
                                              w_data (buff_n);
                                              break;
                                              case 'o':
                                              w_data (buff_o);
                                              break;
                                              case 'p':
                                              w_data (buff_p);
                                              break;
                                              case 'q':
                                              w_data (buff_q);
                                              break;
                                              case 'r':
                                              w_data (buff_r);
                                              break;
                                              case 's':
                                              w_data (buff_s);
                                              break;
                                              case 't':
                                              w_data (buff_t);
                                              break;
                                              case 'u':
                                              w_data (buff_u);
                                              break;
                                              case 'v':
                                              w_data (buff_v);
                                              break;
                                              case 'w':
                                              w_data (buff_w);
                                              break;
                                              case 'x':
                                              w_data (buff_x);
                                              break;
                                              case 'y':
                                              w_data (buff_y);
                                              break;
                                              case 'z':
                                              w_data (buff_z);
                                              break;
                                              default:
                                              case ' ':
                                              w_data (0x00);
                                              break;                                       


                                          }//switch
                            }//for
                  }
                  in->start += 8;
                  if (in->start >= 128){
                        in->start = 0;
                        in->page += 2;
                  }

   
    }

void oled_printf(char text[]) {
int c, q;
for (c = 0; text != NULL; c++)
oprint (text, &in);
for (q = 0; q < (16 - c); q++)
oprint (' ', &in);
}
void prtpage (char c1[], char c2[], char c3[], char c4[]) {
      w_cmd (0xAE);
      oled_printf (c1);
      oled_printf (c2);
      oled_printf (c3);
      oled_printf (c4);
      w_cmd (0xAF);
      delay (3000);
      clear_display ();
      delay (800);
}
                                          

butOn 发表于 2015-4-8 10:50:42

布列松 发表于 2015-4-8 09:19 static/image/common/back.gif
代码在那里?

能不能帮我把那个视频挂上,我不会弄

源码地址:http://github.com/but0n/OLED_Arduino

code-AR 发表于 2015-4-8 12:05:56

butOn 发表于 2015-4-8 10:50 static/image/common/back.gif
能不能帮我把那个视频挂上,我不会弄

源码地址:http://github.com/but0n/OLED_Arduino

http://v.youku.com/v_show/id_XOTI4NTExMTky.html
帮你弄视频,自己写的很不错,最好能够封装起库来,可以让人使用方便嘛!

butOn 发表于 2015-4-8 12:36:43

code-AR 发表于 2015-4-8 12:05 static/image/common/back.gif
帮你弄视频,自己写的很不错,最好能够封装起库来,可以让人使用方便嘛!

嗯,谢谢!

布列松 发表于 2015-4-9 11:35:26

butOn 发表于 2015-4-8 10:43 static/image/common/back.gif
这是3.0版的,我会在我的Github更新
/*
* OLED_Arduino-3.0.ino


编译有误 上传不了

butOn 发表于 2015-4-9 12:35:16

布列松 发表于 2015-4-9 11:35 static/image/common/back.gif
编译有误 上传不了

错误信息是什么?

布列松 发表于 2015-4-10 00:02:36

butOn 发表于 2015-4-9 12:35 static/image/common/back.gif
错误信息是什么?

butOn 发表于 2015-4-10 13:20:54

布列松 发表于 2015-4-10 00:02 static/image/common/back.gif




/*
* OLED_Arduino-3.0.ino
*
* Created by Jack, April 5, 2015.
*
* FUNCTION: This is a driver program to display some pics or words
*                  on the OLED screen.
*
* OLED INFORMATION:
*             96`    128*64,
*      driver chip: SSD1306,
*
* INTERFACE: 4-wire SPI.
*
*/
#define SDIN 9//D1
#define SCLK 10//D0
#define DC 11
#define RST 13
#define CS 12

typedef struct insert {
uint8_t page;
uint8_t start;
}insert;
insert in = {0, 0};
void OLED_INIT ();
void w_cmd (uint8_t c);
void w_data (uint8_t d);
void clear_display ();
void oprint (uint8_t w, insert *in);
void oled_printf (char text[]);
void prtpage (char c1[], char c2[], char c3[], char c4[]);

//ASCII-buff


uint8_t buff_0 = {
0xF0, 0xF8, 0x0C, 0xC4, 0x0C, 0xF8, 0xF0, 0x00,
0x03, 0x07, 0x0C, 0x08, 0x0C, 0x07, 0x03, 0x00
};
uint8_t buff_1 = {
0x00, 0x10, 0x18, 0xFC, 0xFC, 0x00, 0x00, 0x00,
0x00, 0x08, 0x08, 0x0F, 0x0F, 0x08, 0x08, 0x00
};
uint8_t buff_2 = {
0x08, 0x0C, 0x84, 0xC4, 0x64, 0x3C, 0x18, 0x00,
0x0E, 0x0F, 0x09, 0x08, 0x08, 0x0C, 0x0C, 0x00
};
uint8_t buff_3 = {
0x08, 0x0C, 0x44, 0x44, 0x44, 0xFC, 0xB8, 0x00,
0x04, 0x0C, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_4 = {
0xC0, 0xE0, 0xB0, 0x98, 0xFC, 0xFC, 0x80, 0x00,
0x00, 0x00, 0x00, 0x08, 0x0F, 0x0F, 0x08, 0x00
};
uint8_t buff_5 = {
0x7C, 0x7C, 0x44, 0x44, 0x44, 0xC4, 0x84, 0x00,
0x04, 0x0C, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_6 = {
0xF0, 0xF8, 0x4C, 0x44, 0x44, 0xC0, 0x80, 0x00,
0x07, 0x07, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_7 = {
0x0C, 0x0C, 0x04, 0x84, 0xC4, 0x7C, 0x3C, 0x00,
0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00
};
uint8_t buff_8 = {
0xB8, 0xFC, 0x44, 0x44, 0x44, 0xFC, 0xB8, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_9 = {
0x38, 0x7C, 0x44, 0x44, 0x44, 0xFC, 0xF8, 0x00,
0x00, 0x08, 0x08, 0x08, 0x0C, 0x07, 0x03, 0x00
};

uint8_t buff_a = {
0x00, 0xA0, 0xA0, 0xA0, 0xE0, 0xC0, 0x00, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x07, 0x0F, 0x08, 0x00
};
uint8_t buff_b = {
0x04, 0xFC, 0xFC, 0x20, 0x60, 0xC0, 0x80, 0x00,
0x00, 0x0F, 0x0F, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_c = {
0xC0, 0xE0, 0x20, 0x20, 0x20, 0x60, 0x40, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0C, 0x04, 0x00
};
uint8_t buff_d = {
0x80, 0xC0, 0x60, 0x24, 0xFC, 0xFC, 0x00, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x07, 0x0F, 0x08, 0x00
};
uint8_t buff_e = {
0xC0, 0xE0, 0xA0, 0xA0, 0xA0, 0xE0, 0xC0, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0C, 0x04, 0x00
};
uint8_t buff_f = {
0x40, 0xF8, 0xFC, 0x44, 0x0C, 0x18, 0x00, 0x00,
0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00
};
uint8_t buff_g = {
0xC0, 0xE0, 0x20, 0x20, 0xC0, 0xE0, 0x20, 0x00,
0x27, 0x6F, 0x48, 0x48, 0x7F, 0x3F, 0x00, 0x00
};
uint8_t buff_h = {
0x04, 0xFC, 0xFC, 0x40, 0x20, 0xE0, 0xC0, 0x00,
0x08, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x00
};
uint8_t buff_i = {
0x00, 0x00, 0x20, 0xEC, 0xEC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00
};
uint8_t buff_j = {
0x00, 0x00, 0x00, 0x00, 0x20, 0xEC, 0xEC, 0x00,
0x00, 0x30, 0x70, 0x40, 0x40, 0x7F, 0x3F, 0x00
};
uint8_t buff_k = {
0x04, 0xFC, 0xFC, 0x80, 0xC0, 0x60, 0x20, 0x00,
0x08, 0x0F, 0x0F, 0x01, 0x03, 0x0E, 0x0C, 0x00
};
uint8_t buff_l = {
0x00, 0x00, 0x04, 0xFC, 0xFC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00
};
uint8_t buff_m = {
0xE0, 0xE0, 0x60, 0xC0, 0x60, 0xE0, 0xC0, 0x00,
0x0F, 0x0F, 0x00, 0x07, 0x00, 0x0F, 0x0F, 0x00
};
uint8_t buff_n = {
0x20, 0xE0, 0xC0, 0x20, 0x20, 0xE0, 0xC0, 0x00,
0x00, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x00
};
uint8_t buff_o = {
0xC0, 0xE0, 0x20, 0x20, 0x20, 0xE0, 0xC0, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_p = {
0x20, 0xE0, 0xC0, 0x20, 0x20, 0xE0, 0xC0, 0x00,
0x40, 0x7F, 0x7F, 0x48, 0x08, 0x0F, 0x07, 0x00
};
uint8_t buff_q = {
0xC0, 0xE0, 0x20, 0x20, 0xC0, 0xE0, 0x20, 0x00,
0x07, 0x0F, 0x08, 0x48, 0x7F, 0x7F, 0x40, 0x00
};
uint8_t buff_r = {
0x20, 0xE0, 0xC0, 0x60, 0x20, 0xE0, 0xC0, 0x00,
0x08, 0x0F, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00
};
uint8_t buff_s = {
0x40, 0xE0, 0xA0, 0x20, 0x20, 0x60, 0x40, 0x00,
0x04, 0x0C, 0x09, 0x09, 0x0B, 0x0E, 0x04, 0x00
};
uint8_t buff_t = {
0x20, 0x20, 0xF8, 0xFC, 0x20, 0x20, 0x00, 0x00,
0x00, 0x00, 0x07, 0x0F, 0x08, 0x0C, 0x04, 0x00
};
uint8_t buff_u = {
0xE0, 0xE0, 0x00, 0x00, 0xE0, 0xE0, 0x00, 0x00,
0x07, 0x0F, 0x08, 0x08, 0x07, 0x0F, 0x08, 0x00
};
uint8_t buff_v = {
0x00, 0xE0, 0xE0, 0x00, 0x00, 0xE0, 0xE0, 0x00,
0x00, 0x03, 0x07, 0x0C, 0x0C, 0x07, 0x03, 0x00
};
uint8_t buff_w = {
0xE0, 0xE0, 0x00, 0x80, 0x00, 0xE0, 0xE0, 0x00,
0x07, 0x0F, 0x0C, 0x07, 0x0C, 0x0F, 0x07, 0x00
};
uint8_t buff_x = {
0x20, 0x60, 0xC0, 0x80, 0xC0, 0x60, 0x20, 0x00,
0x08, 0x0C, 0x07, 0x03, 0x07, 0x0C, 0x08, 0x00
};
uint8_t buff_y = {
0xE0, 0xE0, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0x00,
0x47, 0x4F, 0x48, 0x48, 0x68, 0x3F, 0x1F, 0x00
};
uint8_t buff_z = {
0x60, 0x60, 0x20, 0xA0, 0xE0, 0x60, 0x20, 0x00,
0x0C, 0x0E, 0x0B, 0x09, 0x08, 0x0C, 0x0C, 0x00
};

    void setup (){
      
      OLED_INIT ();
      clear_display ();//一切好像总是等待那里,指向着同样的事情...
    }

    void loop (){
prtpage ("", "      but0n", "", "");
prtpage ("", "    knowledge", "       is", "   power");
prtpage ("       qq", "", "    32700567", "");
//while (1){}
    }

          void OLED_INIT (){
                  pinMode (SDIN, OUTPUT);
                  pinMode (SCLK, OUTPUT);   
                  pinMode (DC, OUTPUT);
                  pinMode (RST, OUTPUT);
                  pinMode (CS, OUTPUT);
// You can findInitial Sequence on the datasheet
                  
                  digitalWrite (RST, HIGH);// Set RES# as High
                  delay (1);                           // 3us Delay Recommended
                  
                  digitalWrite (RST, LOW);// Reset
                  delay(10);
                  
                  digitalWrite (RST, HIGH);
                  
                  w_cmd (0xAE);// 1.Set Display Off
                  
                  w_cmd (0xD5); // 2.Set display Clock Divide Ratio/Oscillator Frequency
                  w_cmd (0x80);
                  
                  w_cmd (0xA8); // 3.Set Multiplex Ratio
                  w_cmd (0x3F);
                  
                  w_cmd (0xD3); // 4.Set Display Offset
                  w_cmd (0x00);
                  
                  w_cmd (0x40); // 5.Set Display Start Line
                  
// 6.Set Segment Re-Map   0xA1   ***
// 7.Set COM Output Scan Direction   0xC8      ***
// 8.Set COM Pins Hardware Configurartion   0xDA, 0x12      ***
// 9.Set Contrast Control   0x81, 0x66      **^
// 10.Set Pre-Charge Period   0xD9, 0xF1       ***
// 11.Set VCOMH Deselect Level      0xDB, 0x30      **^
// 12.Set Entire Display On/Off      0xA4      ***
// 13.Set Normal/Inverse Display   0xA6    ***
// Clear Screen

//                  w_cmd (0x8D);// 14.Set Charge Pump
//                  w_cmd (0x14);
                  
// 15.Set Display On    0xAF   ***
                  w_cmd (0x20);
                  w_cmd (0x10); //0x00
                  
                  w_cmd (0xA1); // 6.Set Segment Re-Map
                  
                  w_cmd (0xC8); // 7.Set COM Output Scan Direction
                  
                  w_cmd (0xDA); // 8.Set COM Pins Hardware Configurartion
                  w_cmd (0x12);
                  
                  w_cmd (0x81); // 9.Set Contrast Control   0x66
                  w_cmd (0x66); // 0xCF
                  
                  w_cmd (0xD9); // 10.Set Pre-Charge Period
                  w_cmd (0xF1);
                  
                  w_cmd (0xDB); // 11.Set VCOMH Deselect Level0x30
                  w_cmd (0x30); // 0x40
                  
                  w_cmd (0xA4); // 12.Set Entire Display On/Off
                  
                  w_cmd (0xA6); // 13.Set Normal/Inverse Display
                  
                  w_cmd (0x8D);// 14.Set Charge Pump
                  w_cmd (0x14);
                  
                  w_cmd (0xAF); // 15.Set Display On
                  
                  
          }
            void w_cmd (uint8_t c){
                digitalWrite (CS, HIGH);
                digitalWrite (DC, LOW); // Command
                digitalWrite (CS, LOW);
                  shiftOut (SDIN, SCLK, MSBFIRST, c);
                digitalWrite (CS, HIGH);
            }
            
            void w_data (uint8_t d){
                digitalWrite (CS, HIGH);
                digitalWrite (DC, HIGH); // Data
                digitalWrite (CS, LOW);
                  shiftOut (SDIN, SCLK, MSBFIRST, d);
                digitalWrite (CS, HIGH);
            }
                  
                  void clear_display (){
                            w_cmd (0xAE);
                            in = {0, 0};
                            uint8_t x, y; // x = SEG    y = PAGE
                            for (y = 0; y < 8; y++){
                                    w_cmd (0xB0 + y);// Set Page Number
                                    w_cmd (0x00);//Set Low(4bit)
                                    w_cmd (0x10);//Set High (4bit)
                                    for (x = 0; x<128; x++)
                                          w_data (0x00);
                           
                            }
w_cmd (0xAF);
                  }
                  
                  
                  
         
    void oprint (uint8_t w, struct insert *in){
                  if (in->page > 7 ){
                        *in = {0, 0};
                        delay (500);
                        clear_display ();
                  }
                uint8_t t, n;
                for (t = 0; t < 2; t++){
                            w_cmd (0xB0 + in->page + t);
                            w_cmd (in->start & 0x0F);
                            w_cmd (((in->start >> 4) & 0x0F) | 0x10);
                            for (n = 0; n < 8; n++){
                                          switch (w){
                                             
                                              case '0':
                                              w_data (buff_0);
                                              break;
                                              case '1':
                                              w_data (buff_1);
                                              break;
                                              case '2':
                                              w_data (buff_2);
                                              break;
                                              case '3':
                                              w_data (buff_3);
                                              break;
                                              case '4':
                                              w_data (buff_4);
                                              break;
                                              case '5':
                                              w_data (buff_5);
                                              break;
                                              case '6':
                                              w_data (buff_6);
                                              break;
                                              case '7':
                                              w_data (buff_7);
                                              break;
                                              case '8':
                                              w_data (buff_8);
                                              break;
                                              case '9':
                                              w_data (buff_9);
                                              break;
                                              case 'a':
                                              w_data (buff_a);
                                              break;
                                              case 'b':
                                              w_data (buff_b);
                                              break;
                                              case 'c':
                                              w_data (buff_c);
                                              break;
                                              case 'd':
                                              w_data (buff_d);
                                              break;
                                              case 'e':
                                              w_data (buff_e);
                                              break;
                                              case 'f':
                                              w_data (buff_f);
                                              break;
                                              case 'g':
                                              w_data (buff_g);
                                              break;
                                              case 'h':
                                              w_data (buff_h);
                                              break;
                                              case 'i':
                                              w_data (buff_i);
                                              break;
                                              case 'j':
                                              w_data (buff_j);
                                              break;
                                              case 'k':
                                              w_data (buff_k);
                                              break;
                                              case 'l':
                                              w_data (buff_l);
                                              break;
                                              case 'm':
                                              w_data (buff_m);
                                              break;
                                              case 'n':
                                              w_data (buff_n);
                                              break;
                                              case 'o':
                                              w_data (buff_o);
                                              break;
                                              case 'p':
                                              w_data (buff_p);
                                              break;
                                              case 'q':
                                              w_data (buff_q);
                                              break;
                                              case 'r':
                                              w_data (buff_r);
                                              break;
                                              case 's':
                                              w_data (buff_s);
                                              break;
                                              case 't':
                                              w_data (buff_t);
                                              break;
                                              case 'u':
                                              w_data (buff_u);
                                              break;
                                              case 'v':
                                              w_data (buff_v);
                                              break;
                                              case 'w':
                                              w_data (buff_w);
                                              break;
                                              case 'x':
                                              w_data (buff_x);
                                              break;
                                              case 'y':
                                              w_data (buff_y);
                                              break;
                                              case 'z':
                                              w_data (buff_z);
                                              break;
                                              default:
                                              case ' ':
                                              w_data (0x00);
                                              break;                                       


                                          }//switch
                            }//for
                  }
                  in->start += 8;
                  if (in->start >= 128){
                        in->start = 0;
                        in->page += 2;
                  }

   
    }

void oled_printf(char text[]) {
int c, q;
for (c = 0; text != NULL; c++)
oprint (text, &in);
for (q = 0; q < (16 - c); q++)
oprint (' ', &in);
}
void prtpage (char c1[], char c2[], char c3[], char c4[]) {
      w_cmd (0xAE);
      oled_printf (c1);
      oled_printf (c2);
      oled_printf (c3);
      oled_printf (c4);
      w_cmd (0xAF);
      delay (3000);
      clear_display ();
      delay (800);
}
                                          

布列松 发表于 2015-4-10 14:02:39

本帖最后由 布列松 于 2015-4-10 15:40 编辑

butOn 发表于 2015-4-10 13:20 static/image/common/back.gif
/*
* OLED_Arduino-3.0.ino
*


ok 成功上传了 正确显示




奇怪的是我的oled 是少了一个脚的,另外我有一个4脚的oled 那个就显示不了的
页: [1] 2
查看完整版本: 驱动OLED,没用第三方库,处女作