vmh

Dependencies:   mbed

Committer:
wonner163
Date:
Mon Dec 11 08:43:44 2017 +0000
Revision:
0:4562b55bbb43
12

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wonner163 0:4562b55bbb43 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
wonner163 0:4562b55bbb43 2 * Copyright (c) 2007-2010, sford, http://mbed.org
wonner163 0:4562b55bbb43 3 * 2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs
wonner163 0:4562b55bbb43 4 * 2013, v02: WH, Added I2C and SPI bus interfaces
wonner163 0:4562b55bbb43 5 * 2013, v03: WH, Added support for LCD40x4 which uses 2 controllers
wonner163 0:4562b55bbb43 6 * 2013, v04: WH, Added support for Display On/Off, improved 4bit bootprocess
wonner163 0:4562b55bbb43 7 * 2013, v05: WH, Added support for 8x2B, added some UDCs
wonner163 0:4562b55bbb43 8 * 2013, v06: WH, Added support for devices that use internal DC/DC converters
wonner163 0:4562b55bbb43 9 * 2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT
wonner163 0:4562b55bbb43 10 * 2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined pins
wonner163 0:4562b55bbb43 11 * 2014, v09: WH/EO, Added Class for Native SPI controllers such as ST7032
wonner163 0:4562b55bbb43 12 * 2014, v10: WH, Added Class for Native I2C controllers such as ST7032i, Added support for MCP23008 I2C portexpander, Added support for Adafruit module
wonner163 0:4562b55bbb43 13 * 2014, v11: WH, Added support for native I2C controllers such as PCF21XX, Improved the _initCtrl() method to deal with differences between all supported controllers
wonner163 0:4562b55bbb43 14 * 2014, v12: WH, Added support for native I2C controller PCF2119 and native I2C/SPI controllers SSD1803, ST7036, added setContrast method (by JH1PJL) for supported devices (eg ST7032i)
wonner163 0:4562b55bbb43 15 * 2014, v13: WH, Added support for controllers US2066/SSD1311 (OLED), added setUDCBlink method for supported devices (eg SSD1803), fixed issue in setPower()
wonner163 0:4562b55bbb43 16 *@Todo Add AC780S/KS0066i
wonner163 0:4562b55bbb43 17 *
wonner163 0:4562b55bbb43 18 * Permission is hereby granted, free of charge, to any person obtaining a copy
wonner163 0:4562b55bbb43 19 * of this software and associated documentation files (the "Software"), to deal
wonner163 0:4562b55bbb43 20 * in the Software without restriction, including without limitation the rights
wonner163 0:4562b55bbb43 21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wonner163 0:4562b55bbb43 22 * copies of the Software, and to permit persons to whom the Software is
wonner163 0:4562b55bbb43 23 * furnished to do so, subject to the following conditions:
wonner163 0:4562b55bbb43 24 *
wonner163 0:4562b55bbb43 25 * The above copyright notice and this permission notice shall be included in
wonner163 0:4562b55bbb43 26 * all copies or substantial portions of the Software.
wonner163 0:4562b55bbb43 27 *
wonner163 0:4562b55bbb43 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wonner163 0:4562b55bbb43 29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wonner163 0:4562b55bbb43 30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wonner163 0:4562b55bbb43 31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wonner163 0:4562b55bbb43 32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wonner163 0:4562b55bbb43 33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wonner163 0:4562b55bbb43 34 * THE SOFTWARE.
wonner163 0:4562b55bbb43 35 */
wonner163 0:4562b55bbb43 36
wonner163 0:4562b55bbb43 37 #include "TextLCD.h"
wonner163 0:4562b55bbb43 38 #include "mbed.h"
wonner163 0:4562b55bbb43 39
wonner163 0:4562b55bbb43 40 //For Testing only
wonner163 0:4562b55bbb43 41 //DigitalOut led1(LED1);
wonner163 0:4562b55bbb43 42 //DigitalOut led2(LED2);
wonner163 0:4562b55bbb43 43 // led2=!led2;
wonner163 0:4562b55bbb43 44
wonner163 0:4562b55bbb43 45
wonner163 0:4562b55bbb43 46 // User Defined Characters (UDCs) are defined by an 8 byte bitpattern. The P0..P5 form the character pattern.
wonner163 0:4562b55bbb43 47 // P7 P6 P5 P4 P3 P2 P1 P0
wonner163 0:4562b55bbb43 48 // 0 B1 B0 x 0 1 1 1 0
wonner163 0:4562b55bbb43 49 // 1 B1 B0 x 1 0 0 0 1
wonner163 0:4562b55bbb43 50 // . .............
wonner163 0:4562b55bbb43 51 // 7 B1 B0 x 1 0 0 0 1
wonner163 0:4562b55bbb43 52 //
wonner163 0:4562b55bbb43 53 // Blinking UDCs are enabled when a specific controlbit (BE) is set.
wonner163 0:4562b55bbb43 54 // The blinking pixels in the UDC can be controlled by setting additional bits in the UDC bitpattern.
wonner163 0:4562b55bbb43 55 // Bit 6 and Bit 7 in the pattern will control the blinking mode when Blink is enabled through BE.
wonner163 0:4562b55bbb43 56 // B1 B0 Mode
wonner163 0:4562b55bbb43 57 // 0 0 No Blinking in this row of the UDC
wonner163 0:4562b55bbb43 58 // 0 1 Enabled pixels in P4 will blink
wonner163 0:4562b55bbb43 59 // 1 x Enabled pixels in P0..P4 will blink
wonner163 0:4562b55bbb43 60
wonner163 0:4562b55bbb43 61 /** Some sample User Defined Chars 5x7 dots */
wonner163 0:4562b55bbb43 62 //const char udc_ae[] = {0x00, 0x00, 0x1B, 0x05, 0x1F, 0x14, 0x1F, 0x00}; //æ
wonner163 0:4562b55bbb43 63 //const char udc_0e[] = {0x00, 0x00, 0x0E, 0x13, 0x15, 0x19, 0x0E, 0x00}; //ø
wonner163 0:4562b55bbb43 64 //const char udc_ao[] = {0x0E, 0x0A, 0x0E, 0x01, 0x0F, 0x11, 0x0F, 0x00}; //å
wonner163 0:4562b55bbb43 65 //const char udc_AE[] = {0x0F, 0x14, 0x14, 0x1F, 0x14, 0x14, 0x17, 0x00}; //Æ
wonner163 0:4562b55bbb43 66 //const char udc_0E[] = {0x0E, 0x13, 0x15, 0x15, 0x15, 0x19, 0x0E, 0x00}; //Ø
wonner163 0:4562b55bbb43 67 //const char udc_Ao[] = {0x0E, 0x0A, 0x0E, 0x11, 0x1F, 0x11, 0x11, 0x00}; //Å
wonner163 0:4562b55bbb43 68 //const char udc_PO[] = {0x04, 0x0A, 0x0A, 0x1F, 0x1B, 0x1B, 0x1F, 0x00}; //Padlock Open
wonner163 0:4562b55bbb43 69 //const char udc_PC[] = {0x1C, 0x10, 0x08, 0x1F, 0x1B, 0x1B, 0x1F, 0x00}; //Padlock Closed
wonner163 0:4562b55bbb43 70
wonner163 0:4562b55bbb43 71 //const char udc_alpha[] = {0x00, 0x00, 0x0D, 0x12, 0x12, 0x12, 0x0D, 0x00}; //alpha
wonner163 0:4562b55bbb43 72 //const char udc_ohm[] = {0x0E, 0x11, 0x11, 0x11, 0x0A, 0x0A, 0x1B, 0x00}; //ohm
wonner163 0:4562b55bbb43 73 //const char udc_sigma[] = {0x1F, 0x08, 0x04, 0x02, 0x04, 0x08, 0x1F, 0x00}; //sigma
wonner163 0:4562b55bbb43 74 //const char udc_pi[] = {0x1F, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x00}; //pi
wonner163 0:4562b55bbb43 75 //const char udc_root[] = {0x07, 0x04, 0x04, 0x04, 0x14, 0x0C, 0x04, 0x00}; //root
wonner163 0:4562b55bbb43 76
wonner163 0:4562b55bbb43 77 const char udc_0[] = {0x18, 0x14, 0x12, 0x11, 0x12, 0x14, 0x18, 0x00}; // |>
wonner163 0:4562b55bbb43 78 const char udc_1[] = {0x03, 0x05, 0x09, 0x11, 0x09, 0x05, 0x03, 0x00}; // <|
wonner163 0:4562b55bbb43 79 const char udc_2[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}; // |
wonner163 0:4562b55bbb43 80 const char udc_3[] = {0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00}; // ||
wonner163 0:4562b55bbb43 81 const char udc_4[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00}; // |||
wonner163 0:4562b55bbb43 82 const char udc_5[] = {0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x00}; // =
wonner163 0:4562b55bbb43 83 const char udc_6[] = {0x15, 0x0a, 0x15, 0x0a, 0x15, 0x0a, 0x15, 0x00}; // checkerboard
wonner163 0:4562b55bbb43 84 const char udc_7[] = {0x10, 0x08, 0x04, 0x02, 0x01, 0x00, 0x10, 0x00}; // \
wonner163 0:4562b55bbb43 85
wonner163 0:4562b55bbb43 86 const char udc_degr[] = {0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00}; // Degree symbol
wonner163 0:4562b55bbb43 87
wonner163 0:4562b55bbb43 88 const char udc_TM_T[] = {0x1F, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00}; // Trademark T
wonner163 0:4562b55bbb43 89 const char udc_TM_M[] = {0x11, 0x1B, 0x15, 0x11, 0x00, 0x00, 0x00, 0x00}; // Trademark M
wonner163 0:4562b55bbb43 90
wonner163 0:4562b55bbb43 91 //const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Full
wonner163 0:4562b55bbb43 92 //const char udc_Bat_Ha[] = {0x0E, 0x11, 0x13, 0x17, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Half
wonner163 0:4562b55bbb43 93 //const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x00}; // Battery Low
wonner163 0:4562b55bbb43 94 //const char udc_Bat_Hi[] = {0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Full
wonner163 0:4562b55bbb43 95 //const char udc_Bat_Ha[] = {0x0E, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Battery Half
wonner163 0:4562b55bbb43 96 //const char udc_Bat_Lo[] = {0x0E, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F, 0x00}; // Battery Low
wonner163 0:4562b55bbb43 97 const char udc_Bat_Hi[] = {0x8E, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x00}; // Battery Full, Blink
wonner163 0:4562b55bbb43 98 const char udc_Bat_Ha[] = {0x8E, 0x91, 0x91, 0x9F, 0x9F, 0x9F, 0x9F, 0x00}; // Battery Half, Blink
wonner163 0:4562b55bbb43 99 const char udc_Bat_Lo[] = {0x8E, 0x91, 0x91, 0x91, 0x91, 0x9F, 0x9F, 0x00}; // Battery Low, Blink
wonner163 0:4562b55bbb43 100 const char udc_AC[] = {0x0A, 0x0A, 0x1F, 0x11, 0x0E, 0x04, 0x04, 0x00}; // AC Power
wonner163 0:4562b55bbb43 101
wonner163 0:4562b55bbb43 102 //const char udc_smiley[] = {0x00, 0x0A, 0x00, 0x04, 0x11, 0x0E, 0x00, 0x00}; // Smiley
wonner163 0:4562b55bbb43 103 //const char udc_droopy[] = {0x00, 0x0A, 0x00, 0x04, 0x00, 0x0E, 0x11, 0x00}; // Droopey
wonner163 0:4562b55bbb43 104 //const char udc_note[] = {0x01, 0x03, 0x05, 0x09, 0x0B, 0x1B, 0x18, 0x00}; // Note
wonner163 0:4562b55bbb43 105
wonner163 0:4562b55bbb43 106 //const char udc_bar_1[] = {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}; // Bar 1
wonner163 0:4562b55bbb43 107 //const char udc_bar_2[] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00}; // Bar 11
wonner163 0:4562b55bbb43 108 //const char udc_bar_3[] = {0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x00}; // Bar 111
wonner163 0:4562b55bbb43 109 //const char udc_bar_4[] = {0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x00}; // Bar 1111
wonner163 0:4562b55bbb43 110 //const char udc_bar_5[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00}; // Bar 11111
wonner163 0:4562b55bbb43 111
wonner163 0:4562b55bbb43 112 //const char udc_ch_1[] = {0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00}; // Hor bars 4
wonner163 0:4562b55bbb43 113 //const char udc_ch_2[] = {0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x1f}; // Hor bars 4 (inverted)
wonner163 0:4562b55bbb43 114 //const char udc_ch_3[] = {0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15}; // Ver bars 3
wonner163 0:4562b55bbb43 115 //const char udc_ch_4[] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a}; // Ver bars 3 (inverted)
wonner163 0:4562b55bbb43 116 //const char udc_ch_yr[] = {0x08, 0x0f, 0x12, 0x0f, 0x0a, 0x1f, 0x02, 0x02}; // Year (kana)
wonner163 0:4562b55bbb43 117 //const char udc_ch_mo[] = {0x0f, 0x09, 0x0f, 0x09, 0x0f, 0x09, 0x09, 0x13}; // Month (kana)
wonner163 0:4562b55bbb43 118 //const char udc_ch_dy[] = {0x1f, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11, 0x1F}; // Day (kana)
wonner163 0:4562b55bbb43 119 //const char udc_ch_mi[] = {0x0C, 0x0a, 0x11, 0x1f, 0x09, 0x09, 0x09, 0x13}; // minute (kana)
wonner163 0:4562b55bbb43 120
wonner163 0:4562b55bbb43 121 //const char udc_bell[] = {0x04,0x0E,0x0E,0x0E,0x1F,0x00,0x04};
wonner163 0:4562b55bbb43 122 //const char udc_note[] = {0x02,0x03,0x02,0x0E,0x1E,0x0C,0x00};
wonner163 0:4562b55bbb43 123 //const char udc_clock[] = {0x00,0x0E,0x15,0x17,0x11,0x0E,0x00};
wonner163 0:4562b55bbb43 124 //const char udc_heart[] = {0x00,0x0a,0x1F,0x1F,0x0E,0x04,0x00};
wonner163 0:4562b55bbb43 125 //const char udc_duck[] = {0x00,0x0c,0x1D,0x0F,0x0F,0x06,0x00};
wonner163 0:4562b55bbb43 126 //const char udc_check[] = {0x00,0x01,0x03,0x16,0x1C,0x08,0x00};
wonner163 0:4562b55bbb43 127 //const char udc_cross[] = {0x00,0x1B,0x0E,0x04,0x0E,0x1B,0x00};
wonner163 0:4562b55bbb43 128 //const char udc_retarrow[] = {0x01,0x01,0x05,0x09,0x1f,0x08,0x04};
wonner163 0:4562b55bbb43 129
wonner163 0:4562b55bbb43 130 const char udc_None[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
wonner163 0:4562b55bbb43 131 const char udc_All[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
wonner163 0:4562b55bbb43 132
wonner163 0:4562b55bbb43 133 /** Create a TextLCD_Base interface
wonner163 0:4562b55bbb43 134 *
wonner163 0:4562b55bbb43 135 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 136 * @param ctrl LCD controller (default = HD44780)
wonner163 0:4562b55bbb43 137 */
wonner163 0:4562b55bbb43 138 TextLCD_Base::TextLCD_Base(LCDType type, LCDCtrl ctrl) : _type(type), _ctrl(ctrl) {
wonner163 0:4562b55bbb43 139
wonner163 0:4562b55bbb43 140 // Extract LCDType data
wonner163 0:4562b55bbb43 141
wonner163 0:4562b55bbb43 142 // Columns encoded in b7..b0
wonner163 0:4562b55bbb43 143 _nr_cols = (_type & 0xFF);
wonner163 0:4562b55bbb43 144
wonner163 0:4562b55bbb43 145 // Rows encoded in b15..b8
wonner163 0:4562b55bbb43 146 _nr_rows = ((_type >> 8) & 0xFF);
wonner163 0:4562b55bbb43 147
wonner163 0:4562b55bbb43 148 // Addressing mode encoded in b19..b16
wonner163 0:4562b55bbb43 149 _addr_mode = _type & LCD_T_ADR_MSK;
wonner163 0:4562b55bbb43 150 }
wonner163 0:4562b55bbb43 151
wonner163 0:4562b55bbb43 152
wonner163 0:4562b55bbb43 153 /** Init the LCD Controller(s)
wonner163 0:4562b55bbb43 154 * Clear display
wonner163 0:4562b55bbb43 155 */
wonner163 0:4562b55bbb43 156 void TextLCD_Base::_init() {
wonner163 0:4562b55bbb43 157
wonner163 0:4562b55bbb43 158 // Select and configure second LCD controller when needed
wonner163 0:4562b55bbb43 159 if(_type==LCD40x4) {
wonner163 0:4562b55bbb43 160 _ctrl_idx=_LCDCtrl_1; // Select 2nd controller
wonner163 0:4562b55bbb43 161 _initCtrl(); // Init 2nd controller
wonner163 0:4562b55bbb43 162 }
wonner163 0:4562b55bbb43 163
wonner163 0:4562b55bbb43 164 // Select and configure primary LCD controller
wonner163 0:4562b55bbb43 165 _ctrl_idx=_LCDCtrl_0; // Select primary controller
wonner163 0:4562b55bbb43 166 _initCtrl(); // Init primary controller
wonner163 0:4562b55bbb43 167
wonner163 0:4562b55bbb43 168 // Clear whole display and Reset Cursor location
wonner163 0:4562b55bbb43 169 // Note: This will make sure that some 3-line displays that skip topline of a 4-line configuration
wonner163 0:4562b55bbb43 170 // are cleared and init cursor correctly.
wonner163 0:4562b55bbb43 171 cls();
wonner163 0:4562b55bbb43 172 }
wonner163 0:4562b55bbb43 173
wonner163 0:4562b55bbb43 174 /** Init the LCD controller
wonner163 0:4562b55bbb43 175 * 4-bit mode, number of lines, fonttype, no cursor etc
wonner163 0:4562b55bbb43 176 *
wonner163 0:4562b55bbb43 177 * Note: some configurations are commented out because they have not yet been tested due to lack of hardware
wonner163 0:4562b55bbb43 178 */
wonner163 0:4562b55bbb43 179 void TextLCD_Base::_initCtrl() {
wonner163 0:4562b55bbb43 180 int _bias_lines=0; // Set Bias and lines (Instr Set 1), temporary variable.
wonner163 0:4562b55bbb43 181 int _lines=0; // Set lines (Ext Instr Set), temporary variable.
wonner163 0:4562b55bbb43 182
wonner163 0:4562b55bbb43 183 this->_setRS(false); // command mode
wonner163 0:4562b55bbb43 184
wonner163 0:4562b55bbb43 185 wait_ms(20); // Wait 20ms to ensure powered up
wonner163 0:4562b55bbb43 186
wonner163 0:4562b55bbb43 187 // The Controller could be in 8 bit mode (power-on reset) or in 4 bit mode (warm reboot) at this point.
wonner163 0:4562b55bbb43 188 // Follow this procedure to make sure the Controller enters the correct state. The hardware interface
wonner163 0:4562b55bbb43 189 // between the uP and the LCD can only write the 4 most significant bits (Most Significant Nibble, MSN).
wonner163 0:4562b55bbb43 190 // In 4 bit mode the LCD expects the MSN first, followed by the LSN.
wonner163 0:4562b55bbb43 191 //
wonner163 0:4562b55bbb43 192 // Current state: 8 bit mode | 4 bit mode, MSN is next | 4 bit mode, LSN is next
wonner163 0:4562b55bbb43 193 //-------------------------------------------------------------------------------------------------
wonner163 0:4562b55bbb43 194 _writeNibble(0x3); // set 8 bit mode (MSN) and dummy LSN, | set 8 bit mode (MSN), | set dummy LSN,
wonner163 0:4562b55bbb43 195 // remains in 8 bit mode | change to 8 bit mode | remains in 4 bit mode
wonner163 0:4562b55bbb43 196 wait_ms(15); //
wonner163 0:4562b55bbb43 197
wonner163 0:4562b55bbb43 198 _writeNibble(0x3); // set 8 bit mode and dummy LSN, | set 8 bit mode and dummy LSN, | set 8bit mode (MSN),
wonner163 0:4562b55bbb43 199 // remains in 8 bit mode | remains in 8 bit mode | remains in 4 bit mode
wonner163 0:4562b55bbb43 200 wait_ms(15); //
wonner163 0:4562b55bbb43 201
wonner163 0:4562b55bbb43 202 _writeNibble(0x3); // set 8 bit mode and dummy LSN, | set 8 bit mode and dummy LSN, | set dummy LSN,
wonner163 0:4562b55bbb43 203 // remains in 8 bit mode | remains in 8 bit mode | change to 8 bit mode
wonner163 0:4562b55bbb43 204 wait_ms(15); //
wonner163 0:4562b55bbb43 205
wonner163 0:4562b55bbb43 206 // Controller is now in 8 bit mode
wonner163 0:4562b55bbb43 207
wonner163 0:4562b55bbb43 208 _writeNibble(0x2); // Change to 4-bit mode (MSN), the LSN is undefined dummy
wonner163 0:4562b55bbb43 209 wait_us(40); // most instructions take 40us
wonner163 0:4562b55bbb43 210
wonner163 0:4562b55bbb43 211 // Display is now in 4-bit mode
wonner163 0:4562b55bbb43 212 // Note: 4/8 bit mode is ignored for most native SPI and I2C devices. They dont use the parallel bus.
wonner163 0:4562b55bbb43 213 // However, _writeNibble() method is void anyway for native SPI and I2C devices.
wonner163 0:4562b55bbb43 214
wonner163 0:4562b55bbb43 215 // Device specific initialisations: DC/DC converter to generate VLCD or VLED, number of lines etc
wonner163 0:4562b55bbb43 216 switch (_ctrl) {
wonner163 0:4562b55bbb43 217
wonner163 0:4562b55bbb43 218 case KS0078:
wonner163 0:4562b55bbb43 219 // Initialise Display configuration
wonner163 0:4562b55bbb43 220 switch (_type) {
wonner163 0:4562b55bbb43 221 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 222 case LCD8x2B: //8x2B is a special case of 16x1
wonner163 0:4562b55bbb43 223 // case LCD12x1:
wonner163 0:4562b55bbb43 224 case LCD16x1:
wonner163 0:4562b55bbb43 225 // case LCD20x1:
wonner163 0:4562b55bbb43 226 case LCD24x1:
wonner163 0:4562b55bbb43 227 _function = 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs)
wonner163 0:4562b55bbb43 228 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 229 // N=0 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 230 // RE=0 (Dis. Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 231 // DH=1 (Disp shift enable, special mode for KS0078)
wonner163 0:4562b55bbb43 232 // REV=0 (Reverse normal, special mode for KS0078)
wonner163 0:4562b55bbb43 233
wonner163 0:4562b55bbb43 234 _function_1 = 0x04; // Function set 001 DL N RE(1) BE 0 (Ext Regs)
wonner163 0:4562b55bbb43 235 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 236 // N=0 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 237 // RE=1 (Ena Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 238 // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0078)
wonner163 0:4562b55bbb43 239 // 0
wonner163 0:4562b55bbb43 240
wonner163 0:4562b55bbb43 241 _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs)
wonner163 0:4562b55bbb43 242 // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0078)
wonner163 0:4562b55bbb43 243 break;
wonner163 0:4562b55bbb43 244
wonner163 0:4562b55bbb43 245 // case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 246 // case LCD12x3D1: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 247 // case LCD12x4D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 248 // case LCD16x3D: // Special mode for KS0078
wonner163 0:4562b55bbb43 249 // case LCD16x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 250 // case LCD24x3D: // Special mode for KS0078
wonner163 0:4562b55bbb43 251 // case LCD24x3D1: // Special mode for KS0078
wonner163 0:4562b55bbb43 252 case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 253 _function = 0x02; // Function set 001 DL N RE(0) DH REV (Std Regs)
wonner163 0:4562b55bbb43 254 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 255 // N=0 (dont care for 4 line mode)
wonner163 0:4562b55bbb43 256 // RE=0 (Dis. Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 257 // DH=1 (Disp shift enable, special mode for KS0078)
wonner163 0:4562b55bbb43 258 // REV=0 (Reverse normal, special mode for KS0078)
wonner163 0:4562b55bbb43 259
wonner163 0:4562b55bbb43 260 _function_1 = 0x04; // Function set 001 DL N RE(1) BE 0 (Ext Regs)
wonner163 0:4562b55bbb43 261 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 262 // N=0 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 263 // RE=1 (Ena Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 264 // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0078)
wonner163 0:4562b55bbb43 265 // 0
wonner163 0:4562b55bbb43 266
wonner163 0:4562b55bbb43 267 _function_x = 0x01; // Ext Function set 0000 1 FW BW NW (Ext Regs)
wonner163 0:4562b55bbb43 268 // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0078)
wonner163 0:4562b55bbb43 269 break;
wonner163 0:4562b55bbb43 270
wonner163 0:4562b55bbb43 271 case LCD16x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 272 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 273 break;
wonner163 0:4562b55bbb43 274
wonner163 0:4562b55bbb43 275 default:
wonner163 0:4562b55bbb43 276 // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)
wonner163 0:4562b55bbb43 277 _function = 0x0A; // Function set 001 DL N RE(0) DH REV (Std Regs)
wonner163 0:4562b55bbb43 278 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 279 // N=1 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 280 // RE=0 (Dis. Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 281 // DH=1 (Disp shift enable, special mode for KS0078)
wonner163 0:4562b55bbb43 282 // REV=0 (Reverse normal, special mode for KS0078)
wonner163 0:4562b55bbb43 283
wonner163 0:4562b55bbb43 284 _function_1 = 0x0C; // Function set 001 DL N RE(1) BE 0 (Ext Regs)
wonner163 0:4562b55bbb43 285 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 286 // N=1 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 287 // RE=1 (Ena Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 288 // BE=0 (Blink Enable, CG/SEG RAM, special mode for KS0078)
wonner163 0:4562b55bbb43 289 // 0
wonner163 0:4562b55bbb43 290
wonner163 0:4562b55bbb43 291 _function_x = 0x00; // Ext Function set 0000 1 FW BW NW (Ext Regs)
wonner163 0:4562b55bbb43 292 // NW=0 (1,2 line), NW=1 (4 Line, special mode for KS0078)
wonner163 0:4562b55bbb43 293 break;
wonner163 0:4562b55bbb43 294 } // switch type
wonner163 0:4562b55bbb43 295
wonner163 0:4562b55bbb43 296 // init special features
wonner163 0:4562b55bbb43 297 _writeCommand(0x20 | _function_1);// Function set 001 DL N RE(1) BE 0 (Ext Regs)
wonner163 0:4562b55bbb43 298 // DL=0 (4 bits bus), DL=1 (8 bits mode)
wonner163 0:4562b55bbb43 299 // N=0 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 300 // RE=1 (Ena Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 301 // BE=0 (Blink Enable/Disable, CG/SEG RAM, special mode for KS0078)
wonner163 0:4562b55bbb43 302 // 0
wonner163 0:4562b55bbb43 303
wonner163 0:4562b55bbb43 304 _writeCommand(0x08 | _function_x); // Ext Function set 0000 1 FW BW NW (Ext Regs)
wonner163 0:4562b55bbb43 305 // FW=0 (5-dot font, special mode for KS0078)
wonner163 0:4562b55bbb43 306 // BW=0 (Cur BW invert disable, special mode for KS0078)
wonner163 0:4562b55bbb43 307 // NW=0 (1,2 Line), NW=1 (4 line, special mode for KS0078)
wonner163 0:4562b55bbb43 308
wonner163 0:4562b55bbb43 309 _writeCommand(0x10); // Scroll/Shift set 0001 DS/HS4 DS/HS3 DS/HS2 DS/HS1 (Ext Regs)
wonner163 0:4562b55bbb43 310 // Dotscroll/Display shift enable (Special mode for KS0078)
wonner163 0:4562b55bbb43 311
wonner163 0:4562b55bbb43 312 _writeCommand(0x80); // Scroll Quantity set 1 0 SQ5 SQ4 SQ3 SQ2 SQ1 SQ0 (Ext Regs)
wonner163 0:4562b55bbb43 313 // Scroll quantity (Special mode for KS0078)
wonner163 0:4562b55bbb43 314
wonner163 0:4562b55bbb43 315 _writeCommand(0x20 | _function); // Function set 001 DL N RE(0) DH REV (Std Regs)
wonner163 0:4562b55bbb43 316 // DL=0 (4 bits bus), DL=1 (8 bits mode)
wonner163 0:4562b55bbb43 317 // N=0 (1 line mode), N=1 (2 line mode)
wonner163 0:4562b55bbb43 318 // RE=0 (Dis. Extended Regs, special mode for KS0078)
wonner163 0:4562b55bbb43 319 // DH=1 (Disp shift enable/disable, special mode for KS0078)
wonner163 0:4562b55bbb43 320 // REV=0 (Reverse/Normal, special mode for KS0078)
wonner163 0:4562b55bbb43 321 break; // case KS0078 Controller
wonner163 0:4562b55bbb43 322
wonner163 0:4562b55bbb43 323 case ST7032_3V3:
wonner163 0:4562b55bbb43 324 // ST7032 controller: Initialise Voltage booster for VLCD. VDD=3V3
wonner163 0:4562b55bbb43 325 case ST7032_5V:
wonner163 0:4562b55bbb43 326 // ST7032 controller: Disable Voltage booster for VLCD. VDD=5V
wonner163 0:4562b55bbb43 327
wonner163 0:4562b55bbb43 328 // Initialise Display configuration
wonner163 0:4562b55bbb43 329 switch (_type) {
wonner163 0:4562b55bbb43 330 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 331 case LCD8x2B: //8x2B is a special case of 16x1
wonner163 0:4562b55bbb43 332 // case LCD12x1:
wonner163 0:4562b55bbb43 333 case LCD16x1:
wonner163 0:4562b55bbb43 334 // case LCD20x1:
wonner163 0:4562b55bbb43 335 case LCD24x1:
wonner163 0:4562b55bbb43 336 _function = 0x00; // FUNCTION SET 0 0 1 DL=0 (4 bit), N=0 (1-line display mode), F=0 (5*7dot), 0, IS
wonner163 0:4562b55bbb43 337 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 338 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 339 break;
wonner163 0:4562b55bbb43 340
wonner163 0:4562b55bbb43 341 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 342 case LCD12x3D1: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 343 case LCD12x4D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 344 case LCD16x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 345 case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 346 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 347 break;
wonner163 0:4562b55bbb43 348
wonner163 0:4562b55bbb43 349 default:
wonner163 0:4562b55bbb43 350 // All other LCD types are initialised as 2 Line displays
wonner163 0:4562b55bbb43 351 _function = 0x08; // FUNCTION SET 0 0 1 DL=0 (4 bit), N=1 (2-line display mode), F=0 (5*7dot), 0, IS
wonner163 0:4562b55bbb43 352 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 353 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 354 break;
wonner163 0:4562b55bbb43 355 } // switch type
wonner163 0:4562b55bbb43 356
wonner163 0:4562b55bbb43 357 // init special features
wonner163 0:4562b55bbb43 358 _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 DL N F 0 IS=1 Select Instr Set = 1
wonner163 0:4562b55bbb43 359
wonner163 0:4562b55bbb43 360 _writeCommand(0x1C); // Internal OSC frequency adjustment Framefreq=183HZ, Bias will be 1/4 (Instr Set=1)
wonner163 0:4562b55bbb43 361
wonner163 0:4562b55bbb43 362 _contrast = LCD_ST7032_CONTRAST;
wonner163 0:4562b55bbb43 363 _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast Low bits, 0 1 1 1 C3 C2 C1 C0 (IS=1)
wonner163 0:4562b55bbb43 364
wonner163 0:4562b55bbb43 365
wonner163 0:4562b55bbb43 366 if (_ctrl == ST7032_3V3) {
wonner163 0:4562b55bbb43 367 _icon_power = 0x04; // Icon display off, Booster circuit is turned on (IS=1)
wonner163 0:4562b55bbb43 368 // Saved to allow contrast change at later time
wonner163 0:4562b55bbb43 369 }
wonner163 0:4562b55bbb43 370 else {
wonner163 0:4562b55bbb43 371 _icon_power = 0x00; // Icon display off, Booster circuit is turned off (IS=1)
wonner163 0:4562b55bbb43 372 // Saved to allow contrast change at later time
wonner163 0:4562b55bbb43 373 }
wonner163 0:4562b55bbb43 374 _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Set Icon, Booster and Contrast High bits, 0 1 0 1 Ion Bon C5 C4 (IS=1)
wonner163 0:4562b55bbb43 375 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 376
wonner163 0:4562b55bbb43 377 _writeCommand(0x68 | (LCD_ST7032_RAB & 0x07)); // Voltage follower, 0 1 1 0 FOn=1, Ampl ratio Rab2=1, Rab1=0, Rab0=0 (IS=1)
wonner163 0:4562b55bbb43 378 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 379
wonner163 0:4562b55bbb43 380 _writeCommand(0x20 | _function); // Select Instruction Set = 0
wonner163 0:4562b55bbb43 381
wonner163 0:4562b55bbb43 382 break; // case ST7032_3V3 Controller
wonner163 0:4562b55bbb43 383 // case ST7032_5V Controller
wonner163 0:4562b55bbb43 384
wonner163 0:4562b55bbb43 385 case ST7036_3V3:
wonner163 0:4562b55bbb43 386 // ST7036 controller: Initialise Voltage booster for VLCD. VDD=3V3
wonner163 0:4562b55bbb43 387 // Note: supports 1,2 (LCD_T_A) or 3 lines (LCD_T_G)
wonner163 0:4562b55bbb43 388 case ST7036_5V:
wonner163 0:4562b55bbb43 389 // ST7036 controller: Disable Voltage booster for VLCD. VDD=5V
wonner163 0:4562b55bbb43 390 // Note: supports 1,2 (LCD_T_A) or 3 lines (LCD_T_G)
wonner163 0:4562b55bbb43 391
wonner163 0:4562b55bbb43 392 // Initialise Display configuration
wonner163 0:4562b55bbb43 393 switch (_type) {
wonner163 0:4562b55bbb43 394 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 395 case LCD8x2B: //8x2D is a special case of 16x1
wonner163 0:4562b55bbb43 396 // case LCD12x1:
wonner163 0:4562b55bbb43 397 case LCD16x1:
wonner163 0:4562b55bbb43 398 case LCD24x1:
wonner163 0:4562b55bbb43 399 _function = 0x00; // Set function, 0 0 1 DL=0 (4-bit Databus), N=0 (1 Line), DH=0 (5x7font), IS2, IS1 (Select Instruction Set)
wonner163 0:4562b55bbb43 400 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 401 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 402
wonner163 0:4562b55bbb43 403 _bias_lines = 0x04; // Bias: 1/5, 1 or 2-Lines LCD
wonner163 0:4562b55bbb43 404 break;
wonner163 0:4562b55bbb43 405
wonner163 0:4562b55bbb43 406 // case LCD12x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 407 case LCD16x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 408 _function = 0x08; // Set function, 0 0 1 DL=0 (4-bit Databus), N=1 (2 Line), DH=0 (5x7font), IS2,IS1 (Select Instruction Set)
wonner163 0:4562b55bbb43 409 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 410 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 411
wonner163 0:4562b55bbb43 412 _bias_lines = 0x05; // Bias: 1/5, 3-Lines LCD
wonner163 0:4562b55bbb43 413 break;
wonner163 0:4562b55bbb43 414
wonner163 0:4562b55bbb43 415 // case LCD12x3D1: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 416 // case LCD16x3D1: // Special mode for SSD1803
wonner163 0:4562b55bbb43 417 case LCD12x4D: // Special mode for PCF2116
wonner163 0:4562b55bbb43 418 case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 419 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 420 break;
wonner163 0:4562b55bbb43 421
wonner163 0:4562b55bbb43 422 default:
wonner163 0:4562b55bbb43 423 // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)
wonner163 0:4562b55bbb43 424 _function = 0x08; // Set function, 0 0 1 DL=0 (4-bit Databus), N=1 (2 Line), DH=0 (5x7font), IS2,IS1 (Select Instruction Set)
wonner163 0:4562b55bbb43 425 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 426 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 427
wonner163 0:4562b55bbb43 428 _bias_lines = 0x04; // Bias: 1/5, 1 or 2-Lines LCD
wonner163 0:4562b55bbb43 429 break;
wonner163 0:4562b55bbb43 430 } // switch type
wonner163 0:4562b55bbb43 431
wonner163 0:4562b55bbb43 432
wonner163 0:4562b55bbb43 433 // init special features
wonner163 0:4562b55bbb43 434 _writeCommand(0x20 | _function | 0x01); // Set function, IS2,IS1 = 01 (Select Instr Set = 1)
wonner163 0:4562b55bbb43 435 _writeCommand(0x10 | _bias_lines); // Set Bias and 1,2 or 3 lines (Instr Set 1)
wonner163 0:4562b55bbb43 436
wonner163 0:4562b55bbb43 437 _contrast = LCD_ST7036_CONTRAST;
wonner163 0:4562b55bbb43 438 _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast, 0 1 1 1 C3 C2 C1 C0 (Instr Set 1)
wonner163 0:4562b55bbb43 439
wonner163 0:4562b55bbb43 440 if (_ctrl == ST7036_3V3) {
wonner163 0:4562b55bbb43 441 _icon_power = 0x04; // Set Icon, Booster, Contrast High bits, 0 1 0 1 Ion=0 Bon=1 C5 C4 (Instr Set 1)
wonner163 0:4562b55bbb43 442 // Saved to allow contrast change at later time
wonner163 0:4562b55bbb43 443 }
wonner163 0:4562b55bbb43 444 else {
wonner163 0:4562b55bbb43 445 _icon_power = 0x00; // Set Icon, Booster, Contrast High bits, 0 1 0 1 Ion=0 Bon=0 C5 C4 (Instr Set 1)
wonner163 0:4562b55bbb43 446 }
wonner163 0:4562b55bbb43 447
wonner163 0:4562b55bbb43 448 _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Set Contrast C5, C4 (Instr Set 1)
wonner163 0:4562b55bbb43 449 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 450
wonner163 0:4562b55bbb43 451 _writeCommand(0x68 | (LCD_ST7036_RAB & 0x07)); // Voltagefollower On = 1, Ampl ratio Rab2, Rab1, Rab0 = 1 0 1 (Instr Set 1)
wonner163 0:4562b55bbb43 452 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 453
wonner163 0:4562b55bbb43 454 _writeCommand(0x20 | _function); // Set function, IS2,IS1 = 00 (Select Instruction Set = 0)
wonner163 0:4562b55bbb43 455
wonner163 0:4562b55bbb43 456 break; // case ST7036_3V3 Controller
wonner163 0:4562b55bbb43 457 // case ST7036_5V Controller
wonner163 0:4562b55bbb43 458
wonner163 0:4562b55bbb43 459 case SSD1803_3V3:
wonner163 0:4562b55bbb43 460 // SSD1803 controller: Initialise Voltage booster for VLCD. VDD=3V3
wonner163 0:4562b55bbb43 461 // Note: supports 1,2, 3 or 4 lines
wonner163 0:4562b55bbb43 462 // case SSD1803_5V:
wonner163 0:4562b55bbb43 463 // SSD1803 controller: No Voltage booster for VLCD. VDD=5V
wonner163 0:4562b55bbb43 464
wonner163 0:4562b55bbb43 465 // Initialise Display configuration
wonner163 0:4562b55bbb43 466 switch (_type) {
wonner163 0:4562b55bbb43 467 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 468 case LCD8x2B: //8x2D is a special case of 16x1
wonner163 0:4562b55bbb43 469 // case LCD12x1:
wonner163 0:4562b55bbb43 470 case LCD16x1:
wonner163 0:4562b55bbb43 471 case LCD24x1:
wonner163 0:4562b55bbb43 472 _function = 0x00; // Set function 0 0 1 DL N DH RE(0) IS
wonner163 0:4562b55bbb43 473 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 474 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 475 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 476 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 477 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 478 // IS=0
wonner163 0:4562b55bbb43 479
wonner163 0:4562b55bbb43 480 _function_1 = 0x02; // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 481 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 482 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 483 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 484 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 485 // BE=0 Blink Enable off, special feature of SSD1803
wonner163 0:4562b55bbb43 486 // REV=0 Reverse off, special feature of SSD1803
wonner163 0:4562b55bbb43 487
wonner163 0:4562b55bbb43 488 _lines = 0x00; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 489 // NW=0 1-Line LCD (N=0)
wonner163 0:4562b55bbb43 490 break;
wonner163 0:4562b55bbb43 491
wonner163 0:4562b55bbb43 492 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 493 // case LCD12x3D1: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 494 case LCD16x3D: // Special mode for KS0078
wonner163 0:4562b55bbb43 495 // case LCD16x3D1: // Special mode for SSD1803
wonner163 0:4562b55bbb43 496 // case LCD20x3D: // Special mode for SSD1803
wonner163 0:4562b55bbb43 497 _function = 0x00; // Set function 0 0 1 DL N DH RE(0) IS
wonner163 0:4562b55bbb43 498 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 499 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 500 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 501 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 502 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 503 // IS=0
wonner163 0:4562b55bbb43 504
wonner163 0:4562b55bbb43 505 _function_1 = 0x02; // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 506 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 507 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 508 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 509 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 510 // BE=0 Blink Enable off, special feature of SSD1803
wonner163 0:4562b55bbb43 511 // REV=0 Reverse off, special feature of SSD1803
wonner163 0:4562b55bbb43 512
wonner163 0:4562b55bbb43 513 _lines = 0x00; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 514 // NW=1 3-Line LCD (N=0)
wonner163 0:4562b55bbb43 515 break;
wonner163 0:4562b55bbb43 516
wonner163 0:4562b55bbb43 517 case LCD20x4D: // Special mode for SSD1803
wonner163 0:4562b55bbb43 518 _function = 0x08; // Set function 0 0 1 DL N DH RE(0) IS
wonner163 0:4562b55bbb43 519 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 520 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 521 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 522 // N=1 4 Line
wonner163 0:4562b55bbb43 523 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 524 // IS=0
wonner163 0:4562b55bbb43 525
wonner163 0:4562b55bbb43 526 _function_1 = 0x0A; // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 527 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 528 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 529 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 530 // N=1 4 Line
wonner163 0:4562b55bbb43 531 // BE=0 Blink Enable off, special feature of SSD1803
wonner163 0:4562b55bbb43 532 // REV=0 Reverse off, special feature of SSD1803
wonner163 0:4562b55bbb43 533
wonner163 0:4562b55bbb43 534 _lines = 0x01; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 535 // NW=1 4-Line LCD (N=1)
wonner163 0:4562b55bbb43 536 break;
wonner163 0:4562b55bbb43 537
wonner163 0:4562b55bbb43 538 case LCD16x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 539 case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 540 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 541 break;
wonner163 0:4562b55bbb43 542
wonner163 0:4562b55bbb43 543 default:
wonner163 0:4562b55bbb43 544 // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)
wonner163 0:4562b55bbb43 545 _function = 0x08; // Set function 0 0 1 DL N DH RE(0) IS
wonner163 0:4562b55bbb43 546 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 547 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 548 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 549 // N=1 2 line / 4 Line
wonner163 0:4562b55bbb43 550 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 551 // IS=0
wonner163 0:4562b55bbb43 552
wonner163 0:4562b55bbb43 553 _function_1 = 0x0A; // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 554 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 555 // DL=0 4-bit Databus,
wonner163 0:4562b55bbb43 556 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 557 // N=1 2 line / 4 Line
wonner163 0:4562b55bbb43 558 // BE=0 Blink Enable off, special feature of SSD1803
wonner163 0:4562b55bbb43 559 // REV=0 Reverse off, special feature of SSD1803
wonner163 0:4562b55bbb43 560
wonner163 0:4562b55bbb43 561 _lines = 0x00; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 562 // NW=0 2-Line LCD (N=1)
wonner163 0:4562b55bbb43 563 break;
wonner163 0:4562b55bbb43 564 } // switch type
wonner163 0:4562b55bbb43 565
wonner163 0:4562b55bbb43 566
wonner163 0:4562b55bbb43 567 // init special features
wonner163 0:4562b55bbb43 568 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 569 // Select Extended Instruction Set
wonner163 0:4562b55bbb43 570
wonner163 0:4562b55bbb43 571 _writeCommand(0x06); // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1 "Bottom View" (Ext Instr Set)
wonner163 0:4562b55bbb43 572 // _writeCommand(0x05); // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100 "Top View" (Ext Instr Set)
wonner163 0:4562b55bbb43 573 wait_ms(5); // Wait to ensure completion or SSD1803 fails to set Top/Bottom after reset..
wonner163 0:4562b55bbb43 574
wonner163 0:4562b55bbb43 575 _writeCommand(0x08 | _lines); // Set ext function 0 0 0 0 1 FW BW NW 1,2,3 or 4 lines (Ext Instr Set)
wonner163 0:4562b55bbb43 576
wonner163 0:4562b55bbb43 577 _writeCommand(0x10); // Double Height and Bias, 0 0 0 1 UD2=0, UD1=0, BS1=0 Bias 1/5, DH=0 (Ext Instr Set)
wonner163 0:4562b55bbb43 578
wonner163 0:4562b55bbb43 579 // _writeCommand(0x76); // Set TC Control, 0 1 1 1 0 1 1 0 (Ext Instr Set)
wonner163 0:4562b55bbb43 580 // _writeData(0x02); // Set TC data, 0 0 0 0 0 TC2,TC1,TC0 = 0 1 0 (Ext Instr Set)
wonner163 0:4562b55bbb43 581
wonner163 0:4562b55bbb43 582 _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 DL N DH RE(0) IS=1 Select Instruction Set 1
wonner163 0:4562b55bbb43 583 // Select Std Instr set, Select IS=1
wonner163 0:4562b55bbb43 584
wonner163 0:4562b55bbb43 585 _contrast = LCD_SSD1_CONTRAST;
wonner163 0:4562b55bbb43 586 _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast 0 1 1 1 C3, C2, C1, C0 (Instr Set 1)
wonner163 0:4562b55bbb43 587
wonner163 0:4562b55bbb43 588 _icon_power = 0x04; // Icon off, Booster on (Instr Set 1)
wonner163 0:4562b55bbb43 589 // Saved to allow contrast change at later time
wonner163 0:4562b55bbb43 590 _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Set Power, Icon and Contrast, 0 1 0 1 Ion Bon C5 C4 (Instr Set 1)
wonner163 0:4562b55bbb43 591 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 592
wonner163 0:4562b55bbb43 593 _writeCommand(0x68 | (LCD_SSD1_RAB & 0x07)); // Set Voltagefollower 0 1 1 0 Don = 1, Ampl ratio Rab2, Rab1, Rab0 = 1 1 0 (Instr Set 1)
wonner163 0:4562b55bbb43 594 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 595
wonner163 0:4562b55bbb43 596 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 597 // Select Extended Instruction Set 1
wonner163 0:4562b55bbb43 598 _writeCommand(0x10); // Shift/Scroll enable, 0 0 0 1 DS4/HS4 DS3/HS3 DS2/HS2 DS1/HS1 (Ext Instr Set 1)
wonner163 0:4562b55bbb43 599
wonner163 0:4562b55bbb43 600
wonner163 0:4562b55bbb43 601 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 602 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 603
wonner163 0:4562b55bbb43 604 break; // case SSD1803 Controller
wonner163 0:4562b55bbb43 605
wonner163 0:4562b55bbb43 606
wonner163 0:4562b55bbb43 607 // Note1: The PCF21XX family of controllers has several types that dont have an onboard voltage generator for V-LCD.
wonner163 0:4562b55bbb43 608 // You must supply this LCD voltage externally and not try to enable VGen.
wonner163 0:4562b55bbb43 609 // Note2: The early versions of PCF2116 controllers (eg PCF2116C) can not generate sufficiently negative voltage for the LCD at a VDD of 3V3.
wonner163 0:4562b55bbb43 610 // You must supply this voltage externally and not enable VGen or you must use a higher VDD (e.g. 5V) and enable VGen.
wonner163 0:4562b55bbb43 611 // More recent versions of the controller (eg PCF2116K) have an improved VGen that will work with 3V3.
wonner163 0:4562b55bbb43 612 // Note3: See datasheet, PCF2116 and other types provide a V0 pin to control the LCD contrast voltage that is provided by VGen. This pins allows
wonner163 0:4562b55bbb43 613 // contrast control similar to that of pin 3 on the standard 14pin LCD module connector.
wonner163 0:4562b55bbb43 614 // You can disable VGen by connecting Vo to VDD. VLCD will then be used directly as LCD voltage.
wonner163 0:4562b55bbb43 615 // Note4: PCF2113 and PCF2119 are different wrt to VLCD generator! There is no V0 pin. The contrast voltage is software controlled by setting the VA and VB registers.
wonner163 0:4562b55bbb43 616 // Vgen is automatically switched off when the contrast voltage VA or VB is set to 0x00. Note that certain limits apply to allowed values for VA and VB.
wonner163 0:4562b55bbb43 617 // Note5: See datasheet, members of the PCF21XX family support different numbers of rows/columns. Not all can support 3 or 4 rows.
wonner163 0:4562b55bbb43 618 // Note6: See datasheet, the PCF21XX-C and PCF21XX-K use a non-standard character set. This may result is strange looking text when not corrected..
wonner163 0:4562b55bbb43 619
wonner163 0:4562b55bbb43 620 case PCF2113_3V3:
wonner163 0:4562b55bbb43 621 // PCF2113 controller: Initialise Voltage booster for VLCD. VDD=3V3. VA and VB control contrast.
wonner163 0:4562b55bbb43 622 // Initialise Display configuration
wonner163 0:4562b55bbb43 623 switch (_type) {
wonner163 0:4562b55bbb43 624 // case LCD12x1:
wonner163 0:4562b55bbb43 625 // _function = 0x02; // FUNCTION SET 0 0 1 DL=0 4 bit, 0, M=0 1-line/12 chars display mode, SL=1, IS=0
wonner163 0:4562b55bbb43 626 // Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 627 case LCD24x1:
wonner163 0:4562b55bbb43 628 _function = 0x00; // FUNCTION SET 0 0 1 DL=0 4 bit, 0, M=0 1-line/24 chars display mode, SL=0, IS=0
wonner163 0:4562b55bbb43 629 // Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 630 break;
wonner163 0:4562b55bbb43 631
wonner163 0:4562b55bbb43 632 //Tested OK for PCF2113
wonner163 0:4562b55bbb43 633 case LCD12x2:
wonner163 0:4562b55bbb43 634 _function = 0x04; // FUNCTION SET 0 0 1 DL=0 4 bit, 0, M=1 2-line/12 chars display mode, SL=0, IS=0
wonner163 0:4562b55bbb43 635 break;
wonner163 0:4562b55bbb43 636
wonner163 0:4562b55bbb43 637 default:
wonner163 0:4562b55bbb43 638 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 639 break;
wonner163 0:4562b55bbb43 640
wonner163 0:4562b55bbb43 641 } // switch type
wonner163 0:4562b55bbb43 642
wonner163 0:4562b55bbb43 643 // Init special features
wonner163 0:4562b55bbb43 644 _writeCommand(0x20 | _function | 0x01); // Set function, Select Instr Set = 1
wonner163 0:4562b55bbb43 645
wonner163 0:4562b55bbb43 646 _writeCommand(0x04); // Display Conf Set 0000 0, 1, P=0, Q=0 (Instr. Set 1)
wonner163 0:4562b55bbb43 647 _writeCommand(0x10); // Temp Compensation Set 0001 0, 0, TC1=0, TC2=0 (Instr. Set 1)
wonner163 0:4562b55bbb43 648 // _writeCommand(0x42); // HV GEN 0100 S1=1, S2=0 (2x multiplier) (Instr. Set 1)
wonner163 0:4562b55bbb43 649 _writeCommand(0x40 | (LCD_PCF2_S12 & 0x03)); // HV Gen 0100 S1=1, S2=0 (2x multiplier) (Instr. Set 1)
wonner163 0:4562b55bbb43 650
wonner163 0:4562b55bbb43 651 _contrast = LCD_PCF2_CONTRAST;
wonner163 0:4562b55bbb43 652 _writeCommand(0x80 | 0x00 | (_contrast & 0x3F)); // VLCD_set (Instr. Set 1) 1, V=0, VA=contrast
wonner163 0:4562b55bbb43 653 _writeCommand(0x80 | 0x40 | (_contrast & 0x3F)); // VLCD_set (Instr. Set 1) 1, V=1, VB=contrast
wonner163 0:4562b55bbb43 654 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 655
wonner163 0:4562b55bbb43 656 _writeCommand(0x02); // Screen Config 0000 001, L=0 (Instr. Set 1)
wonner163 0:4562b55bbb43 657 _writeCommand(0x08); // ICON Conf 0000 1, IM=0 (Char mode), IB=0 (no icon blink) DM=0 (no direct mode) (Instr. Set 1)
wonner163 0:4562b55bbb43 658
wonner163 0:4562b55bbb43 659 _writeCommand(0x20 | _function); // Set function, Select Instr Set = 0
wonner163 0:4562b55bbb43 660
wonner163 0:4562b55bbb43 661 break; // case PCF2113_3V3 Controller
wonner163 0:4562b55bbb43 662
wonner163 0:4562b55bbb43 663
wonner163 0:4562b55bbb43 664 // case PCF2113_5V:
wonner163 0:4562b55bbb43 665 // PCF2113 controller: No Voltage generator for VLCD. VDD=5V. Contrast voltage controlled by VA or VB.
wonner163 0:4562b55bbb43 666 //@TODO
wonner163 0:4562b55bbb43 667
wonner163 0:4562b55bbb43 668
wonner163 0:4562b55bbb43 669 case PCF2116_3V3:
wonner163 0:4562b55bbb43 670 // PCF2116 controller: Voltage generator for VLCD. VDD=5V. V0 controls contrast voltage.
wonner163 0:4562b55bbb43 671 // Initialise Display configuration
wonner163 0:4562b55bbb43 672 switch (_type) {
wonner163 0:4562b55bbb43 673 // case LCD12x1:
wonner163 0:4562b55bbb43 674 // case LCD12x2:
wonner163 0:4562b55bbb43 675 case LCD24x1:
wonner163 0:4562b55bbb43 676 _writeCommand(0x22); //FUNCTION SET 4 bit, N/M=0 1-line/24 chars display mode, G=1 Vgen on
wonner163 0:4562b55bbb43 677 //Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 678 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 679 break;
wonner163 0:4562b55bbb43 680
wonner163 0:4562b55bbb43 681 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 682 case LCD12x3D1: // Special mode for PCF21XX
wonner163 0:4562b55bbb43 683 case LCD12x4D: // Special mode for PCF21XX:
wonner163 0:4562b55bbb43 684 _writeCommand(0x2E); //FUNCTION SET 4 bit, N=1/M=1 4-line/12 chars display mode, G=1 VGen on
wonner163 0:4562b55bbb43 685 //Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 686 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 687 break;
wonner163 0:4562b55bbb43 688
wonner163 0:4562b55bbb43 689 case LCD24x2:
wonner163 0:4562b55bbb43 690 _writeCommand(0x2A); //FUNCTION SET 4 bit, N=1/M=0 2-line/24 chars display mode, G=1 VGen on
wonner163 0:4562b55bbb43 691 //Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 692 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 693 break;
wonner163 0:4562b55bbb43 694
wonner163 0:4562b55bbb43 695 default:
wonner163 0:4562b55bbb43 696 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 697 break;
wonner163 0:4562b55bbb43 698
wonner163 0:4562b55bbb43 699 } // switch type
wonner163 0:4562b55bbb43 700
wonner163 0:4562b55bbb43 701 break; // case PCF2116_3V3 Controller
wonner163 0:4562b55bbb43 702
wonner163 0:4562b55bbb43 703
wonner163 0:4562b55bbb43 704 //Experimental for cellphone 3-line display, SA=0x74, No Ack supported, Character set C or K, DL = 8 bit, N=0,M=1 (reserved mode !!), external VLCD -2V5
wonner163 0:4562b55bbb43 705 //@TODO
wonner163 0:4562b55bbb43 706 case PCF2116_5V:
wonner163 0:4562b55bbb43 707 // PCF2116 controller: No Voltage generator for VLCD. VDD=5V. V0 controls contrast voltage.
wonner163 0:4562b55bbb43 708 // Initialise Display configuration
wonner163 0:4562b55bbb43 709 switch (_type) {
wonner163 0:4562b55bbb43 710 // case LCD12x1:
wonner163 0:4562b55bbb43 711 // case LCD12x2:
wonner163 0:4562b55bbb43 712 // case LCD24x1:
wonner163 0:4562b55bbb43 713 // _writeCommand(0x20); //FUNCTION SET 4 bit, N/M=0 1-line/24 chars display mode
wonner163 0:4562b55bbb43 714 //Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 715 // wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 716 // break;
wonner163 0:4562b55bbb43 717
wonner163 0:4562b55bbb43 718 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 719 case LCD12x3D1: // Special mode for PCF21XX
wonner163 0:4562b55bbb43 720 case LCD12x4D: // Special mode for PCF21XX:
wonner163 0:4562b55bbb43 721 // _writeCommand(0x34); //FUNCTION SET 8 bit, N=0/M=1 4-line/12 chars display mode OK
wonner163 0:4562b55bbb43 722 // _writeCommand(0x24); //FUNCTION SET 4 bit, N=0/M=1 4-line/12 chars display mode OK
wonner163 0:4562b55bbb43 723 _writeCommand(0x2C); //FUNCTION SET 4 bit, N=1/M=1 4-line/12 chars display mode OK
wonner163 0:4562b55bbb43 724 //Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 725 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 726 break;
wonner163 0:4562b55bbb43 727
wonner163 0:4562b55bbb43 728 // case LCD24x2:
wonner163 0:4562b55bbb43 729 // _writeCommand(0x28); //FUNCTION SET 4 bit, N=1/M=0 2-line/24 chars display mode
wonner163 0:4562b55bbb43 730 //Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 731 // wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 732 // break;
wonner163 0:4562b55bbb43 733
wonner163 0:4562b55bbb43 734 default:
wonner163 0:4562b55bbb43 735 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 736 break;
wonner163 0:4562b55bbb43 737
wonner163 0:4562b55bbb43 738 } // switch type
wonner163 0:4562b55bbb43 739
wonner163 0:4562b55bbb43 740 break; // case PCF2116_5V Controller
wonner163 0:4562b55bbb43 741
wonner163 0:4562b55bbb43 742 case PCF2119_3V3:
wonner163 0:4562b55bbb43 743 // PCF2119 controller: Initialise Voltage booster for VLCD. VDD=3V3. VA and VB control contrast.
wonner163 0:4562b55bbb43 744 // Note1: See datasheet, the PCF2119 supports icons and provides separate constrast control for Icons and characters.
wonner163 0:4562b55bbb43 745 // Note2: Vgen is switched off when the contrast voltage VA or VB is set to 0x00.
wonner163 0:4562b55bbb43 746
wonner163 0:4562b55bbb43 747 //POR or Hardware Reset should be applied
wonner163 0:4562b55bbb43 748 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 749
wonner163 0:4562b55bbb43 750 // Initialise Display configuration
wonner163 0:4562b55bbb43 751 switch (_type) {
wonner163 0:4562b55bbb43 752 case LCD8x1:
wonner163 0:4562b55bbb43 753 // case LCD12x1:
wonner163 0:4562b55bbb43 754 case LCD16x1:
wonner163 0:4562b55bbb43 755 _function = 0x02; // FUNCTION SET DL=0 4 bit, 0 , M=0 1-line/16 chars display mode, SL=1
wonner163 0:4562b55bbb43 756 // Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 757 break;
wonner163 0:4562b55bbb43 758
wonner163 0:4562b55bbb43 759 case LCD24x1:
wonner163 0:4562b55bbb43 760 // case LCD32x1:
wonner163 0:4562b55bbb43 761 _function = 0x00; // FUNCTION SET DL=0 4 bit, 0 , M=0 1-line/32 chars display mode, SL=0
wonner163 0:4562b55bbb43 762 // Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 763 break;
wonner163 0:4562b55bbb43 764
wonner163 0:4562b55bbb43 765 case LCD8x2:
wonner163 0:4562b55bbb43 766 // case LCD12x2:
wonner163 0:4562b55bbb43 767 case LCD16x2:
wonner163 0:4562b55bbb43 768 _function = 0x04; // FUNCTION SET DL=0 4 bit, 0, M=1 2-line/16 chars display mode, SL=0
wonner163 0:4562b55bbb43 769 // Note: 4 bit mode is ignored for I2C mode
wonner163 0:4562b55bbb43 770 break;
wonner163 0:4562b55bbb43 771
wonner163 0:4562b55bbb43 772 default:
wonner163 0:4562b55bbb43 773 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 774 break;
wonner163 0:4562b55bbb43 775
wonner163 0:4562b55bbb43 776 } // switch type
wonner163 0:4562b55bbb43 777
wonner163 0:4562b55bbb43 778 // Init special features
wonner163 0:4562b55bbb43 779 _writeCommand(0x20 | _function | 0x01); // Set function, Select Instruction Set = 1
wonner163 0:4562b55bbb43 780
wonner163 0:4562b55bbb43 781 _writeCommand(0x04); // DISP CONF SET (Instr. Set 1) 0000, 0, 1, P=0, Q=0
wonner163 0:4562b55bbb43 782 _writeCommand(0x10); // TEMP CTRL SET (Instr. Set 1) 0001, 0, 0, TC1=0, TC2=0
wonner163 0:4562b55bbb43 783 // _writeCommand(0x42); // HV GEN (Instr. Set 1) 0100, 0, 0, S1=1, S2=0 (2x multiplier)
wonner163 0:4562b55bbb43 784 _writeCommand(0x40 | (LCD_PCF2_S12 & 0x03)); // HV GEN (Instr. Set 1) 0100, 0, 0, S1=1, S2=0 (2x multiplier)
wonner163 0:4562b55bbb43 785
wonner163 0:4562b55bbb43 786 _contrast = LCD_PCF2_CONTRAST;
wonner163 0:4562b55bbb43 787 _writeCommand(0x80 | 0x00 | (_contrast & 0x3F)); // VLCD_set (Instr. Set 1) V=0, VA=contrast
wonner163 0:4562b55bbb43 788 _writeCommand(0x80 | 0x40 | (_contrast & 0x3F)); // VLCD_set (Instr. Set 1) V=1, VB=contrast
wonner163 0:4562b55bbb43 789 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 790
wonner163 0:4562b55bbb43 791 _writeCommand(0x02); // SCRN CONF (Instr. Set 1) L=0
wonner163 0:4562b55bbb43 792 _writeCommand(0x08); // ICON CONF (Instr. Set 1) IM=0 (Char mode) IB=0 (no icon blink) DM=0 (no direct mode)
wonner163 0:4562b55bbb43 793
wonner163 0:4562b55bbb43 794 _writeCommand(0x20 | _function); // Select Instruction Set = 0
wonner163 0:4562b55bbb43 795
wonner163 0:4562b55bbb43 796 break; // case PCF2119_3V3 Controller
wonner163 0:4562b55bbb43 797
wonner163 0:4562b55bbb43 798 // case PCF2119_5V:
wonner163 0:4562b55bbb43 799 // PCF2119 controller: No Voltage booster for VLCD. VDD=3V3. VA and VB control contrast.
wonner163 0:4562b55bbb43 800 // Note1: See datasheet, the PCF2119 supports icons and provides separate constrast control for Icons and characters.
wonner163 0:4562b55bbb43 801 // Note2: Vgen is switched off when the contrast voltage VA or VB is set to 0x00.
wonner163 0:4562b55bbb43 802 //@TODO
wonner163 0:4562b55bbb43 803
wonner163 0:4562b55bbb43 804 case WS0010:
wonner163 0:4562b55bbb43 805 // WS0010 OLED controller: Initialise DC/DC Voltage converter for LEDs
wonner163 0:4562b55bbb43 806 // Note1: Identical to RS0010
wonner163 0:4562b55bbb43 807 // Note2: supports 1 or 2 lines (and 16x100 graphics)
wonner163 0:4562b55bbb43 808 // supports 4 fonts (English/Japanese (default), Western European-I, English/Russian, Western European-II)
wonner163 0:4562b55bbb43 809 // Cursor/Disp shift set 0001 SC RL 0 0
wonner163 0:4562b55bbb43 810 //
wonner163 0:4562b55bbb43 811 // Mode and Power set 0001 GC PWR 1 1
wonner163 0:4562b55bbb43 812 // GC = 0 (Graph Mode=1, Char Mode=0)
wonner163 0:4562b55bbb43 813 // PWR = 1 (DC/DC On/Off)
wonner163 0:4562b55bbb43 814
wonner163 0:4562b55bbb43 815 //@Todo: This may be needed to enable a warm reboot
wonner163 0:4562b55bbb43 816 //_writeCommand(0x13); // Char mode, DC/DC off
wonner163 0:4562b55bbb43 817 //wait_ms(10); // Wait 10ms to ensure powered down
wonner163 0:4562b55bbb43 818 _writeCommand(0x17); // Char mode, DC/DC on
wonner163 0:4562b55bbb43 819 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 820
wonner163 0:4562b55bbb43 821 // Initialise Display configuration
wonner163 0:4562b55bbb43 822 switch (_type) {
wonner163 0:4562b55bbb43 823 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 824 case LCD8x2B: //8x2B is a special case of 16x1
wonner163 0:4562b55bbb43 825 // case LCD12x1:
wonner163 0:4562b55bbb43 826 case LCD16x1:
wonner163 0:4562b55bbb43 827 case LCD24x1:
wonner163 0:4562b55bbb43 828 _writeCommand(0x20); // Function set 001 DL N F FT1 FT0
wonner163 0:4562b55bbb43 829 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 830 // N=0 (1 line)
wonner163 0:4562b55bbb43 831 // F=0 (5x7 dots font)
wonner163 0:4562b55bbb43 832 // FT=00 (00 = Engl/Jap, 01 = WestEur1, 10 = Engl/Russian, 11 = WestEur2
wonner163 0:4562b55bbb43 833 break;
wonner163 0:4562b55bbb43 834
wonner163 0:4562b55bbb43 835 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 836 case LCD12x3D1: // Special mode for PCF21XX
wonner163 0:4562b55bbb43 837 case LCD12x4D: // Special mode for PCF21XX:
wonner163 0:4562b55bbb43 838 case LCD16x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 839 case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 840 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 841 break;
wonner163 0:4562b55bbb43 842
wonner163 0:4562b55bbb43 843 default:
wonner163 0:4562b55bbb43 844 // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)
wonner163 0:4562b55bbb43 845 _writeCommand(0x28); // Function set 001 DL N F FT1 FT0
wonner163 0:4562b55bbb43 846 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 847 // N=1 (2 lines)
wonner163 0:4562b55bbb43 848 // F=0 (5x7 dots font)
wonner163 0:4562b55bbb43 849 // FT=00 (00 = Engl/Jap, 01 = WestEur1, 10 = Engl/Russian, 11 = WestEur2
wonner163 0:4562b55bbb43 850
wonner163 0:4562b55bbb43 851 break;
wonner163 0:4562b55bbb43 852 } // switch type
wonner163 0:4562b55bbb43 853
wonner163 0:4562b55bbb43 854 break; // case WS0010 Controller
wonner163 0:4562b55bbb43 855
wonner163 0:4562b55bbb43 856
wonner163 0:4562b55bbb43 857 case US2066_3V3:
wonner163 0:4562b55bbb43 858 // US2066/SSD1311 OLED controller, Initialise for VDD=3V3
wonner163 0:4562b55bbb43 859 // Note: supports 1,2, 3 or 4 lines
wonner163 0:4562b55bbb43 860 // case USS2066_5V:
wonner163 0:4562b55bbb43 861 // US2066 controller, VDD=5V
wonner163 0:4562b55bbb43 862
wonner163 0:4562b55bbb43 863 // Initialise Display configuration
wonner163 0:4562b55bbb43 864 switch (_type) {
wonner163 0:4562b55bbb43 865 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 866 case LCD8x2B: //8x2D is a special case of 16x1
wonner163 0:4562b55bbb43 867 // case LCD12x1:
wonner163 0:4562b55bbb43 868 case LCD16x1:
wonner163 0:4562b55bbb43 869 // case LCD20x1:
wonner163 0:4562b55bbb43 870 _function = 0x00; // Set function 0 0 1 X N DH RE(0) IS
wonner163 0:4562b55bbb43 871 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 872 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 873 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 874 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 875 // IS=0
wonner163 0:4562b55bbb43 876
wonner163 0:4562b55bbb43 877 _function_1 = 0x02; // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 878 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 879 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 880 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 881 // BE=0 Blink Enable off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 882 // REV=0 Reverse off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 883
wonner163 0:4562b55bbb43 884 _lines = 0x00; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 885 // NW=0 1-Line LCD (N=0)
wonner163 0:4562b55bbb43 886 break;
wonner163 0:4562b55bbb43 887
wonner163 0:4562b55bbb43 888 case LCD16x1C:
wonner163 0:4562b55bbb43 889 case LCD8x2:
wonner163 0:4562b55bbb43 890 case LCD16x2:
wonner163 0:4562b55bbb43 891 case LCD20x2:
wonner163 0:4562b55bbb43 892 _function = 0x08; // Set function 0 0 1 X N DH RE(0) IS
wonner163 0:4562b55bbb43 893 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 894 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 895 // N=1 2 line / 4 Line
wonner163 0:4562b55bbb43 896 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 897 // IS=0
wonner163 0:4562b55bbb43 898
wonner163 0:4562b55bbb43 899 _function_1 = 0x0A; // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 900 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 901 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 902 // N=1 2 line / 4 Line
wonner163 0:4562b55bbb43 903 // BE=0 Blink Enable off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 904 // REV=0 Reverse off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 905
wonner163 0:4562b55bbb43 906 _lines = 0x00; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 907 // NW=0 2-Line LCD (N=1)
wonner163 0:4562b55bbb43 908 break;
wonner163 0:4562b55bbb43 909
wonner163 0:4562b55bbb43 910 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 911 // case LCD12x3D1: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 912 case LCD16x3D: // Special mode for KS0078, SSD1803 and US2066
wonner163 0:4562b55bbb43 913 // case LCD16x3D1: // Special mode for SSD1803, US2066
wonner163 0:4562b55bbb43 914 // case LCD20x3D: // Special mode for SSD1803, US2066
wonner163 0:4562b55bbb43 915 _function = 0x00; // Set function 0 0 1 X N DH RE(0) IS
wonner163 0:4562b55bbb43 916 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 917 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 918 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 919 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 920 // IS=0
wonner163 0:4562b55bbb43 921
wonner163 0:4562b55bbb43 922 _function_1 = 0x02; // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 923 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 924 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 925 // N=0 1 Line / 3 Line
wonner163 0:4562b55bbb43 926 // BE=0 Blink Enable off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 927 // REV=0 Reverse off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 928
wonner163 0:4562b55bbb43 929 _lines = 0x00; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 930 // NW=1 3-Line LCD (N=0)
wonner163 0:4562b55bbb43 931 break;
wonner163 0:4562b55bbb43 932
wonner163 0:4562b55bbb43 933 case LCD20x4D: // Special mode for SSD1803, US2066
wonner163 0:4562b55bbb43 934 _function = 0x08; // Set function 0 0 1 X N DH RE(0) IS
wonner163 0:4562b55bbb43 935 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 936 // DL=X bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 937 // N=1 2 line / 4 Line
wonner163 0:4562b55bbb43 938 // DH=0 Double Height disable
wonner163 0:4562b55bbb43 939 // IS=0
wonner163 0:4562b55bbb43 940
wonner163 0:4562b55bbb43 941 _function_1 = 0x0A; // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 942 // Saved to allow switch between Instruction sets at later time
wonner163 0:4562b55bbb43 943 // DL=0 bit is ignored for US2066. Uses hardwired pins instead
wonner163 0:4562b55bbb43 944 // N=1 2 line / 4 Line
wonner163 0:4562b55bbb43 945 // BE=0 Blink Enable off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 946 // REV=0 Reverse off, special feature of SSD1803, US2066
wonner163 0:4562b55bbb43 947
wonner163 0:4562b55bbb43 948 _lines = 0x01; // Ext function set 0 0 0 0 1 FW BW NW
wonner163 0:4562b55bbb43 949 // NW=1 4-Line LCD (N=1)
wonner163 0:4562b55bbb43 950 break;
wonner163 0:4562b55bbb43 951
wonner163 0:4562b55bbb43 952 // case LCD24x1:
wonner163 0:4562b55bbb43 953 // case LCD16x3G: // Special mode for ST7036
wonner163 0:4562b55bbb43 954 // case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 955 default:
wonner163 0:4562b55bbb43 956 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 957 break;
wonner163 0:4562b55bbb43 958
wonner163 0:4562b55bbb43 959 } // switch type
wonner163 0:4562b55bbb43 960
wonner163 0:4562b55bbb43 961
wonner163 0:4562b55bbb43 962 // init special features
wonner163 0:4562b55bbb43 963 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 964 // Select Extended Instruction Set
wonner163 0:4562b55bbb43 965
wonner163 0:4562b55bbb43 966 _writeCommand(0x71); // Function Select A: 0 1 1 1 0 0 0 1 (Ext Instr Set)
wonner163 0:4562b55bbb43 967 _writeData(0x00); // Disable Internal VDD
wonner163 0:4562b55bbb43 968
wonner163 0:4562b55bbb43 969 _writeCommand(0x79); // Function Select OLED: 0 1 1 1 1 0 0 1 (Ext Instr Set)
wonner163 0:4562b55bbb43 970
wonner163 0:4562b55bbb43 971 _writeCommand(0xD5); // Display Clock Divide Ratio: 1 1 0 1 0 1 0 1 (Ext Instr Set, OLED Instr Set)
wonner163 0:4562b55bbb43 972 _writeCommand(0x70); // Display Clock Divide Ratio value: 0 1 1 1 0 0 0 0 (Ext Instr Set, OLED Instr Set)
wonner163 0:4562b55bbb43 973
wonner163 0:4562b55bbb43 974 _writeCommand(0x78); // Function Disable OLED: 0 1 1 1 1 0 0 0 (Ext Instr Set)
wonner163 0:4562b55bbb43 975
wonner163 0:4562b55bbb43 976 // _writeCommand(0x06); // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1 "Bottom View" (Ext Instr Set)
wonner163 0:4562b55bbb43 977 _writeCommand(0x05); // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100 "Top View" (Ext Instr Set)
wonner163 0:4562b55bbb43 978
wonner163 0:4562b55bbb43 979 _writeCommand(0x08 | _lines); // Set ext function 0 0 0 0 1 FW BW NW 1,2,3 or 4 lines (Ext Instr Set)
wonner163 0:4562b55bbb43 980
wonner163 0:4562b55bbb43 981 // _writeCommand(0x1C); // Double Height and Bias, 0 0 0 1 UD2=1, UD1=1, X, DH=0 (Ext Instr Set)
wonner163 0:4562b55bbb43 982 // // Default
wonner163 0:4562b55bbb43 983
wonner163 0:4562b55bbb43 984 _writeCommand(0x72); // Function Select B: 0 1 1 1 0 0 1 0 (Ext Instr Set)
wonner163 0:4562b55bbb43 985 _writeData(0x01); // Select ROM A (CGRAM 8, CGROM 248)
wonner163 0:4562b55bbb43 986
wonner163 0:4562b55bbb43 987 _writeCommand(0x79); // Function Select OLED: 0 1 1 1 1 0 0 1 (Ext Instr Set)
wonner163 0:4562b55bbb43 988
wonner163 0:4562b55bbb43 989 _writeCommand(0xDA); // Set Segm Pins Config: 1 1 0 1 1 0 1 0 (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 990 _writeCommand(0x10); // Set Segm Pins Config value: Altern Odd/Even, Disable Remap (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 991
wonner163 0:4562b55bbb43 992 _writeCommand(0xDC); // Function Select C: 1 1 0 1 1 1 0 0 (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 993 // _writeCommand(0x00); // Set internal VSL, GPIO pin HiZ (always read low)
wonner163 0:4562b55bbb43 994 _writeCommand(0x80); // Set external VSL, GPIO pin HiZ (always read low)
wonner163 0:4562b55bbb43 995
wonner163 0:4562b55bbb43 996 _contrast = LCD_US20_CONTRAST;
wonner163 0:4562b55bbb43 997 _writeCommand(0x81); // Set Contrast Control: 1 0 0 0 0 0 0 1 (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 998 _writeCommand((_contrast << 2) | 0x03); // Set Contrast Value: 8 bits, use 6 bits for compatibility
wonner163 0:4562b55bbb43 999
wonner163 0:4562b55bbb43 1000 _writeCommand(0xD9); // Set Phase Length: 1 1 0 1 1 0 0 1 (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 1001 _writeCommand(0xF1); // Set Phase Length Value:
wonner163 0:4562b55bbb43 1002
wonner163 0:4562b55bbb43 1003 _writeCommand(0xDB); // Set VCOMH Deselect Lvl: 1 1 0 1 1 0 1 1 (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 1004 _writeCommand(0x30); // Set VCOMH Deselect Value: 0.83 x VCC
wonner163 0:4562b55bbb43 1005
wonner163 0:4562b55bbb43 1006 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 1007
wonner163 0:4562b55bbb43 1008 //Test Fade/Blinking. Hard Blink on/off, No fade in/out ??
wonner163 0:4562b55bbb43 1009 // _writeCommand(0x23); // Set (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 1010 // _writeCommand(0x3F); // Set interval 128 frames
wonner163 0:4562b55bbb43 1011 //End Test Blinking
wonner163 0:4562b55bbb43 1012
wonner163 0:4562b55bbb43 1013 _writeCommand(0x78); // Function Disable OLED: 0 1 1 1 1 0 0 0 (Ext Instr Set)
wonner163 0:4562b55bbb43 1014
wonner163 0:4562b55bbb43 1015 _writeCommand(0x20 | _function | 0x01); // Set function, 0 0 1 X N DH RE(0) IS=1 Select Instruction Set 1
wonner163 0:4562b55bbb43 1016 // Select Std Instr set, Select IS=1
wonner163 0:4562b55bbb43 1017
wonner163 0:4562b55bbb43 1018 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 1019 // Select Ext Instr Set, IS=1
wonner163 0:4562b55bbb43 1020 _writeCommand(0x10); // Shift/Scroll enable, 0 0 0 1 DS4/HS4 DS3/HS3 DS2/HS2 DS1/HS1 (Ext Instr Set, IS=1)
wonner163 0:4562b55bbb43 1021
wonner163 0:4562b55bbb43 1022
wonner163 0:4562b55bbb43 1023 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 1024 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 1025
wonner163 0:4562b55bbb43 1026 break; // case US2066/SSD1311 Controller
wonner163 0:4562b55bbb43 1027
wonner163 0:4562b55bbb43 1028
wonner163 0:4562b55bbb43 1029 default:
wonner163 0:4562b55bbb43 1030 // Devices fully compatible to HD44780 that do not use any DC/DC Voltage converters but external VLCD, no icons etc
wonner163 0:4562b55bbb43 1031
wonner163 0:4562b55bbb43 1032 // Initialise Display configuration
wonner163 0:4562b55bbb43 1033 switch (_type) {
wonner163 0:4562b55bbb43 1034 case LCD8x1: //8x1 is a regular 1 line display
wonner163 0:4562b55bbb43 1035 case LCD8x2B: //8x2B is a special case of 16x1
wonner163 0:4562b55bbb43 1036 // case LCD12x1:
wonner163 0:4562b55bbb43 1037 case LCD16x1:
wonner163 0:4562b55bbb43 1038 // case LCD20x1:
wonner163 0:4562b55bbb43 1039 case LCD24x1:
wonner163 0:4562b55bbb43 1040 // case LCD40x1:
wonner163 0:4562b55bbb43 1041 _function = 0x00; // Function set 001 DL N F - -
wonner163 0:4562b55bbb43 1042 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 1043 // N=0 (1 line)
wonner163 0:4562b55bbb43 1044 // F=0 (5x7 dots font)
wonner163 0:4562b55bbb43 1045 _writeCommand(0x20 | _function);
wonner163 0:4562b55bbb43 1046 break;
wonner163 0:4562b55bbb43 1047
wonner163 0:4562b55bbb43 1048 case LCD12x3D: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 1049 case LCD12x3D1: // Special mode for KS0078 and PCF21XX
wonner163 0:4562b55bbb43 1050 case LCD12x4D: // Special mode for KS0078 and PCF21XX:
wonner163 0:4562b55bbb43 1051 case LCD16x3D: // Special mode for KS0078
wonner163 0:4562b55bbb43 1052 // case LCD16x3D1: // Special mode for KS0078
wonner163 0:4562b55bbb43 1053 // case LCD24x3D: // Special mode for KS0078
wonner163 0:4562b55bbb43 1054 // case LCD24x3D1: // Special mode for KS0078
wonner163 0:4562b55bbb43 1055 case LCD24x4D: // Special mode for KS0078
wonner163 0:4562b55bbb43 1056 error("Error: LCD Controller type does not support this Display type\n\r");
wonner163 0:4562b55bbb43 1057 break;
wonner163 0:4562b55bbb43 1058
wonner163 0:4562b55bbb43 1059 // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)
wonner163 0:4562b55bbb43 1060 default:
wonner163 0:4562b55bbb43 1061 _function = 0x08; // Function set 001 DL N F - -
wonner163 0:4562b55bbb43 1062 // DL=0 (4 bits bus)
wonner163 0:4562b55bbb43 1063 // Note: 4 bit mode is ignored for native SPI and I2C devices
wonner163 0:4562b55bbb43 1064 // N=1 (2 lines)
wonner163 0:4562b55bbb43 1065 // F=0 (5x7 dots font, only option for 2 line display)
wonner163 0:4562b55bbb43 1066 // - (Don't care)
wonner163 0:4562b55bbb43 1067 _writeCommand(0x20 | _function);
wonner163 0:4562b55bbb43 1068 break;
wonner163 0:4562b55bbb43 1069 } // switch type
wonner163 0:4562b55bbb43 1070
wonner163 0:4562b55bbb43 1071 break; // case default Controller
wonner163 0:4562b55bbb43 1072
wonner163 0:4562b55bbb43 1073 } // switch Controller specific initialisations
wonner163 0:4562b55bbb43 1074
wonner163 0:4562b55bbb43 1075
wonner163 0:4562b55bbb43 1076 // Controller general initialisations
wonner163 0:4562b55bbb43 1077 // _writeCommand(0x01); // cls, and set cursor to 0
wonner163 0:4562b55bbb43 1078 // wait_ms(10); // The CLS command takes 1.64 ms.
wonner163 0:4562b55bbb43 1079 // // Since we are not using the Busy flag, Lets be safe and take 10 ms
wonner163 0:4562b55bbb43 1080
wonner163 0:4562b55bbb43 1081 _writeCommand(0x02); // Return Home
wonner163 0:4562b55bbb43 1082 // Cursor Home, DDRAM Address to Origin
wonner163 0:4562b55bbb43 1083
wonner163 0:4562b55bbb43 1084 _writeCommand(0x06); // Entry Mode 0000 0 1 I/D S
wonner163 0:4562b55bbb43 1085 // Cursor Direction and Display Shift
wonner163 0:4562b55bbb43 1086 // I/D=1 (Cur incr)
wonner163 0:4562b55bbb43 1087 // S=0 (No display shift)
wonner163 0:4562b55bbb43 1088
wonner163 0:4562b55bbb43 1089 _writeCommand(0x14); // Cursor or Display shift 0001 S/C R/L x x
wonner163 0:4562b55bbb43 1090 // S/C=0 Cursor moves
wonner163 0:4562b55bbb43 1091 // R/L=1 Right
wonner163 0:4562b55bbb43 1092 //
wonner163 0:4562b55bbb43 1093
wonner163 0:4562b55bbb43 1094 // _writeCommand(0x0C); // Display Ctrl 0000 1 D C B
wonner163 0:4562b55bbb43 1095 // // Display On, Cursor Off, Blink Off
wonner163 0:4562b55bbb43 1096 setCursor(CurOff_BlkOff);
wonner163 0:4562b55bbb43 1097 setMode(DispOn);
wonner163 0:4562b55bbb43 1098 }
wonner163 0:4562b55bbb43 1099
wonner163 0:4562b55bbb43 1100
wonner163 0:4562b55bbb43 1101 /** Clear the screen, Cursor home.
wonner163 0:4562b55bbb43 1102 */
wonner163 0:4562b55bbb43 1103 void TextLCD_Base::cls() {
wonner163 0:4562b55bbb43 1104
wonner163 0:4562b55bbb43 1105 // Select and configure second LCD controller when needed
wonner163 0:4562b55bbb43 1106 if(_type==LCD40x4) {
wonner163 0:4562b55bbb43 1107 _ctrl_idx=_LCDCtrl_1; // Select 2nd controller
wonner163 0:4562b55bbb43 1108
wonner163 0:4562b55bbb43 1109 // Second LCD controller Cursor always Off
wonner163 0:4562b55bbb43 1110 _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
wonner163 0:4562b55bbb43 1111
wonner163 0:4562b55bbb43 1112 // Second LCD controller Clearscreen
wonner163 0:4562b55bbb43 1113 _writeCommand(0x01); // cls, and set cursor to 0
wonner163 0:4562b55bbb43 1114 wait_ms(10); // The CLS command takes 1.64 ms.
wonner163 0:4562b55bbb43 1115 // Since we are not using the Busy flag, Lets be safe and take 10 ms
wonner163 0:4562b55bbb43 1116
wonner163 0:4562b55bbb43 1117 _ctrl_idx=_LCDCtrl_0; // Select primary controller
wonner163 0:4562b55bbb43 1118 }
wonner163 0:4562b55bbb43 1119
wonner163 0:4562b55bbb43 1120 // Primary LCD controller Clearscreen
wonner163 0:4562b55bbb43 1121 _writeCommand(0x01); // cls, and set cursor to 0
wonner163 0:4562b55bbb43 1122 wait_ms(10); // The CLS command takes 1.64 ms.
wonner163 0:4562b55bbb43 1123 // Since we are not using the Busy flag, Lets be safe and take 10 ms
wonner163 0:4562b55bbb43 1124
wonner163 0:4562b55bbb43 1125 // Restore cursormode on primary LCD controller when needed
wonner163 0:4562b55bbb43 1126 if(_type==LCD40x4) {
wonner163 0:4562b55bbb43 1127 _setCursorAndDisplayMode(_currentMode,_currentCursor);
wonner163 0:4562b55bbb43 1128 }
wonner163 0:4562b55bbb43 1129
wonner163 0:4562b55bbb43 1130 setAddress(0, 0); // Reset Cursor location
wonner163 0:4562b55bbb43 1131 // Note: This is needed because some displays (eg PCF21XX) don't use line 0 in the '3 Line' mode.
wonner163 0:4562b55bbb43 1132 }
wonner163 0:4562b55bbb43 1133
wonner163 0:4562b55bbb43 1134 /** Locate cursor to a screen column and row
wonner163 0:4562b55bbb43 1135 *
wonner163 0:4562b55bbb43 1136 * @param column The horizontal position from the left, indexed from 0
wonner163 0:4562b55bbb43 1137 * @param row The vertical position from the top, indexed from 0
wonner163 0:4562b55bbb43 1138 */
wonner163 0:4562b55bbb43 1139 void TextLCD_Base::locate(int column, int row) {
wonner163 0:4562b55bbb43 1140
wonner163 0:4562b55bbb43 1141 // setAddress() does all the heavy lifting:
wonner163 0:4562b55bbb43 1142 // check column and row sanity,
wonner163 0:4562b55bbb43 1143 // switch controllers for LCD40x4 if needed
wonner163 0:4562b55bbb43 1144 // switch cursor for LCD40x4 if needed
wonner163 0:4562b55bbb43 1145 // set the new memory address to show cursor at correct location
wonner163 0:4562b55bbb43 1146 setAddress(column, row);
wonner163 0:4562b55bbb43 1147 }
wonner163 0:4562b55bbb43 1148
wonner163 0:4562b55bbb43 1149
wonner163 0:4562b55bbb43 1150 /** Write a single character (Stream implementation)
wonner163 0:4562b55bbb43 1151 */
wonner163 0:4562b55bbb43 1152 int TextLCD_Base::_putc(int value) {
wonner163 0:4562b55bbb43 1153 int addr;
wonner163 0:4562b55bbb43 1154
wonner163 0:4562b55bbb43 1155 if (value == '\n') {
wonner163 0:4562b55bbb43 1156 //No character to write
wonner163 0:4562b55bbb43 1157
wonner163 0:4562b55bbb43 1158 //Update Cursor
wonner163 0:4562b55bbb43 1159 _column = 0;
wonner163 0:4562b55bbb43 1160 _row++;
wonner163 0:4562b55bbb43 1161 if (_row >= rows()) {
wonner163 0:4562b55bbb43 1162 _row = 0;
wonner163 0:4562b55bbb43 1163 }
wonner163 0:4562b55bbb43 1164 }
wonner163 0:4562b55bbb43 1165 else {
wonner163 0:4562b55bbb43 1166 //Character to write
wonner163 0:4562b55bbb43 1167 _writeData(value);
wonner163 0:4562b55bbb43 1168
wonner163 0:4562b55bbb43 1169 //Update Cursor
wonner163 0:4562b55bbb43 1170 _column++;
wonner163 0:4562b55bbb43 1171 if (_column >= columns()) {
wonner163 0:4562b55bbb43 1172 _column = 0;
wonner163 0:4562b55bbb43 1173 _row++;
wonner163 0:4562b55bbb43 1174 if (_row >= rows()) {
wonner163 0:4562b55bbb43 1175 _row = 0;
wonner163 0:4562b55bbb43 1176 }
wonner163 0:4562b55bbb43 1177 }
wonner163 0:4562b55bbb43 1178 } //else
wonner163 0:4562b55bbb43 1179
wonner163 0:4562b55bbb43 1180 //Set next memoryaddress, make sure cursor blinks at next location
wonner163 0:4562b55bbb43 1181 addr = getAddress(_column, _row);
wonner163 0:4562b55bbb43 1182 _writeCommand(0x80 | addr);
wonner163 0:4562b55bbb43 1183
wonner163 0:4562b55bbb43 1184 return value;
wonner163 0:4562b55bbb43 1185 }
wonner163 0:4562b55bbb43 1186
wonner163 0:4562b55bbb43 1187
wonner163 0:4562b55bbb43 1188 // get a single character (Stream implementation)
wonner163 0:4562b55bbb43 1189 int TextLCD_Base::_getc() {
wonner163 0:4562b55bbb43 1190 return -1;
wonner163 0:4562b55bbb43 1191 }
wonner163 0:4562b55bbb43 1192
wonner163 0:4562b55bbb43 1193
wonner163 0:4562b55bbb43 1194 // Write a nibble using the 4-bit interface
wonner163 0:4562b55bbb43 1195 void TextLCD_Base::_writeNibble(int value) {
wonner163 0:4562b55bbb43 1196
wonner163 0:4562b55bbb43 1197 // Enable is Low
wonner163 0:4562b55bbb43 1198 this->_setEnable(true);
wonner163 0:4562b55bbb43 1199 this->_setData(value & 0x0F); // Low nibble
wonner163 0:4562b55bbb43 1200 wait_us(1); // Data setup time
wonner163 0:4562b55bbb43 1201 this->_setEnable(false);
wonner163 0:4562b55bbb43 1202 wait_us(1); // Datahold time
wonner163 0:4562b55bbb43 1203
wonner163 0:4562b55bbb43 1204 // Enable is Low
wonner163 0:4562b55bbb43 1205 }
wonner163 0:4562b55bbb43 1206
wonner163 0:4562b55bbb43 1207 // Write a byte using the 4-bit interface
wonner163 0:4562b55bbb43 1208 void TextLCD_Base::_writeByte(int value) {
wonner163 0:4562b55bbb43 1209
wonner163 0:4562b55bbb43 1210 // Enable is Low
wonner163 0:4562b55bbb43 1211 this->_setEnable(true);
wonner163 0:4562b55bbb43 1212 this->_setData(value >> 4); // High nibble
wonner163 0:4562b55bbb43 1213 wait_us(1); // Data setup time
wonner163 0:4562b55bbb43 1214 this->_setEnable(false);
wonner163 0:4562b55bbb43 1215 wait_us(1); // Data hold time
wonner163 0:4562b55bbb43 1216
wonner163 0:4562b55bbb43 1217 this->_setEnable(true);
wonner163 0:4562b55bbb43 1218 this->_setData(value >> 0); // Low nibble
wonner163 0:4562b55bbb43 1219 wait_us(1); // Data setup time
wonner163 0:4562b55bbb43 1220 this->_setEnable(false);
wonner163 0:4562b55bbb43 1221 wait_us(1); // Datahold time
wonner163 0:4562b55bbb43 1222
wonner163 0:4562b55bbb43 1223 // Enable is Low
wonner163 0:4562b55bbb43 1224 }
wonner163 0:4562b55bbb43 1225
wonner163 0:4562b55bbb43 1226 // Write a command byte to the LCD controller
wonner163 0:4562b55bbb43 1227 void TextLCD_Base::_writeCommand(int command) {
wonner163 0:4562b55bbb43 1228
wonner163 0:4562b55bbb43 1229 this->_setRS(false);
wonner163 0:4562b55bbb43 1230 wait_us(1); // Data setup time for RS
wonner163 0:4562b55bbb43 1231
wonner163 0:4562b55bbb43 1232 this->_writeByte(command);
wonner163 0:4562b55bbb43 1233 wait_us(40); // most instructions take 40us
wonner163 0:4562b55bbb43 1234 }
wonner163 0:4562b55bbb43 1235
wonner163 0:4562b55bbb43 1236 // Write a data byte to the LCD controller
wonner163 0:4562b55bbb43 1237 void TextLCD_Base::_writeData(int data) {
wonner163 0:4562b55bbb43 1238
wonner163 0:4562b55bbb43 1239 this->_setRS(true);
wonner163 0:4562b55bbb43 1240 wait_us(1); // Data setup time for RS
wonner163 0:4562b55bbb43 1241
wonner163 0:4562b55bbb43 1242 this->_writeByte(data);
wonner163 0:4562b55bbb43 1243 wait_us(40); // data writes take 40us
wonner163 0:4562b55bbb43 1244 }
wonner163 0:4562b55bbb43 1245
wonner163 0:4562b55bbb43 1246
wonner163 0:4562b55bbb43 1247 // This replaces the original _address() method.
wonner163 0:4562b55bbb43 1248 // It is confusing since it returns the memoryaddress or-ed with the set memorycommand 0x80.
wonner163 0:4562b55bbb43 1249 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
wonner163 0:4562b55bbb43 1250 int TextLCD_Base::_address(int column, int row) {
wonner163 0:4562b55bbb43 1251 return 0x80 | getAddress(column, row);
wonner163 0:4562b55bbb43 1252 }
wonner163 0:4562b55bbb43 1253
wonner163 0:4562b55bbb43 1254
wonner163 0:4562b55bbb43 1255 // This is new method to return the memory address based on row, column and displaytype.
wonner163 0:4562b55bbb43 1256 //
wonner163 0:4562b55bbb43 1257 /** Return the memoryaddress of screen column and row location
wonner163 0:4562b55bbb43 1258 *
wonner163 0:4562b55bbb43 1259 * @param column The horizontal position from the left, indexed from 0
wonner163 0:4562b55bbb43 1260 * @param row The vertical position from the top, indexed from 0
wonner163 0:4562b55bbb43 1261 * @param return The memoryaddress of screen column and row location
wonner163 0:4562b55bbb43 1262 *
wonner163 0:4562b55bbb43 1263 */
wonner163 0:4562b55bbb43 1264 int TextLCD_Base::getAddress(int column, int row) {
wonner163 0:4562b55bbb43 1265
wonner163 0:4562b55bbb43 1266 switch (_addr_mode) {
wonner163 0:4562b55bbb43 1267
wonner163 0:4562b55bbb43 1268 case LCD_T_A:
wonner163 0:4562b55bbb43 1269 //Default addressing mode for 1, 2 and 4 rows (except 40x4)
wonner163 0:4562b55bbb43 1270 //The two available rows are split and stacked on top of eachother. Addressing for 3rd and 4th line continues where lines 1 and 2 were split.
wonner163 0:4562b55bbb43 1271 //Displays top rows when less than four are used.
wonner163 0:4562b55bbb43 1272 switch (row) {
wonner163 0:4562b55bbb43 1273 case 0:
wonner163 0:4562b55bbb43 1274 return 0x00 + column;
wonner163 0:4562b55bbb43 1275 case 1:
wonner163 0:4562b55bbb43 1276 return 0x40 + column;
wonner163 0:4562b55bbb43 1277 case 2:
wonner163 0:4562b55bbb43 1278 return 0x00 + _nr_cols + column;
wonner163 0:4562b55bbb43 1279 case 3:
wonner163 0:4562b55bbb43 1280 return 0x40 + _nr_cols + column;
wonner163 0:4562b55bbb43 1281 // Should never get here.
wonner163 0:4562b55bbb43 1282 default:
wonner163 0:4562b55bbb43 1283 return 0x00;
wonner163 0:4562b55bbb43 1284 }
wonner163 0:4562b55bbb43 1285
wonner163 0:4562b55bbb43 1286 case LCD_T_B:
wonner163 0:4562b55bbb43 1287 // LCD8x2B is a special layout of LCD16x1
wonner163 0:4562b55bbb43 1288 if (row==0)
wonner163 0:4562b55bbb43 1289 return 0x00 + column;
wonner163 0:4562b55bbb43 1290 else
wonner163 0:4562b55bbb43 1291 // return _nr_cols + column;
wonner163 0:4562b55bbb43 1292 return 0x08 + column;
wonner163 0:4562b55bbb43 1293
wonner163 0:4562b55bbb43 1294 case LCD_T_C:
wonner163 0:4562b55bbb43 1295 // LCD16x1C is a special layout of LCD8x2
wonner163 0:4562b55bbb43 1296 // LCD32x1C is a special layout of LCD16x2
wonner163 0:4562b55bbb43 1297 // LCD40x1C is a special layout of LCD20x2
wonner163 0:4562b55bbb43 1298 #if(0)
wonner163 0:4562b55bbb43 1299 if (column < 8)
wonner163 0:4562b55bbb43 1300 return 0x00 + column;
wonner163 0:4562b55bbb43 1301 else
wonner163 0:4562b55bbb43 1302 return 0x40 + (column - 8);
wonner163 0:4562b55bbb43 1303 #else
wonner163 0:4562b55bbb43 1304 if (column < (_nr_cols >> 1))
wonner163 0:4562b55bbb43 1305 return 0x00 + column;
wonner163 0:4562b55bbb43 1306 else
wonner163 0:4562b55bbb43 1307 return 0x40 + (column - (_nr_cols >> 1));
wonner163 0:4562b55bbb43 1308 #endif
wonner163 0:4562b55bbb43 1309
wonner163 0:4562b55bbb43 1310 // Not sure about this one, seems wrong.
wonner163 0:4562b55bbb43 1311 // Left in for compatibility with original library
wonner163 0:4562b55bbb43 1312 // case LCD16x2B:
wonner163 0:4562b55bbb43 1313 // return 0x00 + (row * 40) + column;
wonner163 0:4562b55bbb43 1314
wonner163 0:4562b55bbb43 1315 case LCD_T_D:
wonner163 0:4562b55bbb43 1316 //Alternate addressing mode for 3 and 4 row displays (except 40x4). Used by PCF21XX, KS0078, SSD1803
wonner163 0:4562b55bbb43 1317 //The 4 available rows start at a hardcoded address.
wonner163 0:4562b55bbb43 1318 //Displays top rows when less than four are used.
wonner163 0:4562b55bbb43 1319 switch (row) {
wonner163 0:4562b55bbb43 1320 case 0:
wonner163 0:4562b55bbb43 1321 return 0x00 + column;
wonner163 0:4562b55bbb43 1322 case 1:
wonner163 0:4562b55bbb43 1323 return 0x20 + column;
wonner163 0:4562b55bbb43 1324 case 2:
wonner163 0:4562b55bbb43 1325 return 0x40 + column;
wonner163 0:4562b55bbb43 1326 case 3:
wonner163 0:4562b55bbb43 1327 return 0x60 + column;
wonner163 0:4562b55bbb43 1328 // Should never get here.
wonner163 0:4562b55bbb43 1329 default:
wonner163 0:4562b55bbb43 1330 return 0x00;
wonner163 0:4562b55bbb43 1331 }
wonner163 0:4562b55bbb43 1332
wonner163 0:4562b55bbb43 1333 case LCD_T_D1:
wonner163 0:4562b55bbb43 1334 //Alternate addressing mode for 3 row displays. Used by PCF21XX, KS0078, SSD1803
wonner163 0:4562b55bbb43 1335 //The 4 available rows start at a hardcoded address.
wonner163 0:4562b55bbb43 1336 //Skips top row of 4 row display and starts display at row 1
wonner163 0:4562b55bbb43 1337 switch (row) {
wonner163 0:4562b55bbb43 1338 case 0:
wonner163 0:4562b55bbb43 1339 return 0x20 + column;
wonner163 0:4562b55bbb43 1340 case 1:
wonner163 0:4562b55bbb43 1341 return 0x40 + column;
wonner163 0:4562b55bbb43 1342 case 2:
wonner163 0:4562b55bbb43 1343 return 0x60 + column;
wonner163 0:4562b55bbb43 1344 // Should never get here.
wonner163 0:4562b55bbb43 1345 default:
wonner163 0:4562b55bbb43 1346 return 0x00;
wonner163 0:4562b55bbb43 1347 }
wonner163 0:4562b55bbb43 1348
wonner163 0:4562b55bbb43 1349 case LCD_T_E:
wonner163 0:4562b55bbb43 1350 // LCD40x4 is a special case since it has 2 controllers.
wonner163 0:4562b55bbb43 1351 // Each controller is configured as 40x2 (Type A)
wonner163 0:4562b55bbb43 1352 if (row<2) {
wonner163 0:4562b55bbb43 1353 // Test to see if we need to switch between controllers
wonner163 0:4562b55bbb43 1354 if (_ctrl_idx != _LCDCtrl_0) {
wonner163 0:4562b55bbb43 1355
wonner163 0:4562b55bbb43 1356 // Second LCD controller Cursor Off
wonner163 0:4562b55bbb43 1357 _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
wonner163 0:4562b55bbb43 1358
wonner163 0:4562b55bbb43 1359 // Select primary controller
wonner163 0:4562b55bbb43 1360 _ctrl_idx = _LCDCtrl_0;
wonner163 0:4562b55bbb43 1361
wonner163 0:4562b55bbb43 1362 // Restore cursormode on primary LCD controller
wonner163 0:4562b55bbb43 1363 _setCursorAndDisplayMode(_currentMode, _currentCursor);
wonner163 0:4562b55bbb43 1364 }
wonner163 0:4562b55bbb43 1365
wonner163 0:4562b55bbb43 1366 return 0x00 + (row * 0x40) + column;
wonner163 0:4562b55bbb43 1367 }
wonner163 0:4562b55bbb43 1368 else {
wonner163 0:4562b55bbb43 1369
wonner163 0:4562b55bbb43 1370 // Test to see if we need to switch between controllers
wonner163 0:4562b55bbb43 1371 if (_ctrl_idx != _LCDCtrl_1) {
wonner163 0:4562b55bbb43 1372 // Primary LCD controller Cursor Off
wonner163 0:4562b55bbb43 1373 _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
wonner163 0:4562b55bbb43 1374
wonner163 0:4562b55bbb43 1375 // Select secondary controller
wonner163 0:4562b55bbb43 1376 _ctrl_idx = _LCDCtrl_1;
wonner163 0:4562b55bbb43 1377
wonner163 0:4562b55bbb43 1378 // Restore cursormode on secondary LCD controller
wonner163 0:4562b55bbb43 1379 _setCursorAndDisplayMode(_currentMode, _currentCursor);
wonner163 0:4562b55bbb43 1380 }
wonner163 0:4562b55bbb43 1381
wonner163 0:4562b55bbb43 1382 return 0x00 + ((row-2) * 0x40) + column;
wonner163 0:4562b55bbb43 1383 }
wonner163 0:4562b55bbb43 1384
wonner163 0:4562b55bbb43 1385 case LCD_T_F:
wonner163 0:4562b55bbb43 1386 //Alternate addressing mode for 3 row displays.
wonner163 0:4562b55bbb43 1387 //The first half of 3rd row continues from 1st row, the second half continues from 2nd row.
wonner163 0:4562b55bbb43 1388 switch (row) {
wonner163 0:4562b55bbb43 1389 case 0:
wonner163 0:4562b55bbb43 1390 return 0x00 + column;
wonner163 0:4562b55bbb43 1391 case 1:
wonner163 0:4562b55bbb43 1392 return 0x40 + column;
wonner163 0:4562b55bbb43 1393 case 2:
wonner163 0:4562b55bbb43 1394 if (column < (_nr_cols >> 1)) // check first or second half of line
wonner163 0:4562b55bbb43 1395 return (0x00 + _nr_cols + column);
wonner163 0:4562b55bbb43 1396 else
wonner163 0:4562b55bbb43 1397 return (0x40 + _nr_cols + (column - (_nr_cols >> 1)));
wonner163 0:4562b55bbb43 1398 // Should never get here.
wonner163 0:4562b55bbb43 1399 default:
wonner163 0:4562b55bbb43 1400 return 0x00;
wonner163 0:4562b55bbb43 1401 }
wonner163 0:4562b55bbb43 1402
wonner163 0:4562b55bbb43 1403 case LCD_T_G:
wonner163 0:4562b55bbb43 1404 //Alternate addressing mode for 3 row displays. Used by ST7036
wonner163 0:4562b55bbb43 1405 switch (row) {
wonner163 0:4562b55bbb43 1406 case 0:
wonner163 0:4562b55bbb43 1407 return 0x00 + column;
wonner163 0:4562b55bbb43 1408 case 1:
wonner163 0:4562b55bbb43 1409 return 0x10 + column;
wonner163 0:4562b55bbb43 1410 case 2:
wonner163 0:4562b55bbb43 1411 return 0x20 + column;
wonner163 0:4562b55bbb43 1412 // Should never get here.
wonner163 0:4562b55bbb43 1413 default:
wonner163 0:4562b55bbb43 1414 return 0x00;
wonner163 0:4562b55bbb43 1415 }
wonner163 0:4562b55bbb43 1416
wonner163 0:4562b55bbb43 1417 // Should never get here.
wonner163 0:4562b55bbb43 1418 default:
wonner163 0:4562b55bbb43 1419 return 0x00;
wonner163 0:4562b55bbb43 1420
wonner163 0:4562b55bbb43 1421 } // switch _addr_mode
wonner163 0:4562b55bbb43 1422 }
wonner163 0:4562b55bbb43 1423
wonner163 0:4562b55bbb43 1424
wonner163 0:4562b55bbb43 1425 /** Set the memoryaddress of screen column and row location
wonner163 0:4562b55bbb43 1426 *
wonner163 0:4562b55bbb43 1427 * @param column The horizontal position from the left, indexed from 0
wonner163 0:4562b55bbb43 1428 * @param row The vertical position from the top, indexed from 0
wonner163 0:4562b55bbb43 1429 */
wonner163 0:4562b55bbb43 1430 void TextLCD_Base::setAddress(int column, int row) {
wonner163 0:4562b55bbb43 1431
wonner163 0:4562b55bbb43 1432 // Sanity Check column
wonner163 0:4562b55bbb43 1433 if (column < 0) {
wonner163 0:4562b55bbb43 1434 _column = 0;
wonner163 0:4562b55bbb43 1435 }
wonner163 0:4562b55bbb43 1436 else if (column >= columns()) {
wonner163 0:4562b55bbb43 1437 _column = columns() - 1;
wonner163 0:4562b55bbb43 1438 } else _column = column;
wonner163 0:4562b55bbb43 1439
wonner163 0:4562b55bbb43 1440 // Sanity Check row
wonner163 0:4562b55bbb43 1441 if (row < 0) {
wonner163 0:4562b55bbb43 1442 _row = 0;
wonner163 0:4562b55bbb43 1443 }
wonner163 0:4562b55bbb43 1444 else if (row >= rows()) {
wonner163 0:4562b55bbb43 1445 _row = rows() - 1;
wonner163 0:4562b55bbb43 1446 } else _row = row;
wonner163 0:4562b55bbb43 1447
wonner163 0:4562b55bbb43 1448
wonner163 0:4562b55bbb43 1449 // Compute the memory address
wonner163 0:4562b55bbb43 1450 // For LCD40x4: switch controllers if needed
wonner163 0:4562b55bbb43 1451 // switch cursor if needed
wonner163 0:4562b55bbb43 1452 int addr = getAddress(_column, _row);
wonner163 0:4562b55bbb43 1453
wonner163 0:4562b55bbb43 1454 _writeCommand(0x80 | addr);
wonner163 0:4562b55bbb43 1455 }
wonner163 0:4562b55bbb43 1456
wonner163 0:4562b55bbb43 1457
wonner163 0:4562b55bbb43 1458 /** Return the number of columns
wonner163 0:4562b55bbb43 1459 *
wonner163 0:4562b55bbb43 1460 * @param return The number of columns
wonner163 0:4562b55bbb43 1461 *
wonner163 0:4562b55bbb43 1462 * Note: some configurations are commented out because they have not yet been tested due to lack of hardware
wonner163 0:4562b55bbb43 1463 */
wonner163 0:4562b55bbb43 1464 int TextLCD_Base::columns() {
wonner163 0:4562b55bbb43 1465
wonner163 0:4562b55bbb43 1466 // Columns encoded in b7..b0
wonner163 0:4562b55bbb43 1467 //return (_type & 0xFF);
wonner163 0:4562b55bbb43 1468 return _nr_cols;
wonner163 0:4562b55bbb43 1469 }
wonner163 0:4562b55bbb43 1470
wonner163 0:4562b55bbb43 1471 /** Return the number of rows
wonner163 0:4562b55bbb43 1472 *
wonner163 0:4562b55bbb43 1473 * @param return The number of rows
wonner163 0:4562b55bbb43 1474 *
wonner163 0:4562b55bbb43 1475 * Note: some configurations are commented out because they have not yet been tested due to lack of hardware
wonner163 0:4562b55bbb43 1476 */
wonner163 0:4562b55bbb43 1477 int TextLCD_Base::rows() {
wonner163 0:4562b55bbb43 1478
wonner163 0:4562b55bbb43 1479 // Rows encoded in b15..b8
wonner163 0:4562b55bbb43 1480 //return ((_type >> 8) & 0xFF);
wonner163 0:4562b55bbb43 1481 return _nr_rows;
wonner163 0:4562b55bbb43 1482 }
wonner163 0:4562b55bbb43 1483
wonner163 0:4562b55bbb43 1484 /** Set the Cursormode
wonner163 0:4562b55bbb43 1485 *
wonner163 0:4562b55bbb43 1486 * @param cursorMode The Cursor mode (CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn)
wonner163 0:4562b55bbb43 1487 */
wonner163 0:4562b55bbb43 1488 void TextLCD_Base::setCursor(LCDCursor cursorMode) {
wonner163 0:4562b55bbb43 1489
wonner163 0:4562b55bbb43 1490 // Save new cursor mode, needed when 2 controllers are in use or when display is switched off/on
wonner163 0:4562b55bbb43 1491 _currentCursor = cursorMode;
wonner163 0:4562b55bbb43 1492
wonner163 0:4562b55bbb43 1493 // Configure only current LCD controller
wonner163 0:4562b55bbb43 1494 _setCursorAndDisplayMode(_currentMode, _currentCursor);
wonner163 0:4562b55bbb43 1495 }
wonner163 0:4562b55bbb43 1496
wonner163 0:4562b55bbb43 1497 /** Set the Displaymode
wonner163 0:4562b55bbb43 1498 *
wonner163 0:4562b55bbb43 1499 * @param displayMode The Display mode (DispOff, DispOn)
wonner163 0:4562b55bbb43 1500 */
wonner163 0:4562b55bbb43 1501 void TextLCD_Base::setMode(LCDMode displayMode) {
wonner163 0:4562b55bbb43 1502
wonner163 0:4562b55bbb43 1503 // Save new displayMode, needed when 2 controllers are in use or when cursor is changed
wonner163 0:4562b55bbb43 1504 _currentMode = displayMode;
wonner163 0:4562b55bbb43 1505
wonner163 0:4562b55bbb43 1506 // Select and configure second LCD controller when needed
wonner163 0:4562b55bbb43 1507 if(_type==LCD40x4) {
wonner163 0:4562b55bbb43 1508 if (_ctrl_idx==_LCDCtrl_0) {
wonner163 0:4562b55bbb43 1509 // Configure primary LCD controller
wonner163 0:4562b55bbb43 1510 _setCursorAndDisplayMode(_currentMode, _currentCursor);
wonner163 0:4562b55bbb43 1511
wonner163 0:4562b55bbb43 1512 // Select 2nd controller
wonner163 0:4562b55bbb43 1513 _ctrl_idx=_LCDCtrl_1;
wonner163 0:4562b55bbb43 1514
wonner163 0:4562b55bbb43 1515 // Configure secondary LCD controller
wonner163 0:4562b55bbb43 1516 _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
wonner163 0:4562b55bbb43 1517
wonner163 0:4562b55bbb43 1518 // Restore current controller
wonner163 0:4562b55bbb43 1519 _ctrl_idx=_LCDCtrl_0;
wonner163 0:4562b55bbb43 1520 }
wonner163 0:4562b55bbb43 1521 else {
wonner163 0:4562b55bbb43 1522 // Select primary controller
wonner163 0:4562b55bbb43 1523 _ctrl_idx=_LCDCtrl_0;
wonner163 0:4562b55bbb43 1524
wonner163 0:4562b55bbb43 1525 // Configure primary LCD controller
wonner163 0:4562b55bbb43 1526 _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
wonner163 0:4562b55bbb43 1527
wonner163 0:4562b55bbb43 1528 // Restore current controller
wonner163 0:4562b55bbb43 1529 _ctrl_idx=_LCDCtrl_1;
wonner163 0:4562b55bbb43 1530
wonner163 0:4562b55bbb43 1531 // Configure secondary LCD controller
wonner163 0:4562b55bbb43 1532 _setCursorAndDisplayMode(_currentMode, _currentCursor);
wonner163 0:4562b55bbb43 1533 }
wonner163 0:4562b55bbb43 1534 }
wonner163 0:4562b55bbb43 1535 else {
wonner163 0:4562b55bbb43 1536 // Configure primary LCD controller
wonner163 0:4562b55bbb43 1537 _setCursorAndDisplayMode(_currentMode, _currentCursor);
wonner163 0:4562b55bbb43 1538 }
wonner163 0:4562b55bbb43 1539 }
wonner163 0:4562b55bbb43 1540
wonner163 0:4562b55bbb43 1541
wonner163 0:4562b55bbb43 1542 /** Low level method to restore the cursortype and display mode for current controller
wonner163 0:4562b55bbb43 1543 */
wonner163 0:4562b55bbb43 1544 void TextLCD_Base::_setCursorAndDisplayMode(LCDMode displayMode, LCDCursor cursorType) {
wonner163 0:4562b55bbb43 1545
wonner163 0:4562b55bbb43 1546 // Configure current LCD controller
wonner163 0:4562b55bbb43 1547 _writeCommand(0x08 | displayMode | cursorType);
wonner163 0:4562b55bbb43 1548 }
wonner163 0:4562b55bbb43 1549
wonner163 0:4562b55bbb43 1550 /** Set the Backlight mode
wonner163 0:4562b55bbb43 1551 *
wonner163 0:4562b55bbb43 1552 * @param backlightMode The Backlight mode (LightOff, LightOn)
wonner163 0:4562b55bbb43 1553 */
wonner163 0:4562b55bbb43 1554 void TextLCD_Base::setBacklight(LCDBacklight backlightMode) {
wonner163 0:4562b55bbb43 1555
wonner163 0:4562b55bbb43 1556 if (backlightMode == LightOn) {
wonner163 0:4562b55bbb43 1557 this->_setBL(true);
wonner163 0:4562b55bbb43 1558 }
wonner163 0:4562b55bbb43 1559 else {
wonner163 0:4562b55bbb43 1560 this->_setBL(false);
wonner163 0:4562b55bbb43 1561 }
wonner163 0:4562b55bbb43 1562 }
wonner163 0:4562b55bbb43 1563
wonner163 0:4562b55bbb43 1564 /** Set User Defined Characters
wonner163 0:4562b55bbb43 1565 *
wonner163 0:4562b55bbb43 1566 * @param unsigned char c The Index of the UDC (0..7)
wonner163 0:4562b55bbb43 1567 * @param char *udc_data The bitpatterns for the UDC (8 bytes of 5 significant bits)
wonner163 0:4562b55bbb43 1568 */
wonner163 0:4562b55bbb43 1569 void TextLCD_Base::setUDC(unsigned char c, char *udc_data) {
wonner163 0:4562b55bbb43 1570
wonner163 0:4562b55bbb43 1571 // Select and configure second LCD controller when needed
wonner163 0:4562b55bbb43 1572 if(_type==LCD40x4) {
wonner163 0:4562b55bbb43 1573 _LCDCtrl_Idx current_ctrl_idx = _ctrl_idx; // Temp save current controller
wonner163 0:4562b55bbb43 1574
wonner163 0:4562b55bbb43 1575 // Select primary controller
wonner163 0:4562b55bbb43 1576 _ctrl_idx=_LCDCtrl_0;
wonner163 0:4562b55bbb43 1577
wonner163 0:4562b55bbb43 1578 // Configure primary LCD controller
wonner163 0:4562b55bbb43 1579 _setUDC(c, udc_data);
wonner163 0:4562b55bbb43 1580
wonner163 0:4562b55bbb43 1581 // Select 2nd controller
wonner163 0:4562b55bbb43 1582 _ctrl_idx=_LCDCtrl_1;
wonner163 0:4562b55bbb43 1583
wonner163 0:4562b55bbb43 1584 // Configure secondary LCD controller
wonner163 0:4562b55bbb43 1585 _setUDC(c, udc_data);
wonner163 0:4562b55bbb43 1586
wonner163 0:4562b55bbb43 1587 // Restore current controller
wonner163 0:4562b55bbb43 1588 _ctrl_idx=current_ctrl_idx;
wonner163 0:4562b55bbb43 1589 }
wonner163 0:4562b55bbb43 1590 else {
wonner163 0:4562b55bbb43 1591 // Configure primary LCD controller
wonner163 0:4562b55bbb43 1592 _setUDC(c, udc_data);
wonner163 0:4562b55bbb43 1593 }
wonner163 0:4562b55bbb43 1594
wonner163 0:4562b55bbb43 1595 }
wonner163 0:4562b55bbb43 1596
wonner163 0:4562b55bbb43 1597 /** Low level method to store user defined characters for current controller
wonner163 0:4562b55bbb43 1598 */
wonner163 0:4562b55bbb43 1599 void TextLCD_Base::_setUDC(unsigned char c, char *udc_data) {
wonner163 0:4562b55bbb43 1600
wonner163 0:4562b55bbb43 1601 // Select CG RAM for current LCD controller
wonner163 0:4562b55bbb43 1602 _writeCommand(0x40 + ((c & 0x07) << 3)); //Set CG-RAM address,
wonner163 0:4562b55bbb43 1603 //8 sequential locations needed per UDC
wonner163 0:4562b55bbb43 1604 // Store UDC pattern
wonner163 0:4562b55bbb43 1605 for (int i=0; i<8; i++) {
wonner163 0:4562b55bbb43 1606 _writeData(*udc_data++);
wonner163 0:4562b55bbb43 1607 }
wonner163 0:4562b55bbb43 1608
wonner163 0:4562b55bbb43 1609 //Select DD RAM again for current LCD controller
wonner163 0:4562b55bbb43 1610 int addr = getAddress(_column, _row);
wonner163 0:4562b55bbb43 1611 _writeCommand(0x80 | addr);
wonner163 0:4562b55bbb43 1612 }
wonner163 0:4562b55bbb43 1613
wonner163 0:4562b55bbb43 1614
wonner163 0:4562b55bbb43 1615 /** Set UDC Blink
wonner163 0:4562b55bbb43 1616 * setUDCBlink method is supported by some compatible devices (eg SSD1803)
wonner163 0:4562b55bbb43 1617 *
wonner163 0:4562b55bbb43 1618 * @param blinkMode The Blink mode (BlinkOff, BlinkOn)
wonner163 0:4562b55bbb43 1619 */
wonner163 0:4562b55bbb43 1620 void TextLCD_Base::setUDCBlink(LCDBlink blinkMode){
wonner163 0:4562b55bbb43 1621 // Blinking UDCs are enabled when a specific controlbit (BE) is set.
wonner163 0:4562b55bbb43 1622 // The blinking pixels in the UDC can be controlled by setting additional bits in the UDC bitpattern.
wonner163 0:4562b55bbb43 1623 // UDCs are defined by an 8 byte bitpattern. The P0..P5 form the character pattern.
wonner163 0:4562b55bbb43 1624 // P7 P6 P5 P4 P3 P2 P1 P0
wonner163 0:4562b55bbb43 1625 // 0 B1 B0 x 0 1 1 1 0
wonner163 0:4562b55bbb43 1626 // 1 B1 B0 x 1 0 0 0 1
wonner163 0:4562b55bbb43 1627 // .............
wonner163 0:4562b55bbb43 1628 // 7 B1 B0 x 1 0 0 0 1
wonner163 0:4562b55bbb43 1629 //
wonner163 0:4562b55bbb43 1630 // Bit 6 and Bit 7 in the pattern will control the blinking mode when Blink is enabled through BE.
wonner163 0:4562b55bbb43 1631 // B1 B0 Mode
wonner163 0:4562b55bbb43 1632 // 0 0 No Blinking in this row of the UDC
wonner163 0:4562b55bbb43 1633 // 0 1 Enabled pixels in P4 will blink
wonner163 0:4562b55bbb43 1634 // 1 x Enabled pixels in P0..P4 will blink
wonner163 0:4562b55bbb43 1635
wonner163 0:4562b55bbb43 1636 switch (blinkMode) {
wonner163 0:4562b55bbb43 1637 case BlinkOn:
wonner163 0:4562b55bbb43 1638 // Controllers that support UDC Blink
wonner163 0:4562b55bbb43 1639 switch (_ctrl) {
wonner163 0:4562b55bbb43 1640 case KS0078 :
wonner163 0:4562b55bbb43 1641 _function_1 |= 0x02; // Enable UDC Blink
wonner163 0:4562b55bbb43 1642 _writeCommand(0x20 | _function_1); // Function set 0 0 1 DL N RE(1) BE 0 (Ext Regs)
wonner163 0:4562b55bbb43 1643
wonner163 0:4562b55bbb43 1644 _writeCommand(0x20 | _function); // Function set 0 0 1 DL N RE(0) DH REV (Std Regs)
wonner163 0:4562b55bbb43 1645 break; // case KS0078 Controller
wonner163 0:4562b55bbb43 1646
wonner163 0:4562b55bbb43 1647 case US2066_3V3 :
wonner163 0:4562b55bbb43 1648 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1649 _function_1 |= 0x04; // Enable UDC Blink
wonner163 0:4562b55bbb43 1650 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 1651 // Select Ext Instr Set
wonner163 0:4562b55bbb43 1652
wonner163 0:4562b55bbb43 1653 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 1654 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 1655 break; // case SSD1803, US2066
wonner163 0:4562b55bbb43 1656
wonner163 0:4562b55bbb43 1657 default:
wonner163 0:4562b55bbb43 1658 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1659 break;
wonner163 0:4562b55bbb43 1660 } //switch _ctrl
wonner163 0:4562b55bbb43 1661
wonner163 0:4562b55bbb43 1662 break;
wonner163 0:4562b55bbb43 1663
wonner163 0:4562b55bbb43 1664 case BlinkOff:
wonner163 0:4562b55bbb43 1665 // Controllers that support UDC Blink
wonner163 0:4562b55bbb43 1666 switch (_ctrl) {
wonner163 0:4562b55bbb43 1667 case KS0078 :
wonner163 0:4562b55bbb43 1668 _function_1 &= ~0x02; // Disable UDC Blink
wonner163 0:4562b55bbb43 1669 _writeCommand(0x20 | _function_1); // Function set 0 0 1 DL N RE(1) BE 0 (Ext Regs)
wonner163 0:4562b55bbb43 1670
wonner163 0:4562b55bbb43 1671 _writeCommand(0x20 | _function); // Function set 0 0 1 DL N RE(0) DH REV (Std Regs)
wonner163 0:4562b55bbb43 1672 break; // case KS0078 Controller
wonner163 0:4562b55bbb43 1673
wonner163 0:4562b55bbb43 1674 case US2066_3V3 :
wonner163 0:4562b55bbb43 1675 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1676 _function_1 &= ~0x04; // Disable UDC Blink
wonner163 0:4562b55bbb43 1677 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 1678 // Select Ext Instr Set
wonner163 0:4562b55bbb43 1679
wonner163 0:4562b55bbb43 1680 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 1681 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 1682 break; // case SSD1803, US2066
wonner163 0:4562b55bbb43 1683
wonner163 0:4562b55bbb43 1684 default:
wonner163 0:4562b55bbb43 1685 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1686 break;
wonner163 0:4562b55bbb43 1687 } //switch _ctrl
wonner163 0:4562b55bbb43 1688
wonner163 0:4562b55bbb43 1689 break;
wonner163 0:4562b55bbb43 1690
wonner163 0:4562b55bbb43 1691 default:
wonner163 0:4562b55bbb43 1692 break;
wonner163 0:4562b55bbb43 1693 } // blinkMode
wonner163 0:4562b55bbb43 1694
wonner163 0:4562b55bbb43 1695 } // setUDCBlink()
wonner163 0:4562b55bbb43 1696
wonner163 0:4562b55bbb43 1697
wonner163 0:4562b55bbb43 1698 /** Set Contrast
wonner163 0:4562b55bbb43 1699 * setContrast method is supported by some compatible devices (eg ST7032i) that have onboard LCD voltage generation
wonner163 0:4562b55bbb43 1700 * Initial code for ST70XX imported from fork by JH1PJL
wonner163 0:4562b55bbb43 1701 *
wonner163 0:4562b55bbb43 1702 * @param unsigned char c contrast data (6 significant bits, valid range 0..63, Value 0 will disable the Vgen)
wonner163 0:4562b55bbb43 1703 * @return none
wonner163 0:4562b55bbb43 1704 */
wonner163 0:4562b55bbb43 1705 //@TODO Add support for 40x4 dual controller
wonner163 0:4562b55bbb43 1706 void TextLCD_Base::setContrast(unsigned char c) {
wonner163 0:4562b55bbb43 1707
wonner163 0:4562b55bbb43 1708 // Function set mode stored during Init. Make sure we dont accidentally switch between 1-line and 2-line mode!
wonner163 0:4562b55bbb43 1709 // Icon/Booster mode stored during Init. Make sure we dont accidentally change this!
wonner163 0:4562b55bbb43 1710
wonner163 0:4562b55bbb43 1711 _contrast = c & 0x3F; // Sanity check
wonner163 0:4562b55bbb43 1712
wonner163 0:4562b55bbb43 1713 switch (_ctrl) {
wonner163 0:4562b55bbb43 1714 case PCF2113_3V3 :
wonner163 0:4562b55bbb43 1715 case PCF2119_3V3 :
wonner163 0:4562b55bbb43 1716 if (_contrast < 5) _contrast = 0; // See datasheet. Sanity check for PCF2113/PCF2119
wonner163 0:4562b55bbb43 1717 if (_contrast > 55) _contrast = 55;
wonner163 0:4562b55bbb43 1718
wonner163 0:4562b55bbb43 1719 _writeCommand(0x20 | _function | 0x01); // Set function, Select Instruction Set = 1
wonner163 0:4562b55bbb43 1720 _writeCommand(0x80 | 0x00 | (_contrast & 0x3F)); // VLCD_set (Instr. Set 1) V=0, VA=contrast
wonner163 0:4562b55bbb43 1721 _writeCommand(0x80 | 0x40 | (_contrast & 0x3F)); // VLCD_set (Instr. Set 1) V=1, VB=contrast
wonner163 0:4562b55bbb43 1722 _writeCommand(0x20 | _function); // Select Instruction Set = 0
wonner163 0:4562b55bbb43 1723 break;
wonner163 0:4562b55bbb43 1724
wonner163 0:4562b55bbb43 1725 case ST7032_3V3 :
wonner163 0:4562b55bbb43 1726 case ST7032_5V :
wonner163 0:4562b55bbb43 1727 case ST7036_3V3 :
wonner163 0:4562b55bbb43 1728 // case ST7036_5V :
wonner163 0:4562b55bbb43 1729 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1730 _writeCommand(0x20 | _function | 0x01); // Select Instruction Set = 1
wonner163 0:4562b55bbb43 1731 _writeCommand(0x70 | (_contrast & 0x0F)); // Contrast Low bits
wonner163 0:4562b55bbb43 1732 _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Contrast High bits
wonner163 0:4562b55bbb43 1733 _writeCommand(0x20 | _function); // Select Instruction Set = 0
wonner163 0:4562b55bbb43 1734 break;
wonner163 0:4562b55bbb43 1735
wonner163 0:4562b55bbb43 1736 case US2066_3V3 :
wonner163 0:4562b55bbb43 1737 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 DL N BE RE(1) REV
wonner163 0:4562b55bbb43 1738 // Select Extended Instruction Set
wonner163 0:4562b55bbb43 1739
wonner163 0:4562b55bbb43 1740 _writeCommand(0x79); // Function Select OLED: 0 1 1 1 1 0 0 1 (Ext Instr Set)
wonner163 0:4562b55bbb43 1741
wonner163 0:4562b55bbb43 1742 _writeCommand(0x81); // Set Contrast Control: 1 0 0 0 0 0 0 1 (Ext Instr Set, OLED)
wonner163 0:4562b55bbb43 1743 _writeCommand((_contrast << 2) | 0x03); // Set Contrast Value: 8 bits. Use 6 bits for compatibility
wonner163 0:4562b55bbb43 1744
wonner163 0:4562b55bbb43 1745 _writeCommand(0x78); // Function Disable OLED: 0 1 1 1 1 0 0 0 (Ext Instr Set)
wonner163 0:4562b55bbb43 1746
wonner163 0:4562b55bbb43 1747 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 1748 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 1749 break;
wonner163 0:4562b55bbb43 1750
wonner163 0:4562b55bbb43 1751 #if(0)
wonner163 0:4562b55bbb43 1752 //not yet tested
wonner163 0:4562b55bbb43 1753 case PT6314 :
wonner163 0:4562b55bbb43 1754 // Only 2 significant bits
wonner163 0:4562b55bbb43 1755 // 0x00 = 100%
wonner163 0:4562b55bbb43 1756 // 0x01 = 75%
wonner163 0:4562b55bbb43 1757 // 0x02 = 50%
wonner163 0:4562b55bbb43 1758 // 0x03 = 25%
wonner163 0:4562b55bbb43 1759 _writeCommand(0x20 | _function | ((~_contrast) >> 4)); // Invert and shift to use 2 MSBs
wonner163 0:4562b55bbb43 1760 break;
wonner163 0:4562b55bbb43 1761 #endif
wonner163 0:4562b55bbb43 1762
wonner163 0:4562b55bbb43 1763 default:
wonner163 0:4562b55bbb43 1764 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1765 break;
wonner163 0:4562b55bbb43 1766 } // end switch
wonner163 0:4562b55bbb43 1767 } // end setContrast()
wonner163 0:4562b55bbb43 1768
wonner163 0:4562b55bbb43 1769
wonner163 0:4562b55bbb43 1770 /** Set Power
wonner163 0:4562b55bbb43 1771 * setPower method is supported by some compatible devices (eg SSD1803) that have power down modes
wonner163 0:4562b55bbb43 1772 *
wonner163 0:4562b55bbb43 1773 * @param bool powerOn Power on/off
wonner163 0:4562b55bbb43 1774 * @return none
wonner163 0:4562b55bbb43 1775 */
wonner163 0:4562b55bbb43 1776 //@TODO Add support for 40x4 dual controller
wonner163 0:4562b55bbb43 1777 void TextLCD_Base::setPower(bool powerOn) {
wonner163 0:4562b55bbb43 1778
wonner163 0:4562b55bbb43 1779 if (powerOn) {
wonner163 0:4562b55bbb43 1780 // Switch on
wonner163 0:4562b55bbb43 1781 setMode(DispOn);
wonner163 0:4562b55bbb43 1782
wonner163 0:4562b55bbb43 1783 // Controllers that supports specific Power Down mode
wonner163 0:4562b55bbb43 1784 switch (_ctrl) {
wonner163 0:4562b55bbb43 1785
wonner163 0:4562b55bbb43 1786 // case PCF2113_3V3 :
wonner163 0:4562b55bbb43 1787 // case PCF2119_3V3 :
wonner163 0:4562b55bbb43 1788 // case ST7032_3V3 :
wonner163 0:4562b55bbb43 1789 //@todo
wonner163 0:4562b55bbb43 1790 // enable Booster Bon
wonner163 0:4562b55bbb43 1791
wonner163 0:4562b55bbb43 1792 case WS0010:
wonner163 0:4562b55bbb43 1793 _writeCommand(0x17); // Char mode, DC/DC on
wonner163 0:4562b55bbb43 1794 wait_ms(10); // Wait 10ms to ensure powered up
wonner163 0:4562b55bbb43 1795 break;
wonner163 0:4562b55bbb43 1796
wonner163 0:4562b55bbb43 1797 case KS0078:
wonner163 0:4562b55bbb43 1798 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1799 // case SSD1803_5V :
wonner163 0:4562b55bbb43 1800 _writeCommand(0x20 | _function_1); // Select Ext Instr Set
wonner163 0:4562b55bbb43 1801 _writeCommand(0x02); // Power On
wonner163 0:4562b55bbb43 1802 _writeCommand(0x20 | _function); // Select Std Instr Set
wonner163 0:4562b55bbb43 1803 break;
wonner163 0:4562b55bbb43 1804
wonner163 0:4562b55bbb43 1805 default:
wonner163 0:4562b55bbb43 1806 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1807 break;
wonner163 0:4562b55bbb43 1808 } // end switch
wonner163 0:4562b55bbb43 1809 }
wonner163 0:4562b55bbb43 1810 else {
wonner163 0:4562b55bbb43 1811 // Switch off
wonner163 0:4562b55bbb43 1812 setMode(DispOff);
wonner163 0:4562b55bbb43 1813
wonner163 0:4562b55bbb43 1814 // Controllers that support specific Power Down mode
wonner163 0:4562b55bbb43 1815 switch (_ctrl) {
wonner163 0:4562b55bbb43 1816
wonner163 0:4562b55bbb43 1817 // case PCF2113_3V3 :
wonner163 0:4562b55bbb43 1818 // case PCF2119_3V3 :
wonner163 0:4562b55bbb43 1819 // case ST7032_3V3 :
wonner163 0:4562b55bbb43 1820 //@todo
wonner163 0:4562b55bbb43 1821 // disable Booster Bon
wonner163 0:4562b55bbb43 1822
wonner163 0:4562b55bbb43 1823 case WS0010:
wonner163 0:4562b55bbb43 1824 _writeCommand(0x13); // Char mode, DC/DC off
wonner163 0:4562b55bbb43 1825 break;
wonner163 0:4562b55bbb43 1826
wonner163 0:4562b55bbb43 1827 case KS0078:
wonner163 0:4562b55bbb43 1828 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1829 // case SSD1803_5V :
wonner163 0:4562b55bbb43 1830 _writeCommand(0x20 | _function_1); // Select Ext Instr Set
wonner163 0:4562b55bbb43 1831 _writeCommand(0x03); // Power Down
wonner163 0:4562b55bbb43 1832 _writeCommand(0x20 | _function); // Select Std Instr Set
wonner163 0:4562b55bbb43 1833 break;
wonner163 0:4562b55bbb43 1834
wonner163 0:4562b55bbb43 1835 default:
wonner163 0:4562b55bbb43 1836 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1837 break;
wonner163 0:4562b55bbb43 1838 } // end switch
wonner163 0:4562b55bbb43 1839 }
wonner163 0:4562b55bbb43 1840 } // end setPower()
wonner163 0:4562b55bbb43 1841
wonner163 0:4562b55bbb43 1842
wonner163 0:4562b55bbb43 1843 /** Set Orient
wonner163 0:4562b55bbb43 1844 * setOrient method is supported by some compatible devices (eg SSD1803, US2066) that have top/bottom view modes
wonner163 0:4562b55bbb43 1845 *
wonner163 0:4562b55bbb43 1846 * @param LCDOrient orient Orientation
wonner163 0:4562b55bbb43 1847 * @return none
wonner163 0:4562b55bbb43 1848 */
wonner163 0:4562b55bbb43 1849 void TextLCD_Base::setOrient(LCDOrient orient){
wonner163 0:4562b55bbb43 1850
wonner163 0:4562b55bbb43 1851 switch (orient) {
wonner163 0:4562b55bbb43 1852
wonner163 0:4562b55bbb43 1853 case Top:
wonner163 0:4562b55bbb43 1854 switch (_ctrl) {
wonner163 0:4562b55bbb43 1855 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1856 // case SSD1803_5V :
wonner163 0:4562b55bbb43 1857 case US2066_3V3 :
wonner163 0:4562b55bbb43 1858 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 1859 // Select Extended Instruction Set
wonner163 0:4562b55bbb43 1860 // _writeCommand(0x06); // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1 "Bottom View" (Ext Instr Set)
wonner163 0:4562b55bbb43 1861 _writeCommand(0x05); // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100 "Top View" (Ext Instr Set)
wonner163 0:4562b55bbb43 1862
wonner163 0:4562b55bbb43 1863 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 1864 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 1865 break;
wonner163 0:4562b55bbb43 1866
wonner163 0:4562b55bbb43 1867 default:
wonner163 0:4562b55bbb43 1868 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1869 break;
wonner163 0:4562b55bbb43 1870
wonner163 0:4562b55bbb43 1871 } // end switch _ctrl
wonner163 0:4562b55bbb43 1872 break; // end Top
wonner163 0:4562b55bbb43 1873
wonner163 0:4562b55bbb43 1874 case Bottom:
wonner163 0:4562b55bbb43 1875 switch (_ctrl) {
wonner163 0:4562b55bbb43 1876 case SSD1803_3V3 :
wonner163 0:4562b55bbb43 1877 // case SSD1803_5V :
wonner163 0:4562b55bbb43 1878 case US2066_3V3 :
wonner163 0:4562b55bbb43 1879 _writeCommand(0x20 | _function_1); // Set function, 0 0 1 X N BE RE(1) REV
wonner163 0:4562b55bbb43 1880 // Select Extended Instruction Set
wonner163 0:4562b55bbb43 1881 _writeCommand(0x06); // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1 "Bottom View" (Ext Instr Set)
wonner163 0:4562b55bbb43 1882 // _writeCommand(0x05); // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100 "Top View" (Ext Instr Set)
wonner163 0:4562b55bbb43 1883
wonner163 0:4562b55bbb43 1884 _writeCommand(0x20 | _function); // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
wonner163 0:4562b55bbb43 1885 // Select Std Instr set, Select IS=0
wonner163 0:4562b55bbb43 1886 break;
wonner163 0:4562b55bbb43 1887
wonner163 0:4562b55bbb43 1888 default:
wonner163 0:4562b55bbb43 1889 //Unsupported feature for other controllers
wonner163 0:4562b55bbb43 1890 break;
wonner163 0:4562b55bbb43 1891
wonner163 0:4562b55bbb43 1892 } // end switch _ctrl
wonner163 0:4562b55bbb43 1893
wonner163 0:4562b55bbb43 1894 break; // end Bottom
wonner163 0:4562b55bbb43 1895 } // end switch orient
wonner163 0:4562b55bbb43 1896 } // end setOrient()
wonner163 0:4562b55bbb43 1897
wonner163 0:4562b55bbb43 1898
wonner163 0:4562b55bbb43 1899
wonner163 0:4562b55bbb43 1900 //--------- End TextLCD_Base -----------
wonner163 0:4562b55bbb43 1901
wonner163 0:4562b55bbb43 1902
wonner163 0:4562b55bbb43 1903 //--------- Start TextLCD Bus -----------
wonner163 0:4562b55bbb43 1904
wonner163 0:4562b55bbb43 1905 /* Create a TextLCD interface for using regular mbed pins
wonner163 0:4562b55bbb43 1906 *
wonner163 0:4562b55bbb43 1907 * @param rs Instruction/data control line
wonner163 0:4562b55bbb43 1908 * @param e Enable line (clock)
wonner163 0:4562b55bbb43 1909 * @param d4-d7 Data lines for using as a 4-bit interface
wonner163 0:4562b55bbb43 1910 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 1911 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 1912 * @param e2 Enable2 line (clock for second controller, LCD40x4 only)
wonner163 0:4562b55bbb43 1913 * @param ctrl LCD controller (default = HD44780)
wonner163 0:4562b55bbb43 1914 */
wonner163 0:4562b55bbb43 1915 TextLCD::TextLCD(PinName rs, PinName e,
wonner163 0:4562b55bbb43 1916 PinName d4, PinName d5, PinName d6, PinName d7,
wonner163 0:4562b55bbb43 1917 LCDType type, PinName bl, PinName e2, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 1918 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 1919 _rs(rs), _e(e), _d(d4, d5, d6, d7) {
wonner163 0:4562b55bbb43 1920
wonner163 0:4562b55bbb43 1921 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 1922 if (bl != NC) {
wonner163 0:4562b55bbb43 1923 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 1924 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 1925 }
wonner163 0:4562b55bbb43 1926 else {
wonner163 0:4562b55bbb43 1927 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 1928 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 1929 }
wonner163 0:4562b55bbb43 1930
wonner163 0:4562b55bbb43 1931 // The hardware Enable2 pin is only needed for LCD40x4. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 1932 if (e2 != NC) {
wonner163 0:4562b55bbb43 1933 _e2 = new DigitalOut(e2); //Construct new pin
wonner163 0:4562b55bbb43 1934 _e2->write(0); //Deactivate
wonner163 0:4562b55bbb43 1935 }
wonner163 0:4562b55bbb43 1936 else {
wonner163 0:4562b55bbb43 1937 // No Hardware Enable pin
wonner163 0:4562b55bbb43 1938 _e2 = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 1939 }
wonner163 0:4562b55bbb43 1940
wonner163 0:4562b55bbb43 1941 _init();
wonner163 0:4562b55bbb43 1942 }
wonner163 0:4562b55bbb43 1943
wonner163 0:4562b55bbb43 1944 /** Destruct a TextLCD interface for using regular mbed pins
wonner163 0:4562b55bbb43 1945 *
wonner163 0:4562b55bbb43 1946 * @param none
wonner163 0:4562b55bbb43 1947 * @return none
wonner163 0:4562b55bbb43 1948 */
wonner163 0:4562b55bbb43 1949 TextLCD::~TextLCD() {
wonner163 0:4562b55bbb43 1950 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 1951 if (_e2 != NULL) {delete _e2;} // E2 pin
wonner163 0:4562b55bbb43 1952 }
wonner163 0:4562b55bbb43 1953
wonner163 0:4562b55bbb43 1954
wonner163 0:4562b55bbb43 1955 /** Set E pin (or E2 pin)
wonner163 0:4562b55bbb43 1956 * Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 1957 * Default PinName value for E2 is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
wonner163 0:4562b55bbb43 1958 * @param value true or false
wonner163 0:4562b55bbb43 1959 * @return none
wonner163 0:4562b55bbb43 1960 */
wonner163 0:4562b55bbb43 1961 void TextLCD::_setEnable(bool value) {
wonner163 0:4562b55bbb43 1962
wonner163 0:4562b55bbb43 1963 if(_ctrl_idx==_LCDCtrl_0) {
wonner163 0:4562b55bbb43 1964 if (value) {
wonner163 0:4562b55bbb43 1965 _e = 1; // Set E bit
wonner163 0:4562b55bbb43 1966 }
wonner163 0:4562b55bbb43 1967 else {
wonner163 0:4562b55bbb43 1968 _e = 0; // Reset E bit
wonner163 0:4562b55bbb43 1969 }
wonner163 0:4562b55bbb43 1970 }
wonner163 0:4562b55bbb43 1971 else {
wonner163 0:4562b55bbb43 1972 if (value) {
wonner163 0:4562b55bbb43 1973 if (_e2 != NULL) {_e2->write(1);} //Set E2 bit
wonner163 0:4562b55bbb43 1974 }
wonner163 0:4562b55bbb43 1975 else {
wonner163 0:4562b55bbb43 1976 if (_e2 != NULL) {_e2->write(0);} //Reset E2 bit
wonner163 0:4562b55bbb43 1977 }
wonner163 0:4562b55bbb43 1978 }
wonner163 0:4562b55bbb43 1979 }
wonner163 0:4562b55bbb43 1980
wonner163 0:4562b55bbb43 1981 // Set RS pin
wonner163 0:4562b55bbb43 1982 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 1983 void TextLCD::_setRS(bool value) {
wonner163 0:4562b55bbb43 1984
wonner163 0:4562b55bbb43 1985 if (value) {
wonner163 0:4562b55bbb43 1986 _rs = 1; // Set RS bit
wonner163 0:4562b55bbb43 1987 }
wonner163 0:4562b55bbb43 1988 else {
wonner163 0:4562b55bbb43 1989 _rs = 0; // Reset RS bit
wonner163 0:4562b55bbb43 1990 }
wonner163 0:4562b55bbb43 1991 }
wonner163 0:4562b55bbb43 1992
wonner163 0:4562b55bbb43 1993 /** Set BL pin
wonner163 0:4562b55bbb43 1994 * Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 1995 * Default PinName value is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
wonner163 0:4562b55bbb43 1996 * @param value true or false
wonner163 0:4562b55bbb43 1997 * @return none
wonner163 0:4562b55bbb43 1998 */
wonner163 0:4562b55bbb43 1999 void TextLCD::_setBL(bool value) {
wonner163 0:4562b55bbb43 2000
wonner163 0:4562b55bbb43 2001 if (value) {
wonner163 0:4562b55bbb43 2002 if (_bl != NULL) {_bl->write(1);} //Set BL bit
wonner163 0:4562b55bbb43 2003 }
wonner163 0:4562b55bbb43 2004 else {
wonner163 0:4562b55bbb43 2005 if (_bl != NULL) {_bl->write(0);} //Reset BL bit
wonner163 0:4562b55bbb43 2006 }
wonner163 0:4562b55bbb43 2007 }
wonner163 0:4562b55bbb43 2008
wonner163 0:4562b55bbb43 2009 // Place the 4bit data on the databus
wonner163 0:4562b55bbb43 2010 // Used for mbed pins, I2C bus expander or SPI shifregister
wonner163 0:4562b55bbb43 2011 void TextLCD::_setData(int value) {
wonner163 0:4562b55bbb43 2012 _d = value & 0x0F; // Write Databits
wonner163 0:4562b55bbb43 2013 }
wonner163 0:4562b55bbb43 2014
wonner163 0:4562b55bbb43 2015 //----------- End TextLCD ---------------
wonner163 0:4562b55bbb43 2016
wonner163 0:4562b55bbb43 2017
wonner163 0:4562b55bbb43 2018 //--------- Start TextLCD_I2C -----------
wonner163 0:4562b55bbb43 2019
wonner163 0:4562b55bbb43 2020 /** Create a TextLCD interface using an I2C PC8574 (or PCF8574A) or MCP23008 portexpander
wonner163 0:4562b55bbb43 2021 *
wonner163 0:4562b55bbb43 2022 * @param i2c I2C Bus
wonner163 0:4562b55bbb43 2023 * @param deviceAddress I2C slave address (PCF8574, PCF8574A or MCP23008, default = 0x40)
wonner163 0:4562b55bbb43 2024 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2025 * @param ctrl LCD controller (default = HD44780)
wonner163 0:4562b55bbb43 2026 */
wonner163 0:4562b55bbb43 2027 TextLCD_I2C::TextLCD_I2C(I2C *i2c, char deviceAddress, LCDType type, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2028 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2029 _i2c(i2c){
wonner163 0:4562b55bbb43 2030
wonner163 0:4562b55bbb43 2031 _slaveAddress = deviceAddress & 0xFE;
wonner163 0:4562b55bbb43 2032
wonner163 0:4562b55bbb43 2033 // Setup the I2C bus
wonner163 0:4562b55bbb43 2034 // The max bitrate for PCF8574 is 100kbit, the max bitrate for MCP23008 is 400kbit,
wonner163 0:4562b55bbb43 2035 _i2c->frequency(100000);
wonner163 0:4562b55bbb43 2036
wonner163 0:4562b55bbb43 2037 #if (MCP23008==1)
wonner163 0:4562b55bbb43 2038 // MCP23008 portexpander Init
wonner163 0:4562b55bbb43 2039 _write_register(IODIR, 0x00); // All outputs
wonner163 0:4562b55bbb43 2040 _write_register(IPOL, 0x00); // No reverse polarity
wonner163 0:4562b55bbb43 2041 _write_register(GPINTEN, 0x00); // No interrupt
wonner163 0:4562b55bbb43 2042 _write_register(DEFVAL, 0x00); // Default value to compare against for interrupts
wonner163 0:4562b55bbb43 2043 _write_register(INTCON, 0x00); // No interrupt on changes
wonner163 0:4562b55bbb43 2044 _write_register(IOCON, 0x00); // Interrupt polarity
wonner163 0:4562b55bbb43 2045 _write_register(GPPU, 0x00); // No Pullup
wonner163 0:4562b55bbb43 2046 _write_register(INTF, 0x00); //
wonner163 0:4562b55bbb43 2047 _write_register(INTCAP, 0x00); //
wonner163 0:4562b55bbb43 2048 _write_register(GPIO, 0x00); // Output/Input pins
wonner163 0:4562b55bbb43 2049 _write_register(OLAT, 0x00); // Output Latch
wonner163 0:4562b55bbb43 2050
wonner163 0:4562b55bbb43 2051 // Init the portexpander bus
wonner163 0:4562b55bbb43 2052 _lcd_bus = D_LCD_BUS_DEF;
wonner163 0:4562b55bbb43 2053
wonner163 0:4562b55bbb43 2054 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2055 _write_register(GPIO, _lcd_bus);
wonner163 0:4562b55bbb43 2056 #else
wonner163 0:4562b55bbb43 2057 // PCF8574 of PCF8574A portexpander
wonner163 0:4562b55bbb43 2058
wonner163 0:4562b55bbb43 2059 // Init the portexpander bus
wonner163 0:4562b55bbb43 2060 _lcd_bus = D_LCD_BUS_DEF;
wonner163 0:4562b55bbb43 2061
wonner163 0:4562b55bbb43 2062 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2063 _i2c->write(_slaveAddress, &_lcd_bus, 1);
wonner163 0:4562b55bbb43 2064 #endif
wonner163 0:4562b55bbb43 2065
wonner163 0:4562b55bbb43 2066 _init();
wonner163 0:4562b55bbb43 2067 }
wonner163 0:4562b55bbb43 2068
wonner163 0:4562b55bbb43 2069 // Set E pin (or E2 pin)
wonner163 0:4562b55bbb43 2070 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2071 void TextLCD_I2C::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2072
wonner163 0:4562b55bbb43 2073 if(_ctrl_idx==_LCDCtrl_0) {
wonner163 0:4562b55bbb43 2074 if (value) {
wonner163 0:4562b55bbb43 2075 _lcd_bus |= D_LCD_E; // Set E bit
wonner163 0:4562b55bbb43 2076 }
wonner163 0:4562b55bbb43 2077 else {
wonner163 0:4562b55bbb43 2078 _lcd_bus &= ~D_LCD_E; // Reset E bit
wonner163 0:4562b55bbb43 2079 }
wonner163 0:4562b55bbb43 2080 }
wonner163 0:4562b55bbb43 2081 else {
wonner163 0:4562b55bbb43 2082 if (value) {
wonner163 0:4562b55bbb43 2083 _lcd_bus |= D_LCD_E2; // Set E2 bit
wonner163 0:4562b55bbb43 2084 }
wonner163 0:4562b55bbb43 2085 else {
wonner163 0:4562b55bbb43 2086 _lcd_bus &= ~D_LCD_E2; // Reset E2bit
wonner163 0:4562b55bbb43 2087 }
wonner163 0:4562b55bbb43 2088 }
wonner163 0:4562b55bbb43 2089
wonner163 0:4562b55bbb43 2090 #if (MCP23008==1)
wonner163 0:4562b55bbb43 2091 // MCP23008 portexpander
wonner163 0:4562b55bbb43 2092
wonner163 0:4562b55bbb43 2093 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2094 _write_register(GPIO, _lcd_bus);
wonner163 0:4562b55bbb43 2095 #else
wonner163 0:4562b55bbb43 2096 // PCF8574 of PCF8574A portexpander
wonner163 0:4562b55bbb43 2097
wonner163 0:4562b55bbb43 2098 // write the new data to the I2C portexpander
wonner163 0:4562b55bbb43 2099 _i2c->write(_slaveAddress, &_lcd_bus, 1);
wonner163 0:4562b55bbb43 2100 #endif
wonner163 0:4562b55bbb43 2101 }
wonner163 0:4562b55bbb43 2102
wonner163 0:4562b55bbb43 2103 // Set RS pin
wonner163 0:4562b55bbb43 2104 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2105 void TextLCD_I2C::_setRS(bool value) {
wonner163 0:4562b55bbb43 2106
wonner163 0:4562b55bbb43 2107 if (value) {
wonner163 0:4562b55bbb43 2108 _lcd_bus |= D_LCD_RS; // Set RS bit
wonner163 0:4562b55bbb43 2109 }
wonner163 0:4562b55bbb43 2110 else {
wonner163 0:4562b55bbb43 2111 _lcd_bus &= ~D_LCD_RS; // Reset RS bit
wonner163 0:4562b55bbb43 2112 }
wonner163 0:4562b55bbb43 2113
wonner163 0:4562b55bbb43 2114 #if (MCP23008==1)
wonner163 0:4562b55bbb43 2115 // MCP23008 portexpander
wonner163 0:4562b55bbb43 2116
wonner163 0:4562b55bbb43 2117 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2118 _write_register(GPIO, _lcd_bus);
wonner163 0:4562b55bbb43 2119 #else
wonner163 0:4562b55bbb43 2120 // PCF8574 of PCF8574A portexpander
wonner163 0:4562b55bbb43 2121
wonner163 0:4562b55bbb43 2122 // write the new data to the I2C portexpander
wonner163 0:4562b55bbb43 2123 _i2c->write(_slaveAddress, &_lcd_bus, 1);
wonner163 0:4562b55bbb43 2124 #endif
wonner163 0:4562b55bbb43 2125 }
wonner163 0:4562b55bbb43 2126
wonner163 0:4562b55bbb43 2127 // Set BL pin
wonner163 0:4562b55bbb43 2128 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2129 void TextLCD_I2C::_setBL(bool value) {
wonner163 0:4562b55bbb43 2130
wonner163 0:4562b55bbb43 2131 #if (DFROBOT==1)
wonner163 0:4562b55bbb43 2132 value = !value; // The DFRobot module uses PNP transistor to drive the Backlight. Reverse logic level.
wonner163 0:4562b55bbb43 2133 #endif
wonner163 0:4562b55bbb43 2134
wonner163 0:4562b55bbb43 2135 if (value) {
wonner163 0:4562b55bbb43 2136 _lcd_bus |= D_LCD_BL; // Set BL bit
wonner163 0:4562b55bbb43 2137 }
wonner163 0:4562b55bbb43 2138 else {
wonner163 0:4562b55bbb43 2139 _lcd_bus &= ~D_LCD_BL; // Reset BL bit
wonner163 0:4562b55bbb43 2140 }
wonner163 0:4562b55bbb43 2141
wonner163 0:4562b55bbb43 2142 #if (MCP23008==1)
wonner163 0:4562b55bbb43 2143 // MCP23008 portexpander
wonner163 0:4562b55bbb43 2144
wonner163 0:4562b55bbb43 2145 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2146 _write_register(GPIO, _lcd_bus);
wonner163 0:4562b55bbb43 2147 #else
wonner163 0:4562b55bbb43 2148 // PCF8574 of PCF8574A portexpander
wonner163 0:4562b55bbb43 2149
wonner163 0:4562b55bbb43 2150 // write the new data to the I2C portexpander
wonner163 0:4562b55bbb43 2151 _i2c->write(_slaveAddress, &_lcd_bus, 1);
wonner163 0:4562b55bbb43 2152 #endif
wonner163 0:4562b55bbb43 2153 }
wonner163 0:4562b55bbb43 2154
wonner163 0:4562b55bbb43 2155
wonner163 0:4562b55bbb43 2156 // Place the 4bit data on the databus
wonner163 0:4562b55bbb43 2157 // Used for mbed pins, I2C bus expander or SPI shifregister
wonner163 0:4562b55bbb43 2158 void TextLCD_I2C::_setData(int value) {
wonner163 0:4562b55bbb43 2159 int data;
wonner163 0:4562b55bbb43 2160
wonner163 0:4562b55bbb43 2161 // Set bit by bit to support any mapping of expander portpins to LCD pins
wonner163 0:4562b55bbb43 2162
wonner163 0:4562b55bbb43 2163 data = value & 0x0F;
wonner163 0:4562b55bbb43 2164 if (data & 0x01){
wonner163 0:4562b55bbb43 2165 _lcd_bus |= D_LCD_D4; // Set Databit
wonner163 0:4562b55bbb43 2166 }
wonner163 0:4562b55bbb43 2167 else {
wonner163 0:4562b55bbb43 2168 _lcd_bus &= ~D_LCD_D4; // Reset Databit
wonner163 0:4562b55bbb43 2169 }
wonner163 0:4562b55bbb43 2170
wonner163 0:4562b55bbb43 2171 if (data & 0x02){
wonner163 0:4562b55bbb43 2172 _lcd_bus |= D_LCD_D5; // Set Databit
wonner163 0:4562b55bbb43 2173 }
wonner163 0:4562b55bbb43 2174 else {
wonner163 0:4562b55bbb43 2175 _lcd_bus &= ~D_LCD_D5; // Reset Databit
wonner163 0:4562b55bbb43 2176 }
wonner163 0:4562b55bbb43 2177
wonner163 0:4562b55bbb43 2178 if (data & 0x04) {
wonner163 0:4562b55bbb43 2179 _lcd_bus |= D_LCD_D6; // Set Databit
wonner163 0:4562b55bbb43 2180 }
wonner163 0:4562b55bbb43 2181 else {
wonner163 0:4562b55bbb43 2182 _lcd_bus &= ~D_LCD_D6; // Reset Databit
wonner163 0:4562b55bbb43 2183 }
wonner163 0:4562b55bbb43 2184
wonner163 0:4562b55bbb43 2185 if (data & 0x08) {
wonner163 0:4562b55bbb43 2186 _lcd_bus |= D_LCD_D7; // Set Databit
wonner163 0:4562b55bbb43 2187 }
wonner163 0:4562b55bbb43 2188 else {
wonner163 0:4562b55bbb43 2189 _lcd_bus &= ~D_LCD_D7; // Reset Databit
wonner163 0:4562b55bbb43 2190 }
wonner163 0:4562b55bbb43 2191
wonner163 0:4562b55bbb43 2192 #if (MCP23008==1)
wonner163 0:4562b55bbb43 2193 // MCP23008 portexpander
wonner163 0:4562b55bbb43 2194
wonner163 0:4562b55bbb43 2195 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2196 _write_register(GPIO, _lcd_bus);
wonner163 0:4562b55bbb43 2197 #else
wonner163 0:4562b55bbb43 2198 // PCF8574 of PCF8574A portexpander
wonner163 0:4562b55bbb43 2199
wonner163 0:4562b55bbb43 2200 // write the new data to the I2C portexpander
wonner163 0:4562b55bbb43 2201 _i2c->write(_slaveAddress, &_lcd_bus, 1);
wonner163 0:4562b55bbb43 2202 #endif
wonner163 0:4562b55bbb43 2203
wonner163 0:4562b55bbb43 2204 }
wonner163 0:4562b55bbb43 2205
wonner163 0:4562b55bbb43 2206 // Write data to MCP23008 I2C portexpander
wonner163 0:4562b55bbb43 2207 void TextLCD_I2C::_write_register (int reg, int value) {
wonner163 0:4562b55bbb43 2208 char data[] = {reg, value};
wonner163 0:4562b55bbb43 2209
wonner163 0:4562b55bbb43 2210 _i2c->write(_slaveAddress, data, 2);
wonner163 0:4562b55bbb43 2211 }
wonner163 0:4562b55bbb43 2212
wonner163 0:4562b55bbb43 2213 //---------- End TextLCD_I2C ------------
wonner163 0:4562b55bbb43 2214
wonner163 0:4562b55bbb43 2215
wonner163 0:4562b55bbb43 2216 //--------- Start TextLCD_I2C_N ---------
wonner163 0:4562b55bbb43 2217
wonner163 0:4562b55bbb43 2218 /** Create a TextLCD interface using a controller with native I2C interface
wonner163 0:4562b55bbb43 2219 *
wonner163 0:4562b55bbb43 2220 * @param i2c I2C Bus
wonner163 0:4562b55bbb43 2221 * @param deviceAddress I2C slave address (default = 0x7C)
wonner163 0:4562b55bbb43 2222 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2223 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 2224 * @param ctrl LCD controller (default = ST7032_3V3)
wonner163 0:4562b55bbb43 2225 */
wonner163 0:4562b55bbb43 2226 TextLCD_I2C_N::TextLCD_I2C_N(I2C *i2c, char deviceAddress, LCDType type, PinName bl, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2227 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2228
wonner163 0:4562b55bbb43 2229 _i2c(i2c){
wonner163 0:4562b55bbb43 2230
wonner163 0:4562b55bbb43 2231 _slaveAddress = deviceAddress & 0xFE;
wonner163 0:4562b55bbb43 2232
wonner163 0:4562b55bbb43 2233 // Setup the I2C bus
wonner163 0:4562b55bbb43 2234 // The max bitrate for ST7032i is 400kbit, lets stick to default here
wonner163 0:4562b55bbb43 2235 _i2c->frequency(100000);
wonner163 0:4562b55bbb43 2236
wonner163 0:4562b55bbb43 2237
wonner163 0:4562b55bbb43 2238 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 2239 if (bl != NC) {
wonner163 0:4562b55bbb43 2240 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 2241 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 2242 }
wonner163 0:4562b55bbb43 2243 else {
wonner163 0:4562b55bbb43 2244 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 2245 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 2246 }
wonner163 0:4562b55bbb43 2247
wonner163 0:4562b55bbb43 2248 //Sanity check
wonner163 0:4562b55bbb43 2249 if (_ctrl & LCD_C_I2C) {
wonner163 0:4562b55bbb43 2250 _init();
wonner163 0:4562b55bbb43 2251 }
wonner163 0:4562b55bbb43 2252 else {
wonner163 0:4562b55bbb43 2253 error("Error: LCD Controller type does not support native I2C interface\n\r");
wonner163 0:4562b55bbb43 2254 }
wonner163 0:4562b55bbb43 2255
wonner163 0:4562b55bbb43 2256 }
wonner163 0:4562b55bbb43 2257
wonner163 0:4562b55bbb43 2258 TextLCD_I2C_N::~TextLCD_I2C_N() {
wonner163 0:4562b55bbb43 2259 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 2260 }
wonner163 0:4562b55bbb43 2261
wonner163 0:4562b55bbb43 2262 // Not used in this mode
wonner163 0:4562b55bbb43 2263 void TextLCD_I2C_N::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2264 }
wonner163 0:4562b55bbb43 2265
wonner163 0:4562b55bbb43 2266 // Set RS pin
wonner163 0:4562b55bbb43 2267 // Used for mbed pins, I2C bus expander or SPI shiftregister and native I2C or SPI
wonner163 0:4562b55bbb43 2268 void TextLCD_I2C_N::_setRS(bool value) {
wonner163 0:4562b55bbb43 2269 // The controlbyte defines the meaning of the next byte. This next byte can either be data or command.
wonner163 0:4562b55bbb43 2270 // Start Slaveaddress+RW b7 b6 b5 b4 b3 b2 b1 b0 b7...........b0 Stop
wonner163 0:4562b55bbb43 2271 // Co RS RW 0 0 0 0 0 command or data
wonner163 0:4562b55bbb43 2272 //
wonner163 0:4562b55bbb43 2273 // C0=1 indicates that another controlbyte will follow after the next data or command byte
wonner163 0:4562b55bbb43 2274 // RS=1 means that next byte is data, RS=0 means that next byte is command
wonner163 0:4562b55bbb43 2275 // RW=0 means write to controller. RW=1 means that controller will be read from after the next command.
wonner163 0:4562b55bbb43 2276 // Many native I2C controllers dont support this option and it is not used by this lib.
wonner163 0:4562b55bbb43 2277 //
wonner163 0:4562b55bbb43 2278
wonner163 0:4562b55bbb43 2279 if (value) {
wonner163 0:4562b55bbb43 2280 _controlbyte = 0x40; // Next byte is data, No more control bytes will follow
wonner163 0:4562b55bbb43 2281 }
wonner163 0:4562b55bbb43 2282 else {
wonner163 0:4562b55bbb43 2283 _controlbyte = 0x00; // Next byte is command, No more control bytes will follow
wonner163 0:4562b55bbb43 2284 }
wonner163 0:4562b55bbb43 2285 }
wonner163 0:4562b55bbb43 2286
wonner163 0:4562b55bbb43 2287 // Set BL pin
wonner163 0:4562b55bbb43 2288 void TextLCD_I2C_N::_setBL(bool value) {
wonner163 0:4562b55bbb43 2289 if (_bl) {
wonner163 0:4562b55bbb43 2290 _bl->write(value);
wonner163 0:4562b55bbb43 2291 }
wonner163 0:4562b55bbb43 2292 }
wonner163 0:4562b55bbb43 2293
wonner163 0:4562b55bbb43 2294 // Not used in this mode
wonner163 0:4562b55bbb43 2295 void TextLCD_I2C_N::_setData(int value) {
wonner163 0:4562b55bbb43 2296 }
wonner163 0:4562b55bbb43 2297
wonner163 0:4562b55bbb43 2298 // Write a byte using I2C
wonner163 0:4562b55bbb43 2299 void TextLCD_I2C_N::_writeByte(int value) {
wonner163 0:4562b55bbb43 2300 // The controlbyte defines the meaning of the next byte. This next byte can either be data or command.
wonner163 0:4562b55bbb43 2301 // Start Slaveaddress+RW b7 b6 b5 b4 b3 b2 b1 b0 b7...........b0 Stop
wonner163 0:4562b55bbb43 2302 // Co RS RW 0 0 0 0 0 command or data
wonner163 0:4562b55bbb43 2303 //
wonner163 0:4562b55bbb43 2304 // C0=1 indicates that another controlbyte will follow after the next data or command byte
wonner163 0:4562b55bbb43 2305 // RS=1 means that next byte is data, RS=0 means that next byte is command
wonner163 0:4562b55bbb43 2306 // RW=0 means write to controller. RW=1 means that controller will be read from after the next command.
wonner163 0:4562b55bbb43 2307 // Many native I2C controllers dont support this option and it is not used by this lib.
wonner163 0:4562b55bbb43 2308 //
wonner163 0:4562b55bbb43 2309 char data[] = {_controlbyte, value};
wonner163 0:4562b55bbb43 2310
wonner163 0:4562b55bbb43 2311 #if(LCD_I2C_ACK==1)
wonner163 0:4562b55bbb43 2312 //Controllers that support ACK
wonner163 0:4562b55bbb43 2313 _i2c->write(_slaveAddress, data, 2);
wonner163 0:4562b55bbb43 2314 #else
wonner163 0:4562b55bbb43 2315 //Controllers that dont support ACK
wonner163 0:4562b55bbb43 2316 _i2c->start();
wonner163 0:4562b55bbb43 2317 _i2c->write(_slaveAddress);
wonner163 0:4562b55bbb43 2318 _i2c->write(data[0]);
wonner163 0:4562b55bbb43 2319 _i2c->write(data[1]);
wonner163 0:4562b55bbb43 2320 _i2c->stop();
wonner163 0:4562b55bbb43 2321 #endif
wonner163 0:4562b55bbb43 2322 }
wonner163 0:4562b55bbb43 2323
wonner163 0:4562b55bbb43 2324 //-------- End TextLCD_I2C_N ------------
wonner163 0:4562b55bbb43 2325
wonner163 0:4562b55bbb43 2326
wonner163 0:4562b55bbb43 2327 //--------- Start TextLCD_SPI -----------
wonner163 0:4562b55bbb43 2328
wonner163 0:4562b55bbb43 2329 /** Create a TextLCD interface using an SPI 74595 portexpander
wonner163 0:4562b55bbb43 2330 *
wonner163 0:4562b55bbb43 2331 * @param spi SPI Bus
wonner163 0:4562b55bbb43 2332 * @param cs chip select pin (active low)
wonner163 0:4562b55bbb43 2333 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2334 * @param ctrl LCD controller (default = HD44780)
wonner163 0:4562b55bbb43 2335 */
wonner163 0:4562b55bbb43 2336 TextLCD_SPI::TextLCD_SPI(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2337 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2338 _spi(spi),
wonner163 0:4562b55bbb43 2339 _cs(cs) {
wonner163 0:4562b55bbb43 2340
wonner163 0:4562b55bbb43 2341 // Init cs
wonner163 0:4562b55bbb43 2342 _setCS(true);
wonner163 0:4562b55bbb43 2343
wonner163 0:4562b55bbb43 2344 // Setup the spi for 8 bit data, low steady state clock,
wonner163 0:4562b55bbb43 2345 // rising edge capture, with a 500KHz or 1MHz clock rate
wonner163 0:4562b55bbb43 2346 _spi->format(8,0);
wonner163 0:4562b55bbb43 2347 _spi->frequency(500000);
wonner163 0:4562b55bbb43 2348 //_spi.frequency(1000000);
wonner163 0:4562b55bbb43 2349
wonner163 0:4562b55bbb43 2350 // Init the portexpander bus
wonner163 0:4562b55bbb43 2351 _lcd_bus = D_LCD_BUS_DEF;
wonner163 0:4562b55bbb43 2352
wonner163 0:4562b55bbb43 2353 // write the new data to the portexpander
wonner163 0:4562b55bbb43 2354 _setCS(false);
wonner163 0:4562b55bbb43 2355 _spi->write(_lcd_bus);
wonner163 0:4562b55bbb43 2356 _setCS(true);
wonner163 0:4562b55bbb43 2357
wonner163 0:4562b55bbb43 2358 _init();
wonner163 0:4562b55bbb43 2359 }
wonner163 0:4562b55bbb43 2360
wonner163 0:4562b55bbb43 2361 // Set E pin (or E2 pin)
wonner163 0:4562b55bbb43 2362 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2363 void TextLCD_SPI::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2364
wonner163 0:4562b55bbb43 2365 if(_ctrl_idx==_LCDCtrl_0) {
wonner163 0:4562b55bbb43 2366 if (value) {
wonner163 0:4562b55bbb43 2367 _lcd_bus |= D_LCD_E; // Set E bit
wonner163 0:4562b55bbb43 2368 }
wonner163 0:4562b55bbb43 2369 else {
wonner163 0:4562b55bbb43 2370 _lcd_bus &= ~D_LCD_E; // Reset E bit
wonner163 0:4562b55bbb43 2371 }
wonner163 0:4562b55bbb43 2372 }
wonner163 0:4562b55bbb43 2373 else {
wonner163 0:4562b55bbb43 2374 if (value) {
wonner163 0:4562b55bbb43 2375 _lcd_bus |= D_LCD_E2; // Set E2 bit
wonner163 0:4562b55bbb43 2376 }
wonner163 0:4562b55bbb43 2377 else {
wonner163 0:4562b55bbb43 2378 _lcd_bus &= ~D_LCD_E2; // Reset E2 bit
wonner163 0:4562b55bbb43 2379 }
wonner163 0:4562b55bbb43 2380 }
wonner163 0:4562b55bbb43 2381
wonner163 0:4562b55bbb43 2382 // write the new data to the SPI portexpander
wonner163 0:4562b55bbb43 2383 _setCS(false);
wonner163 0:4562b55bbb43 2384 _spi->write(_lcd_bus);
wonner163 0:4562b55bbb43 2385 _setCS(true);
wonner163 0:4562b55bbb43 2386 }
wonner163 0:4562b55bbb43 2387
wonner163 0:4562b55bbb43 2388 // Set RS pin
wonner163 0:4562b55bbb43 2389 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2390 void TextLCD_SPI::_setRS(bool value) {
wonner163 0:4562b55bbb43 2391
wonner163 0:4562b55bbb43 2392 if (value) {
wonner163 0:4562b55bbb43 2393 _lcd_bus |= D_LCD_RS; // Set RS bit
wonner163 0:4562b55bbb43 2394 }
wonner163 0:4562b55bbb43 2395 else {
wonner163 0:4562b55bbb43 2396 _lcd_bus &= ~D_LCD_RS; // Reset RS bit
wonner163 0:4562b55bbb43 2397 }
wonner163 0:4562b55bbb43 2398
wonner163 0:4562b55bbb43 2399 // write the new data to the SPI portexpander
wonner163 0:4562b55bbb43 2400 _setCS(false);
wonner163 0:4562b55bbb43 2401 _spi->write(_lcd_bus);
wonner163 0:4562b55bbb43 2402 _setCS(true);
wonner163 0:4562b55bbb43 2403 }
wonner163 0:4562b55bbb43 2404
wonner163 0:4562b55bbb43 2405 // Set BL pin
wonner163 0:4562b55bbb43 2406 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2407 void TextLCD_SPI::_setBL(bool value) {
wonner163 0:4562b55bbb43 2408
wonner163 0:4562b55bbb43 2409 if (value) {
wonner163 0:4562b55bbb43 2410 _lcd_bus |= D_LCD_BL; // Set BL bit
wonner163 0:4562b55bbb43 2411 }
wonner163 0:4562b55bbb43 2412 else {
wonner163 0:4562b55bbb43 2413 _lcd_bus &= ~D_LCD_BL; // Reset BL bit
wonner163 0:4562b55bbb43 2414 }
wonner163 0:4562b55bbb43 2415
wonner163 0:4562b55bbb43 2416 // write the new data to the SPI portexpander
wonner163 0:4562b55bbb43 2417 _setCS(false);
wonner163 0:4562b55bbb43 2418 _spi->write(_lcd_bus);
wonner163 0:4562b55bbb43 2419 _setCS(true);
wonner163 0:4562b55bbb43 2420 }
wonner163 0:4562b55bbb43 2421
wonner163 0:4562b55bbb43 2422 // Place the 4bit data on the databus
wonner163 0:4562b55bbb43 2423 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2424 void TextLCD_SPI::_setData(int value) {
wonner163 0:4562b55bbb43 2425 int data;
wonner163 0:4562b55bbb43 2426
wonner163 0:4562b55bbb43 2427 // Set bit by bit to support any mapping of expander portpins to LCD pins
wonner163 0:4562b55bbb43 2428
wonner163 0:4562b55bbb43 2429 data = value & 0x0F;
wonner163 0:4562b55bbb43 2430 if (data & 0x01) {
wonner163 0:4562b55bbb43 2431 _lcd_bus |= D_LCD_D4; // Set Databit
wonner163 0:4562b55bbb43 2432 }
wonner163 0:4562b55bbb43 2433 else {
wonner163 0:4562b55bbb43 2434 _lcd_bus &= ~D_LCD_D4; // Reset Databit
wonner163 0:4562b55bbb43 2435 }
wonner163 0:4562b55bbb43 2436
wonner163 0:4562b55bbb43 2437 if (data & 0x02) {
wonner163 0:4562b55bbb43 2438 _lcd_bus |= D_LCD_D5; // Set Databit
wonner163 0:4562b55bbb43 2439 }
wonner163 0:4562b55bbb43 2440 else {
wonner163 0:4562b55bbb43 2441 _lcd_bus &= ~D_LCD_D5; // Reset Databit
wonner163 0:4562b55bbb43 2442 }
wonner163 0:4562b55bbb43 2443
wonner163 0:4562b55bbb43 2444 if (data & 0x04) {
wonner163 0:4562b55bbb43 2445 _lcd_bus |= D_LCD_D6; // Set Databit
wonner163 0:4562b55bbb43 2446 }
wonner163 0:4562b55bbb43 2447 else {
wonner163 0:4562b55bbb43 2448 _lcd_bus &= ~D_LCD_D6; // Reset Databit
wonner163 0:4562b55bbb43 2449 }
wonner163 0:4562b55bbb43 2450
wonner163 0:4562b55bbb43 2451 if (data & 0x08) {
wonner163 0:4562b55bbb43 2452 _lcd_bus |= D_LCD_D7; // Set Databit
wonner163 0:4562b55bbb43 2453 }
wonner163 0:4562b55bbb43 2454 else {
wonner163 0:4562b55bbb43 2455 _lcd_bus &= ~D_LCD_D7; // Reset Databit
wonner163 0:4562b55bbb43 2456 }
wonner163 0:4562b55bbb43 2457
wonner163 0:4562b55bbb43 2458 // write the new data to the SPI portexpander
wonner163 0:4562b55bbb43 2459 _setCS(false);
wonner163 0:4562b55bbb43 2460 _spi->write(_lcd_bus);
wonner163 0:4562b55bbb43 2461 _setCS(true);
wonner163 0:4562b55bbb43 2462 }
wonner163 0:4562b55bbb43 2463
wonner163 0:4562b55bbb43 2464 // Set CS line.
wonner163 0:4562b55bbb43 2465 // Only used for SPI bus
wonner163 0:4562b55bbb43 2466 void TextLCD_SPI::_setCS(bool value) {
wonner163 0:4562b55bbb43 2467
wonner163 0:4562b55bbb43 2468 if (value) {
wonner163 0:4562b55bbb43 2469 _cs = 1; // Set CS pin
wonner163 0:4562b55bbb43 2470 }
wonner163 0:4562b55bbb43 2471 else {
wonner163 0:4562b55bbb43 2472 _cs = 0; // Reset CS pin
wonner163 0:4562b55bbb43 2473 }
wonner163 0:4562b55bbb43 2474 }
wonner163 0:4562b55bbb43 2475
wonner163 0:4562b55bbb43 2476 //---------- End TextLCD_SPI ------------
wonner163 0:4562b55bbb43 2477
wonner163 0:4562b55bbb43 2478
wonner163 0:4562b55bbb43 2479 //--------- Start TextLCD_SPI_N ---------
wonner163 0:4562b55bbb43 2480
wonner163 0:4562b55bbb43 2481 /** Create a TextLCD interface using a controller with a native SPI4 interface
wonner163 0:4562b55bbb43 2482 *
wonner163 0:4562b55bbb43 2483 * @param spi SPI Bus
wonner163 0:4562b55bbb43 2484 * @param cs chip select pin (active low)
wonner163 0:4562b55bbb43 2485 * @param rs Instruction/data control line
wonner163 0:4562b55bbb43 2486 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2487 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 2488 * @param ctrl LCD controller (default = ST7032_3V3)
wonner163 0:4562b55bbb43 2489 */
wonner163 0:4562b55bbb43 2490 TextLCD_SPI_N::TextLCD_SPI_N(SPI *spi, PinName cs, PinName rs, LCDType type, PinName bl, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2491 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2492 _spi(spi),
wonner163 0:4562b55bbb43 2493 _cs(cs),
wonner163 0:4562b55bbb43 2494 _rs(rs) {
wonner163 0:4562b55bbb43 2495
wonner163 0:4562b55bbb43 2496 // Init CS
wonner163 0:4562b55bbb43 2497 _cs = 1;
wonner163 0:4562b55bbb43 2498
wonner163 0:4562b55bbb43 2499 // Setup the spi for 8 bit data, low steady state clock,
wonner163 0:4562b55bbb43 2500 // rising edge capture, with a 500KHz or 1MHz clock rate
wonner163 0:4562b55bbb43 2501 _spi->format(8,0);
wonner163 0:4562b55bbb43 2502 _spi->frequency(1000000);
wonner163 0:4562b55bbb43 2503
wonner163 0:4562b55bbb43 2504 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 2505 if (bl != NC) {
wonner163 0:4562b55bbb43 2506 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 2507 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 2508 }
wonner163 0:4562b55bbb43 2509 else {
wonner163 0:4562b55bbb43 2510 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 2511 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 2512 }
wonner163 0:4562b55bbb43 2513
wonner163 0:4562b55bbb43 2514 //Sanity check
wonner163 0:4562b55bbb43 2515 if (_ctrl & LCD_C_SPI4) {
wonner163 0:4562b55bbb43 2516 _init();
wonner163 0:4562b55bbb43 2517 }
wonner163 0:4562b55bbb43 2518 else {
wonner163 0:4562b55bbb43 2519 error("Error: LCD Controller type does not support native SPI4 interface\n\r");
wonner163 0:4562b55bbb43 2520 }
wonner163 0:4562b55bbb43 2521 }
wonner163 0:4562b55bbb43 2522
wonner163 0:4562b55bbb43 2523 TextLCD_SPI_N::~TextLCD_SPI_N() {
wonner163 0:4562b55bbb43 2524 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 2525 }
wonner163 0:4562b55bbb43 2526
wonner163 0:4562b55bbb43 2527 // Not used in this mode
wonner163 0:4562b55bbb43 2528 void TextLCD_SPI_N::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2529 }
wonner163 0:4562b55bbb43 2530
wonner163 0:4562b55bbb43 2531 // Set RS pin
wonner163 0:4562b55bbb43 2532 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2533 void TextLCD_SPI_N::_setRS(bool value) {
wonner163 0:4562b55bbb43 2534 _rs = value;
wonner163 0:4562b55bbb43 2535 }
wonner163 0:4562b55bbb43 2536
wonner163 0:4562b55bbb43 2537 // Set BL pin
wonner163 0:4562b55bbb43 2538 void TextLCD_SPI_N::_setBL(bool value) {
wonner163 0:4562b55bbb43 2539 if (_bl) {
wonner163 0:4562b55bbb43 2540 _bl->write(value);
wonner163 0:4562b55bbb43 2541 }
wonner163 0:4562b55bbb43 2542 }
wonner163 0:4562b55bbb43 2543
wonner163 0:4562b55bbb43 2544 // Not used in this mode
wonner163 0:4562b55bbb43 2545 void TextLCD_SPI_N::_setData(int value) {
wonner163 0:4562b55bbb43 2546 }
wonner163 0:4562b55bbb43 2547
wonner163 0:4562b55bbb43 2548 // Write a byte using SPI
wonner163 0:4562b55bbb43 2549 void TextLCD_SPI_N::_writeByte(int value) {
wonner163 0:4562b55bbb43 2550 _cs = 0;
wonner163 0:4562b55bbb43 2551 wait_us(1);
wonner163 0:4562b55bbb43 2552 _spi->write(value);
wonner163 0:4562b55bbb43 2553 wait_us(1);
wonner163 0:4562b55bbb43 2554 _cs = 1;
wonner163 0:4562b55bbb43 2555 }
wonner163 0:4562b55bbb43 2556
wonner163 0:4562b55bbb43 2557 //-------- End TextLCD_SPI_N ------------
wonner163 0:4562b55bbb43 2558
wonner163 0:4562b55bbb43 2559
wonner163 0:4562b55bbb43 2560
wonner163 0:4562b55bbb43 2561 #if(1)
wonner163 0:4562b55bbb43 2562 //Code checked out on logic analyser. Not yet tested on hardware..
wonner163 0:4562b55bbb43 2563
wonner163 0:4562b55bbb43 2564 //-------- Start TextLCD_SPI_N_3_9 --------
wonner163 0:4562b55bbb43 2565
wonner163 0:4562b55bbb43 2566 /** Create a TextLCD interface using a controller with a native SPI3 9 bits interface
wonner163 0:4562b55bbb43 2567 *
wonner163 0:4562b55bbb43 2568 * @param spi SPI Bus
wonner163 0:4562b55bbb43 2569 * @param cs chip select pin (active low)
wonner163 0:4562b55bbb43 2570 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2571 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 2572 * @param ctrl LCD controller (default = AIP31068)
wonner163 0:4562b55bbb43 2573 */
wonner163 0:4562b55bbb43 2574 TextLCD_SPI_N_3_9::TextLCD_SPI_N_3_9(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2575 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2576 _spi(spi),
wonner163 0:4562b55bbb43 2577 _cs(cs) {
wonner163 0:4562b55bbb43 2578
wonner163 0:4562b55bbb43 2579 // Init CS
wonner163 0:4562b55bbb43 2580 _cs = 1;
wonner163 0:4562b55bbb43 2581
wonner163 0:4562b55bbb43 2582 // Setup the spi for 9 bit data, low steady state clock,
wonner163 0:4562b55bbb43 2583 // rising edge capture, with a 500KHz or 1MHz clock rate
wonner163 0:4562b55bbb43 2584 // _spi->format(9,0);
wonner163 0:4562b55bbb43 2585 _spi->format(9,3);
wonner163 0:4562b55bbb43 2586 _spi->frequency(1000000);
wonner163 0:4562b55bbb43 2587
wonner163 0:4562b55bbb43 2588 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 2589 if (bl != NC) {
wonner163 0:4562b55bbb43 2590 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 2591 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 2592 }
wonner163 0:4562b55bbb43 2593 else {
wonner163 0:4562b55bbb43 2594 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 2595 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 2596 }
wonner163 0:4562b55bbb43 2597
wonner163 0:4562b55bbb43 2598 //Sanity check
wonner163 0:4562b55bbb43 2599 if (_ctrl & LCD_C_SPI3_9) {
wonner163 0:4562b55bbb43 2600 _init();
wonner163 0:4562b55bbb43 2601 }
wonner163 0:4562b55bbb43 2602 else {
wonner163 0:4562b55bbb43 2603 error("Error: LCD Controller type does not support native SPI3 9 bits interface\n\r");
wonner163 0:4562b55bbb43 2604 }
wonner163 0:4562b55bbb43 2605 }
wonner163 0:4562b55bbb43 2606
wonner163 0:4562b55bbb43 2607 TextLCD_SPI_N_3_9::~TextLCD_SPI_N_3_9() {
wonner163 0:4562b55bbb43 2608 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 2609 }
wonner163 0:4562b55bbb43 2610
wonner163 0:4562b55bbb43 2611 // Not used in this mode
wonner163 0:4562b55bbb43 2612 void TextLCD_SPI_N_3_9::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2613 }
wonner163 0:4562b55bbb43 2614
wonner163 0:4562b55bbb43 2615 // Set RS pin
wonner163 0:4562b55bbb43 2616 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2617 void TextLCD_SPI_N_3_9::_setRS(bool value) {
wonner163 0:4562b55bbb43 2618 // The controlbits define the meaning of the next byte. This next byte can either be data or command.
wonner163 0:4562b55bbb43 2619 // b8 b7...........b0
wonner163 0:4562b55bbb43 2620 // RS command or data
wonner163 0:4562b55bbb43 2621 //
wonner163 0:4562b55bbb43 2622 // RS=1 means that next byte is data, RS=0 means that next byte is command
wonner163 0:4562b55bbb43 2623 //
wonner163 0:4562b55bbb43 2624
wonner163 0:4562b55bbb43 2625 if (value) {
wonner163 0:4562b55bbb43 2626 _controlbyte = 0x01; // Next byte is data
wonner163 0:4562b55bbb43 2627 }
wonner163 0:4562b55bbb43 2628 else {
wonner163 0:4562b55bbb43 2629 _controlbyte = 0x00; // Next byte is command
wonner163 0:4562b55bbb43 2630 }
wonner163 0:4562b55bbb43 2631
wonner163 0:4562b55bbb43 2632 }
wonner163 0:4562b55bbb43 2633
wonner163 0:4562b55bbb43 2634 // Set BL pin
wonner163 0:4562b55bbb43 2635 void TextLCD_SPI_N_3_9::_setBL(bool value) {
wonner163 0:4562b55bbb43 2636 if (_bl) {
wonner163 0:4562b55bbb43 2637 _bl->write(value);
wonner163 0:4562b55bbb43 2638 }
wonner163 0:4562b55bbb43 2639 }
wonner163 0:4562b55bbb43 2640
wonner163 0:4562b55bbb43 2641 // Not used in this mode
wonner163 0:4562b55bbb43 2642 void TextLCD_SPI_N_3_9::_setData(int value) {
wonner163 0:4562b55bbb43 2643 }
wonner163 0:4562b55bbb43 2644
wonner163 0:4562b55bbb43 2645 // Write a byte using SPI3 9 bits mode
wonner163 0:4562b55bbb43 2646 void TextLCD_SPI_N_3_9::_writeByte(int value) {
wonner163 0:4562b55bbb43 2647 _cs = 0;
wonner163 0:4562b55bbb43 2648 wait_us(1);
wonner163 0:4562b55bbb43 2649 _spi->write( (_controlbyte << 8) | (value & 0xFF));
wonner163 0:4562b55bbb43 2650 wait_us(1);
wonner163 0:4562b55bbb43 2651 _cs = 1;
wonner163 0:4562b55bbb43 2652 }
wonner163 0:4562b55bbb43 2653
wonner163 0:4562b55bbb43 2654 //------- End TextLCD_SPI_N_3_9 -----------
wonner163 0:4562b55bbb43 2655 #endif
wonner163 0:4562b55bbb43 2656
wonner163 0:4562b55bbb43 2657
wonner163 0:4562b55bbb43 2658 #if(1)
wonner163 0:4562b55bbb43 2659 //------- Start TextLCD_SPI_N_3_10 --------
wonner163 0:4562b55bbb43 2660
wonner163 0:4562b55bbb43 2661 /** Create a TextLCD interface using a controller with a native SPI3 10 bits interface
wonner163 0:4562b55bbb43 2662 *
wonner163 0:4562b55bbb43 2663 * @param spi SPI Bus
wonner163 0:4562b55bbb43 2664 * @param cs chip select pin (active low)
wonner163 0:4562b55bbb43 2665 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2666 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 2667 * @param ctrl LCD controller (default = AIP31068)
wonner163 0:4562b55bbb43 2668 */
wonner163 0:4562b55bbb43 2669 TextLCD_SPI_N_3_10::TextLCD_SPI_N_3_10(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2670 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2671 _spi(spi),
wonner163 0:4562b55bbb43 2672 _cs(cs) {
wonner163 0:4562b55bbb43 2673
wonner163 0:4562b55bbb43 2674 // Init CS
wonner163 0:4562b55bbb43 2675 _cs = 1;
wonner163 0:4562b55bbb43 2676
wonner163 0:4562b55bbb43 2677 // Setup the spi for 10 bit data, low steady state clock,
wonner163 0:4562b55bbb43 2678 // rising edge capture, with a 500KHz or 1MHz clock rate
wonner163 0:4562b55bbb43 2679 _spi->format(10,0);
wonner163 0:4562b55bbb43 2680 _spi->frequency(1000000);
wonner163 0:4562b55bbb43 2681
wonner163 0:4562b55bbb43 2682 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 2683 if (bl != NC) {
wonner163 0:4562b55bbb43 2684 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 2685 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 2686 }
wonner163 0:4562b55bbb43 2687 else {
wonner163 0:4562b55bbb43 2688 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 2689 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 2690 }
wonner163 0:4562b55bbb43 2691
wonner163 0:4562b55bbb43 2692 //Sanity check
wonner163 0:4562b55bbb43 2693 if (_ctrl & LCD_C_SPI3_10) {
wonner163 0:4562b55bbb43 2694 _init();
wonner163 0:4562b55bbb43 2695 }
wonner163 0:4562b55bbb43 2696 else {
wonner163 0:4562b55bbb43 2697 error("Error: LCD Controller type does not support native SPI3 10 bits interface\n\r");
wonner163 0:4562b55bbb43 2698 }
wonner163 0:4562b55bbb43 2699 }
wonner163 0:4562b55bbb43 2700
wonner163 0:4562b55bbb43 2701 TextLCD_SPI_N_3_10::~TextLCD_SPI_N_3_10() {
wonner163 0:4562b55bbb43 2702 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 2703 }
wonner163 0:4562b55bbb43 2704
wonner163 0:4562b55bbb43 2705 // Not used in this mode
wonner163 0:4562b55bbb43 2706 void TextLCD_SPI_N_3_10::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2707 }
wonner163 0:4562b55bbb43 2708
wonner163 0:4562b55bbb43 2709 // Set RS pin
wonner163 0:4562b55bbb43 2710 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2711 void TextLCD_SPI_N_3_10::_setRS(bool value) {
wonner163 0:4562b55bbb43 2712 // The controlbits define the meaning of the next byte. This next byte can either be data or command.
wonner163 0:4562b55bbb43 2713 // b9 b8 b7...........b0
wonner163 0:4562b55bbb43 2714 // RS RW command or data
wonner163 0:4562b55bbb43 2715 //
wonner163 0:4562b55bbb43 2716 // RS=1 means that next byte is data, RS=0 means that next byte is command
wonner163 0:4562b55bbb43 2717 // RW=0 means that next byte is writen, RW=1 means that next byte is read (not used in this lib)
wonner163 0:4562b55bbb43 2718 //
wonner163 0:4562b55bbb43 2719
wonner163 0:4562b55bbb43 2720 if (value) {
wonner163 0:4562b55bbb43 2721 _controlbyte = 0x02; // Next byte is data
wonner163 0:4562b55bbb43 2722 }
wonner163 0:4562b55bbb43 2723 else {
wonner163 0:4562b55bbb43 2724 _controlbyte = 0x00; // Next byte is command
wonner163 0:4562b55bbb43 2725 }
wonner163 0:4562b55bbb43 2726
wonner163 0:4562b55bbb43 2727 }
wonner163 0:4562b55bbb43 2728
wonner163 0:4562b55bbb43 2729 // Set BL pin
wonner163 0:4562b55bbb43 2730 void TextLCD_SPI_N_3_10::_setBL(bool value) {
wonner163 0:4562b55bbb43 2731 if (_bl) {
wonner163 0:4562b55bbb43 2732 _bl->write(value);
wonner163 0:4562b55bbb43 2733 }
wonner163 0:4562b55bbb43 2734 }
wonner163 0:4562b55bbb43 2735
wonner163 0:4562b55bbb43 2736 // Not used in this mode
wonner163 0:4562b55bbb43 2737 void TextLCD_SPI_N_3_10::_setData(int value) {
wonner163 0:4562b55bbb43 2738 }
wonner163 0:4562b55bbb43 2739
wonner163 0:4562b55bbb43 2740 // Write a byte using SPI3 10 bits mode
wonner163 0:4562b55bbb43 2741 void TextLCD_SPI_N_3_10::_writeByte(int value) {
wonner163 0:4562b55bbb43 2742 _cs = 0;
wonner163 0:4562b55bbb43 2743 wait_us(1);
wonner163 0:4562b55bbb43 2744 _spi->write( (_controlbyte << 8) | (value & 0xFF));
wonner163 0:4562b55bbb43 2745 wait_us(1);
wonner163 0:4562b55bbb43 2746 _cs = 1;
wonner163 0:4562b55bbb43 2747 }
wonner163 0:4562b55bbb43 2748
wonner163 0:4562b55bbb43 2749 //------- End TextLCD_SPI_N_3_10 ----------
wonner163 0:4562b55bbb43 2750 #endif
wonner163 0:4562b55bbb43 2751
wonner163 0:4562b55bbb43 2752 #if(0)
wonner163 0:4562b55bbb43 2753 //Code to be checked out on logic analyser. Not yet tested on hardware..
wonner163 0:4562b55bbb43 2754
wonner163 0:4562b55bbb43 2755 //------- Start TextLCD_SPI_N_3_16 --------
wonner163 0:4562b55bbb43 2756
wonner163 0:4562b55bbb43 2757 /** Create a TextLCD interface using a controller with a native SPI3 16 bits interface
wonner163 0:4562b55bbb43 2758 *
wonner163 0:4562b55bbb43 2759 * @param spi SPI Bus
wonner163 0:4562b55bbb43 2760 * @param cs chip select pin (active low)
wonner163 0:4562b55bbb43 2761 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2762 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 2763 * @param ctrl LCD controller (default = PT6314)
wonner163 0:4562b55bbb43 2764 */
wonner163 0:4562b55bbb43 2765 TextLCD_SPI_N_3_16::TextLCD_SPI_N_3_16(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2766 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2767 _spi(spi),
wonner163 0:4562b55bbb43 2768 _cs(cs) {
wonner163 0:4562b55bbb43 2769
wonner163 0:4562b55bbb43 2770 // Init CS
wonner163 0:4562b55bbb43 2771 _cs = 1;
wonner163 0:4562b55bbb43 2772
wonner163 0:4562b55bbb43 2773 // Setup the spi for 8 bit data, low steady state clock,
wonner163 0:4562b55bbb43 2774 // rising edge capture, with a 500KHz or 1MHz clock rate
wonner163 0:4562b55bbb43 2775 _spi->format(8,0);
wonner163 0:4562b55bbb43 2776 _spi->frequency(1000000);
wonner163 0:4562b55bbb43 2777
wonner163 0:4562b55bbb43 2778 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 2779 if (bl != NC) {
wonner163 0:4562b55bbb43 2780 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 2781 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 2782 }
wonner163 0:4562b55bbb43 2783 else {
wonner163 0:4562b55bbb43 2784 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 2785 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 2786 }
wonner163 0:4562b55bbb43 2787
wonner163 0:4562b55bbb43 2788 //Sanity check
wonner163 0:4562b55bbb43 2789 if (_ctrl & LCD_C_SPI3_16) {
wonner163 0:4562b55bbb43 2790 _init();
wonner163 0:4562b55bbb43 2791 }
wonner163 0:4562b55bbb43 2792 else {
wonner163 0:4562b55bbb43 2793 error("Error: LCD Controller type does not support native SPI3 16 bits interface\n\r");
wonner163 0:4562b55bbb43 2794 }
wonner163 0:4562b55bbb43 2795 }
wonner163 0:4562b55bbb43 2796
wonner163 0:4562b55bbb43 2797 TextLCD_SPI_N_3_16::~TextLCD_SPI_N_3_16() {
wonner163 0:4562b55bbb43 2798 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 2799 }
wonner163 0:4562b55bbb43 2800
wonner163 0:4562b55bbb43 2801 // Not used in this mode
wonner163 0:4562b55bbb43 2802 void TextLCD_SPI_N_3_16::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2803 }
wonner163 0:4562b55bbb43 2804
wonner163 0:4562b55bbb43 2805 // Set RS pin
wonner163 0:4562b55bbb43 2806 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2807 void TextLCD_SPI_N_3_16::_setRS(bool value) {
wonner163 0:4562b55bbb43 2808 // The 16bit mode is split in 2 bytes. The first byte is for synchronisation and controlbits. The controlbits define the meaning of the next byte.
wonner163 0:4562b55bbb43 2809 // The 8 actual bits represent either a data or a command byte.
wonner163 0:4562b55bbb43 2810 // b15 b14 b13 b12 b11 b10 b9 b8 - b7 b6 b5 b4 b3 b2 b1 b0
wonner163 0:4562b55bbb43 2811 // 1 1 1 1 1 RW RS 0 d7 d6 d5 d4 d3 d2 d1 d0
wonner163 0:4562b55bbb43 2812 //
wonner163 0:4562b55bbb43 2813 // RS=1 means that next byte is data, RS=0 means that next byte is command
wonner163 0:4562b55bbb43 2814 // RW=0 means that next byte is writen, RW=1 means that next byte is read (not used in this lib)
wonner163 0:4562b55bbb43 2815 //
wonner163 0:4562b55bbb43 2816
wonner163 0:4562b55bbb43 2817 if (value) {
wonner163 0:4562b55bbb43 2818 _controlbyte = 0xFA; // Next byte is data
wonner163 0:4562b55bbb43 2819 }
wonner163 0:4562b55bbb43 2820 else {
wonner163 0:4562b55bbb43 2821 _controlbyte = 0xF8; // Next byte is command
wonner163 0:4562b55bbb43 2822 }
wonner163 0:4562b55bbb43 2823 }
wonner163 0:4562b55bbb43 2824
wonner163 0:4562b55bbb43 2825 // Set BL pin
wonner163 0:4562b55bbb43 2826 void TextLCD_SPI_N_3_16::_setBL(bool value) {
wonner163 0:4562b55bbb43 2827 if (_bl) {
wonner163 0:4562b55bbb43 2828 _bl->write(value);
wonner163 0:4562b55bbb43 2829 }
wonner163 0:4562b55bbb43 2830 }
wonner163 0:4562b55bbb43 2831
wonner163 0:4562b55bbb43 2832 // Not used in this mode
wonner163 0:4562b55bbb43 2833 void TextLCD_SPI_N_3_16::_setData(int value) {
wonner163 0:4562b55bbb43 2834 }
wonner163 0:4562b55bbb43 2835
wonner163 0:4562b55bbb43 2836
wonner163 0:4562b55bbb43 2837 // Write a byte using SPI3 16 bits mode
wonner163 0:4562b55bbb43 2838 void TextLCD_SPI_N_3_16::_writeByte(int value) {
wonner163 0:4562b55bbb43 2839 _cs = 0;
wonner163 0:4562b55bbb43 2840 wait_us(1);
wonner163 0:4562b55bbb43 2841
wonner163 0:4562b55bbb43 2842 _spi->write(_controlbyte);
wonner163 0:4562b55bbb43 2843
wonner163 0:4562b55bbb43 2844 _spi->write(value);
wonner163 0:4562b55bbb43 2845
wonner163 0:4562b55bbb43 2846 wait_us(1);
wonner163 0:4562b55bbb43 2847 _cs = 1;
wonner163 0:4562b55bbb43 2848 }
wonner163 0:4562b55bbb43 2849
wonner163 0:4562b55bbb43 2850 //------- End TextLCD_SPI_N_3_16 ----------
wonner163 0:4562b55bbb43 2851 #endif
wonner163 0:4562b55bbb43 2852
wonner163 0:4562b55bbb43 2853 #if(1)
wonner163 0:4562b55bbb43 2854 //------- Start TextLCD_SPI_N_3_24 --------
wonner163 0:4562b55bbb43 2855
wonner163 0:4562b55bbb43 2856 /** Create a TextLCD interface using a controller with a native SPI3 24 bits interface
wonner163 0:4562b55bbb43 2857 *
wonner163 0:4562b55bbb43 2858 * @param spi SPI Bus
wonner163 0:4562b55bbb43 2859 * @param cs chip select pin (active low)
wonner163 0:4562b55bbb43 2860 * @param type Sets the panel size/addressing mode (default = LCD16x2)
wonner163 0:4562b55bbb43 2861 * @param bl Backlight control line (optional, default = NC)
wonner163 0:4562b55bbb43 2862 * @param ctrl LCD controller (default = SSD1803)
wonner163 0:4562b55bbb43 2863 */
wonner163 0:4562b55bbb43 2864 TextLCD_SPI_N_3_24::TextLCD_SPI_N_3_24(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
wonner163 0:4562b55bbb43 2865 TextLCD_Base(type, ctrl),
wonner163 0:4562b55bbb43 2866 _spi(spi),
wonner163 0:4562b55bbb43 2867 _cs(cs) {
wonner163 0:4562b55bbb43 2868
wonner163 0:4562b55bbb43 2869 // Init CS
wonner163 0:4562b55bbb43 2870 _cs = 1;
wonner163 0:4562b55bbb43 2871
wonner163 0:4562b55bbb43 2872 // Setup the spi for 8 bit data, low steady state clock,
wonner163 0:4562b55bbb43 2873 // rising edge capture, with a 500KHz or 1MHz clock rate
wonner163 0:4562b55bbb43 2874 _spi->format(8,0);
wonner163 0:4562b55bbb43 2875 _spi->frequency(1000000);
wonner163 0:4562b55bbb43 2876
wonner163 0:4562b55bbb43 2877 // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
wonner163 0:4562b55bbb43 2878 if (bl != NC) {
wonner163 0:4562b55bbb43 2879 _bl = new DigitalOut(bl); //Construct new pin
wonner163 0:4562b55bbb43 2880 _bl->write(0); //Deactivate
wonner163 0:4562b55bbb43 2881 }
wonner163 0:4562b55bbb43 2882 else {
wonner163 0:4562b55bbb43 2883 // No Hardware Backlight pin
wonner163 0:4562b55bbb43 2884 _bl = NULL; //Construct dummy pin
wonner163 0:4562b55bbb43 2885 }
wonner163 0:4562b55bbb43 2886
wonner163 0:4562b55bbb43 2887 //Sanity check
wonner163 0:4562b55bbb43 2888 if (_ctrl & LCD_C_SPI3_24) {
wonner163 0:4562b55bbb43 2889 _init();
wonner163 0:4562b55bbb43 2890 }
wonner163 0:4562b55bbb43 2891 else {
wonner163 0:4562b55bbb43 2892 error("Error: LCD Controller type does not support native SPI3 24 bits interface\n\r");
wonner163 0:4562b55bbb43 2893 }
wonner163 0:4562b55bbb43 2894 }
wonner163 0:4562b55bbb43 2895
wonner163 0:4562b55bbb43 2896 TextLCD_SPI_N_3_24::~TextLCD_SPI_N_3_24() {
wonner163 0:4562b55bbb43 2897 if (_bl != NULL) {delete _bl;} // BL pin
wonner163 0:4562b55bbb43 2898 }
wonner163 0:4562b55bbb43 2899
wonner163 0:4562b55bbb43 2900 // Not used in this mode
wonner163 0:4562b55bbb43 2901 void TextLCD_SPI_N_3_24::_setEnable(bool value) {
wonner163 0:4562b55bbb43 2902 }
wonner163 0:4562b55bbb43 2903
wonner163 0:4562b55bbb43 2904 // Set RS pin
wonner163 0:4562b55bbb43 2905 // Used for mbed pins, I2C bus expander or SPI shiftregister
wonner163 0:4562b55bbb43 2906 void TextLCD_SPI_N_3_24::_setRS(bool value) {
wonner163 0:4562b55bbb43 2907 // The 24bit mode is split in 3 bytes. The first byte is for synchronisation and controlbits. The controlbits define the meaning of the next two bytes.
wonner163 0:4562b55bbb43 2908 // Each byte encodes 4 actual bits. The 8 actual bits represent either a data or a command byte.
wonner163 0:4562b55bbb43 2909 // b23 b22 b21 b20 b19 b18 b17 b16 - b15 b14 b13 b12 b11 b10 b9 b8 - b7 b6 b5 b4 b3 b2 b1 b0
wonner163 0:4562b55bbb43 2910 // 1 1 1 1 1 RW RS 0 d0 d1 d2 d3 0 0 0 0 d4 d5 d6 d7 0 0 0 0
wonner163 0:4562b55bbb43 2911 //
wonner163 0:4562b55bbb43 2912 // RS=1 means that next byte is data, RS=0 means that next byte is command
wonner163 0:4562b55bbb43 2913 // RW=0 means that next byte is writen, RW=1 means that next byte is read (not used in this lib)
wonner163 0:4562b55bbb43 2914 //
wonner163 0:4562b55bbb43 2915 // Note: SPI3_24 expects LSB first. This is inconsistent with regular SPI convention (and hardware) that sends MSB first.
wonner163 0:4562b55bbb43 2916
wonner163 0:4562b55bbb43 2917 if (value) {
wonner163 0:4562b55bbb43 2918 _controlbyte = 0xFA; // Next byte is data
wonner163 0:4562b55bbb43 2919 }
wonner163 0:4562b55bbb43 2920 else {
wonner163 0:4562b55bbb43 2921 _controlbyte = 0xF8; // Next byte is command
wonner163 0:4562b55bbb43 2922 }
wonner163 0:4562b55bbb43 2923
wonner163 0:4562b55bbb43 2924 }
wonner163 0:4562b55bbb43 2925
wonner163 0:4562b55bbb43 2926 // Set BL pin
wonner163 0:4562b55bbb43 2927 void TextLCD_SPI_N_3_24::_setBL(bool value) {
wonner163 0:4562b55bbb43 2928 if (_bl) {
wonner163 0:4562b55bbb43 2929 _bl->write(value);
wonner163 0:4562b55bbb43 2930 }
wonner163 0:4562b55bbb43 2931 }
wonner163 0:4562b55bbb43 2932
wonner163 0:4562b55bbb43 2933 // Not used in this mode
wonner163 0:4562b55bbb43 2934 void TextLCD_SPI_N_3_24::_setData(int value) {
wonner163 0:4562b55bbb43 2935 }
wonner163 0:4562b55bbb43 2936
wonner163 0:4562b55bbb43 2937 //Mapping table to flip the bits around cause SPI3_24 expects LSB first.
wonner163 0:4562b55bbb43 2938 const uint8_t map3_24[16] = {0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0};
wonner163 0:4562b55bbb43 2939
wonner163 0:4562b55bbb43 2940 // Write a byte using SPI3 24 bits mode
wonner163 0:4562b55bbb43 2941 void TextLCD_SPI_N_3_24::_writeByte(int value) {
wonner163 0:4562b55bbb43 2942 _cs = 0;
wonner163 0:4562b55bbb43 2943 wait_us(1);
wonner163 0:4562b55bbb43 2944 _spi->write(_controlbyte);
wonner163 0:4562b55bbb43 2945
wonner163 0:4562b55bbb43 2946 //Map and send the LSB nibble
wonner163 0:4562b55bbb43 2947 _spi->write( map3_24[value & 0x0F]);
wonner163 0:4562b55bbb43 2948
wonner163 0:4562b55bbb43 2949 //Map and send the MSB nibble
wonner163 0:4562b55bbb43 2950 _spi->write( map3_24[(value >> 4) & 0x0F]);
wonner163 0:4562b55bbb43 2951
wonner163 0:4562b55bbb43 2952 wait_us(1);
wonner163 0:4562b55bbb43 2953 _cs = 1;
wonner163 0:4562b55bbb43 2954 }
wonner163 0:4562b55bbb43 2955
wonner163 0:4562b55bbb43 2956 //------- End TextLCD_SPI_N_3_24 ----------
wonner163 0:4562b55bbb43 2957 #endif