1.44 tft lcd display

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

Committer:
ttodorov
Date:
Thu Jun 13 03:47:51 2013 +0000
Revision:
23:eca4414196ca
Child:
24:ac6e35658037
- ILI9328 driver not working yet

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 #define REG_START_OSC 0x00
ttodorov 23:eca4414196ca 26
ttodorov 23:eca4414196ca 27 #define REG_DRIV_OUT_CTRL 0x01
ttodorov 23:eca4414196ca 28 #define REG_DRIV_WAV_CTRL 0x02
ttodorov 23:eca4414196ca 29 #define REG_ENTRY_MOD 0x03
ttodorov 23:eca4414196ca 30 #define REG_RESIZE_CTRL 0x04
ttodorov 23:eca4414196ca 31 #define REG_DISP_CTRL1 0x07
ttodorov 23:eca4414196ca 32 #define REG_DISP_CTRL2 0x08
ttodorov 23:eca4414196ca 33 #define REG_DISP_CTRL3 0x09
ttodorov 23:eca4414196ca 34 #define REG_DISP_CTRL4 0x0A
ttodorov 23:eca4414196ca 35 #define REG_RGB_DISP_IF_CTRL1 0x0C
ttodorov 23:eca4414196ca 36 #define REG_FRM_MARKER_POS 0x0D
ttodorov 23:eca4414196ca 37 #define REG_RGB_DISP_IF_CTRL2 0x0F
ttodorov 23:eca4414196ca 38 #define REG_PWR_CTRL1 0x10
ttodorov 23:eca4414196ca 39 #define REG_PWR_CTRL2 0x11
ttodorov 23:eca4414196ca 40 #define REG_PWR_CTRL3 0x12
ttodorov 23:eca4414196ca 41 #define REG_PWR_CTRL4 0x13
ttodorov 23:eca4414196ca 42 #define REG_GRAM_HORIZONTAL_ADDR 0x20
ttodorov 23:eca4414196ca 43 #define REG_GRAM_VERTICAL_ADDR 0x21
ttodorov 23:eca4414196ca 44 #define REG_GRAM_READWRITE 0x22
ttodorov 23:eca4414196ca 45 #define REG_PWR_CTRL7 0x29
ttodorov 23:eca4414196ca 46 #define REG_FRM_RATE_COL_CTRL 0x2B
ttodorov 23:eca4414196ca 47 #define REG_GAMMA_CTRL1 0x30
ttodorov 23:eca4414196ca 48 #define REG_GAMMA_CTRL2 0x31
ttodorov 23:eca4414196ca 49 #define REG_GAMMA_CTRL3 0x32
ttodorov 23:eca4414196ca 50 #define REG_GAMMA_CTRL4 0x35
ttodorov 23:eca4414196ca 51 #define REG_GAMMA_CTRL5 0x36
ttodorov 23:eca4414196ca 52 #define REG_GAMMA_CTRL6 0x37
ttodorov 23:eca4414196ca 53 #define REG_GAMMA_CTRL7 0x38
ttodorov 23:eca4414196ca 54 #define REG_GAMMA_CTRL8 0x39
ttodorov 23:eca4414196ca 55 #define REG_GAMMA_CTRL9 0x3C
ttodorov 23:eca4414196ca 56 #define REG_GAMMA_CTRL10 0x3D
ttodorov 23:eca4414196ca 57 #define REG_HORIZONTAL_START_ADDR 0x50
ttodorov 23:eca4414196ca 58 #define REG_HORIZONTAL_END_ADDR 0x51
ttodorov 23:eca4414196ca 59 #define REG_VERTICAL_START_ADDR 0x52
ttodorov 23:eca4414196ca 60 #define REG_VERTICAL_END_ADDR 0x53
ttodorov 23:eca4414196ca 61 #define REG_GATE_SCAN_CTRL1 0x60
ttodorov 23:eca4414196ca 62 #define REG_GATE_SCAN_CTRL2 0x61
ttodorov 23:eca4414196ca 63 #define REG_GATE_SCAN_CTRL3 0x6A
ttodorov 23:eca4414196ca 64 #define REG_PART_IMG1_DISP_POS 0x80
ttodorov 23:eca4414196ca 65 #define REG_PART_IMG1_START_ADDR 0x81
ttodorov 23:eca4414196ca 66 #define REG_PART_IMG1_END_ADDR 0x82
ttodorov 23:eca4414196ca 67 #define REG_PART_IMG2_DISP_POS 0x83
ttodorov 23:eca4414196ca 68 #define REG_PART_IMG2_START_ADDR 0x84
ttodorov 23:eca4414196ca 69 #define REG_PART_IMG2_END_ADDR 0x85
ttodorov 23:eca4414196ca 70 #define REG_PANEL_IF_CTRL1 0x90
ttodorov 23:eca4414196ca 71 #define REG_PANEL_IF_CTRL2 0x92
ttodorov 23:eca4414196ca 72 #define REG_PANEL_IF_CTRL3 0x93
ttodorov 23:eca4414196ca 73 #define REG_PANEL_IF_CTRL4 0x95
ttodorov 23:eca4414196ca 74 #define REG_PANEL_IF_CTRL5 0x97
ttodorov 23:eca4414196ca 75 #define REG_PANEL_IF_CTRL6 0x98
ttodorov 23:eca4414196ca 76
ttodorov 23:eca4414196ca 77 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 78 : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
ttodorov 23:eca4414196ca 79 {
ttodorov 23:eca4414196ca 80 _lcd_port = DATA_PORT;
ttodorov 23:eca4414196ca 81 if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
ttodorov 23:eca4414196ca 82 else _lcd_pin_rd = 0;
ttodorov 23:eca4414196ca 83 }
ttodorov 23:eca4414196ca 84
ttodorov 23:eca4414196ca 85 void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
ttodorov 23:eca4414196ca 86 {
ttodorov 23:eca4414196ca 87 _orientation = orientation;
ttodorov 23:eca4414196ca 88 _colorDepth = colors;
ttodorov 23:eca4414196ca 89
ttodorov 23:eca4414196ca 90 _lcd_pin_reset = HIGH;
ttodorov 23:eca4414196ca 91 wait_ms( 50 );
ttodorov 23:eca4414196ca 92 _lcd_pin_reset = LOW;
ttodorov 23:eca4414196ca 93 wait_ms( 100 );
ttodorov 23:eca4414196ca 94 _lcd_pin_reset = HIGH;
ttodorov 23:eca4414196ca 95 wait_ms( 1000 );
ttodorov 23:eca4414196ca 96 _lcd_pin_cs = HIGH;
ttodorov 23:eca4414196ca 97 if ( _lcd_pin_bl != 0 )
ttodorov 23:eca4414196ca 98 *_lcd_pin_bl = HIGH;
ttodorov 23:eca4414196ca 99 else if ( _bl_pwm != 0 )
ttodorov 23:eca4414196ca 100 *_bl_pwm = _bl_pwm_default;
ttodorov 23:eca4414196ca 101 if ( _lcd_pin_rd != 0 )
ttodorov 23:eca4414196ca 102 *_lcd_pin_rd = HIGH;
ttodorov 23:eca4414196ca 103 _lcd_pin_wr = HIGH;
ttodorov 23:eca4414196ca 104 wait_ms( 15 );
ttodorov 23:eca4414196ca 105
ttodorov 23:eca4414196ca 106 Activate();
ttodorov 23:eca4414196ca 107 //WriteCmdData( 0x00, 0x0001 ); // oscillator: 1 = on, 0 = off
ttodorov 23:eca4414196ca 108 //wait_ms( 1 );
ttodorov 23:eca4414196ca 109 WriteCmdData( REG_DRIV_OUT_CTRL, 0x0100 ); // Driver Output Control Register (R01h)
ttodorov 23:eca4414196ca 110 WriteCmdData( REG_DRIV_WAV_CTRL, 0x0700 ); // LCD Driving Waveform Control (R02h)
ttodorov 23:eca4414196ca 111 WriteCmdData( REG_ENTRY_MOD, 0x1030 ); // Entry Mode (R03h) (BGR=on;ID1=on;ID2=on;AM=0)
ttodorov 23:eca4414196ca 112 WriteCmdData( REG_DISP_CTRL2, 0x0302 );
ttodorov 23:eca4414196ca 113 WriteCmdData( REG_DISP_CTRL3, 0x0000 );
ttodorov 23:eca4414196ca 114 WriteCmdData( REG_DISP_CTRL4, 0x0000 ); // Fmark On
ttodorov 23:eca4414196ca 115 WriteCmdData( REG_PWR_CTRL1, 0x0000 ); // Power Control 1 (R10h)
ttodorov 23:eca4414196ca 116 WriteCmdData( REG_PWR_CTRL2, 0x0007 ); // Power Control 2 (R11h)
ttodorov 23:eca4414196ca 117 WriteCmdData( REG_PWR_CTRL3, 0x0000 ); // Power Control 3 (R12h)
ttodorov 23:eca4414196ca 118 WriteCmdData( REG_PWR_CTRL4, 0x0000 ); // Power Control 4 (R13h)
ttodorov 23:eca4414196ca 119 wait_ms( 1000 );
ttodorov 23:eca4414196ca 120 WriteCmdData( REG_PWR_CTRL1, 0x14B0 ); // Power Control 1 (R10h)
ttodorov 23:eca4414196ca 121 wait_ms( 500 );
ttodorov 23:eca4414196ca 122 WriteCmdData( REG_PWR_CTRL2, 0x0007 ); // Power Control 2 (R11h)
ttodorov 23:eca4414196ca 123 wait_ms( 500 );
ttodorov 23:eca4414196ca 124 WriteCmdData( REG_PWR_CTRL3, 0x008E ); // Power Control 3 (R12h)
ttodorov 23:eca4414196ca 125 WriteCmdData( REG_PWR_CTRL4, 0x0C00 ); // Power Control 4 (R13h)
ttodorov 23:eca4414196ca 126 WriteCmdData( REG_PWR_CTRL7, 0x0015 ); // NVM read data 2 (R29h)
ttodorov 23:eca4414196ca 127 wait_ms( 500 );
ttodorov 23:eca4414196ca 128 WriteCmdData( REG_GAMMA_CTRL1, 0x0000 ); // Gamma Control 1
ttodorov 23:eca4414196ca 129 WriteCmdData( REG_GAMMA_CTRL2, 0x0107 ); // Gamma Control 2
ttodorov 23:eca4414196ca 130 WriteCmdData( REG_GAMMA_CTRL3, 0x0000 ); // Gamma Control 3
ttodorov 23:eca4414196ca 131 WriteCmdData( REG_GAMMA_CTRL4, 0x0203 ); // Gamma Control 4
ttodorov 23:eca4414196ca 132 WriteCmdData( REG_GAMMA_CTRL5, 0x0402 ); // Gamma Control 5
ttodorov 23:eca4414196ca 133 WriteCmdData( REG_GAMMA_CTRL6, 0x0000 ); // Gamma Control 6
ttodorov 23:eca4414196ca 134 WriteCmdData( REG_GAMMA_CTRL7, 0x0207 ); // Gamma Control 7
ttodorov 23:eca4414196ca 135 WriteCmdData( REG_GAMMA_CTRL8, 0x0000 ); // Gamma Control 8
ttodorov 23:eca4414196ca 136 WriteCmdData( REG_GAMMA_CTRL9, 0x0203 ); // Gamma Control 9
ttodorov 23:eca4414196ca 137 WriteCmdData( REG_GAMMA_CTRL10, 0x0403 ); // Gamma Control 10
ttodorov 23:eca4414196ca 138 WriteCmdData( REG_HORIZONTAL_START_ADDR, 0x0000 ); // Window Horizontal RAM Address Start (R50h)
ttodorov 23:eca4414196ca 139 WriteCmdData( REG_HORIZONTAL_END_ADDR, _disp_width - 1); // Window Horizontal RAM Address End (R51h)
ttodorov 23:eca4414196ca 140 WriteCmdData( REG_VERTICAL_START_ADDR, 0x0000 ); // Window Vertical RAM Address Start (R52h)
ttodorov 23:eca4414196ca 141 WriteCmdData( REG_VERTICAL_END_ADDR, _disp_height - 1); // Window Vertical RAM Address End (R53h)
ttodorov 23:eca4414196ca 142 WriteCmdData( REG_GATE_SCAN_CTRL1, 0xa700 ); // Driver Output Control (R60h)
ttodorov 23:eca4414196ca 143 WriteCmdData( REG_GATE_SCAN_CTRL2, 0x0003 ); // Driver Output Control (R61h) - enable VLE
ttodorov 23:eca4414196ca 144 WriteCmdData( REG_PANEL_IF_CTRL1, 0x0010 ); // Panel Interface Control 1 (R90h)
ttodorov 23:eca4414196ca 145
ttodorov 23:eca4414196ca 146 // Display On
ttodorov 23:eca4414196ca 147 WriteCmdData( REG_DISP_CTRL1, 0x0133 ); // Display Control (R07h)
ttodorov 23:eca4414196ca 148 wait_ms( 500 );
ttodorov 23:eca4414196ca 149 WriteCmd( REG_GRAM_READWRITE );
ttodorov 23:eca4414196ca 150
ttodorov 23:eca4414196ca 151 Deactivate();
ttodorov 23:eca4414196ca 152 }
ttodorov 23:eca4414196ca 153
ttodorov 23:eca4414196ca 154 void ILI9328_LCD::Sleep( void )
ttodorov 23:eca4414196ca 155 {
ttodorov 23:eca4414196ca 156 //WriteCmdData( 0x10, 0x0001 ); // sleep mode: 0 = exit, 1 = enter
ttodorov 23:eca4414196ca 157 LCD::Sleep();
ttodorov 23:eca4414196ca 158 }
ttodorov 23:eca4414196ca 159
ttodorov 23:eca4414196ca 160 void ILI9328_LCD::WakeUp( void )
ttodorov 23:eca4414196ca 161 {
ttodorov 23:eca4414196ca 162 //WriteCmdData( 0x10, 0x0000 ); // sleep mode: 0 = exit, 1 = enter
ttodorov 23:eca4414196ca 163 LCD::WakeUp();
ttodorov 23:eca4414196ca 164 }
ttodorov 23:eca4414196ca 165
ttodorov 23:eca4414196ca 166 void ILI9328_LCD::WriteCmd( unsigned short cmd )
ttodorov 23:eca4414196ca 167 {
ttodorov 23:eca4414196ca 168 _lcd_pin_rs = LOW;
ttodorov 23:eca4414196ca 169 _lcd_port->write( cmd );
ttodorov 23:eca4414196ca 170 pulseLow( _lcd_pin_wr );
ttodorov 23:eca4414196ca 171 }
ttodorov 23:eca4414196ca 172
ttodorov 23:eca4414196ca 173 void ILI9328_LCD::WriteData( unsigned short data )
ttodorov 23:eca4414196ca 174 {
ttodorov 23:eca4414196ca 175 _lcd_pin_rs = HIGH;
ttodorov 23:eca4414196ca 176 _lcd_port->write( data );
ttodorov 23:eca4414196ca 177 pulseLow( _lcd_pin_wr );
ttodorov 23:eca4414196ca 178 }
ttodorov 23:eca4414196ca 179
ttodorov 23:eca4414196ca 180 void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
ttodorov 23:eca4414196ca 181 {
ttodorov 23:eca4414196ca 182 /*
ttodorov 23:eca4414196ca 183 if ( _orientation == PORTRAIT || _orientation == PORTRAIT_REV )
ttodorov 23:eca4414196ca 184 {
ttodorov 23:eca4414196ca 185 WriteCmdData( 0x44, ( x2 << 8 ) + x1 );
ttodorov 23:eca4414196ca 186 WriteCmdData( 0x45, y1 );
ttodorov 23:eca4414196ca 187 WriteCmdData( 0x46, y2 );
ttodorov 23:eca4414196ca 188 WriteCmdData( 0x4e, x1 );
ttodorov 23:eca4414196ca 189 WriteCmdData( 0x4f, y1 );
ttodorov 23:eca4414196ca 190 }
ttodorov 23:eca4414196ca 191 else
ttodorov 23:eca4414196ca 192 {
ttodorov 23:eca4414196ca 193 WriteCmdData( 0x44, ( y2 << 8 ) + y1 );
ttodorov 23:eca4414196ca 194 WriteCmdData( 0x45, x1 );
ttodorov 23:eca4414196ca 195 WriteCmdData( 0x46, x2 );
ttodorov 23:eca4414196ca 196 WriteCmdData( 0x4e, y1 );
ttodorov 23:eca4414196ca 197 WriteCmdData( 0x4f, x1 );
ttodorov 23:eca4414196ca 198 }
ttodorov 23:eca4414196ca 199 */
ttodorov 23:eca4414196ca 200 WriteCmdData( REG_HORIZONTAL_START_ADDR, x1 );
ttodorov 23:eca4414196ca 201 WriteCmdData( REG_HORIZONTAL_END_ADDR, x2 );
ttodorov 23:eca4414196ca 202 WriteCmdData( REG_VERTICAL_START_ADDR, y1 );
ttodorov 23:eca4414196ca 203 WriteCmdData( REG_VERTICAL_END_ADDR, y2 );
ttodorov 23:eca4414196ca 204 WriteCmdData( REG_GRAM_HORIZONTAL_ADDR, x1 );
ttodorov 23:eca4414196ca 205 WriteCmdData( REG_GRAM_VERTICAL_ADDR, y1 );
ttodorov 23:eca4414196ca 206 WriteCmd( 0x22 );
ttodorov 23:eca4414196ca 207 }
ttodorov 23:eca4414196ca 208
ttodorov 23:eca4414196ca 209 void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
ttodorov 23:eca4414196ca 210 {
ttodorov 23:eca4414196ca 211 unsigned char r, g, b;
ttodorov 23:eca4414196ca 212 unsigned short clr;
ttodorov 23:eca4414196ca 213 if ( _colorDepth == RGB16 )
ttodorov 23:eca4414196ca 214 {
ttodorov 23:eca4414196ca 215 switch ( mode )
ttodorov 23:eca4414196ca 216 {
ttodorov 23:eca4414196ca 217 case RGB16:
ttodorov 23:eca4414196ca 218 WriteData( color & 0xFFFF );
ttodorov 23:eca4414196ca 219 break;
ttodorov 23:eca4414196ca 220 case RGB18:
ttodorov 23:eca4414196ca 221 r = ( color >> 10 ) & 0xF8;
ttodorov 23:eca4414196ca 222 g = ( color >> 4 ) & 0xFC;
ttodorov 23:eca4414196ca 223 b = ( color >> 1 ) & 0x1F;
ttodorov 23:eca4414196ca 224 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
ttodorov 23:eca4414196ca 225 WriteData( clr );
ttodorov 23:eca4414196ca 226 break;
ttodorov 23:eca4414196ca 227 case RGB24:
ttodorov 23:eca4414196ca 228 r = ( color >> 16 ) & 0xF8;
ttodorov 23:eca4414196ca 229 g = ( color >> 8 ) & 0xFC;
ttodorov 23:eca4414196ca 230 b = color & 0xF8;
ttodorov 23:eca4414196ca 231 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
ttodorov 23:eca4414196ca 232 WriteData( clr );
ttodorov 23:eca4414196ca 233 break;
ttodorov 23:eca4414196ca 234 }
ttodorov 23:eca4414196ca 235 }
ttodorov 23:eca4414196ca 236 else if ( _colorDepth == RGB18 )
ttodorov 23:eca4414196ca 237 {
ttodorov 23:eca4414196ca 238 switch ( mode )
ttodorov 23:eca4414196ca 239 {
ttodorov 23:eca4414196ca 240 case RGB16:
ttodorov 23:eca4414196ca 241 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
ttodorov 23:eca4414196ca 242 g = ( color >> 3 ) & 0xFC;
ttodorov 23:eca4414196ca 243 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
ttodorov 23:eca4414196ca 244 WriteData( ( r << 8 ) | g );
ttodorov 23:eca4414196ca 245 WriteData( b );
ttodorov 23:eca4414196ca 246 break;
ttodorov 23:eca4414196ca 247 case RGB18:
ttodorov 23:eca4414196ca 248 b = ( color << 2 ) & 0xFC;
ttodorov 23:eca4414196ca 249 g = ( color >> 4 ) & 0xFC;
ttodorov 23:eca4414196ca 250 r = ( color >> 10 ) & 0xFC;
ttodorov 23:eca4414196ca 251 WriteData( ( r << 8 ) | g );
ttodorov 23:eca4414196ca 252 WriteData( b );
ttodorov 23:eca4414196ca 253 break;
ttodorov 23:eca4414196ca 254 case RGB24:
ttodorov 23:eca4414196ca 255 r = ( color >> 16 ) & 0xFC;
ttodorov 23:eca4414196ca 256 g = ( color >> 8 ) & 0xFC;
ttodorov 23:eca4414196ca 257 b = color & 0xFC;
ttodorov 23:eca4414196ca 258 WriteData( ( r << 8 ) | g );
ttodorov 23:eca4414196ca 259 WriteData( b );
ttodorov 23:eca4414196ca 260 break;
ttodorov 23:eca4414196ca 261 }
ttodorov 23:eca4414196ca 262 }
ttodorov 23:eca4414196ca 263 }