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: EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem
TextLCD.h
00001 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 00002 * Copyright (c) 2007-2010, sford, http://mbed.org 00003 * 2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs 00004 * 2013, v02: WH, Added I2C and SPI bus interfaces 00005 * 2013, v03: WH, Added support for LCD40x4 which uses 2 controllers 00006 * 2013, v04: WH, Added support for Display On/Off, improved 4bit bootprocess 00007 * 2013, v05: WH, Added support for 8x2B, added some UDCs 00008 * 2013, v06: WH, Added support for devices that use internal DC/DC converters 00009 * 2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a copy 00012 * of this software and associated documentation files (the "Software"), to deal 00013 * in the Software without restriction, including without limitation the rights 00014 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00015 * copies of the Software, and to permit persons to whom the Software is 00016 * furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included in 00019 * all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00022 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00024 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00026 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00027 * THE SOFTWARE. 00028 */ 00029 00030 #ifndef MBED_TEXTLCD_H 00031 #define MBED_TEXTLCD_H 00032 00033 #include "mbed.h" 00034 00035 00036 /** A TextLCD interface for driving 4-bit HD44780-based LCDs 00037 * 00038 * Currently supports 8x1, 8x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels 00039 * Interface options include direct mbed pins, I2C portexpander (PCF8474) or SPI bus shiftregister (74595) 00040 * Supports some controllers that provide internal DC/DC converters for VLCD or VLED. 00041 * 00042 * @code 00043 * #include "mbed.h" 00044 * #include "TextLCD.h" 00045 * 00046 * // I2C Communication 00047 * I2C i2c_lcd(p28,p27); // SDA, SCL 00048 * 00049 * // SPI Communication 00050 * SPI spi_lcd(p5, NC, p7); // MOSI, MISO, SCLK 00051 * 00052 * TextLCD lcd(p15, p16, p17, p18, p19, p20); // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=HD44780 00053 * //TextLCD lcd(&spi_lcd, p8, TextLCD::LCD40x4); // SPI bus, CS pin, LCD Type 00054 * //TextLCD lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type 00055 * //TextLCD lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2, TextLCD::WS0010); // I2C bus, PCF8574 Slaveaddress, LCD Type, Device Type 00056 * 00057 * int main() { 00058 * lcd.printf("Hello World!\n"); 00059 * } 00060 * @endcode 00061 */ 00062 00063 //Pin Defines for I2C PCF8574 and SPI 74595 Bus interfaces 00064 //LCD and serial portexpanders should be wired accordingly 00065 // 00066 #if (1) 00067 //Definitions for hardware used by WH 00068 //Note: LCD RW pin must be connected to GND 00069 // E2 is used for LCD40x4 (second controller) 00070 // BL may be used for future expansion to control backlight 00071 #define D_LCD_PIN_D4 0 00072 #define D_LCD_PIN_D5 1 00073 #define D_LCD_PIN_D6 2 00074 #define D_LCD_PIN_D7 3 00075 #define D_LCD_PIN_RS 4 00076 #define D_LCD_PIN_E 5 00077 #define D_LCD_PIN_E2 6 00078 #define D_LCD_PIN_BL 7 00079 00080 #define D_LCD_PIN_RW D_LCD_PIN_E2 00081 00082 #else 00083 00084 //Definitions for LCD2004 Module from DFROBOT, See http://arduino-info.wikispaces.com/LCD-Blue-I2C 00085 //This hardware is different from earlier/different Arduino I2C LCD displays 00086 //Note: LCD RW pin must be kept LOW 00087 // E2 is not available on default Arduino hardware and does not support LCD40x4 (second controller) 00088 // BL is used to control backlight 00089 #define D_LCD_PIN_RS 0 00090 #define D_LCD_PIN_RW 1 00091 #define D_LCD_PIN_E 2 00092 #define D_LCD_PIN_BL 3 00093 #define D_LCD_PIN_D4 4 00094 #define D_LCD_PIN_D5 5 00095 #define D_LCD_PIN_D6 6 00096 #define D_LCD_PIN_D7 7 00097 00098 #define D_LCD_PIN_E2 D_LCD_PIN_RW 00099 #endif 00100 00101 //Bitpattern Defines for I2C PCF8574 and SPI 74595 Bus 00102 // 00103 #define D_LCD_D4 (1<<D_LCD_PIN_D4) 00104 #define D_LCD_D5 (1<<D_LCD_PIN_D5) 00105 #define D_LCD_D6 (1<<D_LCD_PIN_D6) 00106 #define D_LCD_D7 (1<<D_LCD_PIN_D7) 00107 #define D_LCD_RS (1<<D_LCD_PIN_RS) 00108 #define D_LCD_E (1<<D_LCD_PIN_E) 00109 #define D_LCD_E2 (1<<D_LCD_PIN_E2) 00110 #define D_LCD_BL (1<<D_LCD_PIN_BL) 00111 //#define D_LCD_RW (1<<D_LCD_PIN_RW) 00112 00113 00114 #define D_LCD_BUS_MSK (D_LCD_D4 | D_LCD_D5 | D_LCD_D6 | D_LCD_D7) 00115 #define D_LCD_BUS_DEF 0x00 00116 00117 00118 /** Some sample User Defined Chars 5x7 dots */ 00119 const char udc_ae[] = {0x00, 0x00, 0x1B, 0x05, 0x1F, 0x14, 0x1F, 0x00}; //æ 00120 const char udc_0e[] = {0x00, 0x00, 0x0E, 0x13, 0x15, 0x19, 0x0E, 0x00}; //ø 00121 const char udc_ao[] = {0x0E, 0x0A, 0x0E, 0x01, 0x0F, 0x11, 0x0F, 0x00}; //å 00122 const char udc_AE[] = {0x0F, 0x14, 0x14, 0x1F, 0x14, 0x14, 0x17, 0x00}; //Æ 00123 const char udc_0E[] = {0x0E, 0x13, 0x15, 0x15, 0x15, 0x19, 0x0E, 0x00}; //Ø 00124 const char udc_Ao[] = {0x0E, 0x0A, 0x0E, 0x11, 0x1F, 0x11, 0x11, 0x00}; //Å 00125 const char udc_PO[] = {0x04, 0x0A, 0x0A, 0x1F, 0x1B, 0x1B, 0x1F, 0x00}; //Padlock Open 00126 const char udc_PC[] = {0x1C, 0x10, 0x08, 0x1F, 0x1B, 0x1B, 0x1F, 0x00}; //Padlock Closed 00127 00128 const char udc_0[] = {0x18, 0x14, 0x12, 0x11, 0x12, 0x14, 0x18, 0x00}; // |> 00129 const char udc_1[] = {0x03, 0x05, 0x09, 0x11, 0x09, 0x05, 0x03, 0x00}; // <| 00130 const char udc_2[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}; // | 00131 const char udc_3[] = {0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00}; // || 00132 const char udc_4[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00}; // ||| 00133 const char udc_5[] = {0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x00}; // = 00134 const char udc_6[] = {0x15, 0x0a, 0x15, 0x0a, 0x15, 0x0a, 0x15, 0x00}; // checkerboard 00135 const char udc_7[] = {0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x10, 0x00}; // \ 00136 00137 const char udc_degr[] = {0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00}; // Degree symbol 00138 00139 const char udc_TM_T[] = {0x1F, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00}; // Trademark T 00140 const char udc_TM_M[] = {0x11, 0x1B, 0x15, 0x11, 0x00, 0x00, 0x00, 0x00}; // Trademark M 00141 00142 //const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Full 00143 //const char udc_Bat_Ha[] = {0x0E, 0x11, 0x13, 0x17, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Half 00144 //const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x00}; // Battery Low 00145 const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Full 00146 const char udc_Bat_Ha[] = {0x0E, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Half 00147 const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F, 0x00}; // Battery Low 00148 const char udc_AC[] = {0x0A, 0x0A, 0x1F, 0x11, 0x0E, 0x04, 0x04, 0x00}; // AC Power 00149 00150 //const char udc_smiley[] = {0x00, 0x0A, 0x00, 0x04, 0x11, 0x0E, 0x00, 0x00}; // Smiley 00151 //const char udc_droopy[] = {0x00, 0x0A, 0x00, 0x04, 0x00, 0x0E, 0x11, 0x00}; // Droopey 00152 //const char udc_note[] = {0x01, 0x03, 0x05, 0x09, 0x0B, 0x1B, 0x18, 0x00}; // Note 00153 00154 //const char udc_bar_1[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}; // Bar 1 00155 //const char udc_bar_2[] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00}; // Bar 11 00156 //const char udc_bar_3[] = {0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x00}; // Bar 111 00157 //const char udc_bar_4[] = {0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x00}; // Bar 1111 00158 //const char udc_bar_5[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Bar 11111 00159 00160 00161 /** A TextLCD interface for driving 4-bit HD44780-based LCDs 00162 * 00163 * Currently supports 8x1, 8x2, 12x2, 12x4, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4, 40x2 and 40x4 panels 00164 * 00165 */ 00166 class TextLCD : public Stream { 00167 public: 00168 00169 /** LCD panel format */ 00170 enum LCDType { 00171 LCD8x1, /**< 8x1 LCD panel */ 00172 LCD8x2, /**< 8x2 LCD panel */ 00173 LCD8x2B, /**< 8x2 LCD panel (actually 16x1) */ 00174 LCD12x2, /**< 12x2 LCD panel */ 00175 LCD12x4, /**< 12x4 LCD panel */ 00176 LCD16x1, /**< 16x1 LCD panel (actually 8x2) */ 00177 LCD16x2, /**< 16x2 LCD panel (default) */ 00178 LCD16x2B, /**< 16x2 LCD panel alternate addressing */ 00179 LCD16x4, /**< 16x4 LCD panel */ 00180 LCD20x2, /**< 20x2 LCD panel */ 00181 LCD20x4, /**< 20x4 LCD panel */ 00182 LCD24x2, /**< 24x2 LCD panel */ 00183 LCD24x4, /**< 24x4 LCD panel, special mode KS0078 */ 00184 LCD40x2, /**< 40x2 LCD panel */ 00185 LCD40x4 /**< 40x4 LCD panel, Two controller version */ 00186 }; 00187 00188 /** LCD Controller Device */ 00189 enum LCDCtrl { 00190 HD44780, /**< HD44780 (default) */ 00191 WS0010, /**< WS0010 OLED Controller */ 00192 ST7036 /**< ST7036 */ 00193 }; 00194 00195 00196 /** LCD Cursor control */ 00197 enum LCDCursor { 00198 CurOff_BlkOff = 0x00, /**< Cursor Off, Blinking Char Off */ 00199 CurOn_BlkOff = 0x02, /**< Cursor On, Blinking Char Off */ 00200 CurOff_BlkOn = 0x01, /**< Cursor Off, Blinking Char On */ 00201 CurOn_BlkOn = 0x03 /**< Cursor On, Blinking Char On */ 00202 }; 00203 00204 00205 /** LCD Display control */ 00206 enum LCDMode { 00207 DispOff = 0x00, /**< Display Off */ 00208 DispOn = 0x04 /**< Display On */ 00209 }; 00210 00211 /** LCD Backlight control */ 00212 enum LCDBacklight { 00213 LightOff, /**< Backlight Off */ 00214 LightOn /**< Backlight On */ 00215 }; 00216 00217 /** Create a TextLCD interface for using regular mbed pins 00218 * 00219 * @param rs Instruction/data control line 00220 * @param e Enable line (clock) 00221 * @param d4-d7 Data lines for using as a 4-bit interface 00222 * @param type Sets the panel size/addressing mode (default = LCD16x2) 00223 * @param bl Backlight control line (optional, default = NC) 00224 * @param e2 Enable2 line (clock for second controller, LCD40x4 only) 00225 * @param ctrl LCD controller (default = HD44780) 00226 */ 00227 TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2, PinName bl = NC, PinName e2 = NC, LCDCtrl ctrl = HD44780); 00228 00229 /** Create a TextLCD interface using an I2C PC8574 portexpander 00230 * 00231 * @param i2c I2C Bus 00232 * @param deviceAddress I2C slave address (PCF8574) 00233 * @param type Sets the panel size/addressing mode (default = LCD16x2) 00234 * @param ctrl LCD controller (default = HD44780) 00235 */ 00236 TextLCD(I2C *i2c, char deviceAddress, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780); 00237 00238 00239 /** Create a TextLCD interface using an SPI 74595 portexpander 00240 * 00241 * @param spi SPI Bus 00242 * @param cs chip select pin (active low) 00243 * @param type Sets the panel size/addressing mode (default = LCD16x2) 00244 * @param ctrl LCD controller (default = HD44780) 00245 */ 00246 TextLCD(SPI *spi, PinName cs, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780); 00247 00248 00249 #if DOXYGEN_ONLY 00250 /** Write a character to the LCD 00251 * 00252 * @param c The character to write to the display 00253 */ 00254 int putc(int c); 00255 00256 /** Write a formated string to the LCD 00257 * 00258 * @param format A printf-style format string, followed by the 00259 * variables to use in formating the string. 00260 */ 00261 int printf(const char* format, ...); 00262 #endif 00263 00264 /** Locate to a screen column and row 00265 * 00266 * @param column The horizontal position from the left, indexed from 0 00267 * @param row The vertical position from the top, indexed from 0 00268 */ 00269 void locate(int column, int row); 00270 00271 00272 /** Return the memoryaddress of screen column and row location 00273 * 00274 * @param column The horizontal position from the left, indexed from 0 00275 * @param row The vertical position from the top, indexed from 0 00276 * @param return The memoryaddress of screen column and row location 00277 */ 00278 int getAddress(int column, int row); 00279 00280 00281 /** Set the memoryaddress of screen column and row location 00282 * 00283 * @param column The horizontal position from the left, indexed from 0 00284 * @param row The vertical position from the top, indexed from 0 00285 */ 00286 void setAddress(int column, int row); 00287 00288 00289 /** Clear the screen and locate to 0,0 */ 00290 void cls(); 00291 00292 /** Return the number of rows 00293 * 00294 * @param return The number of rows 00295 */ 00296 int rows(); 00297 00298 /** Return the number of columns 00299 * 00300 * @param return The number of columns 00301 */ 00302 int columns(); 00303 00304 /** Set the Cursormode 00305 * 00306 * @param cursorMode The Cursor mode (CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn) 00307 */ 00308 void setCursor(LCDCursor cursorMode); 00309 00310 00311 /** Set the Displaymode 00312 * 00313 * @param displayMode The Display mode (DispOff, DispOn) 00314 */ 00315 void setMode(TextLCD::LCDMode displayMode); 00316 00317 /** Set the Backlight mode 00318 * 00319 * @param backlightMode The Backlight mode (LightOff, LightOn) 00320 */ 00321 void setBacklight(TextLCD::LCDBacklight backlightMode); 00322 00323 00324 /** Set User Defined Characters 00325 * 00326 * @param unsigned char c The Index of the UDC (0..7) 00327 * @param char *udc_data The bitpatterns for the UDC (8 bytes of 5 significant bits) 00328 */ 00329 void setUDC(unsigned char c, char *udc_data); 00330 00331 00332 protected: 00333 /* LCD Bus control */ 00334 enum _LCDBus { 00335 _PinBus, /*< Regular mbed pins */ 00336 _I2CBus, /*< I2C PCF8574 Portexpander */ 00337 _SPIBus /*< SPI 74595 Shiftregister */ 00338 }; 00339 00340 /* LCD controller select, mainly used for LCD40x4 */ 00341 enum _LCDCtrl_Idx { 00342 _LCDCtrl_0, /*< Primary */ 00343 _LCDCtrl_1, /*< Secondary */ 00344 }; 00345 00346 // Stream implementation functions 00347 virtual int _putc(int value); 00348 virtual int _getc(); 00349 00350 //Low level methods for LCD controller 00351 void _init(); 00352 void _initCtrl(); 00353 int _address(int column, int row); 00354 void _setCursor(TextLCD::LCDCursor show); 00355 void _setUDC(unsigned char c, char *udc_data); 00356 void _setCursorAndDisplayMode(TextLCD::LCDMode displayMode, TextLCD::LCDCursor cursorType); 00357 00358 //Low level write operations to LCD controller 00359 void _writeNibble(int value); 00360 void _writeByte(int value); 00361 void _writeCommand(int command); 00362 void _writeData(int data); 00363 00364 //Low level writes to LCD Bus (serial or parallel) 00365 void _setEnable(bool value); 00366 void _setRS(bool value); 00367 void _setBL(bool value); 00368 void _setData(int value); 00369 void _setCS(bool value); 00370 00371 //Low level writes to LCD serial bus only 00372 void _writeBus(); 00373 00374 00375 // Regular mbed pins bus 00376 DigitalOut _rs, _e, _bl, _e2; 00377 BusOut _d; 00378 00379 // I2C bus 00380 I2C *_i2c; 00381 unsigned char _slaveAddress; 00382 00383 // SPI bus 00384 SPI *_spi; 00385 DigitalOut _cs; 00386 00387 //Bus Interface type 00388 _LCDBus _busType; 00389 00390 // Internal bus mirror value for serial bus only 00391 char _lcd_bus; 00392 00393 //Display type 00394 LCDType _type; 00395 00396 //Display type 00397 LCDMode _currentMode; 00398 00399 //Controller type 00400 LCDCtrl _ctrl; 00401 00402 //Controller select, mainly used for LCD40x4 00403 _LCDCtrl_Idx _ctrl_idx; 00404 00405 // Cursor 00406 int _column; 00407 int _row; 00408 LCDCursor _currentCursor; 00409 }; 00410 00411 #endif
Generated on Tue Jul 12 2022 14:07:59 by
1.7.2