Driver for Text OLED display - 20x4 Winstar or Newhaven, with Winstar WS0010 driver IC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextOLED20x4.cpp Source File

TextOLED20x4.cpp

00001 /* mbed TextOLED20x4 Library, for a 4-bit LCD based on HD44780
00002  * CopyLeft Rod Coleman 2012
00003  * Copyright (c) 2007-2010, sford, http://mbed.org
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #include "TextOLED20x4.h"
00025 #include "mbed.h"
00026 
00027 TextOLED20x4::TextOLED20x4(PinName rs, PinName e, PinName d4, PinName d5,
00028                  PinName d6, PinName d7, LCDType type) : _rs(rs),
00029         _e(e), _d(d4, d5, d6, d7),
00030         _type(type) {
00031 
00032     _e  = 1;
00033     _rs = 0;            // command mode
00034 
00035  // Winstar 20x4 WH2004-NYG- needs 40ms after VDD> 4,5V
00036     /*
00037     wait(0.5);
00038     writeCommand(0x2); // 4-bit mode
00039     wait(0.05);
00040     writeCommand(0x28);    // Function set 001 BW N F - -
00041     wait(0.05);
00042     */
00043     //RC:2011-11-23: Newhaven 20x4 OLED data sheet method
00044     wait(0.33);
00045     writeCommand(0x2); // 4-bit mode
00046     wait(0.05);
00047     writeCommand(0x2); // 4-bit mode
00048     wait(0.05);
00049     writeCommand(0x28); // display OFF, "Function Set". 5x8 Font. UK/JP table. Newhaven say 0x08, but this loses 2 rows!
00050     wait(0.05); 
00051     writeCommand(0x1); // display clear. takes 6,2ms@fosc=250kHz
00052     wait(0.05);
00053     writeCommand(0x6); // entry mode set
00054     wait(0.05); 
00055     writeCommand(0x2); // 4-bit mode
00056     wait(0.05);                  
00057     writeCommand(0x0C); // ON-OFF ctrl: turns display ON, no cursor. Use 0x0E for cursor ON.
00058     /* 0x28 also works for Winstar WEH002004ALPP5N00000 OLED display. 0x29= westEuro fon table, 0x2A = UK/Russian
00059 */
00060 
00061     wait(0.015);        // Wait 15ms to ensure powered up
00062 /*
00063     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00064     for (int i=0; i<3; i++) {
00065         writeByte(0x3);
00066         wait(0.00164);  // this command takes 1.64ms, so wait for it
00067     }
00068     writeByte(0x2);     // 4-bit mode
00069     wait(0.000040f);    // most instructions take 40us
00070 
00071     writeCommand(0x28); // Function set 001 BW N F - -
00072     writeCommand(0x0C);
00073     writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00074     cls();*/
00075 }
00076 
00077 void TextOLED20x4::character(int column, int row, int c) {
00078     int a = address(column, row);
00079     writeCommand(a);
00080     writeData(c);
00081 }
00082 
00083 void TextOLED20x4::cls() {
00084     writeCommand(0x01); // cls, and set cursor to 0
00085     wait(0.00164f);     // This command takes 1.64 ms
00086     locate(0, 0);
00087 }
00088 
00089 void TextOLED20x4::locate(int column, int row) {
00090     _column = column;
00091     _row = row;
00092 }
00093 
00094 int TextOLED20x4::_putc(int value) {
00095     if (value == '\n') {
00096         _column = 0;
00097         _row++;
00098         if (_row >= rows()) {
00099             _row = 0;
00100         }
00101     } else {
00102         character(_column, _row, value);
00103         _column++;
00104         if (_column >= columns()) {
00105             _column = 0;
00106             _row++;
00107             if (_row >= rows()) {
00108                 _row = 0;
00109             }
00110         }
00111     }
00112     return value;
00113 }
00114 
00115 int TextOLED20x4::_getc() {
00116     return -1;
00117 }
00118 
00119 void TextOLED20x4::writeByte(int value) {
00120     wait_us(1);
00121     _e = 1;          // RC added. Must go high at least 20ns after RS settled.
00122     _d = value >> 4;
00123     wait_us(1);
00124     _e = 0;
00125     wait_us(1);
00126     _d = value >> 0;
00127     _e = 1;
00128     wait_us(1);  // E high time is 250ns min, and low time 250ns min.
00129     _e = 0;
00130     wait_us(100);  // most instructions take 40us
00131     //_e = 1;
00132 }
00133 
00134 void TextOLED20x4::writeCommand(int command) {
00135     _e = 0;
00136     _rs = 0;
00137     writeByte(command);
00138 }
00139 
00140 void TextOLED20x4::writeData(int data) {
00141     _e = 0;
00142     _rs = 1;
00143     writeByte(data);
00144 }
00145 
00146 int TextOLED20x4::address(int column, int row) {
00147    /* switch (_type) {
00148         case LCD20x4:*/
00149             switch (row) {
00150                 case 0:
00151                     return 0x80 + column;
00152                 case 1:
00153                     return 0xc0 + column;
00154                 case 2:
00155                     return 0x94 + column;
00156                 case 3:
00157                     return 0xd4 + column;
00158             }
00159             return 0x80 + column;
00160             /*
00161         case LCD16x2B:
00162             return 0x80 + (row * 40) + column;
00163         case LCD16x2:
00164         case LCD20x2:
00165         default:
00166             return 0x80 + (row * 0x40) + column;
00167     }*/
00168 }
00169 
00170 int TextOLED20x4::columns() {
00171    return 20; 
00172     /* switch (_type) {
00173        case LCD20x4:
00174         case LCD20x2:
00175             return 20;
00176         case LCD16x2:
00177         case LCD16x2B:
00178         default:
00179             return 16;
00180     }*/
00181 }
00182 
00183 int TextOLED20x4::rows() {
00184     return 4;
00185     /*switch (_type) {
00186         case LCD20x4:
00187             return 4;
00188         case LCD16x2:
00189         case LCD16x2B:
00190         case LCD20x2:
00191         default:
00192             return 2;
00193     }*/
00194 }