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