1.44 tft lcd display

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

Committer:
ttodorov
Date:
Sun Jun 16 02:53:06 2013 +0000
Revision:
25:6cffb758c075
Parent:
24:ac6e35658037
Child:
26:28f64fbcf7df
- finish porting ILI9325/8 driver: screen rotation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ttodorov 23:eca4414196ca 1 /*
ttodorov 23:eca4414196ca 2 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
ttodorov 23:eca4414196ca 3 * Copyright (C)2012-2013 Todor Todorov.
ttodorov 23:eca4414196ca 4 *
ttodorov 23:eca4414196ca 5 * This library is free software; you can redistribute it and/or
ttodorov 23:eca4414196ca 6 * modify it under the terms of the GNU Lesser General Public
ttodorov 23:eca4414196ca 7 * License as published by the Free Software Foundation; either
ttodorov 23:eca4414196ca 8 * version 2.1 of the License, or (at your option) any later version.
ttodorov 23:eca4414196ca 9 *
ttodorov 23:eca4414196ca 10 * This library is distributed in the hope that it will be useful,
ttodorov 23:eca4414196ca 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ttodorov 23:eca4414196ca 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ttodorov 23:eca4414196ca 13 * Lesser General Public License for more details.
ttodorov 23:eca4414196ca 14 *
ttodorov 23:eca4414196ca 15 * You should have received a copy of the GNU Lesser General Public
ttodorov 23:eca4414196ca 16 * License along with this library; if not, write to:
ttodorov 23:eca4414196ca 17 *
ttodorov 23:eca4414196ca 18 * Free Software Foundation, Inc.
ttodorov 23:eca4414196ca 19 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
ttodorov 23:eca4414196ca 20 *
ttodorov 23:eca4414196ca 21 *********************************************************************/
ttodorov 23:eca4414196ca 22 #include "ili9328.h"
ttodorov 23:eca4414196ca 23 #include "helpers.h"
ttodorov 23:eca4414196ca 24
ttodorov 23:eca4414196ca 25 ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
ttodorov 23:eca4414196ca 26 : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
ttodorov 23:eca4414196ca 27 {
ttodorov 23:eca4414196ca 28 _lcd_port = DATA_PORT;
ttodorov 23:eca4414196ca 29 if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
ttodorov 23:eca4414196ca 30 else _lcd_pin_rd = 0;
ttodorov 23:eca4414196ca 31 }
ttodorov 23:eca4414196ca 32
ttodorov 23:eca4414196ca 33 void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
ttodorov 23:eca4414196ca 34 {
ttodorov 23:eca4414196ca 35 _orientation = orientation;
ttodorov 23:eca4414196ca 36 _colorDepth = colors;
ttodorov 23:eca4414196ca 37
ttodorov 23:eca4414196ca 38 _lcd_pin_reset = HIGH;
ttodorov 23:eca4414196ca 39 wait_ms( 50 );
ttodorov 23:eca4414196ca 40 _lcd_pin_reset = LOW;
ttodorov 23:eca4414196ca 41 wait_ms( 100 );
ttodorov 23:eca4414196ca 42 _lcd_pin_reset = HIGH;
ttodorov 23:eca4414196ca 43 wait_ms( 1000 );
ttodorov 23:eca4414196ca 44 _lcd_pin_cs = HIGH;
ttodorov 23:eca4414196ca 45 if ( _lcd_pin_bl != 0 )
ttodorov 23:eca4414196ca 46 *_lcd_pin_bl = HIGH;
ttodorov 23:eca4414196ca 47 else if ( _bl_pwm != 0 )
ttodorov 23:eca4414196ca 48 *_bl_pwm = _bl_pwm_default;
ttodorov 23:eca4414196ca 49 if ( _lcd_pin_rd != 0 )
ttodorov 23:eca4414196ca 50 *_lcd_pin_rd = HIGH;
ttodorov 23:eca4414196ca 51 _lcd_pin_wr = HIGH;
ttodorov 23:eca4414196ca 52 wait_ms( 15 );
ttodorov 23:eca4414196ca 53
ttodorov 23:eca4414196ca 54 Activate();
ttodorov 25:6cffb758c075 55
ttodorov 25:6cffb758c075 56 short drivOut = 0;
ttodorov 25:6cffb758c075 57 short entryMod = 0;
ttodorov 25:6cffb758c075 58 short gateScan = 0x2700;
ttodorov 24:ac6e35658037 59 switch ( _orientation )
ttodorov 24:ac6e35658037 60 {
ttodorov 25:6cffb758c075 61 case LANDSCAPE:
ttodorov 25:6cffb758c075 62 drivOut = 0x0100;
ttodorov 25:6cffb758c075 63 entryMod |= 0x0038;
ttodorov 25:6cffb758c075 64 gateScan |= 0x0000;
ttodorov 25:6cffb758c075 65 break;
ttodorov 25:6cffb758c075 66
ttodorov 25:6cffb758c075 67 case LANDSCAPE_REV:
ttodorov 25:6cffb758c075 68 drivOut = 0x0000;
ttodorov 25:6cffb758c075 69 entryMod |= 0x0038;
ttodorov 25:6cffb758c075 70 gateScan |= 0x8000;
ttodorov 25:6cffb758c075 71 break;
ttodorov 25:6cffb758c075 72
ttodorov 25:6cffb758c075 73 case PORTRAIT_REV:
ttodorov 25:6cffb758c075 74 drivOut = 0x0000;
ttodorov 25:6cffb758c075 75 entryMod |= 0x0030;
ttodorov 25:6cffb758c075 76 gateScan |= 0x0000;
ttodorov 25:6cffb758c075 77 break;
ttodorov 25:6cffb758c075 78
ttodorov 24:ac6e35658037 79 case PORTRAIT:
ttodorov 24:ac6e35658037 80 default:
ttodorov 25:6cffb758c075 81 drivOut = 0x0100;
ttodorov 25:6cffb758c075 82 entryMod |= 0x0030;
ttodorov 25:6cffb758c075 83 gateScan |= 0x8000;
ttodorov 24:ac6e35658037 84 break;
ttodorov 24:ac6e35658037 85 }
ttodorov 24:ac6e35658037 86 switch ( _colorDepth )
ttodorov 24:ac6e35658037 87 {
ttodorov 24:ac6e35658037 88 case RGB16:
ttodorov 24:ac6e35658037 89 default:
ttodorov 25:6cffb758c075 90 entryMod |= 0x1000;
ttodorov 24:ac6e35658037 91 break;
ttodorov 24:ac6e35658037 92 }
ttodorov 24:ac6e35658037 93
ttodorov 24:ac6e35658037 94 WriteCmdData( 0xE5, 0x78F0 ); // set SRAM internal timing
ttodorov 25:6cffb758c075 95 WriteCmdData( 0x01, drivOut ); // set Driver Output Control
ttodorov 24:ac6e35658037 96 WriteCmdData( 0x02, 0x0200 ); // set 1 line inversion
ttodorov 25:6cffb758c075 97 WriteCmdData( 0x03, entryMod ); // set GRAM write direction and BGR=1.
ttodorov 24:ac6e35658037 98 WriteCmdData( 0x04, 0x0000 ); // Resize register
ttodorov 24:ac6e35658037 99 WriteCmdData( 0x08, 0x0207 ); // set the back porch and front porch
ttodorov 24:ac6e35658037 100 WriteCmdData( 0x09, 0x0000 ); // set non-display area refresh cycle ISC[3:0]
ttodorov 24:ac6e35658037 101 WriteCmdData( 0x0A, 0x0000 ); // FMARK function
ttodorov 24:ac6e35658037 102 WriteCmdData( 0x0C, 0x0000 ); // RGB interface setting
ttodorov 24:ac6e35658037 103 WriteCmdData( 0x0D, 0x0000 ); // Frame marker Position
ttodorov 24:ac6e35658037 104 WriteCmdData( 0x0F, 0x0000 ); // RGB interface polarity
ttodorov 24:ac6e35658037 105 // ----------- Power On sequence ----------- //
ttodorov 24:ac6e35658037 106 WriteCmdData( 0x10, 0x0000 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
ttodorov 24:ac6e35658037 107 WriteCmdData( 0x11, 0x0007 ); // DC1[2:0], DC0[2:0], VC[2:0]
ttodorov 24:ac6e35658037 108 WriteCmdData( 0x12, 0x0000 ); // VREG1OUT voltage
ttodorov 24:ac6e35658037 109 WriteCmdData( 0x13, 0x0000 ); // VDV[4:0] for VCOM amplitude
ttodorov 24:ac6e35658037 110 WriteCmdData( 0x07, 0x0001 );
ttodorov 24:ac6e35658037 111 wait_ms( 200 ); // Dis-charge capacitor power voltage
ttodorov 24:ac6e35658037 112 WriteCmdData( 0x10, 0x1690 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
ttodorov 24:ac6e35658037 113 WriteCmdData( 0x11, 0x0227 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
ttodorov 24:ac6e35658037 114 wait_ms( 50 ); // Delay 50ms
ttodorov 24:ac6e35658037 115 WriteCmdData( 0x12, 0x000D ); // 0012
ttodorov 24:ac6e35658037 116 wait_ms( 50 ); // Delay 50ms
ttodorov 24:ac6e35658037 117 WriteCmdData( 0x13, 0x1200 ); // VDV[4:0] for VCOM amplitude
ttodorov 24:ac6e35658037 118 WriteCmdData( 0x29, 0x000A ); // 04 VCM[5:0] for VCOMH
ttodorov 24:ac6e35658037 119 WriteCmdData( 0x2B, 0x000D ); // Set Frame Rate
ttodorov 24:ac6e35658037 120 wait_ms( 50 ); // Delay 50ms
ttodorov 24:ac6e35658037 121 WriteCmdData( 0x20, 0x0000 ); // GRAM horizontal Address
ttodorov 24:ac6e35658037 122 WriteCmdData( 0x21, 0x0000 ); // GRAM Vertical Address
ttodorov 24:ac6e35658037 123 // ----------- Adjust the Gamma Curve ----------//
ttodorov 24:ac6e35658037 124 WriteCmdData( 0x30, 0x0000 );
ttodorov 24:ac6e35658037 125 WriteCmdData( 0x31, 0x0404 );
ttodorov 24:ac6e35658037 126 WriteCmdData( 0x32, 0x0003 );
ttodorov 24:ac6e35658037 127 WriteCmdData( 0x35, 0x0405 );
ttodorov 24:ac6e35658037 128 WriteCmdData( 0x36, 0x0808 );
ttodorov 24:ac6e35658037 129 WriteCmdData( 0x37, 0x0407 );
ttodorov 24:ac6e35658037 130 WriteCmdData( 0x38, 0x0303 );
ttodorov 24:ac6e35658037 131 WriteCmdData( 0x39, 0x0707 );
ttodorov 24:ac6e35658037 132 WriteCmdData( 0x3C, 0x0504 );
ttodorov 24:ac6e35658037 133 WriteCmdData( 0x3D, 0x0808 );
ttodorov 24:ac6e35658037 134 //------------------ Set GRAM area ---------------//
ttodorov 24:ac6e35658037 135 WriteCmdData( 0x50, 0x0000 ); // Horizontal GRAM Start Address
ttodorov 24:ac6e35658037 136 WriteCmdData( 0x51, 0x00EF ); // Horizontal GRAM End Address
ttodorov 24:ac6e35658037 137 WriteCmdData( 0x52, 0x0000 ); // Vertical GRAM Start Address
ttodorov 24:ac6e35658037 138 WriteCmdData( 0x53, 0x013F ); // Vertical GRAM Start Address
ttodorov 25:6cffb758c075 139 WriteCmdData( 0x60, gateScan ); // Gate Scan Line (0xA700)
ttodorov 24:ac6e35658037 140 WriteCmdData( 0x61, 0x0000 ); // NDL,VLE, REV
ttodorov 24:ac6e35658037 141 WriteCmdData( 0x6A, 0x0000 ); // set scrolling line
ttodorov 24:ac6e35658037 142 //-------------- Partial Display Control ---------//
ttodorov 24:ac6e35658037 143 WriteCmdData( 0x80, 0x0000 );
ttodorov 24:ac6e35658037 144 WriteCmdData( 0x81, 0x0000 );
ttodorov 24:ac6e35658037 145 WriteCmdData( 0x82, 0x0000 );
ttodorov 24:ac6e35658037 146 WriteCmdData( 0x83, 0x0000 );
ttodorov 24:ac6e35658037 147 WriteCmdData( 0x84, 0x0000 );
ttodorov 24:ac6e35658037 148 WriteCmdData( 0x85, 0x0000 );
ttodorov 24:ac6e35658037 149 //-------------- Panel Control -------------------//
ttodorov 24:ac6e35658037 150 WriteCmdData( 0x90, 0x0010 );
ttodorov 24:ac6e35658037 151 WriteCmdData( 0x92, 0x0000 );
ttodorov 24:ac6e35658037 152 WriteCmdData( 0x07, 0x0133 ); // 262K color and display ON
ttodorov 24:ac6e35658037 153
ttodorov 23:eca4414196ca 154 Deactivate();
ttodorov 23:eca4414196ca 155 }
ttodorov 23:eca4414196ca 156
ttodorov 23:eca4414196ca 157 void ILI9328_LCD::Sleep( void )
ttodorov 23:eca4414196ca 158 {
ttodorov 25:6cffb758c075 159 WriteCmdData( 0x10, 0x0001 );//0x1692 ); // enter sleep mode
ttodorov 23:eca4414196ca 160 LCD::Sleep();
ttodorov 23:eca4414196ca 161 }
ttodorov 23:eca4414196ca 162
ttodorov 23:eca4414196ca 163 void ILI9328_LCD::WakeUp( void )
ttodorov 23:eca4414196ca 164 {
ttodorov 25:6cffb758c075 165 WriteCmdData( 0x10, 0x0000 );//0x1690 ); // exit sleep mode
ttodorov 23:eca4414196ca 166 LCD::WakeUp();
ttodorov 23:eca4414196ca 167 }
ttodorov 23:eca4414196ca 168
ttodorov 23:eca4414196ca 169 void ILI9328_LCD::WriteCmd( unsigned short cmd )
ttodorov 23:eca4414196ca 170 {
ttodorov 23:eca4414196ca 171 _lcd_pin_rs = LOW;
ttodorov 23:eca4414196ca 172 _lcd_port->write( cmd );
ttodorov 23:eca4414196ca 173 pulseLow( _lcd_pin_wr );
ttodorov 23:eca4414196ca 174 }
ttodorov 23:eca4414196ca 175
ttodorov 23:eca4414196ca 176 void ILI9328_LCD::WriteData( unsigned short data )
ttodorov 23:eca4414196ca 177 {
ttodorov 23:eca4414196ca 178 _lcd_pin_rs = HIGH;
ttodorov 23:eca4414196ca 179 _lcd_port->write( data );
ttodorov 23:eca4414196ca 180 pulseLow( _lcd_pin_wr );
ttodorov 23:eca4414196ca 181 }
ttodorov 23:eca4414196ca 182
ttodorov 23:eca4414196ca 183 void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
ttodorov 23:eca4414196ca 184 {
ttodorov 25:6cffb758c075 185 switch ( _orientation )
ttodorov 25:6cffb758c075 186 {
ttodorov 25:6cffb758c075 187 case LANDSCAPE:
ttodorov 25:6cffb758c075 188 case LANDSCAPE_REV:
ttodorov 25:6cffb758c075 189 WriteCmdData( 0x20, y1 );
ttodorov 25:6cffb758c075 190 WriteCmdData( 0x21, x1 );
ttodorov 25:6cffb758c075 191 WriteCmdData( 0x50, y1 );
ttodorov 25:6cffb758c075 192 WriteCmdData( 0x52, x1 );
ttodorov 25:6cffb758c075 193 WriteCmdData( 0x51, y2 );
ttodorov 25:6cffb758c075 194 WriteCmdData( 0x53, x2 );
ttodorov 25:6cffb758c075 195 break;
ttodorov 25:6cffb758c075 196
ttodorov 25:6cffb758c075 197 case PORTRAIT_REV:
ttodorov 25:6cffb758c075 198 case PORTRAIT:
ttodorov 25:6cffb758c075 199 default:
ttodorov 25:6cffb758c075 200 WriteCmdData( 0x20, x1 );
ttodorov 25:6cffb758c075 201 WriteCmdData( 0x21, y1 );
ttodorov 25:6cffb758c075 202 WriteCmdData( 0x50, x1 );
ttodorov 25:6cffb758c075 203 WriteCmdData( 0x52, y1 );
ttodorov 25:6cffb758c075 204 WriteCmdData( 0x51, x2 );
ttodorov 25:6cffb758c075 205 WriteCmdData( 0x53, y2 );
ttodorov 25:6cffb758c075 206 break;
ttodorov 25:6cffb758c075 207 }
ttodorov 23:eca4414196ca 208 WriteCmd( 0x22 );
ttodorov 23:eca4414196ca 209 }
ttodorov 23:eca4414196ca 210
ttodorov 23:eca4414196ca 211 void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
ttodorov 23:eca4414196ca 212 {
ttodorov 23:eca4414196ca 213 unsigned char r, g, b;
ttodorov 23:eca4414196ca 214 unsigned short clr;
ttodorov 23:eca4414196ca 215 if ( _colorDepth == RGB16 )
ttodorov 23:eca4414196ca 216 {
ttodorov 23:eca4414196ca 217 switch ( mode )
ttodorov 23:eca4414196ca 218 {
ttodorov 23:eca4414196ca 219 case RGB16:
ttodorov 23:eca4414196ca 220 WriteData( color & 0xFFFF );
ttodorov 23:eca4414196ca 221 break;
ttodorov 23:eca4414196ca 222 case RGB18:
ttodorov 23:eca4414196ca 223 r = ( color >> 10 ) & 0xF8;
ttodorov 23:eca4414196ca 224 g = ( color >> 4 ) & 0xFC;
ttodorov 23:eca4414196ca 225 b = ( color >> 1 ) & 0x1F;
ttodorov 23:eca4414196ca 226 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
ttodorov 23:eca4414196ca 227 WriteData( clr );
ttodorov 23:eca4414196ca 228 break;
ttodorov 23:eca4414196ca 229 case RGB24:
ttodorov 23:eca4414196ca 230 r = ( color >> 16 ) & 0xF8;
ttodorov 23:eca4414196ca 231 g = ( color >> 8 ) & 0xFC;
ttodorov 23:eca4414196ca 232 b = color & 0xF8;
ttodorov 23:eca4414196ca 233 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
ttodorov 23:eca4414196ca 234 WriteData( clr );
ttodorov 23:eca4414196ca 235 break;
ttodorov 23:eca4414196ca 236 }
ttodorov 23:eca4414196ca 237 }
ttodorov 23:eca4414196ca 238 else if ( _colorDepth == RGB18 )
ttodorov 23:eca4414196ca 239 {
ttodorov 23:eca4414196ca 240 switch ( mode )
ttodorov 23:eca4414196ca 241 {
ttodorov 23:eca4414196ca 242 case RGB16:
ttodorov 23:eca4414196ca 243 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
ttodorov 23:eca4414196ca 244 g = ( color >> 3 ) & 0xFC;
ttodorov 23:eca4414196ca 245 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
ttodorov 23:eca4414196ca 246 WriteData( ( r << 8 ) | g );
ttodorov 23:eca4414196ca 247 WriteData( b );
ttodorov 23:eca4414196ca 248 break;
ttodorov 23:eca4414196ca 249 case RGB18:
ttodorov 23:eca4414196ca 250 b = ( color << 2 ) & 0xFC;
ttodorov 23:eca4414196ca 251 g = ( color >> 4 ) & 0xFC;
ttodorov 23:eca4414196ca 252 r = ( color >> 10 ) & 0xFC;
ttodorov 23:eca4414196ca 253 WriteData( ( r << 8 ) | g );
ttodorov 23:eca4414196ca 254 WriteData( b );
ttodorov 23:eca4414196ca 255 break;
ttodorov 23:eca4414196ca 256 case RGB24:
ttodorov 23:eca4414196ca 257 r = ( color >> 16 ) & 0xFC;
ttodorov 23:eca4414196ca 258 g = ( color >> 8 ) & 0xFC;
ttodorov 23:eca4414196ca 259 b = color & 0xFC;
ttodorov 23:eca4414196ca 260 WriteData( ( r << 8 ) | g );
ttodorov 23:eca4414196ca 261 WriteData( b );
ttodorov 23:eca4414196ca 262 break;
ttodorov 23:eca4414196ca 263 }
ttodorov 23:eca4414196ca 264 }
ttodorov 23:eca4414196ca 265 }