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 TextLCD_piano by
TextLCD.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 "TextLCD.h" 00024 #include "mbed.h" 00025 00026 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, 00027 PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw), 00028 _e(e), _d(d0, d1, d2, d3), _type(type) { 00029 _rs = 0; // command mode 00030 _rw = 0; 00031 _e = 0; 00032 _d.output(); // data out 00033 00034 wait(0.05); // Wait 50ms to ensure powered up 00035 00036 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00037 for (int i=0; i<3; i++) { 00038 _e = 1; 00039 wait(0.000001f); 00040 _d = 0x3; 00041 wait(0.000001f); 00042 _e = 0; 00043 wait(0.004f); // 4ms 00044 } 00045 _e = 1; 00046 wait(0.000001f); 00047 _d = 0x2; // 4 Bit mode 00048 wait(0.000001f); 00049 _e = 0; 00050 00051 writeCommand(0x28); // Function set 4 Bit, 2Line, 5*7 00052 writeCommand(0x08); // Display off 00053 writeCommand(0x01); // clear Display 00054 writeCommand(0x04); // cursor right, Display is not shifted 00055 writeCommand(0x0C); // Display on , Cursor off 00056 } 00057 00058 void TextLCD::character(int column, int row, int c) { 00059 int a = address(column, row); 00060 writeCommand(a); 00061 writeData(c); 00062 } 00063 00064 void TextLCD::cls() { 00065 writeCommand(0x01); // cls, and set cursor to 0 00066 wait(0.00164f); 00067 locate(0, 0); 00068 } 00069 00070 void TextLCD::locate(int column, int row) { 00071 _column = column; 00072 _row = row; 00073 } 00074 00075 /**int TextLCD::putc(int values) { 00076 return(_putc(values)); 00077 }**/ 00078 00079 int TextLCD::_putc(int value) { 00080 if (value == '\n') { 00081 _column = 0; 00082 _row++; 00083 if (_row >= rows()) { 00084 _row = 0; 00085 } 00086 } else { 00087 character(_column, _row, value); 00088 _column++; 00089 if (_column >= columns()) { 00090 _column = 0; 00091 _row++; 00092 if (_row >= rows()) { 00093 _row = 0; 00094 } 00095 } 00096 } 00097 return value; 00098 } 00099 00100 int TextLCD::_getc() { 00101 int a = address(_column, _row); 00102 writeCommand(a); 00103 return (readData()); 00104 } 00105 00106 void TextLCD::writeByte(int value) { 00107 _e = 1; 00108 wait(0.000040f); 00109 _d = value >> 4; 00110 wait(0.000040f); 00111 _e = 0; 00112 wait(0.000040f); 00113 _e = 1; 00114 wait(0.000040f); 00115 _d = value >> 0; 00116 wait(0.000040f); 00117 _e = 0; 00118 } 00119 00120 void TextLCD::writeCommand(int command) { 00121 waitBusy(); // check if display is ready 00122 _rs = 0; 00123 writeByte(command); 00124 } 00125 00126 int TextLCD::readData(){ 00127 int input; 00128 waitBusy(); 00129 _rw = 1; 00130 _rs = 1; 00131 wait(0.000001f); 00132 _d.input(); // switch Data port to input 00133 _e = 1; 00134 wait(0.000001f); 00135 input = _d.read() << 4; // high nibble 00136 _e = 0; 00137 wait(0.000001f); 00138 _e = 1; 00139 wait(0.000001f); 00140 input = input | _d.read(); // low nibble 00141 _e = 0; 00142 return (input); 00143 } 00144 00145 void TextLCD::waitBusy(){ 00146 int input; 00147 _rw = 1; 00148 _rs = 0; 00149 wait(0.000001f); 00150 _d.input(); // switch Data port to input 00151 do{ 00152 _e = 1; 00153 wait(0.000001f); 00154 input = _d.read(); 00155 _e = 0; 00156 wait(0.000001f); 00157 _e = 1; 00158 wait(0.000001f); 00159 _e = 0; 00160 }while((0x8 & input) == 0x8); // wait until display is ready 00161 _rw = 0; 00162 _d.output(); // switch port back to output 00163 } 00164 00165 void TextLCD::writeData(int data) { 00166 waitBusy(); 00167 _rs = 1; 00168 writeByte(data); 00169 } 00170 00171 00172 // set user defined char 00173 void TextLCD::writeCGRAM(int address, int pattern[8]){ 00174 int i; 00175 address = address & 0x07; //max 8 char 00176 for(i=0;i<8;i++){ 00177 waitBusy(); // check if display is ready 00178 _rs = 0; 00179 writeByte(0x40 + address * 8 + i); 00180 writeData(pattern[i]); 00181 } 00182 } 00183 00184 00185 int TextLCD::address(int column, int row) { 00186 switch (_type) { 00187 case LCD20x4: 00188 switch (row) { 00189 case 0: 00190 return 0x80 + column; 00191 case 1: 00192 return 0xc0 + column; 00193 case 2: 00194 return 0x94 + column; 00195 case 3: 00196 return 0xd4 + column; 00197 } 00198 case LCD16x2B: 00199 return 0x80 + (row * 40) + column; 00200 case LCD16x2: 00201 case LCD20x2: 00202 default: 00203 return 0x80 + (row * 0x40) + column; 00204 } 00205 } 00206 00207 int TextLCD::columns() { 00208 switch (_type) { 00209 case LCD20x4: 00210 case LCD20x2: 00211 return 20; 00212 case LCD16x2: 00213 case LCD16x2B: 00214 default: 00215 return 16; 00216 } 00217 } 00218 00219 int TextLCD::rows() { 00220 switch (_type) { 00221 case LCD20x4: 00222 return 4; 00223 case LCD16x2: 00224 case LCD16x2B: 00225 case LCD20x2: 00226 default: 00227 return 2; 00228 } 00229 }
Generated on Tue Jul 19 2022 04:04:32 by
1.7.2
