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.
NewTextLCD.cpp
00001 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 00002 * Copyright (c) 2007-2010, sford, http://mbed.org 00003 * Changes by Erik Kerger 00004 * 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a copy 00007 * of this software and associated documentation files (the "Software"), to deal 00008 * in the Software without restriction, including without limitation the rights 00009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00010 * copies of the Software, and to permit persons to whom the Software is 00011 * furnished to do so, subject to the following conditions: 00012 * 00013 * The above copyright notice and this permission notice shall be included in 00014 * all copies or substantial portions of the Software. 00015 * 00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00022 * THE SOFTWARE. 00023 */ 00024 00025 #include "NewTextLCD.h" 00026 #include "mbed.h" 00027 00028 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d4, PinName d5, 00029 PinName d6, PinName d7, LCDType type) : _rs(rs), _rw(rw), _e(e), 00030 _d(d4, d5, d6, d7), _type(type) { 00031 _rs = 0; // command mode 00032 _rw = 0; // r/w in defined state 00033 _e = 0; // 0 --> inactive (most important change to original TextLCD) 00034 00035 if (_type != LCDuser) 00036 setLCDparam(_type); // otherwise rows, colums, comdelay, adresses must be set before 00037 00038 wait(0.050f); // Wait 50ms to ensure powered up 00039 00040 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00041 // this sets controler into 8 bit mode, so we have a defined state 00042 for (int i=0; i<3; i++) { 00043 _e = 1; 00044 __nop(); 00045 __nop(); 00046 _d = 0x3; 00047 __nop(); // data setup time at least 60 ns 00048 _e = 0; 00049 wait(0.005f); // 5ms 00050 } 00051 _e = 1; 00052 __nop(); 00053 __nop(); 00054 _d = 0x2; // 4 Bit mode (after this command, we have to send 2 nibbles) 00055 __nop(); 00056 _e = 0; 00057 wait_us(LCDparam.delay); 00058 00059 writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7 00060 writeCommand(0x08); // Display off 00061 cls(); // clear display, reset _column and _row 00062 writeCommand(0x04); // cursor right, Display is not shifted 00063 writeCommand(0x0C); // Display on , Cursor off 00064 } 00065 00066 void TextLCD::character(int column, int row, int c) { 00067 int a = 0x80 | (LCDparam.adresses[row & 3] + column); 00068 writeCommand(a); // set cursor address 00069 writeData(c); // write char 00070 } 00071 00072 void TextLCD::cls() { 00073 writeCommand(0x01); // cls, and set cursor to 0 00074 locate(0, 0); // set internal position 00075 wait_us(45 * LCDparam.delay); // CLS need much time 00076 } 00077 00078 void TextLCD::locate(int column, int row) { 00079 _column = column; // set position for next char 00080 _row = row; // note: cursor is not set yet 00081 } 00082 00083 00084 00085 int TextLCD::_putc(int value) { 00086 if (value == '\n') { 00087 _column = 0; 00088 _row++; 00089 if (_row >= LCDparam.rows) { 00090 _row = 0; 00091 } 00092 } else { 00093 character(_column, _row, value); 00094 _column++; 00095 if (_column >= LCDparam.columns) { 00096 _column = 0; 00097 _row++; 00098 if (_row >= LCDparam.rows) { 00099 _row = 0; 00100 } 00101 } 00102 } 00103 return value; 00104 } 00105 00106 // Dummy function - read not supported 00107 int TextLCD::_getc() { 00108 return -1; 00109 } 00110 00111 void TextLCD::writeByte(int value) { 00112 _e = 1; 00113 __nop(); 00114 _d = value >> 4; // higher nibble first 00115 __nop(); 00116 _e = 0; 00117 __nop(); 00118 __nop(); 00119 _e = 1; 00120 __nop(); 00121 _d = value >> 0; // then lower 00122 __nop(); 00123 _e = 0; 00124 } 00125 00126 void TextLCD::writeCommand(int command) { 00127 _rs = 0; 00128 writeByte(command); 00129 wait_us(LCDparam.delay); 00130 } 00131 00132 00133 void TextLCD::writeData(int data) { 00134 _rs = 1; 00135 writeByte(data); 00136 wait_us(LCDparam.delay); 00137 } 00138 00139 00140 // set user defined char 00141 void TextLCD::writeCGRAM(int address, int pattern[8]){ 00142 int i; 00143 address = address & 7; //max 8 char 00144 for(i=0;i<8;i++){ 00145 writeCommand(0x40 | (address * 8) + i); 00146 writeData(pattern[i]); 00147 } 00148 } 00149 00150 void TextLCD::setLCDparam(LCDType _type){ 00151 switch (_type) { 00152 00153 case LCD16x2: 00154 case LCD16x2B: 00155 LCDparam.columns = 16; 00156 break; 00157 case LCD20x2: 00158 case LCD20x4: 00159 LCDparam.columns = 20; 00160 break; 00161 case LCD24x2: 00162 LCDparam.columns = 24; 00163 break; 00164 } 00165 if (_type == LCD20x4) 00166 LCDparam.rows = 4; 00167 else 00168 LCDparam.rows = 2; 00169 00170 LCDparam.adresses[0] = 0; 00171 00172 if (_type == LCD16x2B) 00173 LCDparam.adresses[1] = 40; 00174 else 00175 LCDparam.adresses[1] = 0x40; 00176 00177 if (_type == LCD20x4) { 00178 LCDparam.adresses[2] = 0x14; 00179 LCDparam.adresses[3] = 0x54;} 00180 else { 00181 LCDparam.adresses[2] = 0; 00182 LCDparam.adresses[3] = 0;} 00183 00184 LCDparam.delay = 50; // 50 us delays as default 00185 } 00186
Generated on Wed Jul 13 2022 07:13:43 by
1.7.2