OLED SSD1306

TheBeginner
Schreiberling
Avatar
Gender: n/a
Location: Wunsiedel Bayern
Age: 66
Posts: 755
Registered: 06 / 2013
Subject:

OLED SSD1306

 · 
Posted: 02.06.2015 - 21:06  ·  #1
Da ich mir auf die schnelle bei E..y ein kleines OLed SSD1306 mit 128x64 Pixel gekauft habe.
Habe ich mir so einiges aus dem Internet zusammen gesucht, hiermit bin ich zufrieden.
Es geht, es funzt, es macht das was ich sehen will :-)





Code

{ Const Declarations }

Const

OFFSET          : byte = 2;
SCREEN_HOR_SIZE : byte = 128;
SCREEN_VER_SIZE : byte = 64;

SSD1306_SETCONTRAST                      : byte = $81;
SSD1306_DISPLAYALLON_RESUME              : byte = $A4;
SSD1306_DISPLAYALLON                     : byte = $A5;
SSD1306_NORMALDISPLAY                    : byte = $A6;
SSD1306_INVERTDISPLAY                    : byte = $A7;
SSD1306_DISPLAYOFF                       : byte = $AE;
SSD1306_DISPLAYON                        : byte = $AF;

SSD1306_SETDISPLAYOFFSET                 : byte = $D3;
SSD1306_SETCOMPINS                       : byte = $DA;

SSD1306_SETVCOMDETECT                    : byte = $DB;

SSD1306_SETDISPLAYCLOCKDIV               : byte = $D5;
SSD1306_SETPRECHARGE                     : byte = $D9;

SSD1306_SETMULTIPLEX                     : byte = $A8;

SSD1306_SETLOWCOLUMN                     : byte = $00;  // 00
SSD1306_SETHIGHCOLUMN                    : byte = $10;  // 10

SSD1306_SETSTARTLINE                     : byte = $40;  // $40

SSD1306_MEMORYMODE                       : byte = $20;
SSD1306_COLUMNADDR                       : byte = $21;  //
SSD1306_PAGEADDR                         : byte = $22;  //

SSD1306_COMSCANINC                       : byte = $C0;
SSD1306_COMSCANDEC                       : byte = $C8;

SSD1306_SEGREMAP                         : byte = $A1;   //A0

SSD1306_CHARGEPUMP                       : byte = $8D;

SSD1306_EXTERNALVCC                      : byte = $1;
SSD1306_SWITCHCAPVCC                     : byte = $2;

// Scrolling #defines


myarry : array[32..122,1..6] of byte =
   (($00, $00, $00, $00, $00, $00) , // " " $20       0
    ($16, $19, $01, $19, $16, $00) , // !   $21       1     // Ohm Zeichen da ich kein ! brauche
    ($00, $07, $00, $07, $00, $00) , // "   $22       2
    ($14, $7f, $14, $7f, $14, $00) , // #   $23       3
    ($24, $2a, $7f, $2a, $12, $00) , // $   $24       4
    ($23, $13, $08, $64, $62, $00) , // %   $25       5
    ($36, $49, $55, $22, $50, $00) , // &   $26       6
    ($00, $05, $03, $00, $00, $00) , // '   $27       7
    ($00, $1c, $22, $41, $00, $00) , // (   $28       8
    ($00, $41, $22, $1c, $00, $00) , // )   $29       9
    ($14, $08, $3e, $08, $14, $00) , // *   $2A       10
    ($08, $08, $3e, $08, $08, $00) , // +   $2B       11
    ($00, $50, $30, $00, $00, $00) , // ,   $2C       2
    ($08, $08, $08, $08, $08, $00) , // -   $2D       3
    ($00, $60, $60, $00, $00, $00) , // .   $2E       4
    ($20, $10, $08, $04, $02, $00) , // /   $2F       5
    ($3e, $51, $49, $45, $3e, $00) , // 0   $30       6
    ($00, $42, $7f, $40, $00, $00) , // 1   $31       17---
    ($42, $61, $51, $49, $46, $00) , // 2   $32       8
    ($21, $41, $45, $4b, $31, $00) , // 3   $33       9
    ($18, $14, $12, $7f, $10, $00) , // 4   $34       20
    ($27, $45, $45, $45, $39, $00) , // 5   $35       1
    ($3c, $4a, $49, $49, $30, $00) , // 6   $36       2
    ($01, $71, $09, $05, $03, $00) , // 7   $37       3
    ($36, $49, $49, $49, $36, $00) , // 8   $38       4
    ($06, $49, $49, $29, $1e, $00) , // 9   $39       5
    ($00, $36, $36, $00, $00, $00) , // :   $3A       6
    ($00, $56, $36, $00, $00, $00) , // ;   $3B       7
    ($08, $14, $22, $41, $00, $00) , // <   $3C       8
    ($14, $14, $14, $14, $14, $00) , // =   $3D       9
    ($00, $41, $22, $14, $08, $00) , // >   $3E       30
    ($02, $01, $51, $09, $06, $00) , // ?   $3F       31
    ($32, $49, $79, $41, $3e, $00) , // @   $40       32
     ($7e, $11, $11, $11, $7e, $00) , // A   $41      33----
     ($7f, $49, $49, $49, $36, $00) , // B   $42
     ($3e, $41, $41, $41, $22, $00) , // C   $43
     ($7f, $41, $41, $22, $1c, $00) , // D   $44
     ($7f, $49, $49, $49, $41, $00) , // E   $45
     ($7f, $09, $09, $09, $01, $00) , // F   $46
     ($3e, $41, $49, $49, $7a, $00) , // G   $47
     ($7f, $08, $08, $08, $7f, $00) , // H   $48
     ($00, $41, $7f, $41, $00, $00) , // I   $49
     ($20, $40, $41, $3f, $01, $00) , // J   $4A
     ($7f, $08, $14, $22, $41, $00) , // K   $4B
     ($7f, $40, $40, $40, $40, $00) , // L   $4C
     ($7f, $02, $0c, $02, $7f, $00) , // M   $4D
     ($7f, $04, $08, $10, $7f, $00) , // N   $4E
     ($3e, $41, $41, $41, $3e, $00) , // O   $4F
     ($7f, $09, $09, $09, $06, $00) , // P   $50
     ($3e, $41, $51, $21, $5e, $00) , // Q   $51
     ($7f, $09, $19, $29, $46, $00) , // R   $52
     ($46, $49, $49, $49, $31, $00) , // S   $53
     ($01, $01, $7f, $01, $01, $00) , // T   $54
     ($3f, $40, $40, $40, $3f, $00) , // U   $55
     ($1f, $20, $40, $20, $1f, $00) , // V   $56
     ($3f, $40, $38, $40, $3f, $00) , // W   $57
     ($63, $14, $08, $14, $63, $00) , // X   $58
     ($07, $08, $70, $08, $07, $00) , // Y   $59
     ($61, $51, $49, $45, $43, $00) , // Z   $5A
     ($00, $7f, $41, $41, $00, $00) , // [   $5B
     ($02, $04, $08, $10, $20, $00) , // "\" $5C
     ($00, $41, $41, $7f, $00, $00) , // ]   $5D
     ($04, $02, $01, $02, $04, $00) , // ^   $5E
     ($40, $40, $40, $40, $40, $00) , // _   $5F
     ($00, $01, $02, $04, $00, $00) , // `   $60
     ($20, $54, $54, $54, $78, $00) , // a   $61
     ($7f, $48, $44, $44, $38, $00) , // b   $62
     ($38, $44, $44, $44, $20, $00) , // c   $63
     ($38, $44, $44, $48, $7f, $00) , // d   $64
     ($38, $54, $54, $54, $18, $00) , // e   $65
     ($08, $7e, $09, $01, $02, $00) , // f   $66
     ($0c, $52, $52, $52, $3e, $00) , // g   $67
     ($7f, $08, $04, $04, $78, $00) , // h   $68
     ($00, $44, $7d, $40, $00, $00) , // i   $69
     ($20, $40, $44, $3d, $00, $00) , // j   $6A
     ($7f, $10, $28, $44, $00, $00) , // k   $6B
     ($00, $41, $7f, $40, $00, $00) , // l   $6C
     ($7c, $04, $18, $04, $78, $00) , // m   $6D
     ($7c, $08, $04, $04, $78, $00) , // n   $6E
     ($38, $44, $44, $44, $38, $00) , // o   $6F
     ($7c, $14, $14, $14, $08, $00) , // p   $70
     ($08, $14, $14, $18, $7c, $00) , // q   $71
     ($7c, $08, $04, $04, $08, $00) , // r   $72
     ($48, $54, $54, $54, $20, $00) , // s   $73
     ($04, $3f, $44, $40, $20, $00) , // t   $74
     ($3c, $40, $40, $20, $7c, $00) , // u   $75
     ($1c, $20, $40, $20, $1c, $00) , // v   $76
     ($3c, $40, $30, $40, $3c, $00) , // w   $77
     ($44, $28, $10, $28, $44, $00) , // x   $78
     ($0c, $50, $50, $50, $3c, $00) , // y   $79
     ($44, $64, $54, $4c, $44, $00)); // z   $7A







//----------------------------------------------------------



//------------------------------------------------------------------------------
//  Sende Command Oled
//------------------------------------------------------------------------------
Procedure ssd1306_command(cmd1 : byte);
Begin
   I2Cout($3C,$80,cmd1);
end ssd1306_command;

//------------------------------------------------------------------------------
// Sende Data Oled
//------------------------------------------------------------------------------
Procedure ssd1306_data(cmd1 : byte);
Begin
   I2Cout($3C,$40,cmd1);
end ssd1306_data;


//------------------------------------------------------------------------------
// // 0111100 = $3C   I2C Adresse
//------------------------------------------------------------------------------
Procedure Init_OLED;
var x : byte;
Begin
  if I2Cstat($3C) then
// Init sequence for 128x64 OLED module
    ssd1306_command(SSD1306_DISPLAYOFF);                    // 0xAE
    ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV);            // 0xD5
    ssd1306_command($80);                                   // the suggested ratio 0x80
// Multiplex Ratio
    ssd1306_command(SSD1306_SETMULTIPLEX);                  // 0xA8
    ssd1306_command($3F);                                   // alt 3F
// Display Offset
    ssd1306_command(SSD1306_SETDISPLAYOFFSET);              // 0xD3
    ssd1306_command($0);                                    // no offset
//Display Startline
    ssd1306_command(SSD1306_SETSTARTLINE);                  // $40
// Set Charge Pump
    ssd1306_command(SSD1306_CHARGEPUMP);                    // 0x8D
    ssd1306_command($14);
// Re-map
    ssd1306_command(SSD1306_SEGREMAP);                      // A1
// COM Output Scan Direction
    ssd1306_command(SSD1306_COMSCANDEC);                    // C8

// COM Pins Hardware Configuration
    ssd1306_command(SSD1306_SETCOMPINS);                    // 0xDA
    ssd1306_command($12);
// Contrast Control Register
    ssd1306_command(SSD1306_SETCONTRAST);                   // 0x81 Contrast
    ssd1306_command($CF);
// Set Pre Charge Period
    ssd1306_command(SSD1306_SETPRECHARGE);                  // 0xd9
    ssd1306_command($F1);                                   // alt 22 // $22 - External, $F1 - Internal
// Set VCOMH Deselect level
    ssd1306_command(SSD1306_SETVCOMDETECT);                 // 0xDB
    ssd1306_command($40);                                   // $40 $20
// Display OFF
    ssd1306_command(SSD1306_DISPLAYALLON_RESUME);           // 0xA4
    ssd1306_command(SSD1306_NORMALDISPLAY);                 // 0xA6
//--turn on oled panel
    ssd1306_command(SSD1306_DISPLAYON);                     // 0xAF
endif;
end Init_OLED;



//------------------------------------------------------------------------------
// Setze Oled Adresse
//------------------------------------------------------------------------------
Procedure SetAddress(page, lowerAddr,  higherAddr : Byte);
Begin
   ssd1306_command(page);
   ssd1306_command(lowerAddr);
   ssd1306_command(higherAddr);
End SetAddress;

//------------------------------------------------------------------------------
// Function: ClearDisplay()
//------------------------------------------------------------------------------
Procedure ClearDisplay(data : Byte);
var page : Byte;
    col  : Byte;
 begin
    For Page := $B0 To $B8 do
        SetAddress(page, $00 + OFFSET, $10);
        For col := 0 To SCREEN_HOR_SIZE - 1  do
            ssd1306_data(data);
        endfor;
    endfor;
end ClearDisplay;


//------------------------------------------------------------------------------
// Sende Char an Oled
//------------------------------------------------------------------------------
Procedure oledWriteChar1x(letter : char;   page : Byte;    column : Byte;    invert : Boolean);
var
    lx   :  string[3];
    le   :  byte;
    send :  byte;
    i    :  byte;
Begin
   lx := IntToStr(Ord(letter));
   le:= StrToint(lx);

   for i:= 1 to 6 do
     send:= myarry[le,i];
     ssd1306_data(send);
   endfor;
End oledWriteChar1x;
//------------------------------------------------------------------------------
// Prüfe Ausgabe
//------------------------------------------------------------------------------
Procedure oledString(text:String[16];   page:Byte;   column:Byte;   invert:Boolean);
var  i, x : Byte;
Begin
    If ((page > = 0) And (page < 8)) Then
        i := column;
        page := page + $B0;     //Page Adresse B0 = page0   B1 = page1
        x := 0;
        ssd1306_command(page);
        ssd1306_command($00 + (column And $0F));
        ssd1306_command($10 + ((column shr 4)And $0F));
        For x:= 1 to length(text) do
            oledWriteChar1x(text[x], page, i, invert);
        endfor;
    EndIf;
End oledString;





Code

{--------------------------------------------------------------}
{ Main Program }
{$IDATA}
begin
  InitPorts;
  EnableInts;
  mDelay(350);                    //Dieser Delay muss sein
  Init_OLED;                   //Sonst bleibt bei mir das OLed schwarz
  mDelay(250);                    //Dieser Delay muss sein
  clearDisplay($00);
//Schreibe in Zeile 2 ab Pos 0 Hello
  oledString('Hello ' ,2,0,true);




Gruß Frank
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   36   50 · Page-Gen-Time: 0.021521s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI