Trying i2c LCD using TextLCD library
Fork of TextLCD by
Diff: TextLCD.cpp
- Revision:
- 42:9585a1cce408
- Parent:
- 37:ce348c002929
--- a/TextLCD.cpp Sun Mar 29 13:08:03 2015 +0000 +++ b/TextLCD.cpp Mon Apr 10 07:48:10 2017 +0000 @@ -18,6 +18,12 @@ * 2014, v15: WH, Added AC780 support, added I2C expander modules, fixed setBacklight() for inverted logic modules. Fixed bug in LCD_SPI_N define * 2014, v16: WH, Added ST7070 and KS0073 support, added setIcon(), clrIcon() and setInvert() method for supported devices * 2015, v17: WH, Clean up low-level _writeCommand() and _writeData(), Added support for alternative fonttables (eg PCF21XX), Added ST7066_ACM controller for ACM1602 module + * 2015, v18: WH, Performance improvement I2C portexpander + * 2015, v19: WH, Fixed Adafruit I2C/SPI portexpander pinmappings, fixed SYDZ Backlight + * 2015, v20: WH, Fixed occasional Init fail caused by insufficient wait time after ReturnHome command (0x02), Added defines to reduce memory footprint (eg LCD_ICON), + * Fixed and Added more fonttable support for PCF2119R_3V3, Added HD66712 controller. + * 2015, v21: WH, Added LCD32x2 defines and code, Fixed KS0073 DL=1 init for SPI, Added defines to reduce memory footprint (LCD_TWO_CTRL, LCD_CONTRAST, LCD_UTF8_FONT) + * Added SPLC792A controller, Added UTF8_2_LCD decode for Cyrilic font (By Andriy Ribalko). Added setFont() * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,6 +46,7 @@ #include "mbed.h" #include "TextLCD.h" #include "TextLCD_UDC.inc" +#include "TextLCD_UTF8.inc" /** Create a TextLCD_Base interface * @@ -50,17 +57,17 @@ // Extract LCDType data - // Columns encoded in b7..b0 - _nr_cols = (_type & 0xFF); - - // Rows encoded in b15..b8 - _nr_rows = ((_type >> 8) & 0xFF); - - // Addressing mode encoded in b19..b16 + // Columns encoded in b15..b8 + _nr_cols = (_type & LCD_T_COL_MSK) >> LCD_T_COL_SHFT; + + // Rows encoded in b23..b16 + _nr_rows = (_type & LCD_T_ROW_MSK) >> LCD_T_ROW_SHFT; + + // Addressing mode encoded in b27..b24 _addr_mode = _type & LCD_T_ADR_MSK; // Font table, encoded in LCDCtrl - _font = _type & LCD_C_FNT_MSK; + _font = _ctrl & LCD_C_FNT_MSK; } /** Init the LCD Controller(s) @@ -69,12 +76,16 @@ * @return none */ void TextLCD_Base::_init(_LCDDatalength dl) { + + wait_ms(100); // Wait 100ms to ensure powered up +#if (LCD_TWO_CTRL == 1) // Select and configure second LCD controller when needed if(_type==LCD40x4) { _ctrl_idx=_LCDCtrl_1; // Select 2nd controller _initCtrl(dl); // Init 2nd controller } +#endif // Select and configure primary LCD controller _ctrl_idx=_LCDCtrl_0; // Select primary controller @@ -91,7 +102,7 @@ * The controller is accessed in 4-bit parallel mode either directly via mbed pins or through I2C or SPI expander. * Some controllers also support native I2C or SPI interfaces. * - * @param _LCDDatalength dl sets the 4 or 8 bit datalength of data/commands. Required for some native serial modes. + * @param _LCDDatalength dl sets the 4 or 8 bit datalength of data/commands. Required for some native serial modes that dont work when DL=0. * @return none * * Note: some configurations are commented out because they have not yet been tested due to lack of hardware @@ -101,8 +112,6 @@ int _lines=0; // Set lines (Ext Instr Set), temporary variable. this->_setRS(false); // command mode - - wait_ms(20); // Wait 20ms to ensure powered up if (dl == _LCD_DL_4) { // The Controller could be in 8 bit mode (power-on reset) or in 4 bit mode (warm reboot) at this point. @@ -110,18 +119,18 @@ // between the uP and the LCD can only write the 4 most significant bits (Most Significant Nibble, MSN). // In 4 bit mode the LCD expects the MSN first, followed by the LSN. // - // Current state: 8 bit mode | 4 bit mode, MSN is next | 4 bit mode, LSN is next + // Current state: 8 bit mode | 4 bit mode, MSN is next | 4 bit mode, LSN is next //------------------------------------------------------------------------------------------------- - _writeNibble(0x3); // set 8 bit mode (MSN) and dummy LSN, | set 8 bit mode (MSN), | set dummy LSN, - // remains in 8 bit mode | change to 8 bit mode | remains in 4 bit mode + _writeNibble(0x3); // set 8 bit mode (MSN) and dummy LSN, | set 8 bit mode (MSN), | set dummy LSN, + // remains in 8 bit mode | remains in 4 bit mode | remains in 4 bit mode wait_ms(15); // - _writeNibble(0x3); // set 8 bit mode and dummy LSN, | set 8 bit mode and dummy LSN, | set 8bit mode (MSN), - // remains in 8 bit mode | remains in 8 bit mode | remains in 4 bit mode + _writeNibble(0x3); // set 8 bit mode (MSN) and dummy LSN, | set dummy LSN, | set 8bit mode (MSN), + // remains in 8 bit mode | change to 8 bit mode | remains in 4 bit mode wait_ms(15); // - _writeNibble(0x3); // set 8 bit mode and dummy LSN, | set 8 bit mode and dummy LSN, | set dummy LSN, - // remains in 8 bit mode | remains in 8 bit mode | change to 8 bit mode + _writeNibble(0x3); // set 8 bit mode (MSN) and dummy LSN, | set 8 bit mode (MSN) and dummy LSN, | set dummy LSN, + // remains in 8 bit mode | remains in 8 bit mode | change to 8 bit mode wait_ms(15); // // Controller is now in 8 bit mode @@ -132,6 +141,11 @@ // Controller is now in 4-bit mode // Note: 4/8 bit mode is ignored for most native SPI and I2C devices. They dont use the parallel bus. // However, _writeNibble() method is void anyway for native SPI and I2C devices. + } + else { + // Reset in 8 bit mode, final Function set will follow + _writeCommand(0x30); // Function set 0 0 1 DL=1 N F x x + wait_ms(1); // most instructions take 40us } // Device specific initialisations: DC/DC converter to generate VLCD or VLED, number of lines etc @@ -140,82 +154,94 @@ case KS0073: // Initialise Display configuration switch (_type) { +// case LCD6x1: case LCD8x1: //8x1 is a regular 1 line display +// case LCD8x2B: //8x1 is a 16x1 line display case LCD12x1: case LCD16x1: case LCD20x1: case LCD24x1: // case LCD32x1: // EXT pin is High, extension driver needed -// case LCD40x1: // EXT pin is High, extension driver needed - _function = 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs) - // DL=0 (4 bits bus) - // N=0 (1-line mode, N=1 2-line mode) - // RE=0 (Dis. Extended Regs, special mode for KS0073) - // DH=1 (Disp shift enable, special mode for KS0073) - // REV=0 (Reverse normal, special mode for KS0073) - - _function_1 = 0x04; // Function set 001 DL N RE(1) BE LP (Ext Regs) - // DL=0 (4 bits bus) - // N=0 (1-line mode, N=1 2-line mode) - // RE=1 (Ena Extended Regs, special mode for KS0073) - // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0073) - // LP=0 (LP=1 Low power mode, LP=0 Normal) +// case LCD40x1: // EXT pin is High, extension driver needed +// case LCD52x1: // EXT pin is High, extension driver needed + _function = dl | 0x02; // Set function, 0 0 1 DL, N, RE(0), DH, REV + // Note: 4 bit mode is NOT ignored for native SPI ! + // DL=1 (8 bits bus), DL=0 (4 bits bus) + // N=0 (1-line mode), N=1 (2-line mode), dont care for 4 line mode + // RE=0 (Dis. Extended Regs, special mode for KS0073) + // DH=1 (Disp shift enable, special mode for KS0073) + // REV=0 (Reverse normal, special mode for KS0073) + + _function_1 = dl | 0x04; // Set function, 0 0 1 DL, N, RE(1), BE, LP (Ext Regs) + // Note: 4 bit mode is NOT ignored for native SPI ! + // DL=1 (8 bits bus), DL=0 (4 bits bus) + // N=0 (1-line mode), N=1 (2-line mode), dont care for 4 line mode + // RE=1 (Ena Extended Regs, special mode for KS0073) + // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0073) + // LP=0 (LP=1 Low power mode, LP=0 Normal) _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs) - // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0073) + // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0073) break; // case LCD12x3D: // Special mode for KS0073, KS0078 and PCF21XX // case LCD12x3D1: // Special mode for KS0073, KS0078 and PCF21XX case LCD12x4D: // Special mode for KS0073, KS0078 and PCF21XX // case LCD16x3D: // Special mode for KS0073, KS0078 +// case LCD16x3D1: // Special mode for KS0073, KS0078 // case LCD16x4D: // Special mode for KS0073, KS0078 case LCD20x4D: // Special mode for KS0073, KS0078 - _function = 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs) - // DL=0 (4 bits bus) - // N=0 (dont care for 4 line mode) - // RE=0 (Dis. Extended Regs, special mode for KS0073) - // DH=1 (Disp shift enable, special mode for KS0073) - // REV=0 (Reverse normal, special mode for KS0073) + _function = dl | 0x02; // Set function, 0 0 1 DL, N, RE(0), DH, REV + // Note: 4 bit mode is NOT ignored for native SPI ! + // DL=1 (8 bits bus), DL=0 (4 bits bus) + // N=0 (1-line mode), N=1 (2-line mode), dont care for 4 line mode + // RE=0 (Dis. Extended Regs, special mode for KS0073) + // DH=1 (Disp shift enable, special mode for KS0073) + // REV=0 (Reverse normal, special mode for KS0073) - _function_1 = 0x04; // Function set 001 DL N RE(1) BE LP (Ext Regs) - // DL=0 (4 bits bus) - // N=0 (1-line mode), N=1 (2-line mode) - // RE=1 (Ena Extended Regs, special mode for KS0073) - // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0073) - // LP=0 (LP=1 Low power mode, LP=0 Normal) + _function_1 = dl | 0x04; // Set function, 0 0 1 DL, N, RE(1), BE, LP (Ext Regs) + // Note: 4 bit mode is NOT ignored for native SPI ! + // DL=1 (8 bits bus), DL=0 (4 bits bus) + // N=0 (1-line mode), N=1 (2-line mode), dont care for 4 line mode + // RE=1 (Ena Extended Regs, special mode for KS0073) + // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0073) + // LP=0 (LP=1 Low power mode, LP=0 Normal) _function_x = 0x01; // Ext Function set 0000 1 FW BW NW (Ext Regs) - // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0073) + // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0073) break; - - case LCD16x3G: // Special mode for ST7036 -// case LCD24x3D: // Special mode for KS0078 -// case LCD24x3D1: // Special mode for KS0078 - case LCD24x4D: // Special mode for KS0078 - error("Error: LCD Controller type does not support this Display type\n\r"); - break; - - default: - // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4) - _function = 0x0A; // Function set 001 DL N RE(0) DH REV (Std Regs) - // DL=0 (4 bits bus) - // N=1 (2-line mode), N=0 (1-line mode) - // RE=0 (Dis. Extended Regs, special mode for KS0073) - // DH=1 (Disp shift enable, special mode for KS0073) - // REV=0 (Reverse normal, special mode for KS0073) +// case LCD6x2: + case LCD8x2: + case LCD16x2: +// case LCD16x1C: + case LCD20x2: + case LCD24x2: + case LCD32x2: + // All other LCD types are initialised as 2 Line displays + _function = dl | 0x0A; // Set function, 0 0 1 DL, N, RE(0), DH, REV + // Note: 4 bit mode is NOT ignored for native SPI ! + // DL=1 (8 bits bus), DL=0 (4 bits bus) + // N=1 (2-line mode), N=0 (1-line mode) + // RE=0 (Dis. Extended Regs, special mode for KS0073) + // DH=1 (Disp shift enable, special mode for KS0073) + // REV=0 (Reverse normal, special mode for KS0073) - _function_1 = 0x0C; // Function set 001 DL N RE(1) BE LP (Ext Regs) - // DL=0 (4 bits bus) - // N=1 (2 line mode), N=0 (1-line mode) - // RE=1 (Ena Extended Regs, special mode for KS0073) - // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0073) - // LP=0 (LP=1 Low power mode, LP=0 Normal) + _function_1 = dl | 0x0C; // Set function, 0 0 1 DL, N, RE(1), BE, LP (Ext Regs) + // Note: 4 bit mode is NOT ignored for native SPI ! + // DL=1 (8 bits bus), DL=0 (4 bits bus) + // N=1 (2 line mode), N=0 (1-line mode) + // RE=1 (Ena Extended Regs, special mode for KS0073) + // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0073) + // LP=0 (LP=1 Low power mode, LP=0 Normal) _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs) // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0073) break; + + default: + error("Error: LCD Controller type does not support this Display type\n\r"); + break; } // switch type // init special features @@ -255,14 +281,14 @@ case LCD16x1: // case LCD20x1: case LCD24x1: - _function = 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs) + _function = dl | 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs) // DL=0 (4 bits bus) // N=0 (1 line mode), N=1 (2 line mode) // RE=0 (Dis. Extended Regs, special mode for KS0078) // DH=1 (Disp shift enable, special mode for KS0078) // REV=0 (Reverse normal, special mode for KS0078) - _function_1 = 0x04; // Function set 001 DL N RE(1) BE 0 (Ext Regs) + _function_1 = dl | 0x04; // Function set 001 DL N RE(1) BE 0 (Ext Regs) // DL=0 (4 bits bus) // N=0 (1 line mode), N=1 (2 line mode) // RE=1 (Ena Extended Regs, special mode for KS0078) @@ -282,14 +308,14 @@ // case LCD24x3D: // Special mode for KS0078 // case LCD24x3D1: // Special mode for KS0078 case LCD24x4D: // Special mode for KS0078 - _function = 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs) + _function = dl | 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs) // DL=0 (4 bits bus) // N=0 (dont care for 4 line mode) // RE=0 (Dis. Extended Regs, special mode for KS0078) // DH=1 (Disp shift enable, special mode for KS0078) // REV=0 (Reverse normal, special mode for KS0078) - _function_1 = 0x04; // Function set 001 DL N RE(1) BE 0 (Ext Regs) + _function_1 = dl | 0x04; // Function set 001 DL N RE(1) BE 0 (Ext Regs) // DL=0 (4 bits bus) // N=0 (1 line mode), N=1 (2 line mode) // RE=1 (Ena Extended Regs, special mode for KS0078) @@ -300,20 +326,23 @@ // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0078) break; - case LCD16x3G: // Special mode for ST7036 - error("Error: LCD Controller type does not support this Display type\n\r"); - break; - - default: +// case LCD6x2: + case LCD8x2: + case LCD16x2: +// case LCD16x1C: + case LCD20x2: + case LCD24x2: + case LCD32x2: + case LCD40x2: // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4) - _function = 0x0A; // Function set 001 DL N RE(0) DH REV (Std Regs) + _function = dl | 0x0A; // Function set 001 DL N RE(0) DH REV (Std Regs) // DL=0 (4 bits bus) // N=1 (1 line mode), N=1 (2 line mode) // RE=0 (Dis. Extended Regs, special mode for KS0078) // DH=1 (Disp shift enable, special mode for KS0078) // REV=0 (Reverse normal, special mode for KS0078) - _function_1 = 0x0C; // Function set 001 DL N RE(1) BE 0 (Ext Regs) + _function_1 = dl | 0x0C; // Function set 001 DL N RE(1) BE 0 (Ext Regs) // DL=0 (4 bits bus) // N=1 (1 line mode), N=1 (2 line mode) // RE=1 (Ena Extended Regs, special mode for KS0078) @@ -323,6 +352,10 @@ _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs) // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0078) break; + + default: + error("Error: LCD Controller type does not support this Display type\n\r"); + break; } // switch type // init special features @@ -354,9 +387,10 @@ case ST7032_3V3: // ST7032 controller: Initialise Voltage booster for VLCD. VDD=3V3 + // Note: very similar to SPLC792A case ST7032_5V: // ST7032 controller: Disable Voltage booster for VLCD. VDD=5V - + // Initialise Display configuration switch (_type) { case LCD8x1: //8x1 is a regular 1 line display @@ -390,14 +424,16 @@ _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 DL N F 0 IS=1 Select Instr Set = 1 _writeCommand(0x1C); // Internal OSC frequency adjustment Framefreq=183HZ, Bias will be 1/4 (Instr Set=1) + // Note: Bias and Osc register not available on SPLC792A _contrast = LCD_ST7032_CONTRAST; _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast Low bits, 0 1 1 1 C3 C2 C1 C0 (IS=1) if (_ctrl == ST7032_3V3) { -// _icon_power = 0x04; // Icon display off, Booster circuit is turned on (IS=1) - _icon_power = 0x0C; // Icon display on, Booster circuit is turned on (IS=1) +// _icon_power = 0x04; // Icon display off (Bit3=0), Booster circuit is turned on (Bit2=1) (IS=1) + _icon_power = 0x0C; // Icon display on (Bit3=1), Booster circuit is turned on (Bit2=1) (IS=1) + // Note: Booster circuit always on for SPLC792A, Bit2 is dont care // Saved to allow contrast change at later time } else { @@ -595,6 +631,8 @@ // NW=1 3-Line LCD (N=0) break; +// case LCD10x2D: // Special mode for SSD1803, 4-line mode but switch to double height font + case LCD10x4D: // Special mode for SSD1803 case LCD20x4D: // Special mode for SSD1803 _function = 0x08; // Set function 0 0 1 DL N DH RE(0) IS // Saved to allow switch between Instruction sets at later time @@ -868,6 +906,7 @@ break; // case PCF2116_5V Controller case PCF2119_3V3: + case PCF2119R_3V3: // PCF2119 controller: Initialise Voltage booster for VLCD. VDD=3V3. VA and VB control contrast. // Note1: See datasheet, the PCF2119 supports icons and provides separate constrast control for Icons and characters. // Note2: Vgen is switched off when the contrast voltage VA or VB is set to 0x00. @@ -906,7 +945,11 @@ // Init special features _writeCommand(0x20 | _function | 0x01); // Set function, Select Instruction Set = 1 - _writeCommand(0x04); // DISP CONF SET (Instr. Set 1) 0000, 0, 1, P=0, Q=0 +// _writeCommand(0x04); // DISP CONF SET (Instr. Set 1) 0000, 0, 1, P=0, Q=0 (IC at Bottom) +// _writeCommand(0x05); // Display Conf Set 0000, 0, 1, P=0, Q=1 +// _writeCommand(0x06); // Display Conf Set 0000, 0, 1, P=1, Q=0 + _writeCommand(0x07); // Display Conf Set 0000, 0, 1, P=1, Q=1 (IC at Top) + _writeCommand(0x10); // TEMP CTRL SET (Instr. Set 1) 0001, 0, 0, TC1=0, TC2=0 // _writeCommand(0x42); // HV GEN (Instr. Set 1) 0100, 0, 0, S1=1, S2=0 (2x multiplier) _writeCommand(0x40 | (LCD_PCF2_S12 & 0x03)); // HV GEN (Instr. Set 1) 0100, 0, 0, S1=1, S2=0 (2x multiplier) @@ -1198,7 +1241,169 @@ _contrast = LCD_PT63_CONTRAST; _writeCommand(0x20 | _function | ((~_contrast) >> 4)); // Invert and shift to use 2 MSBs break; // case PT6314 Controller (VFD) - + + + case HD66712: + // Initialise Display configuration + switch (_type) { + case LCD8x1: //8x1 is a regular 1 line display + case LCD12x1: + case LCD16x1: + case LCD20x1: + case LCD24x1: +// case LCD32x1: // EXT pin is High, extension driver needed + _function = 0x02; // Function set 001 DL N RE(0) - - (Std Regs) + // DL=0 (4 bits bus) + // N=0 (1-line mode, N=1 2-line mode) + // RE=0 (Dis. Extended Regs, special mode for HD66712) + // + + _function_1 = 0x04; // Function set 001 DL N RE(1) BE LP (Ext Regs) + // DL=0 (4 bits bus) + // N=0 (1-line mode, N=1 2-line mode) + // RE=1 (Ena Extended Regs; special mode for HD66712) + // BE=0 (Blink Enable, CG/SEG RAM; special mode for HD66712) + // LP=0 (LP=1 Low power mode, LP=0 Normal; special mode for HD66712) + + _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs) + // NW=0 (1,2 line), NW=1 (4 Line, special mode for HD66712) + break; + +// case LCD12x3D: // Special mode for KS0073, KS0078, PCF21XX and HD66712 +// case LCD12x3D1: // Special mode for KS0073, KS0078, PCF21XX and HD66712 + case LCD12x4D: // Special mode for KS0073, KS0078, PCF21XX and HD66712 +// case LCD16x3D: // Special mode for KS0073, KS0078 and HD66712 +// case LCD16x4D: // Special mode for KS0073, KS0078 and HD66712 + case LCD20x4D: // Special mode for KS0073, KS0078 and HD66712 + _function = 0x02; // Function set 001 DL N RE(0) - - (Std Regs) + // DL=0 (4 bits bus) + // N=0 (1-line mode, N=1 2-line mode) + // RE=0 (Dis. Extended Regs, special mode for HD66712) + // + + _function_1 = 0x04; // Function set 001 DL N RE(1) BE LP (Ext Regs) + // DL=0 (4 bits bus) + // N=0 (1-line mode, N=1 2-line mode) + // RE=1 (Ena Extended Regs; special mode for HD66712) + // BE=0 (Blink Enable, CG/SEG RAM; special mode for HD66712) + // LP=0 (LP=1 Low power mode, LP=0 Normal; special mode for HD66712) + + _function_x = 0x01; // Ext Function set 0000 1 FW BW NW (Ext Regs) + // NW=0 (1,2 line), NW=1 (4 Line, special mode for HD66712) + break; + + case LCD16x3G: // Special mode for ST7036 +// case LCD24x3D: // Special mode for KS0078 +// case LCD24x3D1: // Special mode for KS0078 + case LCD24x4D: // Special mode for KS0078 + error("Error: LCD Controller type does not support this Display type\n\r"); + break; + + default: + // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4) + _function = 0x0A; // Function set 001 DL N RE(0) - - (Std Regs) + // DL=0 (4 bits bus) + // N=1 (2-line mode), N=0 (1-line mode) + // RE=0 (Dis. Extended Regs, special mode for HD66712) + + _function_1 = 0x0C; // Function set 001 DL N RE(1) BE LP (Ext Regs) + // DL=0 (4 bits bus) + // N=1 (2 line mode), N=0 (1-line mode) + // RE=1 (Ena Extended Regs, special mode for HD66712) + // BE=0 (Blink Enable, CG/SEG RAM, special mode for HD66712) + // LP=0 (LP=1 Low power mode, LP=0 Normal) + + _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs) + // NW=0 (1,2 line), NW=1 (4 Line, special mode for HD66712) + break; + } // switch type + + // init special features + _writeCommand(0x20 | _function_1);// Function set 001 DL N RE(1) BE LP (Ext Regs) + // DL=0 (4 bits bus), DL=1 (8 bits mode) + // N=0 (1 line mode), N=1 (2 line mode) + // RE=1 (Ena Extended Regs, special mode for HD66712) + // BE=0 (Blink Enable/Disable, CG/SEG RAM, special mode for HD66712) + // LP=0 (LP=1 Low power mode, LP=0 Normal) + + _writeCommand(0x08 | _function_x); // Ext Function set 0000 1 FW BW NW (Ext Regs) + // FW=0 (5-dot font, special mode for HD66712) + // BW=0 (Cur BW invert disable, special mode for HD66712) + // NW=0 (1,2 Line), NW=1 (4 line, special mode for HD66712) + + _writeCommand(0x10); // Scroll/Shift set 0001 HS4 HS3 HS2 HS1 (Ext Regs) + // Dotscroll/Display shift enable (Special mode for HD66712) + + _writeCommand(0x80); // Scroll Quantity set 1 0 HDS5 HDS4 HDS3 HDS2 HDS1 HDS0 (Ext Regs) + // Scroll quantity (Special mode for HD66712) + + _writeCommand(0x20 | _function); // Function set 001 DL N RE(0) DH REV (Std Regs) + // DL=0 (4 bits bus), DL=1 (8 bits mode) + // N=0 (1 line mode), N=1 (2 line mode) + // RE=0 (Dis. Extended Regs, special mode for HD66712) + // DH=1 (Disp shift enable/disable, special mode for HD66712) + // REV=0 (Reverse/Normal, special mode for HD66712) + break; // case HD66712 Controller + + case SPLC792A_3V3: + // SPLC792A controller: Initialise Voltage booster for VLCD. VDD=3V3 + // Note very similar to ST7032 + + // Initialise Display configuration + switch (_type) { + case LCD8x1: //8x1 is a regular 1 line display + case LCD8x2B: //8x2B is a special case of 16x1 +// case LCD12x1: + case LCD16x1: +// case LCD20x1: + case LCD24x1: + _function = 0x00; // FUNCTION SET 0 0 1 DL=0 (4 bit), N=0 (1-line display mode), F=0 (5*7dot), 0, IS + // Note: 4 bit mode is ignored for native SPI and I2C devices + // Saved to allow switch between Instruction sets at later time + break; + + case LCD12x3D: // Special mode for KS0078 and PCF21XX + case LCD12x3D1: // Special mode for KS0078 and PCF21XX + case LCD12x4D: // Special mode for KS0078 and PCF21XX + case LCD16x3G: // Special mode for ST7036 + case LCD24x4D: // Special mode for KS0078 + error("Error: LCD Controller type does not support this Display type\n\r"); + break; + + default: + // All other LCD types are initialised as 2 Line displays + _function = 0x08; // FUNCTION SET 0 0 1 DL=0 (4 bit), N=1 (2-line display mode), F=0 (5*7dot), 0, IS + // Note: 4 bit mode is ignored for native SPI and I2C devices + // Saved to allow switch between Instruction sets at later time + break; + } // switch type + + // init special features + _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 DL N F 0 IS=1 Select Instr Set = 1 + +//SPLC792A Does not support Bias and Internal Osc register +// _writeCommand(0x1C); // Internal OSC frequency adjustment Framefreq=183HZ, Bias will be 1/4 (Instr Set=1) + + _contrast = LCD_SPLC792A_CONTRAST; + _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast Low bits, 0 1 1 1 C3 C2 C1 C0 (IS=1) + + +// _icon_power = 0x04; // Icon display off (Bit3=0), Booster circuit is turned on (Bit2=1) (IS=1) + _icon_power = 0x0C; // Icon display on (Bit3=1), Booster circuit is turned on (Bit2=1) (IS=1) + // Note: Booster circuit always on for SPLC792A, Bit2 is dont care + // Saved to allow contrast change at later time + + _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Set Icon, Booster and Contrast High bits, 0 1 0 1 Ion Bon C5 C4 (IS=1) + wait_ms(10); // Wait 10ms to ensure powered up + + _writeCommand(0x68 | (LCD_SPLC792A_RAB & 0x07)); // Voltage follower, 0 1 1 0 FOn=1, Ampl ratio Rab2=1, Rab1=0, Rab0=0 (IS=1) + // Note: Follower circuit always on for SPLC792A, Bit3 is dont care + wait_ms(10); // Wait 10ms to ensure powered up + + _writeCommand(0x20 | _function); // Select Instruction Set = 0 + + break; // case SPLC792A_3V3 Controller + case ST7066_ACM: // ST7066 4/8 bit, I2C on ACM1602 using a PIC default: // Devices fully compatible to HD44780 that do not use any DC/DC Voltage converters but external VLCD, no icons etc @@ -1246,12 +1451,13 @@ } // switch Controller specific initialisations // Controller general initialisations -// _writeCommand(0x01); // cls, and set cursor to 0 +// _writeCommand(0x01); // Clear Display and set cursor to 0 // wait_ms(10); // The CLS command takes 1.64 ms. // // Since we are not using the Busy flag, Lets be safe and take 10 ms - _writeCommand(0x02); // Return Home - // Cursor Home, DDRAM Address to Origin + _writeCommand(0x02); // Cursor Home, DDRAM Address to Origin + wait_ms(10); // The Return Home command takes 1.64 ms. + // Since we are not using the Busy flag, Lets be safe and take 10 ms _writeCommand(0x06); // Entry Mode 0000 0 1 I/D S // Cursor Direction and Display Shift @@ -1266,15 +1472,19 @@ // _writeCommand(0x0C); // Display Ctrl 0000 1 D C B // // Display On, Cursor Off, Blink Off - setCursor(CurOff_BlkOff); +// setCursor(CurOff_BlkOff); + setCursor(CurOn_BlkOff); setMode(DispOn); } /** Clear the screen, Cursor home. + * Note: The whole display is initialised to charcode 0x20, which may not be a 'space' on some controllers with a + * different fontset such as the PCF2116C or PCF2119R. In this case you should fill the display with 'spaces'. */ void TextLCD_Base::cls() { +#if (LCD_TWO_CTRL == 1) // Select and configure second LCD controller when needed if(_type==LCD40x4) { _ctrl_idx=_LCDCtrl_1; // Select 2nd controller @@ -1284,21 +1494,29 @@ // Second LCD controller Clearscreen _writeCommand(0x01); // cls, and set cursor to 0 - wait_ms(10); // The CLS command takes 1.64 ms. + wait_ms(20); // The CLS command takes 1.64 ms. // Since we are not using the Busy flag, Lets be safe and take 10 ms _ctrl_idx=_LCDCtrl_0; // Select primary controller } + // Primary LCD controller Clearscreen _writeCommand(0x01); // cls, and set cursor to 0 - wait_ms(10); // The CLS command takes 1.64 ms. + wait_ms(20); // The CLS command takes 1.64 ms. // Since we are not using the Busy flag, Lets be safe and take 10 ms // Restore cursormode on primary LCD controller when needed if(_type==LCD40x4) { _setCursorAndDisplayMode(_currentMode,_currentCursor); } + +#else + // Support only one LCD controller + _writeCommand(0x01); // cls, and set cursor to 0 + wait_ms(20); // The CLS command takes 1.64 ms. + // Since we are not using the Busy flag, Lets be safe and take 10 ms +#endif setAddress(0, 0); // Reset Cursor location // Note: This is needed because some displays (eg PCF21XX) don't use line 0 in the '3 Line' mode. @@ -1337,11 +1555,23 @@ } else { //Character to write -#if (LCD_DEFAULT_FONT == 1) + +#if (LCD_DEF_FONT == 1) //Default HD44780 font _writeData(value); +#elif (LCD_C_FONT == 1) || (LCD_R_FONT == 1) //PCF21xxC or PCF21xxR font + _writeData(ASCII_2_LCD(value)); +#elif (LCD_UTF8_FONT == 1) // UTF8 2 byte font (eg Cyrillic) +// value = UTF_2_LCD(value, utf_seq_rec_first_cyr, utf_seq_recode_cyr, &utf_rnd_recode_cyr[0][0]); + value = UTF_2_LCD(value); + if (value >= 0) { + _writeData(value); + + // Only increment cursor when there is something to write + // Continue below to closing bracket... #else - _writeData(ASCII_2_LCD(value)); -#endif + _writeData('?'); //Oops, no font defined +#endif + //Update Cursor _column++; if (_column >= columns()) { @@ -1350,7 +1580,19 @@ if (_row >= rows()) { _row = 0; } - } + } + +#if (LCD_DEF_FONT == 1) //Default HD44780 font + +#elif (LCD_C_FONT == 1) || (LCD_R_FONT == 1) //PCF21xxC or PCF21xxR font + +#elif (LCD_UTF8_FONT == 1) //UTF8 2 byte font (eg Cyrillic) + // Continue code above to close bracket... + } // if (value >= 0) {.. +#else + +#endif + } //else //Set next memoryaddress, make sure cursor blinks at next location @@ -1366,6 +1608,8 @@ return -1; } + +#if ((LCD_C_FONT == 1) || (LCD_R_FONT == 1)) //PCF21xxC or PCF21xxR font /** Convert ASCII character code to the LCD fonttable code * * @param c The character to write to the display @@ -1373,23 +1617,88 @@ */ int TextLCD_Base::ASCII_2_LCD (int c) { -//LCD_C_FT0 is default for HD44780 and compatible series - if (_font == LCD_C_FT0) return c; - -//LCD_C_FT1 for PCF21XXC series -//Used code from Suga koubou library for PCF2119 +//LCD_C_F0 is default for HD44780 and compatible series +// if (_font == LCD_C_F0) return c; + +//LCD_C_FC for PCF21XXC series +//LCD_C_FR for PCF21XXR series +//Used code from Suga koubou library for PCF2119K and PCF2119R if (((c >= ' ') && (c <= '?')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) { c |= 0x80; - } else if (c >= 0xf0 && c <= 0xff) { - c &= 0x0f; + } else if (c >= 0xF0 && c <= 0xFF) { + c &= 0x0F; } return c; - -//LCD_C_FT2 ... -//@TODO add more, eg cyrillic -//@TODO add method to switch between fonts for controllers that support this } - +#endif + +#if(LCD_UTF8_FONT == 1) + +/** Convert UTF8 2-byte character code to the LCD fonttable code + * @param c The character to write to the display + * @return character code for the specific fonttable of the controller or -1 if UTF8 code is not yet complete or incorrect + * + * Orig by Andriy, Modified by WH + * + * Note: The UTF8 decoding table for a specific controller is defined and selected in file TextLCD_UTF8.inc + * The table is accessed in this UTF_2_LCD() method through + * #define UTF_FIRST, UTF_LAST, UTF_SEQ_REC_FIRST, UTF_SEQ_REC_LAST and + * #define UTF_SEQ_RECODE and UTF_RND_RECODE + */ +int TextLCD_Base::UTF_2_LCD (int c) { + int utf_code; + int utf_low_byte; // Low byte UTF8 + static int utf_hi_byte = 0; // High byte UTF8 + + if (c < 0x80) { // Regular ASCII code, no need to convert + return c; + } + else { // UTF8 handling, See wikipedia.org/wiki/UTF-8 and www.utf8-chartable.de +// printf("0x%X ", c); + + if (c >= 0xC0) { // First UTF8 byte should be formatted as 110b bbaa, Do sanity check + utf_hi_byte = c & 0x1F; // Mask out significant bits (0x1F) and save high byte + return -1; // Nothing to display as yet, wait for second UTF8 byte + } + + if (c <= 0xBF) { // Second UTF8 byte should be formatted as 10aa aaaa, Do sanity check + utf_low_byte = c & 0x3F; // Mask out significant bits (0x3F) + + // Compose UTF character code from UTF8 bytes. The UTF codes will be between U+0080 and U+07FF + utf_code = (utf_hi_byte << 6) | utf_low_byte; // 00000bbb aaaaaaaa +// printf("0x%4X ", utf_code); + + // Sanity check on UTF codes + // For example Cyrillic characters are UTF encoded between 0x0400 and 0x04FF + if ((utf_code < UTF_FIRST) || (utf_code > UTF_LAST)) { + return -1; // Invalid UTF8 code + }; + + //Map some specific UTF codes on a character in LCD fonttable using a special correcting lookup table + for (char i=0; UTF_RND_RECODE[i][0]; i++) { // Step through table until endvalue 0 is found or until a match is found + if (utf_code == UTF_RND_RECODE[i][0]) { // UTF8 code match is found + c = UTF_RND_RECODE[1][1]; + return c; // found match in correcting random table + } + } + + //Sanity check on table idx range + if ((utf_code < UTF_SEQ_REC_FIRST) || (utf_code > UTF_SEQ_REC_LAST)) { + return -1; // Invalid UTF8 code + }; + + //Map all other UTF codes on a character in LCD fonttable using a sequential lookup table + c = UTF_SEQ_RECODE[utf_code - UTF_SEQ_REC_FIRST]; + return c; // entry in sequential table + } + else { + return -1; // Invalid UTF8 code for second byte + } + } // End UTF8 handling +} + +#endif + #if(LCD_PRINTF != 1) /** Write a character to the LCD @@ -1422,7 +1731,7 @@ // Enable is Low this->_setEnable(true); - this->_setData(value); // Low nibble + this->_setData(value); // Low nibble of value on D4..D7 wait_us(1); // Data setup time this->_setEnable(false); wait_us(1); // Datahold time @@ -1504,8 +1813,8 @@ case 3: return 0x40 + _nr_cols + column; // Should never get here. - default: - return 0x00; +// default: +// return 0x00; } case LCD_T_B: @@ -1532,11 +1841,6 @@ return 0x40 + (column - (_nr_cols >> 1)); #endif -// Not sure about this one, seems wrong. -// Left in for compatibility with original library -// case LCD16x2B: -// return 0x00 + (row * 40) + column; - case LCD_T_D: //Alternate addressing mode for 3 and 4 row displays (except 40x4). Used by PCF21XX, KS0073, KS0078, SSD1803 //The 4 available rows start at a hardcoded address. @@ -1551,8 +1855,8 @@ case 3: return 0x60 + column; // Should never get here. - default: - return 0x00; +// default: +// return 0x00; } case LCD_T_D1: @@ -1567,8 +1871,8 @@ case 2: return 0x60 + column; // Should never get here. - default: - return 0x00; +// default: +// return 0x00; } case LCD_T_E: @@ -1621,8 +1925,8 @@ else return (0x40 + _nr_cols + (column - (_nr_cols >> 1))); // Should never get here. - default: - return 0x00; +// default: +// return 0x00; } case LCD_T_G: @@ -1635,8 +1939,8 @@ case 2: return 0x20 + column; // Should never get here. - default: - return 0x00; +// default: +// return 0x00; } // Should never get here. @@ -1727,7 +2031,8 @@ // Save new displayMode, needed when 2 controllers are in use or when cursor is changed _currentMode = displayMode; - + +#if (LCD_TWO_CTRL == 1) // Select and configure second LCD controller when needed if(_type==LCD40x4) { if (_ctrl_idx==_LCDCtrl_0) { @@ -1761,6 +2066,11 @@ // Configure primary LCD controller _setCursorAndDisplayMode(_currentMode, _currentCursor); } +#else + // Support only one LCD controller + _setCursorAndDisplayMode(_currentMode, _currentCursor); + +#endif } /** Low level method to restore the cursortype and display mode for current controller @@ -1810,7 +2120,8 @@ * @param char *udc_data The bitpatterns for the UDC (8 bytes of 5 significant bits for bitpattern and 3 bits for blinkmode (advanced types)) */ void TextLCD_Base::setUDC(unsigned char c, char *udc_data) { - + +#if (LCD_TWO_CTRL == 1) // Select and configure second LCD controller when needed if(_type==LCD40x4) { _LCDCtrl_Idx current_ctrl_idx = _ctrl_idx; // Temp save current controller @@ -1834,6 +2145,10 @@ // Configure primary LCD controller _setUDC(c, udc_data); } +#else + // Support only one LCD controller + _setUDC(c, udc_data); +#endif } /** Low level method to store user defined characters for current controller @@ -1848,7 +2163,8 @@ case PCF2113_3V3 : // Some UDCs may be used for Icons case PCF2116_3V3 : case PCF2116_5V : - case PCF2119_3V3 : // Some UDCs may be used for Icons + case PCF2119_3V3 : // Some UDCs may be used for Icons + case PCF2119R_3V3: // Some UDCs may be used for Icons c = c & 0x0F; // mask down to valid range break; @@ -1874,6 +2190,7 @@ _writeCommand(0x80 | addr); } +#if(LCD_BLINK == 1) /** Set UDC Blink and Icon blink * setUDCBlink method is supported by some compatible devices (eg SSD1803) * @@ -1905,13 +2222,14 @@ case BlinkOn: // Controllers that support UDC/Icon Blink switch (_ctrl) { - case KS0073 : - case KS0078 : + case KS0073 : + case KS0078 : + case HD66712 : _function_1 |= 0x02; // Enable UDC/Icon Blink _writeCommand(0x20 | _function_1); // Function set 0 0 1 DL N RE(1) BE 0/LP (Ext Regs) _writeCommand(0x20 | _function); // Function set 0 0 1 DL N RE(0) DH REV (Std Regs) - break; // case KS0073, KS0078 Controller + break; // case KS0073, KS0078, HD66712 Controller case US2066_3V3 : case SSD1803_3V3 : @@ -1926,6 +2244,7 @@ case PCF2103_3V3 : case PCF2113_3V3 : case PCF2119_3V3 : + case PCF2119R_3V3 : // Enable Icon Blink _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1 _writeCommand(0x08 | 0x02); // ICON Conf 0000 1, IM=0 (Char mode), IB=1 (Icon blink), 0 (Instr. Set 1) @@ -1943,13 +2262,14 @@ case BlinkOff: // Controllers that support UDC Blink switch (_ctrl) { - case KS0073 : - case KS0078 : + case KS0073 : + case KS0078 : + case HD66712: _function_1 &= ~0x02; // Disable UDC/Icon Blink _writeCommand(0x20 | _function_1); // Function set 0 0 1 DL N RE(1) BE 0/LP (Ext Regs) _writeCommand(0x20 | _function); // Function set 0 0 1 DL N RE(0) DH REV (Std Regs) - break; // case KS0073, KS0078 Controller + break; // case KS0073, KS0078, HD66712 Controller case US2066_3V3 : case SSD1803_3V3 : @@ -1962,8 +2282,9 @@ break; // case SSD1803, US2066 case PCF2103_3V3 : - case PCF2113_3V3 : - case PCF2119_3V3 : + case PCF2113_3V3 : + case PCF2119_3V3 : + case PCF2119R_3V3 : // Disable Icon Blink _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1 _writeCommand(0x08); // ICON Conf 0000 1, IM=0 (Char mode), IB=1 (Icon blink), 0 (Instr. Set 1) @@ -1983,8 +2304,9 @@ } // blinkMode } // setUDCBlink() - - +#endif + +#if(LCD_CONTRAST == 1) /** Set Contrast * setContrast method is supported by some compatible devices (eg ST7032i) that have onboard LCD voltage generation * Initial code for ST70XX imported from fork by JH1PJL @@ -2002,7 +2324,8 @@ switch (_ctrl) { case PCF2113_3V3 : - case PCF2119_3V3 : + case PCF2119_3V3 : + case PCF2119R_3V3 : if (_contrast < 5) _contrast = 0; // See datasheet. Sanity check for PCF2113/PCF2119 if (_contrast > 55) _contrast = 55; @@ -2016,7 +2339,8 @@ case ST7032_5V : case ST7036_3V3 : // case ST7036_5V : - case SSD1803_3V3 : + case SSD1803_3V3 : + case SPLC792A_3V3 : _writeCommand(0x20 | _function | 0x01); // Select Instruction Set = 1 _writeCommand(0x70 | (_contrast & 0x0F)); // Contrast Low bits _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Contrast High bits @@ -2053,8 +2377,9 @@ break; } // end switch } // end setContrast() - - +#endif + +#if(LCD_POWER == 1) /** Set Power * setPower method is supported by some compatible devices (eg SSD1803) that have power down modes * @@ -2072,7 +2397,8 @@ switch (_ctrl) { // case PCF2113_3V3 : -// case PCF2119_3V3 : +// case PCF2119_3V3 : +// case PCF2119R_3V3 : // case ST7032_3V3 : //@todo // enable Booster Bon @@ -2104,7 +2430,8 @@ switch (_ctrl) { // case PCF2113_3V3 : -// case PCF2119_3V3 : +// case PCF2119_3V3 : +// case PCF2119R_3V3 : // case ST7032_3V3 : //@todo // disable Booster Bon @@ -2128,8 +2455,9 @@ } // end switch } } // end setPower() - - +#endif + +#if(LCD_ORIENT == 1) /** Set Orient * setOrient method is supported by some compatible devices (eg SSD1803, US2066) that have top/bottom view modes * @@ -2145,11 +2473,17 @@ case PCF2103_3V3: case PCF2116_3V3: case PCF2116_5V: - case PCF2119_3V3: _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1 _writeCommand(0x05); // Display Conf Set 0000 0, 1, P=0, Q=1 (Instr. Set 1) _writeCommand(0x20 | _function); // Set function, Select Instr Set = 0 break; + + case PCF2119_3V3: + case PCF2119R_3V3: + _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1 + _writeCommand(0x07); // Display Conf Set 0000 0, 1, P=1, Q=1 (Instr. Set 1) + _writeCommand(0x20 | _function); // Set function, Select Instr Set = 0 + break; case SSD1803_3V3 : // case SSD1803_5V : @@ -2187,11 +2521,17 @@ case PCF2103_3V3: case PCF2116_3V3: case PCF2116_5V: - case PCF2119_3V3: _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1 _writeCommand(0x06); // Display Conf Set 0000 0, 1, P=1, Q=0 (Instr. Set 1) _writeCommand(0x20 | _function); // Set function, Select Instr Set = 0 break; + + case PCF2119_3V3: + case PCF2119R_3V3 : + _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1 + _writeCommand(0x04); // Display Conf Set 0000 0, 1, P=0, Q=0 (Instr. Set 1) + _writeCommand(0x20 | _function); // Set function, Select Instr Set = 0 + break; case SSD1803_3V3 : // case SSD1803_5V : @@ -2231,7 +2571,9 @@ break; // end Bottom } // end switch orient } // end setOrient() - +#endif + +#if(LCD_BIGFONT == 1) /** Set Big Font * setBigFont method is supported by some compatible devices (eg SSD1803, US2066) * @@ -2347,8 +2689,107 @@ } // end switch lines } // end setBigFont() - - +#endif + + +#if (LCD_FONTSEL == 1) +/** Set Font + * setFont method is supported by some compatible devices (eg SSD1803, US2066, ST7070) + * + * @param LCDFont font The selected Font + * @return none + * + * Note: most controllers support only one font and the hardware specific + * fonttable is encoded as part of the controller type number (eg PCF21XXC or PCF21XXR). + * Some controllers support multiple tables that can only be selected by logic levels on a few pins. + * Some controllers also support runtime fontable switching through a specific instruction + */ +void TextLCD_Base::setFont(LCDFont font) { + + switch (font) { + case Font_RA: // UK/EU + switch (_ctrl) { + case SSD1803_3V3 : + case US2066_3V3 : + _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV + // Select Extended Instruction Set + _writeCommand(0x72); // ROM Select command, 0 1 1 1 0 0 1 0 (Ext Instr Set) + _writeData(0x00); // ROM_0 Select data, 0 0 0 0 ROM2 ROM1 0 0 (Ext Instr Set) + + _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS (Std Instr Set) + + _font = font; // Save active font + break; // end SSD1803, US2066 + + case ST7070: + //ST7070 does not support Cursorblink. The P bit selects the font instead ! + _writeCommand(0x08 | _currentMode | (_currentCursor & 0x02)); + + _font = font; // Save active font + break; // end ST7070 + + default: + break; // end default + } // end switch _ctrl + break; // end Font_RA + + case Font_RB: // UK/CYR + switch (_ctrl) { + case SSD1803_3V3 : + case US2066_3V3 : + _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV + // Select Extended Instruction Set + _writeCommand(0x72); // ROM Select command, 0 1 1 1 0 0 1 0 (Ext Instr Set) + _writeData(0x04); // ROM_0 Select data, 0 0 0 0 ROM2 ROM1 0 0 (Ext Instr Set) + + _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS (Std Instr Set) + + _font = font; // Save active font + break; // end SSD1803, US2066 + + case ST7070: + //ST7070 does not support Cursorblink. The P bit selects the font instead ! + _writeCommand(0x08 | _currentMode | (_currentCursor & 0x02) | 0x01); + + _font = font; // Save active font + break; // end ST7070 + + default: + break; // end default + } // end switch _ctrl + break; // end Font_RB + + case Font_0: //Font_O is pretty similar to ROM_C + case Font_RC: // UK/JAP + switch (_ctrl) { + case SSD1803_3V3 : + case US2066_3V3 : + _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV + // Select Extended Instruction Set + _writeCommand(0x72); // ROM Select command, 0 1 1 1 0 0 1 0 (Ext Instr Set) + _writeData(0x08); // ROM_0 Select data, 0 0 0 0 ROM2 ROM1 0 0 (Ext Instr Set) + + _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS (Std Instr Set) + + _font = font; // Save active font + break; // end SSD1803, US2066 + + default: + break; // end default + } // end switch _ctrl + break; // end Font_RC + } // end switch font + + //SSD1803 seems to screw up cursor position after selecting new font. Restore to make sure... + //Set next memoryaddress, make sure cursor blinks at next location + int addr = getAddress(_column, _row); + _writeCommand(0x80 | addr); + +} +#endif + + +#if(LCD_ICON==1) /** Set Icons * * @param unsigned char idx The Index of the icon pattern (0..15) for KS0073 and similar controllers @@ -2394,6 +2835,7 @@ case ST7032_3V3: case ST7032_5V: + case SPLC792A_3V3: _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 DL N F 0 IS=1 Select Instr Set = 1 _writeCommand(0x40 | (idx & 0x0F)); // Set Icon Address, mask Address to valid range (Instr Set 1) @@ -2427,7 +2869,8 @@ case PCF2103_3V3: case PCF2113_3V3: - case PCF2119_3V3: + case PCF2119_3V3: + case PCF2119R_3V3: // Store UDC/Icon pattern for PCF2103 and PCF2113: // 3 x 8 rows x 5 bits = 120 bits for Normal pattern (UDC 0..2) and // 3 x 8 rows x 5 bits = 120 bits for Blink pattern (UDC 4..6) @@ -2491,6 +2934,7 @@ case ST7032_3V3: case ST7032_5V: + case SPLC792A_3V3: _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 DL N F 0 IS=1 Select Instr Set = 1 for (idx=0; idx<16; idx++) { @@ -2548,7 +2992,8 @@ } break; // case PCF2103_3V3 Controller - case PCF2119_3V3: + case PCF2119_3V3: + case PCF2119R_3V3: // PCF2119 uses part of the UDC RAM to control Icons // Select CG RAM @@ -2576,8 +3021,9 @@ int addr = getAddress(_column, _row); _writeCommand(0x80 | addr); } //end clrIcon() - - +#endif + +#if(LCD_INVERT == 1) /** Set Invert * setInvert method is supported by some compatible devices (eg KS0073) to swap between black and white * @@ -2635,6 +3081,7 @@ } // end switch } } // end setInvert() +#endif //--------- End TextLCD_Base ----------- @@ -2676,8 +3123,8 @@ // No Hardware Enable pin _e2 = NULL; //Construct dummy pin } - - _init(_LCD_DL_4); // Set Datalength to 4 bit for mbed bus interfaces + + _init(_LCD_DL_4); // Set Datalength to 4 bit for mbed bus interfaces } /** Destruct a TextLCD interface for using regular mbed pins @@ -2791,7 +3238,7 @@ // _writeRegister(OLAT, 0x00); // Output Latch // Init the portexpander bus - _lcd_bus = D_LCD_BUS_DEF; + _lcd_bus = LCD_BUS_I2C_DEF; // write the new data to the portexpander _writeRegister(GPIO, _lcd_bus); @@ -2799,7 +3246,7 @@ // PCF8574 of PCF8574A portexpander // Init the portexpander bus - _lcd_bus = D_LCD_BUS_DEF; + _lcd_bus = LCD_BUS_I2C_DEF; // write the new data to the portexpander _i2c->write(_slaveAddress, &_lcd_bus, 1); @@ -2812,22 +3259,33 @@ // Used for mbed I2C bus expander void TextLCD_I2C::_setEnableBit(bool value) { +#if (LCD_TWO_CTRL == 1) if(_ctrl_idx==_LCDCtrl_0) { if (value) { - _lcd_bus |= D_LCD_E; // Set E bit + _lcd_bus |= LCD_BUS_I2C_E; // Set E bit } else { - _lcd_bus &= ~D_LCD_E; // Reset E bit + _lcd_bus &= ~LCD_BUS_I2C_E; // Reset E bit } } else { if (value) { - _lcd_bus |= D_LCD_E2; // Set E2 bit + _lcd_bus |= LCD_BUS_I2C_E2; // Set E2 bit } else { - _lcd_bus &= ~D_LCD_E2; // Reset E2bit + _lcd_bus &= ~LCD_BUS_I2C_E2; // Reset E2bit } } +#else +// Support only one controller + if (value) { + _lcd_bus |= LCD_BUS_I2C_E; // Set E bit + } + else { + _lcd_bus &= ~LCD_BUS_I2C_E; // Reset E bit + } + +#endif } // Set E pin (or E2 pin) @@ -2856,10 +3314,10 @@ void TextLCD_I2C::_setRS(bool value) { if (value) { - _lcd_bus |= D_LCD_RS; // Set RS bit + _lcd_bus |= LCD_BUS_I2C_RS; // Set RS bit } else { - _lcd_bus &= ~D_LCD_RS; // Reset RS bit + _lcd_bus &= ~LCD_BUS_I2C_RS; // Reset RS bit } #if (MCP23008==1) @@ -2880,10 +3338,10 @@ void TextLCD_I2C::_setBL(bool value) { if (value) { - _lcd_bus |= D_LCD_BL; // Set BL bit + _lcd_bus |= LCD_BUS_I2C_BL; // Set BL bit } else { - _lcd_bus &= ~D_LCD_BL; // Reset BL bit + _lcd_bus &= ~LCD_BUS_I2C_BL; // Reset BL bit } #if (MCP23008==1) @@ -2899,41 +3357,66 @@ #endif } - +#if(0) +// New optimized v018 +// Test faster _writeByte 0.11s vs 0.27s for a 20x4 fillscreen (PCF8574), same as v018 +// Place the 4bit data in the databus shadowvalue +// Used for mbed I2C bus expander +const char _LCD_DATA_BITS[16] = { + 0x00, + ( LCD_BUS_I2C_D4), + ( LCD_BUS_I2C_D5 ), + ( LCD_BUS_I2C_D5 | LCD_BUS_I2C_D4), + ( LCD_BUS_I2C_D6 ), + ( LCD_BUS_I2C_D6 | LCD_BUS_I2C_D4), + ( LCD_BUS_I2C_D6 | LCD_BUS_I2C_D5 ), + ( LCD_BUS_I2C_D6 | LCD_BUS_I2C_D5 | LCD_BUS_I2C_D4), + (LCD_BUS_I2C_D7 ), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D4), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D5 ), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D5 | LCD_BUS_I2C_D4), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D6 ), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D6 | LCD_BUS_I2C_D4), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D6 | LCD_BUS_I2C_D5 ), + (LCD_BUS_I2C_D7 | LCD_BUS_I2C_D6 | LCD_BUS_I2C_D5 | LCD_BUS_I2C_D4) + }; +void TextLCD_I2C::_setDataBits(int value) { + + //Clear all databits + _lcd_bus &= ~LCD_BUS_I2C_MSK; + + // Set bit by bit to support any mapping of expander portpins to LCD pins + _lcd_bus |= _LCD_DATA_BITS[value & 0x0F]; +} +#endif + +// Test faster _writeByte 0.11s vs 0.27s for a 20x4 fillscreen (PCF8574) // Place the 4bit data in the databus shadowvalue // Used for mbed I2C bus expander void TextLCD_I2C::_setDataBits(int value) { + //Clear all databits + _lcd_bus &= ~LCD_BUS_I2C_MSK; + // Set bit by bit to support any mapping of expander portpins to LCD pins if (value & 0x01){ - _lcd_bus |= D_LCD_D4; // Set Databit - } - else { - _lcd_bus &= ~D_LCD_D4; // Reset Databit + _lcd_bus |= LCD_BUS_I2C_D4; // Set Databit } if (value & 0x02){ - _lcd_bus |= D_LCD_D5; // Set Databit - } - else { - _lcd_bus &= ~D_LCD_D5; // Reset Databit + _lcd_bus |= LCD_BUS_I2C_D5; // Set Databit } if (value & 0x04) { - _lcd_bus |= D_LCD_D6; // Set Databit - } - else { - _lcd_bus &= ~D_LCD_D6; // Reset Databit + _lcd_bus |= LCD_BUS_I2C_D6; // Set Databit } if (value & 0x08) { - _lcd_bus |= D_LCD_D7; // Set Databit + _lcd_bus |= LCD_BUS_I2C_D7; // Set Databit } - else { - _lcd_bus &= ~D_LCD_D7; // Reset Databit - } } + // Place the 4bit data on the databus // Used for mbed pins, I2C bus expander or SPI shifregister void TextLCD_I2C::_setData(int value) { @@ -3032,8 +3515,7 @@ TextLCD_SPI::TextLCD_SPI(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) : TextLCD_Base(type, ctrl), _spi(spi), - _cs(cs) { - + _cs(cs) { // Init cs _cs = 1; @@ -3043,8 +3525,10 @@ _spi->frequency(500000); //_spi.frequency(1000000); + wait_ms(100); // Wait 100ms to ensure LCD powered up + // Init the portexpander bus - _lcd_bus = D_LCD_BUS_DEF; + _lcd_bus = LCD_BUS_SPI_DEF; // write the new data to the portexpander _cs = 0; @@ -3060,18 +3544,18 @@ if(_ctrl_idx==_LCDCtrl_0) { if (value) { - _lcd_bus |= D_LCD_E; // Set E bit + _lcd_bus |= LCD_BUS_SPI_E; // Set E bit } else { - _lcd_bus &= ~D_LCD_E; // Reset E bit + _lcd_bus &= ~LCD_BUS_SPI_E; // Reset E bit } } else { if (value) { - _lcd_bus |= D_LCD_E2; // Set E2 bit + _lcd_bus |= LCD_BUS_SPI_E2; // Set E2 bit } else { - _lcd_bus &= ~D_LCD_E2; // Reset E2 bit + _lcd_bus &= ~LCD_BUS_SPI_E2; // Reset E2 bit } } @@ -3086,10 +3570,10 @@ void TextLCD_SPI::_setRS(bool value) { if (value) { - _lcd_bus |= D_LCD_RS; // Set RS bit + _lcd_bus |= LCD_BUS_SPI_RS; // Set RS bit } else { - _lcd_bus &= ~D_LCD_RS; // Reset RS bit + _lcd_bus &= ~LCD_BUS_SPI_RS; // Reset RS bit } // write the new data to the SPI portexpander @@ -3103,10 +3587,10 @@ void TextLCD_SPI::_setBL(bool value) { if (value) { - _lcd_bus |= D_LCD_BL; // Set BL bit + _lcd_bus |= LCD_BUS_SPI_BL; // Set BL bit } else { - _lcd_bus &= ~D_LCD_BL; // Reset BL bit + _lcd_bus &= ~LCD_BUS_SPI_BL; // Reset BL bit } // write the new data to the SPI portexpander @@ -3121,31 +3605,31 @@ // Set bit by bit to support any mapping of expander portpins to LCD pins if (value & 0x01) { - _lcd_bus |= D_LCD_D4; // Set Databit + _lcd_bus |= LCD_BUS_SPI_D4; // Set Databit } else { - _lcd_bus &= ~D_LCD_D4; // Reset Databit + _lcd_bus &= ~LCD_BUS_SPI_D4; // Reset Databit } if (value & 0x02) { - _lcd_bus |= D_LCD_D5; // Set Databit + _lcd_bus |= LCD_BUS_SPI_D5; // Set Databit } else { - _lcd_bus &= ~D_LCD_D5; // Reset Databit + _lcd_bus &= ~LCD_BUS_SPI_D5; // Reset Databit } if (value & 0x04) { - _lcd_bus |= D_LCD_D6; // Set Databit + _lcd_bus |= LCD_BUS_SPI_D6; // Set Databit } else { - _lcd_bus &= ~D_LCD_D6; // Reset Databit + _lcd_bus &= ~LCD_BUS_SPI_D6; // Reset Databit } if (value & 0x08) { - _lcd_bus |= D_LCD_D7; // Set Databit + _lcd_bus |= LCD_BUS_SPI_D7; // Set Databit } else { - _lcd_bus &= ~D_LCD_D7; // Reset Databit + _lcd_bus &= ~LCD_BUS_SPI_D7; // Reset Databit } // write the new data to the SPI portexpander