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