Initial commit
Fork of TextLCD by
Embed:
(wiki syntax)
Show/hide line numbers
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 using an SPI 74595 portexpander 00218 * 00219 * @param spi SPI Bus 00220 * @param cs chip select pin (active low) 00221 * @param type Sets the panel size/addressing mode (default = LCD16x2) 00222 * @param ctrl LCD controller (default = HD44780) 00223 */ 00224 TextLCD(SPI *spi, PinName cs, PinName rs, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780); 00225 00226 00227 #if DOXYGEN_ONLY 00228 /** Write a character to the LCD 00229 * 00230 * @param c The character to write to the display 00231 */ 00232 int putc(int c); 00233 00234 /** Write a formated string to the LCD 00235 * 00236 * @param format A printf-style format string, followed by the 00237 * variables to use in formating the string. 00238 */ 00239 int printf(const char* format, ...); 00240 #endif 00241 00242 /** Locate to a screen column and row 00243 * 00244 * @param column The horizontal position from the left, indexed from 0 00245 * @param row The vertical position from the top, indexed from 0 00246 */ 00247 void locate(int column, int row); 00248 00249 00250 /** Return the memoryaddress of screen column and row location 00251 * 00252 * @param column The horizontal position from the left, indexed from 0 00253 * @param row The vertical position from the top, indexed from 0 00254 * @param return The memoryaddress of screen column and row location 00255 */ 00256 int getAddress(int column, int row); 00257 00258 00259 /** Set the memoryaddress of screen column and row location 00260 * 00261 * @param column The horizontal position from the left, indexed from 0 00262 * @param row The vertical position from the top, indexed from 0 00263 */ 00264 void setAddress(int column, int row); 00265 00266 00267 /** Clear the screen and locate to 0,0 */ 00268 void cls(); 00269 00270 /** Return the number of rows 00271 * 00272 * @param return The number of rows 00273 */ 00274 int rows(); 00275 00276 /** Return the number of columns 00277 * 00278 * @param return The number of columns 00279 */ 00280 int columns(); 00281 00282 /** Set the Cursormode 00283 * 00284 * @param cursorMode The Cursor mode (CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn) 00285 */ 00286 void setCursor(LCDCursor cursorMode); 00287 00288 00289 /** Set the Displaymode 00290 * 00291 * @param displayMode The Display mode (DispOff, DispOn) 00292 */ 00293 void setMode(TextLCD::LCDMode displayMode); 00294 00295 00296 /** Set User Defined Characters 00297 * 00298 * @param unsigned char c The Index of the UDC (0..7) 00299 * @param char *udc_data The bitpatterns for the UDC (8 bytes of 5 significant bits) 00300 */ 00301 void setUDC(unsigned char c, char *udc_data); 00302 00303 00304 protected: 00305 /* LCD Bus control */ 00306 enum _LCDBus { 00307 _PinBus, /*< Regular mbed pins */ 00308 _I2CBus, /*< I2C PCF8574 Portexpander */ 00309 _SPIBus /*< SPI 74595 Shiftregister */ 00310 }; 00311 00312 /* LCD controller select, mainly used for LCD40x4 */ 00313 enum _LCDCtrl_Idx { 00314 _LCDCtrl_0, /*< Primary */ 00315 _LCDCtrl_1, /*< Secondary */ 00316 }; 00317 00318 // Stream implementation functions 00319 virtual int _putc(int value); 00320 virtual int _getc(); 00321 00322 //Low level methods for LCD controller 00323 void _init(); 00324 void _initCtrl(); 00325 int _address(int column, int row); 00326 void _setCursor(TextLCD::LCDCursor show); 00327 void _setUDC(unsigned char c, char *udc_data); 00328 void _setCursorAndDisplayMode(TextLCD::LCDMode displayMode, TextLCD::LCDCursor cursorType); 00329 00330 //Low level write operations to LCD controller 00331 void _writeNibble(int value); 00332 void _writeByte(int value); 00333 void _writeCommand(int command); 00334 void _writeData(int data); 00335 00336 //Low level writes to LCD Bus (serial or parallel) 00337 void _setEnable(bool value); 00338 void _setRS(bool value); 00339 void _setBL(bool value); 00340 void _setData(int value); 00341 void _setCS(bool value); 00342 00343 //Low level writes to LCD serial bus only 00344 void _writeBus(); 00345 00346 00347 // Regular mbed pins bus 00348 DigitalOut _rs, _e, _bl, _e2; 00349 BusOut _d; 00350 00351 // I2C bus 00352 I2C *_i2c; 00353 unsigned char _slaveAddress; 00354 00355 // SPI bus 00356 SPI *_spi; 00357 DigitalOut _cs; 00358 00359 //Bus Interface type 00360 _LCDBus _busType; 00361 00362 // Internal bus mirror value for serial bus only 00363 char _lcd_bus; 00364 00365 //Display type 00366 LCDType _type; 00367 00368 //Display type 00369 LCDMode _currentMode; 00370 00371 //Controller type 00372 LCDCtrl _ctrl; 00373 00374 //Controller select, mainly used for LCD40x4 00375 _LCDCtrl_Idx _ctrl_idx; 00376 00377 // Cursor 00378 int _column; 00379 int _row; 00380 LCDCursor _currentCursor; 00381 }; 00382 00383 #endif
Generated on Sun Jul 17 2022 18:32:24 by 1.7.2