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.
Fork of TextLCD by
Revision 21:4984088e349c, committed 2014-12-05
- Comitter:
- anmar
- Date:
- Fri Dec 05 10:26:07 2014 +0000
- Parent:
- 20:e0da005a777f
- Commit message:
- Initial commit
Changed in this revision
TextLCD.cpp | Show annotated file Show diff for this revision Revisions of this file |
TextLCD.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/TextLCD.cpp Mon May 13 19:29:13 2013 +0000 +++ b/TextLCD.cpp Fri Dec 05 10:26:07 2014 +0000 @@ -31,71 +31,20 @@ #include "mbed.h" -/* Create a TextLCD interface for using regular mbed pins - * - * @param rs Instruction/data control line - * @param e Enable line (clock) - * @param d4-d7 Data lines for using as a 4-bit interface - * @param type Sets the panel size/addressing mode (default = LCD16x2) - * @param bl Backlight control line (optional, default = NC) - * @param e2 Enable2 line (clock for second controller, LCD40x4 only) - * @param ctrl LCD controller (default = HD44780) - */ -TextLCD::TextLCD(PinName rs, PinName e, - PinName d4, PinName d5, PinName d6, PinName d7, - LCDType type, PinName bl, PinName e2, LCDCtrl ctrl) : _rs(rs), _e(e), _bl(bl), _e2(e2), - _d(d4, d5, d6, d7), - _cs(NC), - _type(type), - _ctrl(ctrl) { - _busType = _PinBus; - - _init(); - -} - -/* Create a TextLCD interface using an I2C PC8574 portexpander - * - * @param i2c I2C Bus - * @param deviceAddress I2C slave address (PCF8574) - * @param type Sets the panel size/addressing mode (default = LCD16x2) - * @param ctrl LCD controller (default = HD44780) - */ -TextLCD::TextLCD(I2C *i2c, char deviceAddress, LCDType type, LCDCtrl ctrl) : - _rs(NC), _e(NC), _bl(NC), _e2(NC), - _d(NC), - _i2c(i2c), - _cs(NC), - _type(type), - _ctrl(ctrl) { - - _slaveAddress = deviceAddress; - _busType = _I2CBus; - - - // Init the portexpander bus - _lcd_bus = D_LCD_BUS_DEF; - - // write the new data to the portexpander - _i2c->write(_slaveAddress, &_lcd_bus, 1); - - _init(); - -} - - /* Create a TextLCD interface using an SPI 74595 portexpander + /* Create a TextLCD interface using SPI * * @param spi SPI Bus * @param cs chip select pin (active low) * @param type Sets the panel size/addressing mode (default = LCD16x2) * @param ctrl LCD controller (default = HD44780) */ -TextLCD::TextLCD(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) : - _rs(NC), _e(NC), _bl(NC), _e2(NC), +TextLCD::TextLCD(SPI *spi, PinName cs, PinName rs, LCDType type, LCDCtrl ctrl) : + _e(NC), _bl(NC), _e2(NC), _d(NC), _spi(spi), _cs(cs), + _rs(rs), _type(type), _ctrl(ctrl) { @@ -162,23 +111,20 @@ wait_ms(20); // Wait 20ms to ensure powered up - // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) +/* // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) for (int i=0; i<3; i++) { _writeNibble(0x3); wait_ms(15); // This command takes 1.64ms, so wait for it } _writeNibble(0x2); // 4-bit mode wait_us(40); // most instructions take 40us - +*/ // Display is now in 4-bit mode - // Device specific initialisations for DC/DC converter to generate VLCD or VLED - switch (_ctrl) { - case ST7036: - // ST7036 controller: Initialise Voltage booster for VLCD. VDD=5V + // ST7036 controller: Initialise Voltage booster for VLCD. VDD=3.3V // Note: supports 1,2 or 3 lines - _writeByte( 0x29 ); // 4-bit Databus, 2 Lines, Select Instruction table 1 + _writeByte( 0x39 ); // 8-bit Databus, 2 Lines, Select Instruction table 1 wait_ms(30); // > 26,3ms _writeByte( 0x14 ); // Bias: 1/5, 2-Lines LCD wait_ms(30); // > 26,3ms @@ -188,33 +134,16 @@ wait_ms(200); // > 200ms! _writeByte( 0x78 ); // Set Contrast C3, C2, C1, C0 wait_ms(30); // > 26,3ms - _writeByte( 0x28 ); // Return to Instruction table 0 + _writeByte( 0x38 ); // Return to Instruction table 0 + wait_ms(50); + _writeByte( 0x0f ); // Return to Instruction table 0 wait_ms(50); - - break; - - case WS0010: - // WS0010 OLED controller: Initialise DC/DC Voltage converter for LEDs - // Note: supports 1 or 2 lines (and 16x100 graphics) - // supports 4 fonts (English/Japanese (default), Western European-I, English/Russian, Western European-II) + _writeByte( 0x01 ); // Return to Instruction table 0 + wait_ms(50); + _writeByte( 0x06 ); // Return to Instruction table 0 + wait_ms(50); + - // Cursor/Disp shift set 0001 SC RL 0 0 - // - // Mode en Power set 0001 GC PWR 1 1 - // GC = 0 (Graph Mode=1, Char Mode=0) - // PWR = (DC/DC On/Off) - - //_writeCommand(0x13); // DC/DC off - - _writeCommand(0x17); // DC/DC on - wait_ms(10); - - break; - - default: - // Devices that do not use DC/DC Voltage converters but external VLCD - break; - } // Initialise Display configuration switch (_type) { @@ -259,7 +188,7 @@ // All other LCD types are initialised as 2 Line displays (including LCD40x4) default: - _writeCommand(0x28); // Function set 001 DL N F - - + _writeCommand(0x39); // Function set 001 DL N F - - // DL=0 (4 bits bus) // N=1 (2 lines) // F=0 (5x7 dots font, only option for 2 line display) @@ -370,228 +299,32 @@ return -1; } -// Set E pin (or E2 pin) -// Used for mbed pins, I2C bus expander or SPI shifregister -void TextLCD::_setEnable(bool value) { - - switch(_busType) { - case _PinBus : - if(_ctrl_idx==TextLCD::_LCDCtrl_0) { - if (value) - _e = 1; // Set E bit - else - _e = 0; // Reset E bit - } - else { - if (value) - _e2 = 1; // Set E2 bit - else - _e2 = 0; // Reset E2 bit - } - - break; - - case _I2CBus : - - if(_ctrl_idx==TextLCD::_LCDCtrl_0) { - if (value) - _lcd_bus |= D_LCD_E; // Set E bit - else - _lcd_bus &= ~D_LCD_E; // Reset E bit - } - else { - if (value) - _lcd_bus |= D_LCD_E2; // Set E2 bit - else - _lcd_bus &= ~D_LCD_E2; // Reset E2bit - } - - // write the new data to the I2C portexpander - _i2c->write(_slaveAddress, &_lcd_bus, 1); - - break; - - case _SPIBus : - if(_ctrl_idx==TextLCD::_LCDCtrl_0) { - if (value) - _lcd_bus |= D_LCD_E; // Set E bit - else - _lcd_bus &= ~D_LCD_E; // Reset E bit - } - else { - if (value) - _lcd_bus |= D_LCD_E2; // Set E2 bit - else - _lcd_bus &= ~D_LCD_E2; // Reset E2 bit - } - - // write the new data to the SPI portexpander - _setCS(false); - _spi->write(_lcd_bus); - _setCS(true); - - break; - } -} // Set RS pin // Used for mbed pins, I2C bus expander or SPI shifregister -void TextLCD::_setRS(bool value) { - - switch(_busType) { - case _PinBus : - if (value) - _rs = 1; // Set RS bit - else - _rs = 0; // Reset RS bit - - break; - - case _I2CBus : - if (value) - _lcd_bus |= D_LCD_RS; // Set RS bit - else - _lcd_bus &= ~D_LCD_RS; // Reset RS bit - - // write the new data to the I2C portexpander - _i2c->write(_slaveAddress, &_lcd_bus, 1); - - break; - - case _SPIBus : - if (value) - _lcd_bus |= D_LCD_RS; // Set RS bit - else - _lcd_bus &= ~D_LCD_RS; // Reset RS bit - - // write the new data to the SPI portexpander - _setCS(false); - _spi->write(_lcd_bus); - _setCS(true); - - break; - } - +void TextLCD::_setRS(bool value) +{ + if (value) + _rs = 1; // Set RS bit + else + _rs = 0; // Reset RS bit } -// Set BL pin -// Used for mbed pins, I2C bus expander or SPI shifregister -void TextLCD::_setBL(bool value) { - - switch(_busType) { - case _PinBus : - if (value) - _bl = 1; // Set BL bit - else - _bl = 0; // Reset BL bit - - break; - - case _I2CBus : - if (value) - _lcd_bus |= D_LCD_BL; // Set BL bit - else - _lcd_bus &= ~D_LCD_BL; // Reset BL bit - - // write the new data to the I2C portexpander - _i2c->write(_slaveAddress, &_lcd_bus, 1); - - break; - - case _SPIBus : - if (value) - _lcd_bus |= D_LCD_BL; // Set BL bit - else - _lcd_bus &= ~D_LCD_BL; // Reset BL bit - - // write the new data to the SPI portexpander - _setCS(false); - _spi->write(_lcd_bus); - _setCS(true); - - break; - } - -} - - - // Place the 4bit data on the databus // Used for mbed pins, I2C bus expander or SPI shifregister -void TextLCD::_setData(int value) { - int data; - - switch(_busType) { - case _PinBus : - _d = value & 0x0F; // Write Databits - - break; - - case _I2CBus : - data = value & 0x0F; - if (data & 0x01) - _lcd_bus |= D_LCD_D4; // Set Databit - else - _lcd_bus &= ~D_LCD_D4; // Reset Databit - - if (data & 0x02) - _lcd_bus |= D_LCD_D5; // Set Databit - else - _lcd_bus &= ~D_LCD_D5; // Reset Databit - - if (data & 0x04) - _lcd_bus |= D_LCD_D6; // Set Databit - else - _lcd_bus &= ~D_LCD_D6; // Reset Databit - - if (data & 0x08) - _lcd_bus |= D_LCD_D7; // Set Databit - else - _lcd_bus &= ~D_LCD_D7; // Reset Databit - - // write the new data to the I2C portexpander - _i2c->write(_slaveAddress, &_lcd_bus, 1); - - break; - - case _SPIBus : - - data = value & 0x0F; - if (data & 0x01) - _lcd_bus |= D_LCD_D4; // Set Databit - else - _lcd_bus &= ~D_LCD_D4; // Reset Databit - - if (data & 0x02) - _lcd_bus |= D_LCD_D5; // Set Databit - else - _lcd_bus &= ~D_LCD_D5; // Reset Databit - - if (data & 0x04) - _lcd_bus |= D_LCD_D6; // Set Databit - else - _lcd_bus &= ~D_LCD_D6; // Reset Databit - - if (data & 0x08) - _lcd_bus |= D_LCD_D7; // Set Databit - else - _lcd_bus &= ~D_LCD_D7; // Reset Databit - - // write the new data to the SPI portexpander - _setCS(false); - _spi->write(_lcd_bus); - _setCS(true); - - break; - } - +void TextLCD::_setData(int value) +{ + // write the new data to the SPI portexpander + _setCS(false); + _spi->write(value); + _setCS(true); } // Set CS line. // Only used for SPI bus -void TextLCD::_setCS(bool value) { - +void TextLCD::_setCS(bool value) +{ if (value) { _cs = 1; // Set CS pin } @@ -601,41 +334,13 @@ } -// Write a nibble using the 4-bit interface -// Used for mbed pins, I2C bus expander or SPI shifregister -void TextLCD::_writeNibble(int value) { - -// Enable is Low - _setEnable(true); - _setData(value & 0x0F); // Low nibble - wait_us(1); // Data setup time - _setEnable(false); - wait_us(1); // Datahold time - -// Enable is Low - -} - // Write a byte using the 4-bit interface // Used for mbed pins, I2C bus expander or SPI shifregister void TextLCD::_writeByte(int value) { -// Enable is Low - _setEnable(true); - _setData(value >> 4); // High nibble + _setData(value); // High nibble wait_us(1); // Data setup time - _setEnable(false); - wait_us(1); // Data hold time - - _setEnable(true); - _setData(value >> 0); // Low nibble - wait_us(1); // Data setup time - _setEnable(false); - wait_us(1); // Datahold time - -// Enable is Low - } void TextLCD::_writeCommand(int command) { @@ -657,33 +362,6 @@ } -#if (0) -// This is the original _address() method. -// It is confusing since it returns the memoryaddress or-ed with the set memorycommand 0x80. -// Left it in here for compatibility with older code. New applications should use getAddress() instead. -// -int TextLCD::_address(int column, int row) { - switch (_type) { - case LCD20x4: - switch (row) { - case 0: - return 0x80 + column; - case 1: - return 0xc0 + column; - case 2: - return 0x94 + column; - case 3: - return 0xd4 + column; - } - case LCD16x2B: - return 0x80 + (row * 40) + column; - case LCD16x2: - case LCD20x2: - default: - return 0x80 + (row * 0x40) + column; - } -} -#endif // This replaces the original _address() method. @@ -975,16 +653,6 @@ _writeCommand(0x08 | displayMode | cursorType); } -// Set the Backlight mode (Off/On) -void TextLCD::setBacklight(TextLCD::LCDBacklight backlightMode) { - - if (backlightMode == LightOn) { - _setBL(true); - } - else { - _setBL(false); - } -} void TextLCD::setUDC(unsigned char c, char *udc_data) {
--- a/TextLCD.h Mon May 13 19:29:13 2013 +0000 +++ b/TextLCD.h Fri Dec 05 10:26:07 2014 +0000 @@ -214,28 +214,6 @@ LightOn /**< Backlight On */ }; - /** Create a TextLCD interface for using regular mbed pins - * - * @param rs Instruction/data control line - * @param e Enable line (clock) - * @param d4-d7 Data lines for using as a 4-bit interface - * @param type Sets the panel size/addressing mode (default = LCD16x2) - * @param bl Backlight control line (optional, default = NC) - * @param e2 Enable2 line (clock for second controller, LCD40x4 only) - * @param ctrl LCD controller (default = HD44780) - */ - 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); - - /** Create a TextLCD interface using an I2C PC8574 portexpander - * - * @param i2c I2C Bus - * @param deviceAddress I2C slave address (PCF8574) - * @param type Sets the panel size/addressing mode (default = LCD16x2) - * @param ctrl LCD controller (default = HD44780) - */ - TextLCD(I2C *i2c, char deviceAddress, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780); - - /** Create a TextLCD interface using an SPI 74595 portexpander * * @param spi SPI Bus @@ -243,7 +221,7 @@ * @param type Sets the panel size/addressing mode (default = LCD16x2) * @param ctrl LCD controller (default = HD44780) */ - TextLCD(SPI *spi, PinName cs, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780); + TextLCD(SPI *spi, PinName cs, PinName rs, LCDType type = LCD16x2, LCDCtrl ctrl = HD44780); #if DOXYGEN_ONLY @@ -314,13 +292,7 @@ */ void setMode(TextLCD::LCDMode displayMode); - /** Set the Backlight mode - * - * @param backlightMode The Backlight mode (LightOff, LightOn) - */ - void setBacklight(TextLCD::LCDBacklight backlightMode); - - + /** Set User Defined Characters * * @param unsigned char c The Index of the UDC (0..7)