Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of myTextLCD by
myTextLCD.cpp
00001 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 00002 * Copyright (c) 2007-2010, sford, http://mbed.org 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 #include "myTextLCD.h" 00024 #include "mbed.h" 00025 #include "MODSERIAL.h" 00026 00027 extern MODSERIAL pc; // definiert in main 00028 00029 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5, 00030 PinName d6, PinName d7, LCDType type) : _rs(rs), 00031 _e(e), _d(d4, d5, d6, d7), 00032 _type(type) { 00033 00034 _e = 1; 00035 _rs = 0; // command mode 00036 _type = LCD16x2; 00037 00038 // die Anzeige soll hier zwischengespeichert werden 00039 for(int i = 0; i < 16; i++) 00040 { 00041 zeile1_text[i] = 0x20; // Leerzeichen 00042 zeile2_text[i] = 0x20; // Leerzeichen 00043 zeile1_flag[i] = false; 00044 zeile2_flag[i] = false; 00045 } 00046 00047 lcd_flag = true; 00048 00049 wait(0.015); // Wait 15ms to ensure powered up 00050 00051 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00052 for (int i=0; i<3; i++) { 00053 writeByte(0x3); 00054 wait(0.00164); // this command takes 1.64ms, so wait for it 00055 } 00056 writeByte(0x2); // 4-bit mode 00057 wait(0.000040f); // most instructions take 40us 00058 00059 writeCommand(0x28); // Function set 001 BW N F - - 00060 writeCommand(0x0C); 00061 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00062 cls(); 00063 } 00064 00065 //--------------------------------------------------- 00066 // in der Phase, wo die LCD Anzeige von einer Eingabe 00067 // benötigt wird, kann mit diesem Falg die Ausgabe 00068 // abgeschaltet werden. 00069 00070 void TextLCD::set_lcd_flag(bool flag) 00071 { 00072 lcd_flag = flag; 00073 } 00074 00075 // hier werden die Daten in die LCD Anzeige geschrieben 00076 // im neuen Ablauf werden hier die Daten in die Variable geschrieben 00077 00078 void TextLCD::character(int column, int row, int c) 00079 { 00080 00081 if (row == 0) 00082 { 00083 if (zeile1_text[column] != c) 00084 { 00085 zeile1_text[column] = c; 00086 zeile1_flag[column] = true; 00087 } 00088 } 00089 else 00090 { 00091 if (zeile2_text[column] != c) 00092 { 00093 zeile2_text[column] = c; 00094 zeile2_flag[column] = true; 00095 } 00096 } 00097 } 00098 00099 // ----------------------------------------------------- 00100 // den Text im Speicher auf die LCD Anzeige ausgeben 00101 00102 void TextLCD::writeLCD() 00103 { 00104 int i; 00105 00106 if (lcd_flag) 00107 { 00108 // Text auf Zeile 1 ausgeben 00109 for (i = 0; i<16; i++) 00110 { 00111 if (zeile1_flag[i]) 00112 { 00113 int a = address(i, 0); 00114 writeCommand(a); 00115 writeData(zeile1_text[i]); 00116 zeile1_flag[i] = false; 00117 } 00118 } 00119 00120 // Text auf Zeile 2 ausgeben 00121 for (i = 0; i<16; i++) 00122 { 00123 if (zeile2_flag[i]) 00124 { 00125 int a = address(i, 1); 00126 writeCommand(a); 00127 writeData(zeile2_text[i]); 00128 zeile2_flag[i] = false; 00129 } 00130 } 00131 } 00132 } 00133 00134 // -------------------------------------------------------- 00135 // Nachdem die LCD Anzeige wieder die Freigabe bekommt 00136 // wird der ganze Speicher auf die LCD Anzeige ausgeben 00137 // 00138 void TextLCD::writeALL() 00139 { 00140 int i, a; 00141 00142 // Text auf Zeile 1 ausgeben 00143 a = address(0, 0); 00144 writeCommand(a); 00145 wait_us(40); 00146 00147 for (i = 0; i<16; i++) 00148 { 00149 writeData(zeile1_text[i]); 00150 zeile1_flag[i] = false; 00151 } 00152 00153 // Text auf Zeile 2 ausgeben 00154 00155 a = address(0, 1); 00156 writeCommand(a); 00157 wait_us(40); 00158 00159 for (i = 0; i<16; i++) 00160 { 00161 writeData(zeile2_text[i]); 00162 zeile2_flag[i] = false; 00163 } 00164 00165 // Text auf Zeile 1 ausgeben 00166 a = address(0, 0); 00167 writeCommand(a); 00168 wait_us(40); 00169 } 00170 00171 void TextLCD::cls() 00172 { 00173 writeCommand(0x01); // cls, and set cursor to 0 00174 wait(0.00164f); // This command takes 1.64 ms 00175 locate(0, 0); 00176 00177 } 00178 00179 void TextLCD::locate(int column, int row) 00180 { 00181 _column = column; 00182 _row = row; 00183 } 00184 00185 int TextLCD::_putc(int value) 00186 { 00187 if (value == '\n') { 00188 _column = 0; 00189 _row++; 00190 if (_row >= rows()) { 00191 _row = 0; 00192 } 00193 } else { 00194 character(_column, _row, value); 00195 _column++; 00196 if (_column >= columns()) { 00197 _column = 0; 00198 _row++; 00199 if (_row >= rows()) { 00200 _row = 0; 00201 } 00202 } 00203 } 00204 return value; 00205 } 00206 00207 int TextLCD::_getc() { 00208 return -1; 00209 } 00210 00211 void TextLCD::writeByte(int value) 00212 { 00213 _d = value >> 4; 00214 wait_us(1); 00215 _e = 0; 00216 wait_us(40); 00217 _e = 1; 00218 _d = value >> 0; 00219 wait_us(1); 00220 _e = 0; 00221 wait_us(40); 00222 _e = 1; 00223 wait_us(42); // most instructions take 40us 00224 } 00225 00226 void TextLCD::writeCommand(int command) 00227 { 00228 _rs = 0; 00229 writeByte(command); 00230 } 00231 00232 void TextLCD::writeData(int data) 00233 { 00234 _rs = 1; 00235 writeByte(data); 00236 } 00237 00238 int TextLCD::address(int column, int row) 00239 { 00240 switch (_type) { 00241 case LCD20x4: 00242 switch (row) { 00243 case 0: 00244 return 0x80 + column; 00245 case 1: 00246 return 0xc0 + column; 00247 case 2: 00248 return 0x94 + column; 00249 case 3: 00250 return 0xd4 + column; 00251 } 00252 case LCD16x2B: 00253 return 0x80 + (row * 40) + column; 00254 case LCD16x2: 00255 case LCD20x2: 00256 default: 00257 return 0x80 + (row * 0x40) + column; 00258 } 00259 } 00260 00261 int TextLCD::columns() 00262 { 00263 switch (_type) { 00264 case LCD20x4: 00265 case LCD20x2: 00266 return 20; 00267 case LCD16x2: 00268 case LCD16x2B: 00269 default: 00270 return 16; 00271 } 00272 } 00273 00274 int TextLCD::rows() 00275 { 00276 switch (_type) { 00277 case LCD20x4: 00278 return 4; 00279 case LCD16x2: 00280 case LCD16x2B: 00281 case LCD20x2: 00282 default: 00283 return 2; 00284 } 00285 } 00286 00287 void TextLCD::print_LCD(char* Pbuffer,char line_num, char offset) 00288 { 00289 unsigned int i=0; 00290 00291 int a = address(offset,line_num); 00292 writeCommand(a); 00293 00294 while (Pbuffer[i] != 0) 00295 { 00296 writeData(Pbuffer[i]); 00297 i++; 00298 } 00299 }
Generated on Wed Jul 27 2022 12:02:53 by
