Driver for Text OLED display - 20x4 Winstar or Newhaven, with Winstar WS0010 driver IC
TextOLED20x4.cpp@3:d955c3213b6a, 2013-01-21 (annotated)
- Committer:
- RodColeman
- Date:
- Mon Jan 21 11:26:17 2013 +0000
- Revision:
- 3:d955c3213b6a
- Parent:
- 2:c7834290ea1c
added return value for out-of-range column address.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RodColeman | 2:c7834290ea1c | 1 | /* mbed TextOLED20x4 Library, for a 4-bit LCD based on HD44780 |
RodColeman | 2:c7834290ea1c | 2 | * CopyLeft Rod Coleman 2012 |
RodColeman | 0:4d453b617559 | 3 | * Copyright (c) 2007-2010, sford, http://mbed.org |
RodColeman | 0:4d453b617559 | 4 | * |
RodColeman | 0:4d453b617559 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
RodColeman | 0:4d453b617559 | 6 | * of this software and associated documentation files (the "Software"), to deal |
RodColeman | 0:4d453b617559 | 7 | * in the Software without restriction, including without limitation the rights |
RodColeman | 0:4d453b617559 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
RodColeman | 0:4d453b617559 | 9 | * copies of the Software, and to permit persons to whom the Software is |
RodColeman | 0:4d453b617559 | 10 | * furnished to do so, subject to the following conditions: |
RodColeman | 0:4d453b617559 | 11 | * |
RodColeman | 0:4d453b617559 | 12 | * The above copyright notice and this permission notice shall be included in |
RodColeman | 0:4d453b617559 | 13 | * all copies or substantial portions of the Software. |
RodColeman | 0:4d453b617559 | 14 | * |
RodColeman | 0:4d453b617559 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
RodColeman | 0:4d453b617559 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
RodColeman | 0:4d453b617559 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
RodColeman | 0:4d453b617559 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
RodColeman | 0:4d453b617559 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
RodColeman | 0:4d453b617559 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
RodColeman | 0:4d453b617559 | 21 | * THE SOFTWARE. |
RodColeman | 0:4d453b617559 | 22 | */ |
RodColeman | 0:4d453b617559 | 23 | |
RodColeman | 2:c7834290ea1c | 24 | #include "TextOLED20x4.h" |
RodColeman | 0:4d453b617559 | 25 | #include "mbed.h" |
RodColeman | 0:4d453b617559 | 26 | |
RodColeman | 2:c7834290ea1c | 27 | TextOLED20x4::TextOLED20x4(PinName rs, PinName e, PinName d4, PinName d5, |
RodColeman | 0:4d453b617559 | 28 | PinName d6, PinName d7, LCDType type) : _rs(rs), |
RodColeman | 0:4d453b617559 | 29 | _e(e), _d(d4, d5, d6, d7), |
RodColeman | 0:4d453b617559 | 30 | _type(type) { |
RodColeman | 0:4d453b617559 | 31 | |
RodColeman | 0:4d453b617559 | 32 | _e = 1; |
RodColeman | 0:4d453b617559 | 33 | _rs = 0; // command mode |
RodColeman | 0:4d453b617559 | 34 | |
RodColeman | 0:4d453b617559 | 35 | // Winstar 20x4 WH2004-NYG- needs 40ms after VDD> 4,5V |
RodColeman | 0:4d453b617559 | 36 | /* |
RodColeman | 0:4d453b617559 | 37 | wait(0.5); |
RodColeman | 0:4d453b617559 | 38 | writeCommand(0x2); // 4-bit mode |
RodColeman | 0:4d453b617559 | 39 | wait(0.05); |
RodColeman | 0:4d453b617559 | 40 | writeCommand(0x28); // Function set 001 BW N F - - |
RodColeman | 0:4d453b617559 | 41 | wait(0.05); |
RodColeman | 0:4d453b617559 | 42 | */ |
RodColeman | 0:4d453b617559 | 43 | //RC:2011-11-23: Newhaven 20x4 OLED data sheet method |
RodColeman | 0:4d453b617559 | 44 | wait(0.33); |
RodColeman | 0:4d453b617559 | 45 | writeCommand(0x2); // 4-bit mode |
RodColeman | 0:4d453b617559 | 46 | wait(0.05); |
RodColeman | 0:4d453b617559 | 47 | writeCommand(0x2); // 4-bit mode |
RodColeman | 0:4d453b617559 | 48 | wait(0.05); |
RodColeman | 0:4d453b617559 | 49 | writeCommand(0x28); // display OFF, "Function Set". 5x8 Font. UK/JP table. Newhaven say 0x08, but this loses 2 rows! |
RodColeman | 0:4d453b617559 | 50 | wait(0.05); |
RodColeman | 0:4d453b617559 | 51 | writeCommand(0x1); // display clear. takes 6,2ms@fosc=250kHz |
RodColeman | 0:4d453b617559 | 52 | wait(0.05); |
RodColeman | 0:4d453b617559 | 53 | writeCommand(0x6); // entry mode set |
RodColeman | 0:4d453b617559 | 54 | wait(0.05); |
RodColeman | 0:4d453b617559 | 55 | writeCommand(0x2); // 4-bit mode |
RodColeman | 0:4d453b617559 | 56 | wait(0.05); |
RodColeman | 0:4d453b617559 | 57 | writeCommand(0x0C); // ON-OFF ctrl: turns display ON, no cursor. Use 0x0E for cursor ON. |
RodColeman | 0:4d453b617559 | 58 | /* 0x28 also works for Winstar WEH002004ALPP5N00000 OLED display. 0x29= westEuro fon table, 0x2A = UK/Russian |
RodColeman | 0:4d453b617559 | 59 | */ |
RodColeman | 0:4d453b617559 | 60 | |
RodColeman | 0:4d453b617559 | 61 | wait(0.015); // Wait 15ms to ensure powered up |
RodColeman | 0:4d453b617559 | 62 | /* |
RodColeman | 0:4d453b617559 | 63 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
RodColeman | 0:4d453b617559 | 64 | for (int i=0; i<3; i++) { |
RodColeman | 0:4d453b617559 | 65 | writeByte(0x3); |
RodColeman | 0:4d453b617559 | 66 | wait(0.00164); // this command takes 1.64ms, so wait for it |
RodColeman | 0:4d453b617559 | 67 | } |
RodColeman | 0:4d453b617559 | 68 | writeByte(0x2); // 4-bit mode |
RodColeman | 0:4d453b617559 | 69 | wait(0.000040f); // most instructions take 40us |
RodColeman | 0:4d453b617559 | 70 | |
RodColeman | 0:4d453b617559 | 71 | writeCommand(0x28); // Function set 001 BW N F - - |
RodColeman | 0:4d453b617559 | 72 | writeCommand(0x0C); |
RodColeman | 0:4d453b617559 | 73 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
RodColeman | 0:4d453b617559 | 74 | cls();*/ |
RodColeman | 0:4d453b617559 | 75 | } |
RodColeman | 0:4d453b617559 | 76 | |
RodColeman | 2:c7834290ea1c | 77 | void TextOLED20x4::character(int column, int row, int c) { |
RodColeman | 0:4d453b617559 | 78 | int a = address(column, row); |
RodColeman | 0:4d453b617559 | 79 | writeCommand(a); |
RodColeman | 0:4d453b617559 | 80 | writeData(c); |
RodColeman | 0:4d453b617559 | 81 | } |
RodColeman | 0:4d453b617559 | 82 | |
RodColeman | 2:c7834290ea1c | 83 | void TextOLED20x4::cls() { |
RodColeman | 0:4d453b617559 | 84 | writeCommand(0x01); // cls, and set cursor to 0 |
RodColeman | 0:4d453b617559 | 85 | wait(0.00164f); // This command takes 1.64 ms |
RodColeman | 0:4d453b617559 | 86 | locate(0, 0); |
RodColeman | 0:4d453b617559 | 87 | } |
RodColeman | 0:4d453b617559 | 88 | |
RodColeman | 2:c7834290ea1c | 89 | void TextOLED20x4::locate(int column, int row) { |
RodColeman | 0:4d453b617559 | 90 | _column = column; |
RodColeman | 0:4d453b617559 | 91 | _row = row; |
RodColeman | 0:4d453b617559 | 92 | } |
RodColeman | 0:4d453b617559 | 93 | |
RodColeman | 2:c7834290ea1c | 94 | int TextOLED20x4::_putc(int value) { |
RodColeman | 0:4d453b617559 | 95 | if (value == '\n') { |
RodColeman | 0:4d453b617559 | 96 | _column = 0; |
RodColeman | 0:4d453b617559 | 97 | _row++; |
RodColeman | 0:4d453b617559 | 98 | if (_row >= rows()) { |
RodColeman | 0:4d453b617559 | 99 | _row = 0; |
RodColeman | 0:4d453b617559 | 100 | } |
RodColeman | 0:4d453b617559 | 101 | } else { |
RodColeman | 0:4d453b617559 | 102 | character(_column, _row, value); |
RodColeman | 0:4d453b617559 | 103 | _column++; |
RodColeman | 0:4d453b617559 | 104 | if (_column >= columns()) { |
RodColeman | 0:4d453b617559 | 105 | _column = 0; |
RodColeman | 0:4d453b617559 | 106 | _row++; |
RodColeman | 0:4d453b617559 | 107 | if (_row >= rows()) { |
RodColeman | 0:4d453b617559 | 108 | _row = 0; |
RodColeman | 0:4d453b617559 | 109 | } |
RodColeman | 0:4d453b617559 | 110 | } |
RodColeman | 0:4d453b617559 | 111 | } |
RodColeman | 0:4d453b617559 | 112 | return value; |
RodColeman | 0:4d453b617559 | 113 | } |
RodColeman | 0:4d453b617559 | 114 | |
RodColeman | 2:c7834290ea1c | 115 | int TextOLED20x4::_getc() { |
RodColeman | 0:4d453b617559 | 116 | return -1; |
RodColeman | 0:4d453b617559 | 117 | } |
RodColeman | 0:4d453b617559 | 118 | |
RodColeman | 2:c7834290ea1c | 119 | void TextOLED20x4::writeByte(int value) { |
RodColeman | 0:4d453b617559 | 120 | wait_us(1); |
RodColeman | 0:4d453b617559 | 121 | _e = 1; // RC added. Must go high at least 20ns after RS settled. |
RodColeman | 0:4d453b617559 | 122 | _d = value >> 4; |
RodColeman | 0:4d453b617559 | 123 | wait_us(1); |
RodColeman | 0:4d453b617559 | 124 | _e = 0; |
RodColeman | 0:4d453b617559 | 125 | wait_us(1); |
RodColeman | 0:4d453b617559 | 126 | _d = value >> 0; |
RodColeman | 0:4d453b617559 | 127 | _e = 1; |
RodColeman | 0:4d453b617559 | 128 | wait_us(1); // E high time is 250ns min, and low time 250ns min. |
RodColeman | 0:4d453b617559 | 129 | _e = 0; |
RodColeman | 0:4d453b617559 | 130 | wait_us(100); // most instructions take 40us |
RodColeman | 0:4d453b617559 | 131 | //_e = 1; |
RodColeman | 0:4d453b617559 | 132 | } |
RodColeman | 0:4d453b617559 | 133 | |
RodColeman | 2:c7834290ea1c | 134 | void TextOLED20x4::writeCommand(int command) { |
RodColeman | 0:4d453b617559 | 135 | _e = 0; |
RodColeman | 0:4d453b617559 | 136 | _rs = 0; |
RodColeman | 0:4d453b617559 | 137 | writeByte(command); |
RodColeman | 0:4d453b617559 | 138 | } |
RodColeman | 0:4d453b617559 | 139 | |
RodColeman | 2:c7834290ea1c | 140 | void TextOLED20x4::writeData(int data) { |
RodColeman | 0:4d453b617559 | 141 | _e = 0; |
RodColeman | 0:4d453b617559 | 142 | _rs = 1; |
RodColeman | 0:4d453b617559 | 143 | writeByte(data); |
RodColeman | 0:4d453b617559 | 144 | } |
RodColeman | 0:4d453b617559 | 145 | |
RodColeman | 2:c7834290ea1c | 146 | int TextOLED20x4::address(int column, int row) { |
RodColeman | 0:4d453b617559 | 147 | /* switch (_type) { |
RodColeman | 0:4d453b617559 | 148 | case LCD20x4:*/ |
RodColeman | 0:4d453b617559 | 149 | switch (row) { |
RodColeman | 0:4d453b617559 | 150 | case 0: |
RodColeman | 0:4d453b617559 | 151 | return 0x80 + column; |
RodColeman | 0:4d453b617559 | 152 | case 1: |
RodColeman | 0:4d453b617559 | 153 | return 0xc0 + column; |
RodColeman | 0:4d453b617559 | 154 | case 2: |
RodColeman | 0:4d453b617559 | 155 | return 0x94 + column; |
RodColeman | 0:4d453b617559 | 156 | case 3: |
RodColeman | 0:4d453b617559 | 157 | return 0xd4 + column; |
RodColeman | 3:d955c3213b6a | 158 | } |
RodColeman | 3:d955c3213b6a | 159 | return 0x80 + column; |
RodColeman | 3:d955c3213b6a | 160 | /* |
RodColeman | 0:4d453b617559 | 161 | case LCD16x2B: |
RodColeman | 0:4d453b617559 | 162 | return 0x80 + (row * 40) + column; |
RodColeman | 0:4d453b617559 | 163 | case LCD16x2: |
RodColeman | 0:4d453b617559 | 164 | case LCD20x2: |
RodColeman | 0:4d453b617559 | 165 | default: |
RodColeman | 0:4d453b617559 | 166 | return 0x80 + (row * 0x40) + column; |
RodColeman | 0:4d453b617559 | 167 | }*/ |
RodColeman | 0:4d453b617559 | 168 | } |
RodColeman | 0:4d453b617559 | 169 | |
RodColeman | 2:c7834290ea1c | 170 | int TextOLED20x4::columns() { |
RodColeman | 0:4d453b617559 | 171 | return 20; |
RodColeman | 0:4d453b617559 | 172 | /* switch (_type) { |
RodColeman | 0:4d453b617559 | 173 | case LCD20x4: |
RodColeman | 0:4d453b617559 | 174 | case LCD20x2: |
RodColeman | 0:4d453b617559 | 175 | return 20; |
RodColeman | 0:4d453b617559 | 176 | case LCD16x2: |
RodColeman | 0:4d453b617559 | 177 | case LCD16x2B: |
RodColeman | 0:4d453b617559 | 178 | default: |
RodColeman | 0:4d453b617559 | 179 | return 16; |
RodColeman | 0:4d453b617559 | 180 | }*/ |
RodColeman | 0:4d453b617559 | 181 | } |
RodColeman | 0:4d453b617559 | 182 | |
RodColeman | 2:c7834290ea1c | 183 | int TextOLED20x4::rows() { |
RodColeman | 0:4d453b617559 | 184 | return 4; |
RodColeman | 0:4d453b617559 | 185 | /*switch (_type) { |
RodColeman | 0:4d453b617559 | 186 | case LCD20x4: |
RodColeman | 0:4d453b617559 | 187 | return 4; |
RodColeman | 0:4d453b617559 | 188 | case LCD16x2: |
RodColeman | 0:4d453b617559 | 189 | case LCD16x2B: |
RodColeman | 0:4d453b617559 | 190 | case LCD20x2: |
RodColeman | 0:4d453b617559 | 191 | default: |
RodColeman | 0:4d453b617559 | 192 | return 2; |
RodColeman | 0:4d453b617559 | 193 | }*/ |
RodColeman | 0:4d453b617559 | 194 | } |