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.
Dependencies: mbed Servo HC-SR04 max6675
TextLCD.cpp
00001 /* mbed I2CTextLCD Library, for a 4-bit LCD driven by I2C and a PCF8574 00002 * Copyright (c) 2007-2010, sford, rcocking 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 /* This is a hack of Simon Ford's direct-driven TextLCD code. 00024 * It should be refactored to extract a common superclass 00025 * but my C++ isn't yet good enough :) 00026 * 00027 * The code assumes the following connections between the PCF8574 00028 * and the LCD 00029 * 00030 * nc - D0 00031 * nc - D1 00032 * nc - D2 00033 * nc - D3 00034 * P0 - D4 00035 * P1 - D5 00036 * P2 - D6 00037 * P3 - D7 00038 * P4 - E 00039 * P5 - nc 00040 * P6 - nc 00041 * P7 - RS 00042 * gnd - R/W 00043 * 00044 * D0-3 of the LCD are not connected because we work in 4-bit mode 00045 * R/W is hardwired to gound, as we only ever write to the LCD 00046 * A0-2 on the PCF8574 can be set in any combination; you will need to modify 00047 * the I2C address in the I2CTextLCD constructor. 00048 * Remember that the mbed uses 8-bit addresses, which should be 00049 * in the range 0x40-0x4E for the PCF8574 00050 */ 00051 #include "TextLCD.h" 00052 #include "mbed.h" 00053 00054 TextLCD::TextLCD(PinName sda, PinName scl, int i2cAddress, LCDType type) : _i2c(sda, scl), 00055 _type(type) { 00056 _i2cAddress = i2cAddress; 00057 wait(0.015); // Wait 15ms to ensure powered up 00058 00059 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00060 for (int i=0; i<3; i++) { 00061 writeByte(0x3, false); 00062 wait(0.00164); // this command takes 1.64ms, so wait for it 00063 } 00064 writeByte(0x2, false); // 4-bit mode 00065 wait(0.000040f); // most instructions take 40us 00066 00067 writeCommand(0x28); // Function set 001 BW N F - - 00068 writeCommand(0x0C); 00069 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00070 cls(); 00071 } 00072 00073 void TextLCD::character(int column, int row, int c) { 00074 int a = address(column, row); 00075 writeCommand(a); 00076 writeData(c); 00077 } 00078 00079 void TextLCD::cls() { 00080 writeCommand(0x01); // cls, and set cursor to 0 00081 wait(0.00164f); // This command takes 1.64 ms 00082 locate(0, 0); 00083 } 00084 00085 void TextLCD::locate(int column, int row) { 00086 _column = column; 00087 _row = row; 00088 } 00089 00090 int TextLCD::_putc(int value) { 00091 if (value == '\n') { 00092 _column = 0; 00093 _row++; 00094 if (_row >= rows()) { 00095 _row = 0; 00096 } 00097 } else { 00098 character(_column, _row, value); 00099 _column++; 00100 if (_column >= columns()) { 00101 _column = 0; 00102 _row++; 00103 if (_row >= rows()) { 00104 _row = 0; 00105 } 00106 } 00107 } 00108 return value; 00109 } 00110 00111 int TextLCD::_getc() { 00112 return -1; 00113 } 00114 00115 void TextLCD::writeI2CByte(int data) { 00116 // equivalent to writeI2CByte 00117 char * cmd = new char[1]; 00118 cmd[0] = data; 00119 _i2c.write(_i2cAddress, cmd, 1); 00120 } 00121 00122 void TextLCD::writeNibble(int data, bool rs) { 00123 data<<=4; 00124 data|=8; 00125 if (rs) { 00126 data = data | RS_ON; // set rs bit 00127 } 00128 data |= E_ON; // E on 00129 writeI2CByte(data); 00130 data ^= E_ON; // E off 00131 wait_us(1); 00132 writeI2CByte(data); 00133 wait_us(1000); 00134 } 00135 00136 void TextLCD::writeByte(int data, bool rs) { 00137 writeNibble(data >> 4, rs); 00138 writeNibble(data & 0x0F, rs); 00139 } 00140 00141 void TextLCD::writeCommand(int command) { 00142 // equivalent to ard commandWrite 00143 writeByte(command, false); 00144 } 00145 00146 void TextLCD::writeData(int data) { 00147 00148 writeByte(data, true); 00149 } 00150 00151 int TextLCD::address(int column, int row) { 00152 switch (_type) { 00153 case LCD20x4: 00154 switch (row) { 00155 case 0: 00156 return 0x80 + column; 00157 case 1: 00158 return 0xc0 + column; 00159 case 2: 00160 return 0x94 + column; 00161 case 3: 00162 return 0xd4 + column; 00163 } 00164 case LCD16x2B: 00165 return 0x80 + (row * 40) + column; 00166 case LCD16x2: 00167 case LCD20x2: 00168 default: 00169 return 0x80 + (row * 0x40) + column; 00170 } 00171 } 00172 00173 int TextLCD::columns() { 00174 switch (_type) { 00175 case LCD20x4: 00176 case LCD20x2: 00177 return 20; 00178 case LCD16x2: 00179 case LCD16x2B: 00180 default: 00181 return 16; 00182 } 00183 } 00184 00185 int TextLCD::rows() { 00186 switch (_type) { 00187 case LCD20x4: 00188 return 4; 00189 case LCD16x2: 00190 case LCD16x2B: 00191 case LCD20x2: 00192 default: 00193 return 2; 00194 } 00195 }
Generated on Thu Aug 4 2022 20:28:15 by
1.7.2