Jens Altenburg / Mbed 2 deprecated Fachbuch_Initialisierungen

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers oled.cpp Source File

oled.cpp

00001 /**
00002  * SSD1306xLED - Library for the SSD1306 based OLED/PLED 128x64 displays
00003  *
00004  * @author Neven Boyanov
00005  *
00006  * This is part of the Tinusaur/SSD1306xLED project.
00007  *
00008  * Copyright (c) 2018 Neven Boyanov, The Tinusaur Team. All Rights Reserved.
00009  * Distributed as open source software under MIT License, see LICENSE.txt file.
00010  * Retain in your source code the link http://tinusaur.org to the Tinusaur project.
00011  *
00012  * Source code available at: https://bitbucket.org/tinusaur/ssd1306xled
00013  *
00014  */
00015 
00016 // ============================================================================
00017 // ACKNOWLEDGEMENTS:
00018 // - Some code and ideas initially based on "IIC_wtihout_ACK" 
00019 //   by http://www.14blog.com/archives/1358 (defunct)
00020 // - Init sequence used info from Adafruit_SSD1306.cpp init code.
00021 // ============================================================================
00022 
00023 // ============================================================================
00024 // STATEMENT:
00025 // - gründlich aufgeräumt 
00026 //   29.05.2020 by Jens Altenburg
00027 // ============================================================================
00028 
00029 #include "oled.h"
00030 #include "font6_8.h"
00031 #include "font8_16.h"
00032 
00033 // ----------------------------------------------------------------------------
00034 
00035 void ssd1306_start_command(void);       // Initiate transmission of command
00036 void ssd1306_start_data(void);          // Initiate transmission of data
00037 void ssd1306_data_byte(byte);           // Transmission 1 byte of data
00038 void ssd1306_stop(void);                // Finish transmission
00039 
00040 // ----------------------------------------------------------------------------
00041 
00042 const byte ssd1306_init_sequence[] = {  // Initialization Sequence
00043 
00044     0xAE,           // Set Display ON/OFF - AE=OFF, AF=ON
00045     0xD5, 0xF0,     // Set display clock divide ratio/oscillator frequency, set divide ratio
00046     0xA8, 0x3F,     // Set multiplex ratio (1 to 64) ... (height - 1)
00047     0xD3, 0x00,     // Set display offset. 00 = no offset
00048     0x40 | 0x00,    // Set start line address, at 0.
00049     0x8D, 0x14,     // Charge Pump Setting, 14h = Enable Charge Pump
00050     0x20, 0x00,     // Set Memory Addressing Mode - 00=Horizontal, 01=Vertical, 10=Page, 11=Invalid
00051     0xA0 | 0x01,    // Set Segment Re-map
00052     0xC8,           // Set COM Output Scan Direction
00053     0xDA, 0x12,     // Set COM Pins Hardware Configuration - 128x32:0x02, 128x64:0x12
00054     0x81, 0x3F,     // Set contrast control register
00055     0xD9, 0x22,     // Set pre-charge period (0x22 or 0xF1)
00056     0xDB, 0x20,     // Set Vcomh Deselect Level - 0x00: 0.65 x VCC, 0x20: 0.77 x VCC (RESET), 0x30: 0.83 x VCC
00057     0xA4,           // Entire Display ON (resume) - output RAM to display
00058     0xA6,           // Set Normal/Inverse Display mode. A6=Normal; A7=Inverse
00059     0x2E,           // Deactivate Scroll command
00060     0xAF,           // Set Display ON/OFF - AE=OFF, AF=ON
00061     //
00062     0x22, 0x00, 0x3f,   // Set Page Address (start,end)
00063     0x21, 0x00, 0x7f,   // Set Column Address (start,end)
00064     //
00065     };
00066 
00067 
00068 // ----------------------------------------------------------------------------
00069 
00070 void i2csw_start(void);
00071 void i2csw_stop(void);
00072 void i2csw_byte(byte);
00073 
00074 // ----------------------------------------------------------------------------
00075 
00076 void i2csw_start(void) {
00077     vSDAOutput();
00078     /* SCL ist immer Output */
00079     vSDA_H();       /* SDA setzen */
00080     vSCL_H();       /* SCL setzen */
00081     vSDA_L();       /* SDA löschen */
00082     vSCL_L();     
00083     }
00084 
00085 void i2csw_stop(void) {
00086     vSDA_L();       /* SDA löschen */
00087     vSCL_L();       /* SCL löschen */
00088     vSDA_H();       /* SDA, SCL setzen */
00089     vSCL_H();
00090     vSDAInput();    /* SDA-Datenrichtung -> Input */
00091     }
00092 
00093 void i2csw_byte(byte bData) {
00094     byte i;
00095     for (i = 0; i < 8; i++) {
00096         if ((bData << i) & 0x80) vSDA_H(); 
00097         else                     vSDA_L(); 
00098         vI2CShort();    /* set-up-Zeit */
00099         vSCL_H(); 
00100         vI2CDelay();    /* SCL-high-time */
00101         vSCL_L(); 
00102         vI2CShort();    /* set-up-Zeit */
00103         }
00104     vSDA_L(); 
00105     vI2CShort();        /* set-up-Zeit */
00106     vSCL_H(); 
00107     vI2CDelay();        /* SCL-high-time */
00108     vSCL_L(); 
00109     vI2CShort();        /* set-up-Zeit */
00110     }
00111 
00112 // ============================================================================
00113 
00114 void ssd1306_start_command(void) {
00115     i2csw_start();
00116     i2csw_byte(0x78);           // Slave address: R/W(SA0)=0 - write
00117     i2csw_byte(0x00);           // Control byte: D/C=0 - write command
00118 }
00119 
00120 void ssd1306_start_data(void) {
00121     i2csw_start();
00122     i2csw_byte(0x78);           // Slave address, R/W(SA0)=0 - write
00123     i2csw_byte(0x40);           // Control byte: D/C=1 - write data
00124 }
00125 
00126 void ssd1306_data_byte(byte b) {
00127     i2csw_byte(b);
00128 }
00129 
00130 void ssd1306_stop(void) {
00131     i2csw_stop();
00132 }
00133 
00134 // ============================================================================
00135 
00136 void vOledInit(void) {
00137     byte i;
00138     ssd1306_start_command();    // Initiate transmission of command
00139     for (i = 0; i < sizeof (ssd1306_init_sequence); i++) {
00140         ssd1306_data_byte(ssd1306_init_sequence[i]);    // Send the command out
00141     }
00142     ssd1306_stop(); // Finish transmission
00143 }
00144 
00145 void ssd1306_setpos(byte x, byte y) {
00146     ssd1306_start_command();
00147     ssd1306_data_byte(0xb0 | (y & 0x07));   // Set page start address
00148     ssd1306_data_byte(x & 0x0f);            // Set the lower nibble of the column start address
00149     ssd1306_data_byte(0x10 | (x >> 4));     // Set the higher nibble of the column start address
00150     ssd1306_stop(); // Finish transmission
00151 }
00152 
00153 void ssd1306_fill4(byte p1, byte p2, byte p3, byte p4) {
00154     word i;
00155     ssd1306_setpos(0, 0);
00156     ssd1306_start_data();   // Initiate transmission of data
00157     for (i = 0; i < 128 * 8 / 4; i++) {
00158         ssd1306_data_byte(p1);
00159         ssd1306_data_byte(p2);
00160         ssd1306_data_byte(p3);
00161         ssd1306_data_byte(p4);
00162     }
00163     ssd1306_stop(); // Finish transmission
00164 }
00165 
00166 // ----------------------------------------------------------------------------
00167 
00168 void ssd1306tx_char(byte ch) {
00169     byte i;
00170     word j = (ch-32)*6;
00171     ssd1306_start_data();
00172     for (i = 0; i < 6; i++) {
00173         ssd1306_data_byte(ssd1306xled_font6x8data[j + i]);
00174     }
00175     ssd1306_stop();
00176 } 
00177 
00178 void ssd1306tx_large(byte ch, byte x, byte y) {
00179     byte i, yy;
00180     word j = (ch-32)*16;
00181     yy = y >> 1;
00182     ssd1306_setpos(x, (yy));
00183     ssd1306_start_data();
00184     for (i = 0; i < 8; i++) {
00185         ssd1306_data_byte(ssd1306xled_font8x16data[j + i]);
00186         }
00187     ssd1306_stop();
00188     ssd1306_setpos(x, (yy+1));
00189     ssd1306_start_data();
00190     for (i = 8; i < 16; i++) {
00191         ssd1306_data_byte(ssd1306xled_font8x16data[j + i]);
00192         }
00193     ssd1306_stop();
00194     } 
00195 
00196 // ============================================================================