TextLCD library for controlling various LCD panels based on the HD44780 4-bit interface

Fork of myTextLCD by V09

Committer:
rs27
Date:
Sat Jan 03 15:26:34 2015 +0000
Revision:
9:87fdf4a45de7
Parent:
TextLCD.cpp@7:44f34c09bd37
123;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:ac48b187213c 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
simon 6:e4cb7ddee0d3 2 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:ac48b187213c 3 *
simon 1:ac48b187213c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:ac48b187213c 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:ac48b187213c 6 * in the Software without restriction, including without limitation the rights
simon 1:ac48b187213c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:ac48b187213c 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:ac48b187213c 9 * furnished to do so, subject to the following conditions:
simon 1:ac48b187213c 10 *
simon 1:ac48b187213c 11 * The above copyright notice and this permission notice shall be included in
simon 1:ac48b187213c 12 * all copies or substantial portions of the Software.
simon 1:ac48b187213c 13 *
simon 1:ac48b187213c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:ac48b187213c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:ac48b187213c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:ac48b187213c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:ac48b187213c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:ac48b187213c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:ac48b187213c 20 * THE SOFTWARE.
simon 1:ac48b187213c 21 */
simon 1:ac48b187213c 22
rs27 9:87fdf4a45de7 23 #include "myTextLCD.h"
simon 1:ac48b187213c 24 #include "mbed.h"
rs27 9:87fdf4a45de7 25 #include "MODSERIAL.h"
rs27 9:87fdf4a45de7 26
rs27 9:87fdf4a45de7 27 extern MODSERIAL pc; // definiert in main
simon 1:ac48b187213c 28
simon 7:44f34c09bd37 29 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
simon 7:44f34c09bd37 30 PinName d6, PinName d7, LCDType type) : _rs(rs),
simon 7:44f34c09bd37 31 _e(e), _d(d4, d5, d6, d7),
simon 1:ac48b187213c 32 _type(type) {
simon 1:ac48b187213c 33
simon 1:ac48b187213c 34 _e = 1;
simon 1:ac48b187213c 35 _rs = 0; // command mode
rs27 9:87fdf4a45de7 36 _type = LCD16x2;
rs27 9:87fdf4a45de7 37
rs27 9:87fdf4a45de7 38 // die Anzeige soll hier zwischengespeichert werden
rs27 9:87fdf4a45de7 39 for(int i = 0; i < 16; i++)
rs27 9:87fdf4a45de7 40 {
rs27 9:87fdf4a45de7 41 zeile1_text[i] = 0x20; // Leerzeichen
rs27 9:87fdf4a45de7 42 zeile2_text[i] = 0x20; // Leerzeichen
rs27 9:87fdf4a45de7 43 zeile1_flag[i] = false;
rs27 9:87fdf4a45de7 44 zeile2_flag[i] = false;
rs27 9:87fdf4a45de7 45 }
rs27 9:87fdf4a45de7 46
rs27 9:87fdf4a45de7 47 lcd_flag = true;
rs27 9:87fdf4a45de7 48
simon 1:ac48b187213c 49 wait(0.015); // Wait 15ms to ensure powered up
simon 1:ac48b187213c 50
simon 1:ac48b187213c 51 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 1:ac48b187213c 52 for (int i=0; i<3; i++) {
simon 1:ac48b187213c 53 writeByte(0x3);
simon 1:ac48b187213c 54 wait(0.00164); // this command takes 1.64ms, so wait for it
simon 1:ac48b187213c 55 }
simon 1:ac48b187213c 56 writeByte(0x2); // 4-bit mode
simon 1:ac48b187213c 57 wait(0.000040f); // most instructions take 40us
simon 1:ac48b187213c 58
simon 1:ac48b187213c 59 writeCommand(0x28); // Function set 001 BW N F - -
simon 1:ac48b187213c 60 writeCommand(0x0C);
simon 1:ac48b187213c 61 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
simon 1:ac48b187213c 62 cls();
simon 1:ac48b187213c 63 }
simon 1:ac48b187213c 64
rs27 9:87fdf4a45de7 65 //---------------------------------------------------
rs27 9:87fdf4a45de7 66 // in der Phase, wo die LCD Anzeige von einer Eingabe
rs27 9:87fdf4a45de7 67 // benötigt wird, kann mit diesem Falg die Ausgabe
rs27 9:87fdf4a45de7 68 // abgeschaltet werden.
rs27 9:87fdf4a45de7 69
rs27 9:87fdf4a45de7 70 void TextLCD::set_lcd_flag(bool flag)
rs27 9:87fdf4a45de7 71 {
rs27 9:87fdf4a45de7 72 lcd_flag = flag;
rs27 9:87fdf4a45de7 73 }
rs27 9:87fdf4a45de7 74
rs27 9:87fdf4a45de7 75 // hier werden die Daten in die LCD Anzeige geschrieben
rs27 9:87fdf4a45de7 76 // im neuen Ablauf werden hier die Daten in die Variable geschrieben
rs27 9:87fdf4a45de7 77
rs27 9:87fdf4a45de7 78 void TextLCD::character(int column, int row, int c)
rs27 9:87fdf4a45de7 79 {
rs27 9:87fdf4a45de7 80
rs27 9:87fdf4a45de7 81 if (row == 0)
rs27 9:87fdf4a45de7 82 {
rs27 9:87fdf4a45de7 83 if (zeile1_text[column] != c)
rs27 9:87fdf4a45de7 84 {
rs27 9:87fdf4a45de7 85 zeile1_text[column] = c;
rs27 9:87fdf4a45de7 86 zeile1_flag[column] = true;
rs27 9:87fdf4a45de7 87 }
rs27 9:87fdf4a45de7 88 }
rs27 9:87fdf4a45de7 89 else
rs27 9:87fdf4a45de7 90 {
rs27 9:87fdf4a45de7 91 if (zeile2_text[column] != c)
rs27 9:87fdf4a45de7 92 {
rs27 9:87fdf4a45de7 93 zeile2_text[column] = c;
rs27 9:87fdf4a45de7 94 zeile2_flag[column] = true;
rs27 9:87fdf4a45de7 95 }
rs27 9:87fdf4a45de7 96 }
simon 1:ac48b187213c 97 }
simon 1:ac48b187213c 98
rs27 9:87fdf4a45de7 99 // -----------------------------------------------------
rs27 9:87fdf4a45de7 100 // den Text im Speicher auf die LCD Anzeige ausgeben
rs27 9:87fdf4a45de7 101
rs27 9:87fdf4a45de7 102 void TextLCD::writeLCD()
rs27 9:87fdf4a45de7 103 {
rs27 9:87fdf4a45de7 104 int i;
rs27 9:87fdf4a45de7 105
rs27 9:87fdf4a45de7 106 if (lcd_flag)
rs27 9:87fdf4a45de7 107 {
rs27 9:87fdf4a45de7 108 // Text auf Zeile 1 ausgeben
rs27 9:87fdf4a45de7 109 for (i = 0; i<16; i++)
rs27 9:87fdf4a45de7 110 {
rs27 9:87fdf4a45de7 111 if (zeile1_flag[i])
rs27 9:87fdf4a45de7 112 {
rs27 9:87fdf4a45de7 113 int a = address(i, 0);
rs27 9:87fdf4a45de7 114 writeCommand(a);
rs27 9:87fdf4a45de7 115 writeData(zeile1_text[i]);
rs27 9:87fdf4a45de7 116 zeile1_flag[i] = false;
rs27 9:87fdf4a45de7 117 }
rs27 9:87fdf4a45de7 118 }
rs27 9:87fdf4a45de7 119
rs27 9:87fdf4a45de7 120 // Text auf Zeile 2 ausgeben
rs27 9:87fdf4a45de7 121 for (i = 0; i<16; i++)
rs27 9:87fdf4a45de7 122 {
rs27 9:87fdf4a45de7 123 if (zeile2_flag[i])
rs27 9:87fdf4a45de7 124 {
rs27 9:87fdf4a45de7 125 int a = address(i, 1);
rs27 9:87fdf4a45de7 126 writeCommand(a);
rs27 9:87fdf4a45de7 127 writeData(zeile2_text[i]);
rs27 9:87fdf4a45de7 128 zeile2_flag[i] = false;
rs27 9:87fdf4a45de7 129 }
rs27 9:87fdf4a45de7 130 }
rs27 9:87fdf4a45de7 131 }
rs27 9:87fdf4a45de7 132 }
rs27 9:87fdf4a45de7 133
rs27 9:87fdf4a45de7 134 // --------------------------------------------------------
rs27 9:87fdf4a45de7 135 // Nachdem die LCD Anzeige wieder die Freigabe bekommt
rs27 9:87fdf4a45de7 136 // wird der ganze Speicher auf die LCD Anzeige ausgeben
rs27 9:87fdf4a45de7 137 //
rs27 9:87fdf4a45de7 138 void TextLCD::writeALL()
rs27 9:87fdf4a45de7 139 {
rs27 9:87fdf4a45de7 140 int i, a;
rs27 9:87fdf4a45de7 141
rs27 9:87fdf4a45de7 142 // Text auf Zeile 1 ausgeben
rs27 9:87fdf4a45de7 143 a = address(0, 0);
rs27 9:87fdf4a45de7 144 writeCommand(a);
rs27 9:87fdf4a45de7 145 wait_us(40);
rs27 9:87fdf4a45de7 146
rs27 9:87fdf4a45de7 147 for (i = 0; i<16; i++)
rs27 9:87fdf4a45de7 148 {
rs27 9:87fdf4a45de7 149 writeData(zeile1_text[i]);
rs27 9:87fdf4a45de7 150 zeile1_flag[i] = false;
rs27 9:87fdf4a45de7 151 }
rs27 9:87fdf4a45de7 152
rs27 9:87fdf4a45de7 153 // Text auf Zeile 2 ausgeben
rs27 9:87fdf4a45de7 154
rs27 9:87fdf4a45de7 155 a = address(0, 1);
rs27 9:87fdf4a45de7 156 writeCommand(a);
rs27 9:87fdf4a45de7 157 wait_us(40);
rs27 9:87fdf4a45de7 158
rs27 9:87fdf4a45de7 159 for (i = 0; i<16; i++)
rs27 9:87fdf4a45de7 160 {
rs27 9:87fdf4a45de7 161 writeData(zeile2_text[i]);
rs27 9:87fdf4a45de7 162 zeile2_flag[i] = false;
rs27 9:87fdf4a45de7 163 }
rs27 9:87fdf4a45de7 164
rs27 9:87fdf4a45de7 165 // Text auf Zeile 1 ausgeben
rs27 9:87fdf4a45de7 166 a = address(0, 0);
rs27 9:87fdf4a45de7 167 writeCommand(a);
rs27 9:87fdf4a45de7 168 wait_us(40);
rs27 9:87fdf4a45de7 169 }
rs27 9:87fdf4a45de7 170
rs27 9:87fdf4a45de7 171 void TextLCD::cls()
rs27 9:87fdf4a45de7 172 {
simon 1:ac48b187213c 173 writeCommand(0x01); // cls, and set cursor to 0
simon 1:ac48b187213c 174 wait(0.00164f); // This command takes 1.64 ms
simon 1:ac48b187213c 175 locate(0, 0);
rs27 9:87fdf4a45de7 176
simon 1:ac48b187213c 177 }
simon 1:ac48b187213c 178
rs27 9:87fdf4a45de7 179 void TextLCD::locate(int column, int row)
rs27 9:87fdf4a45de7 180 {
simon 1:ac48b187213c 181 _column = column;
simon 1:ac48b187213c 182 _row = row;
simon 1:ac48b187213c 183 }
simon 1:ac48b187213c 184
rs27 9:87fdf4a45de7 185 int TextLCD::_putc(int value)
rs27 9:87fdf4a45de7 186 {
simon 1:ac48b187213c 187 if (value == '\n') {
simon 1:ac48b187213c 188 _column = 0;
simon 1:ac48b187213c 189 _row++;
simon 1:ac48b187213c 190 if (_row >= rows()) {
simon 1:ac48b187213c 191 _row = 0;
simon 1:ac48b187213c 192 }
simon 1:ac48b187213c 193 } else {
simon 1:ac48b187213c 194 character(_column, _row, value);
simon 1:ac48b187213c 195 _column++;
simon 1:ac48b187213c 196 if (_column >= columns()) {
simon 1:ac48b187213c 197 _column = 0;
simon 1:ac48b187213c 198 _row++;
simon 1:ac48b187213c 199 if (_row >= rows()) {
simon 1:ac48b187213c 200 _row = 0;
simon 1:ac48b187213c 201 }
simon 1:ac48b187213c 202 }
simon 1:ac48b187213c 203 }
simon 1:ac48b187213c 204 return value;
simon 1:ac48b187213c 205 }
simon 1:ac48b187213c 206
simon 1:ac48b187213c 207 int TextLCD::_getc() {
simon 1:ac48b187213c 208 return -1;
simon 1:ac48b187213c 209 }
simon 1:ac48b187213c 210
rs27 9:87fdf4a45de7 211 void TextLCD::writeByte(int value)
rs27 9:87fdf4a45de7 212 {
simon 1:ac48b187213c 213 _d = value >> 4;
rs27 9:87fdf4a45de7 214 wait_us(1);
simon 1:ac48b187213c 215 _e = 0;
rs27 9:87fdf4a45de7 216 wait_us(40);
simon 1:ac48b187213c 217 _e = 1;
simon 1:ac48b187213c 218 _d = value >> 0;
rs27 9:87fdf4a45de7 219 wait_us(1);
simon 1:ac48b187213c 220 _e = 0;
rs27 9:87fdf4a45de7 221 wait_us(40);
simon 1:ac48b187213c 222 _e = 1;
rs27 9:87fdf4a45de7 223 wait_us(42); // most instructions take 40us
simon 1:ac48b187213c 224 }
simon 1:ac48b187213c 225
rs27 9:87fdf4a45de7 226 void TextLCD::writeCommand(int command)
rs27 9:87fdf4a45de7 227 {
simon 1:ac48b187213c 228 _rs = 0;
simon 1:ac48b187213c 229 writeByte(command);
simon 1:ac48b187213c 230 }
simon 1:ac48b187213c 231
rs27 9:87fdf4a45de7 232 void TextLCD::writeData(int data)
rs27 9:87fdf4a45de7 233 {
simon 1:ac48b187213c 234 _rs = 1;
simon 1:ac48b187213c 235 writeByte(data);
simon 1:ac48b187213c 236 }
simon 1:ac48b187213c 237
rs27 9:87fdf4a45de7 238 int TextLCD::address(int column, int row)
rs27 9:87fdf4a45de7 239 {
simon 1:ac48b187213c 240 switch (_type) {
simon 1:ac48b187213c 241 case LCD20x4:
simon 1:ac48b187213c 242 switch (row) {
simon 1:ac48b187213c 243 case 0:
simon 1:ac48b187213c 244 return 0x80 + column;
simon 1:ac48b187213c 245 case 1:
simon 1:ac48b187213c 246 return 0xc0 + column;
simon 1:ac48b187213c 247 case 2:
simon 1:ac48b187213c 248 return 0x94 + column;
simon 1:ac48b187213c 249 case 3:
simon 1:ac48b187213c 250 return 0xd4 + column;
simon 1:ac48b187213c 251 }
simon 1:ac48b187213c 252 case LCD16x2B:
simon 4:bf5b706f8d32 253 return 0x80 + (row * 40) + column;
simon 1:ac48b187213c 254 case LCD16x2:
simon 1:ac48b187213c 255 case LCD20x2:
simon 1:ac48b187213c 256 default:
simon 4:bf5b706f8d32 257 return 0x80 + (row * 0x40) + column;
simon 1:ac48b187213c 258 }
simon 1:ac48b187213c 259 }
simon 1:ac48b187213c 260
rs27 9:87fdf4a45de7 261 int TextLCD::columns()
rs27 9:87fdf4a45de7 262 {
simon 1:ac48b187213c 263 switch (_type) {
simon 1:ac48b187213c 264 case LCD20x4:
simon 1:ac48b187213c 265 case LCD20x2:
simon 1:ac48b187213c 266 return 20;
simon 1:ac48b187213c 267 case LCD16x2:
simon 1:ac48b187213c 268 case LCD16x2B:
simon 1:ac48b187213c 269 default:
simon 1:ac48b187213c 270 return 16;
simon 1:ac48b187213c 271 }
simon 1:ac48b187213c 272 }
simon 1:ac48b187213c 273
rs27 9:87fdf4a45de7 274 int TextLCD::rows()
rs27 9:87fdf4a45de7 275 {
simon 1:ac48b187213c 276 switch (_type) {
simon 1:ac48b187213c 277 case LCD20x4:
simon 1:ac48b187213c 278 return 4;
simon 1:ac48b187213c 279 case LCD16x2:
simon 1:ac48b187213c 280 case LCD16x2B:
simon 1:ac48b187213c 281 case LCD20x2:
simon 1:ac48b187213c 282 default:
simon 1:ac48b187213c 283 return 2;
simon 1:ac48b187213c 284 }
simon 1:ac48b187213c 285 }
rs27 9:87fdf4a45de7 286
rs27 9:87fdf4a45de7 287 void TextLCD::print_LCD(char* Pbuffer,char line_num, char offset)
rs27 9:87fdf4a45de7 288 {
rs27 9:87fdf4a45de7 289 unsigned int i=0;
rs27 9:87fdf4a45de7 290
rs27 9:87fdf4a45de7 291 int a = address(offset,line_num);
rs27 9:87fdf4a45de7 292 writeCommand(a);
rs27 9:87fdf4a45de7 293
rs27 9:87fdf4a45de7 294 while (Pbuffer[i] != 0)
rs27 9:87fdf4a45de7 295 {
rs27 9:87fdf4a45de7 296 writeData(Pbuffer[i]);
rs27 9:87fdf4a45de7 297 i++;
rs27 9:87fdf4a45de7 298 }
rs27 9:87fdf4a45de7 299 }