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.
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 e, PinName d4, PinName d5, 00027 PinName d6, PinName d7, LCDType type) : _rs(rs), 00028 _e(e), _d(d4, d5, d6, d7), 00029 _type(type) { 00030 00031 _e = 1; 00032 _rs = 0; // command mode 00033 00034 wait(0.015); // Wait 15ms 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 writeByte(0x3); 00039 wait(0.00164); // this command takes 1.64ms, so wait for it 00040 } 00041 writeByte(0x2); // 4-bit mode 00042 wait(0.000040f); // most instructions take 40us 00043 00044 writeCommand(0x28); // Function set 001 BW N F - - 00045 writeCommand(0x0C); 00046 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00047 cls(); 00048 } 00049 00050 00051 void TextLCD::init(void) 00052 { 00053 _e = 1; 00054 _rs = 0; // command mode 00055 00056 wait(0.015); // Wait 15ms to ensure powered up 00057 00058 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00059 for (int i=0; i<3; i++) { 00060 writeByte(0x3); 00061 wait(0.00164); // this command takes 1.64ms, so wait for it 00062 } 00063 writeByte(0x2); // 4-bit mode 00064 wait(0.000040f); // most instructions take 40us 00065 00066 writeCommand(0x28); // Function set 001 BW N F - - 00067 writeCommand(0x0C); 00068 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00069 } 00070 00071 00072 void TextLCD::character(int column, int row, int c) { 00073 int a = address(column, row); 00074 writeCommand(a); 00075 writeData(c); 00076 } 00077 00078 void TextLCD::cls() { 00079 writeCommand(0x01); // cls, and set cursor to 0 00080 wait(0.00164f); // This command takes 1.64 ms 00081 locate(0, 0); 00082 } 00083 00084 void TextLCD::locate(int column, int row) { 00085 _column = column; 00086 _row = row; 00087 } 00088 00089 int TextLCD::_putc(int value) { 00090 if (value == '\n') { 00091 _column = 0; 00092 _row++; 00093 if (_row >= rows()) { 00094 _row = 0; 00095 } 00096 } else { 00097 character(_column, _row, value); 00098 _column++; 00099 if (_column >= columns()) { 00100 _column = 0; 00101 _row++; 00102 if (_row >= rows()) { 00103 _row = 0; 00104 } 00105 } 00106 } 00107 return value; 00108 } 00109 00110 int TextLCD::_getc() { 00111 return -1; 00112 } 00113 00114 void TextLCD::writeByte(int value) { 00115 _d = value >> 4; 00116 wait(0.000040f); // most instructions take 40us 00117 _e = 0; 00118 wait(0.000040f); 00119 _e = 1; 00120 _d = value >> 0; 00121 wait(0.000040f); 00122 _e = 0; 00123 wait(0.000040f); // most instructions take 40us 00124 _e = 1; 00125 } 00126 00127 void TextLCD::writeCommand(int command) { 00128 _rs = 0; 00129 writeByte(command); 00130 } 00131 00132 void TextLCD::writeData(int data) { 00133 _rs = 1; 00134 writeByte(data); 00135 } 00136 00137 int TextLCD::address(int column, int row) { 00138 switch (_type) { 00139 case LCD20x4: 00140 switch (row) { 00141 case 0: 00142 return 0x80 + column; 00143 case 1: 00144 return 0xc0 + column; 00145 case 2: 00146 return 0x94 + column; 00147 case 3: 00148 return 0xd4 + column; 00149 } 00150 case LCD16x2B: 00151 return 0x80 + (row * 40) + column; 00152 case LCD16x2: 00153 case LCD20x2: 00154 default: 00155 return 0x80 + (row * 0x40) + column; 00156 } 00157 } 00158 00159 int TextLCD::columns() { 00160 switch (_type) { 00161 case LCD20x4: 00162 case LCD20x2: 00163 return 20; 00164 case LCD16x2: 00165 case LCD16x2B: 00166 default: 00167 return 16; 00168 } 00169 } 00170 00171 int TextLCD::rows() { 00172 switch (_type) { 00173 case LCD20x4: 00174 return 4; 00175 case LCD16x2: 00176 case LCD16x2B: 00177 case LCD20x2: 00178 default: 00179 return 2; 00180 } 00181 }
Generated on Mon Dec 19 2022 23:45:35 by
1.7.2