Initial commit

Fork of TextLCD by Wim Huiskamp

Committer:
wim
Date:
Sun Feb 10 18:43:51 2013 +0000
Revision:
14:0c32b66b14b8
Parent:
13:24506ba22480
Child:
15:b70ebfffb258
Added support for I2C and SPI bus interfaces

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:ac48b187213c 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
simon 6:e4cb7ddee0d3 2 * Copyright (c) 2007-2010, sford, http://mbed.org
wim 13:24506ba22480 3 * 2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs
wim 13:24506ba22480 4 * 2013, v02: WH, Added I2C and SPI bus interfaces
simon 1:ac48b187213c 5 *
simon 1:ac48b187213c 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:ac48b187213c 7 * of this software and associated documentation files (the "Software"), to deal
simon 1:ac48b187213c 8 * in the Software without restriction, including without limitation the rights
simon 1:ac48b187213c 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:ac48b187213c 10 * copies of the Software, and to permit persons to whom the Software is
simon 1:ac48b187213c 11 * furnished to do so, subject to the following conditions:
simon 2:227356c7d12c 12 *
simon 1:ac48b187213c 13 * The above copyright notice and this permission notice shall be included in
simon 1:ac48b187213c 14 * all copies or substantial portions of the Software.
simon 2:227356c7d12c 15 *
simon 1:ac48b187213c 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:ac48b187213c 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:ac48b187213c 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:ac48b187213c 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:ac48b187213c 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:ac48b187213c 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:ac48b187213c 22 * THE SOFTWARE.
simon 1:ac48b187213c 23 */
simon 1:ac48b187213c 24
simon 1:ac48b187213c 25 #ifndef MBED_TEXTLCD_H
simon 1:ac48b187213c 26 #define MBED_TEXTLCD_H
simon 1:ac48b187213c 27
simon 1:ac48b187213c 28 #include "mbed.h"
simon 2:227356c7d12c 29
simon 5:a53b3e2d6f1e 30 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
simon 2:227356c7d12c 31 *
wim 10:dd9b3a696acd 32 * Currently supports 8x1, 8x2, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4 and 40x2 panels
simon 2:227356c7d12c 33 *
simon 2:227356c7d12c 34 * @code
simon 2:227356c7d12c 35 * #include "mbed.h"
simon 2:227356c7d12c 36 * #include "TextLCD.h"
simon 5:a53b3e2d6f1e 37 *
wim 11:9ec02df863a1 38 * TextLCD lcd(p15, p16, p17, p18, p19, p20); // RS, E, D4-D7
simon 5:a53b3e2d6f1e 39 *
simon 2:227356c7d12c 40 * int main() {
simon 2:227356c7d12c 41 * lcd.printf("Hello World!\n");
simon 2:227356c7d12c 42 * }
simon 2:227356c7d12c 43 * @endcode
simon 2:227356c7d12c 44 */
wim 8:03116f75b66e 45
wim 13:24506ba22480 46
wim 13:24506ba22480 47 //Pin Defines for I2C PCF8574 and SPI 74595 Bus
wim 13:24506ba22480 48 //LCD and serial portexpanders should be wired accordingly
wim 13:24506ba22480 49 //Note: LCD RW pin must be connected to GND
wim 13:24506ba22480 50 // E2 may be used for future expansion to LCD40x4
wim 13:24506ba22480 51 // BL may be used for future expansion to control backlight
wim 13:24506ba22480 52 //
wim 13:24506ba22480 53 #define D_LCD_PIN_D4 0x00
wim 13:24506ba22480 54 #define D_LCD_PIN_D5 0x01
wim 13:24506ba22480 55 #define D_LCD_PIN_D6 0x02
wim 13:24506ba22480 56 #define D_LCD_PIN_D7 0x03
wim 13:24506ba22480 57 #define D_LCD_PIN_RS 0x04
wim 13:24506ba22480 58 #define D_LCD_PIN_E 0x05
wim 13:24506ba22480 59 #define D_LCD_PIN_E2 0x06
wim 13:24506ba22480 60 #define D_LCD_PIN_BL 0x07
wim 13:24506ba22480 61
wim 13:24506ba22480 62 #define D_LCD_BUS_MSK 0x0F
wim 13:24506ba22480 63
wim 13:24506ba22480 64 //Bitpattern Defines for I2C PCF8574 and SPI 74595 Bus
wim 13:24506ba22480 65 //
wim 13:24506ba22480 66 #define D_LCD_D4 (1<<D_LCD_PIN_D4)
wim 13:24506ba22480 67 #define D_LCD_D5 (1<<D_LCD_PIN_D5)
wim 13:24506ba22480 68 #define D_LCD_D6 (1<<D_LCD_PIN_D6)
wim 13:24506ba22480 69 #define D_LCD_D7 (1<<D_LCD_PIN_D7)
wim 13:24506ba22480 70 #define D_LCD_RS (1<<D_LCD_PIN_RS)
wim 13:24506ba22480 71 #define D_LCD_E (1<<D_LCD_PIN_E)
wim 13:24506ba22480 72 #define D_LCD_E2 (1<<D_LCD_PIN_E2)
wim 13:24506ba22480 73 #define D_LCD_BL (1<<D_LCD_PIN_BL)
wim 13:24506ba22480 74
wim 13:24506ba22480 75
wim 13:24506ba22480 76
wim 13:24506ba22480 77 /** Some sample User Defined Chars 5x7 dots */
wim 11:9ec02df863a1 78 const char udc_ae[] = {0x00, 0x00, 0x1B, 0x05, 0x1F, 0x14, 0x1F, 0x00}; //æ
wim 11:9ec02df863a1 79 const char udc_0e[] = {0x00, 0x00, 0x0E, 0x13, 0x15, 0x19, 0x0E, 0x00}; //ø
wim 11:9ec02df863a1 80 const char udc_ao[] = {0x0E, 0x0A, 0x0E, 0x01, 0x0F, 0x11, 0x0F, 0x00}; //å
wim 11:9ec02df863a1 81 const char udc_AE[] = {0x0F, 0x14, 0x14, 0x1F, 0x14, 0x14, 0x17, 0x00}; //Æ
wim 11:9ec02df863a1 82 const char udc_0E[] = {0x0E, 0x13, 0x15, 0x15, 0x15, 0x19, 0x0E, 0x00}; //Ø
wim 11:9ec02df863a1 83 const char udc_AA[] = {0x0E, 0x0A, 0x0E, 0x11, 0x1F, 0x11, 0x11, 0x00}; //Å
wim 11:9ec02df863a1 84
wim 11:9ec02df863a1 85 const char udc_0[] = {0x18, 0x14, 0x12, 0x11, 0x12, 0x14, 0x18, 0x00}; // |>
wim 11:9ec02df863a1 86 const char udc_1[] = {0x03, 0x05, 0x09, 0x11, 0x09, 0x05, 0x03, 0x00}; // <|
wim 11:9ec02df863a1 87 const char udc_2[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}; // |
wim 11:9ec02df863a1 88 const char udc_3[] = {0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00}; // ||
wim 11:9ec02df863a1 89 const char udc_4[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00}; // |||
wim 11:9ec02df863a1 90 const char udc_5[] = {0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x00}; // =
wim 11:9ec02df863a1 91 const char udc_6[] = {0x15, 0x0a, 0x15, 0x0a, 0x15, 0x0a, 0x15, 0x00}; // checkerboard
wim 11:9ec02df863a1 92 const char udc_7[] = {0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x10, 0x00}; // \
wim 11:9ec02df863a1 93
wim 13:24506ba22480 94 const char udc_degr[] = {0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00}; // Degree symbol
wim 13:24506ba22480 95
wim 13:24506ba22480 96 const char udc_TM_T[] = {0x1F, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00}; // Trademark T
wim 13:24506ba22480 97 const char udc_TM_M[] = {0x11, 0x1B, 0x15, 0x11, 0x00, 0x00, 0x00, 0x00}; // Trademark M
wim 13:24506ba22480 98
wim 13:24506ba22480 99 //const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Full
wim 13:24506ba22480 100 //const char udc_Bat_Ha[] = {0x0E, 0x11, 0x13, 0x17, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Half
wim 13:24506ba22480 101 //const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x00}; // Battery Low
wim 13:24506ba22480 102 const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Full
wim 13:24506ba22480 103 const char udc_Bat_Ha[] = {0x0E, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Half
wim 13:24506ba22480 104 const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F, 0x00}; // Battery Low
wim 13:24506ba22480 105
wim 13:24506ba22480 106 const char udc_bar_1[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}; // Bar 1
wim 13:24506ba22480 107 const char udc_bar_2[] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00}; // Bar 11
wim 13:24506ba22480 108 const char udc_bar_3[] = {0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x00}; // Bar 111
wim 13:24506ba22480 109 const char udc_bar_4[] = {0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x00}; // Bar 1111
wim 13:24506ba22480 110 const char udc_bar_5[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Bar 11111
wim 13:24506ba22480 111
wim 11:9ec02df863a1 112
wim 11:9ec02df863a1 113 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
wim 11:9ec02df863a1 114 *
wim 13:24506ba22480 115 * Currently supports 8x1, 8x2, 16x1, 16x2, 16x4, 20x2, 20x4, 24x2, 24x4 and 40x2 panels
wim 11:9ec02df863a1 116 *
wim 11:9ec02df863a1 117 */
simon 1:ac48b187213c 118 class TextLCD : public Stream {
simon 1:ac48b187213c 119 public:
simon 1:ac48b187213c 120
simon 2:227356c7d12c 121 /** LCD panel format */
simon 1:ac48b187213c 122 enum LCDType {
wim 8:03116f75b66e 123 LCD8x1, /**< 8x1 LCD panel */
wim 13:24506ba22480 124 LCD8x2, /**< 8x2 LCD panel */
wim 13:24506ba22480 125 LCD16x1, /**< 16x1 LCD panel (actually 8x2) */
wim 8:03116f75b66e 126 LCD16x2, /**< 16x2 LCD panel (default) */
wim 8:03116f75b66e 127 LCD16x2B, /**< 16x2 LCD panel alternate addressing */
wim 8:03116f75b66e 128 LCD16x4, /**< 16x4 LCD panel */
wim 8:03116f75b66e 129 LCD20x2, /**< 20x2 LCD panel */
wim 8:03116f75b66e 130 LCD20x4, /**< 20x4 LCD panel */
wim 9:0893d986e717 131 LCD24x2, /**< 24x2 LCD panel */
wim 10:dd9b3a696acd 132 LCD24x4, /**< 24x4 LCD panel, special mode KS0078 */
wim 9:0893d986e717 133 LCD40x2 /**< 40x2 LCD panel */
simon 1:ac48b187213c 134 };
simon 1:ac48b187213c 135
wim 10:dd9b3a696acd 136 /** LCD Cursor control */
wim 10:dd9b3a696acd 137 enum LCDCursor {
wim 12:6bf9d9957d31 138 CurOff_BlkOff, /**< Cursor Off, Blinking Char Off */
wim 12:6bf9d9957d31 139 CurOn_BlkOff, /**< Cursor On, Blinking Char Off */
wim 12:6bf9d9957d31 140 CurOff_BlkOn, /**< Cursor Off, Blinking Char On */
wim 12:6bf9d9957d31 141 CurOn_BlkOn, /**< Cursor On, Blinking Char On */
wim 10:dd9b3a696acd 142 };
wim 10:dd9b3a696acd 143
wim 10:dd9b3a696acd 144
wim 13:24506ba22480 145 /** Create a TextLCD interface for using regural mbed pins
simon 2:227356c7d12c 146 *
simon 2:227356c7d12c 147 * @param rs Instruction/data control line
simon 2:227356c7d12c 148 * @param e Enable line (clock)
simon 7:44f34c09bd37 149 * @param d4-d7 Data lines for using as a 4-bit interface
simon 2:227356c7d12c 150 * @param type Sets the panel size/addressing mode (default = LCD16x2)
simon 2:227356c7d12c 151 */
simon 7:44f34c09bd37 152 TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
wim 13:24506ba22480 153
wim 13:24506ba22480 154 /** Create a TextLCD interface using an I2C PC8574 portexpander
wim 13:24506ba22480 155 *
wim 13:24506ba22480 156 * @param i2c I2C Bus
wim 13:24506ba22480 157 * @param deviceAddress I2C slave address (PCF8574)
wim 13:24506ba22480 158 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wim 13:24506ba22480 159 */
wim 13:24506ba22480 160 TextLCD(I2C *i2c, char deviceAddress, LCDType type = LCD16x2);
wim 13:24506ba22480 161
wim 13:24506ba22480 162
wim 13:24506ba22480 163 /** Create a TextLCD interface using an SPI 74595 portexpander
wim 13:24506ba22480 164 *
wim 13:24506ba22480 165 * @param spi SPI Bus
wim 13:24506ba22480 166 * @param cs chip select pin (active low)
wim 13:24506ba22480 167 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wim 13:24506ba22480 168 */
wim 14:0c32b66b14b8 169 TextLCD(SPI *spi, PinName cs, LCDType type = LCD16x2);
wim 13:24506ba22480 170
simon 2:227356c7d12c 171
simon 2:227356c7d12c 172 #if DOXYGEN_ONLY
simon 2:227356c7d12c 173 /** Write a character to the LCD
simon 2:227356c7d12c 174 *
simon 2:227356c7d12c 175 * @param c The character to write to the display
simon 2:227356c7d12c 176 */
simon 2:227356c7d12c 177 int putc(int c);
simon 2:227356c7d12c 178
simon 2:227356c7d12c 179 /** Write a formated string to the LCD
simon 2:227356c7d12c 180 *
simon 2:227356c7d12c 181 * @param format A printf-style format string, followed by the
simon 2:227356c7d12c 182 * variables to use in formating the string.
simon 2:227356c7d12c 183 */
simon 2:227356c7d12c 184 int printf(const char* format, ...);
simon 2:227356c7d12c 185 #endif
simon 2:227356c7d12c 186
simon 2:227356c7d12c 187 /** Locate to a screen column and row
simon 2:227356c7d12c 188 *
simon 2:227356c7d12c 189 * @param column The horizontal position from the left, indexed from 0
simon 2:227356c7d12c 190 * @param row The vertical position from the top, indexed from 0
simon 2:227356c7d12c 191 */
simon 1:ac48b187213c 192 void locate(int column, int row);
simon 2:227356c7d12c 193
wim 10:dd9b3a696acd 194
wim 10:dd9b3a696acd 195 /** Return the memoryaddress of screen column and row location
wim 10:dd9b3a696acd 196 *
wim 10:dd9b3a696acd 197 * @param column The horizontal position from the left, indexed from 0
wim 10:dd9b3a696acd 198 * @param row The vertical position from the top, indexed from 0
wim 10:dd9b3a696acd 199 * @param return The memoryaddress of screen column and row location
wim 10:dd9b3a696acd 200 */
wim 9:0893d986e717 201 int getAddress(int column, int row);
wim 10:dd9b3a696acd 202
wim 10:dd9b3a696acd 203
wim 10:dd9b3a696acd 204 /** Set the memoryaddress of screen column and row location
wim 10:dd9b3a696acd 205 *
wim 10:dd9b3a696acd 206 * @param column The horizontal position from the left, indexed from 0
wim 10:dd9b3a696acd 207 * @param row The vertical position from the top, indexed from 0
wim 10:dd9b3a696acd 208 */
wim 9:0893d986e717 209 void setAddress(int column, int row);
wim 9:0893d986e717 210
wim 10:dd9b3a696acd 211
simon 2:227356c7d12c 212 /** Clear the screen and locate to 0,0 */
simon 1:ac48b187213c 213 void cls();
simon 2:227356c7d12c 214
wim 10:dd9b3a696acd 215 /** Return the number of rows
wim 10:dd9b3a696acd 216 *
wim 10:dd9b3a696acd 217 * @param return The number of rows
wim 10:dd9b3a696acd 218 */
simon 1:ac48b187213c 219 int rows();
wim 10:dd9b3a696acd 220
wim 10:dd9b3a696acd 221 /** Return the number of columns
wim 10:dd9b3a696acd 222 *
wim 10:dd9b3a696acd 223 * @param return The number of columns
wim 10:dd9b3a696acd 224 */
wim 10:dd9b3a696acd 225 int columns();
simon 2:227356c7d12c 226
wim 11:9ec02df863a1 227 /** Set the Cursormode
wim 11:9ec02df863a1 228 *
wim 11:9ec02df863a1 229 * @param show The Cursor mode (CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn)
wim 11:9ec02df863a1 230 */
wim 13:24506ba22480 231 void setCursor(LCDCursor show);
wim 11:9ec02df863a1 232
wim 11:9ec02df863a1 233
wim 11:9ec02df863a1 234 /** Set User Defined Characters
wim 11:9ec02df863a1 235 *
wim 11:9ec02df863a1 236 * @param unsigned char c The Index of the UDC (0..7)
wim 12:6bf9d9957d31 237 * @param char *udc_data The bitpatterns for the UDC (8 bytes of 5 significant bits)
wim 11:9ec02df863a1 238 */
wim 11:9ec02df863a1 239 void setUDC(unsigned char c, char *udc_data);
wim 11:9ec02df863a1 240
simon 1:ac48b187213c 241 protected:
wim 14:0c32b66b14b8 242 /* LCD Bus control */
wim 13:24506ba22480 243 enum _LCDBus {
wim 14:0c32b66b14b8 244 _PinBus, /*< Regular mbed pins */
wim 14:0c32b66b14b8 245 _I2CBus, /*< I2C PCF8574 Portexpander */
wim 14:0c32b66b14b8 246 _SPIBus /*< SPI 74595 */
wim 13:24506ba22480 247 };
wim 13:24506ba22480 248
simon 1:ac48b187213c 249 // Stream implementation functions
simon 1:ac48b187213c 250 virtual int _putc(int value);
simon 1:ac48b187213c 251 virtual int _getc();
simon 1:ac48b187213c 252
wim 13:24506ba22480 253 void _init();
wim 13:24506ba22480 254 int _address(int column, int row);
wim 13:24506ba22480 255 void _character(int column, int row, int c);
wim 13:24506ba22480 256
wim 13:24506ba22480 257 //Low level writes to LCD Bus (serial or parallel)
wim 13:24506ba22480 258 void _setEnable(bool value);
wim 13:24506ba22480 259 void _setRS(bool value);
wim 13:24506ba22480 260 void _setData(int value);
wim 14:0c32b66b14b8 261 void _setCS(bool value);
wim 14:0c32b66b14b8 262
wim 13:24506ba22480 263 //Low level writes to LCD serial bus only
wim 13:24506ba22480 264 void _writeBus();
wim 13:24506ba22480 265
wim 13:24506ba22480 266 //Low level writes to LCD
wim 13:24506ba22480 267 void _writeByte(int value);
wim 13:24506ba22480 268 void _writeCommand(int command);
wim 13:24506ba22480 269 void _writeData(int data);
wim 13:24506ba22480 270
wim 13:24506ba22480 271 // Regular mbed pins bus
simon 1:ac48b187213c 272 DigitalOut _rs, _e;
simon 1:ac48b187213c 273 BusOut _d;
wim 13:24506ba22480 274
wim 13:24506ba22480 275 // I2C bus
wim 13:24506ba22480 276 I2C *_i2c;
wim 13:24506ba22480 277 unsigned char _slaveAddress;
wim 13:24506ba22480 278
wim 13:24506ba22480 279 // SPI bus
wim 14:0c32b66b14b8 280 SPI *_spi;
wim 14:0c32b66b14b8 281 DigitalOut _cs;
wim 13:24506ba22480 282
wim 13:24506ba22480 283 //Bus Interface type
wim 13:24506ba22480 284 _LCDBus _busType;
wim 13:24506ba22480 285
wim 13:24506ba22480 286 // Internal bus mirror value for serial only
wim 13:24506ba22480 287 char _lcd_bus;
wim 13:24506ba22480 288
wim 13:24506ba22480 289 //Display type
simon 1:ac48b187213c 290 LCDType _type;
simon 1:ac48b187213c 291
wim 13:24506ba22480 292 // Cursor
simon 1:ac48b187213c 293 int _column;
simon 1:ac48b187213c 294 int _row;
wim 10:dd9b3a696acd 295 LCDCursor _cursor;
simon 1:ac48b187213c 296 };
simon 1:ac48b187213c 297
simon 1:ac48b187213c 298 #endif