mcufriend 2.4 TFT LCD Shield Lib

Dependents:   Nucleo_LCD_mcufriend_test

Fork of 24_TFT_STMNUCLEO by Carlos Silva

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ili9328.cpp Source File

ili9328.cpp

00001 /*
00002  * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
00003  * Copyright (C)2012-2013 Todor Todorov.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to:
00017  *
00018  * Free Software Foundation, Inc.
00019  * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
00020  *
00021  *********************************************************************/
00022 #include "ili9328.h"
00023 #include "helpers.h"
00024 
00025 ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
00026     : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
00027 {
00028     _lcd_port = DATA_PORT;
00029     if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
00030     else _lcd_pin_rd = 0;
00031 }
00032 
00033 void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
00034 {
00035     _orientation = orientation;
00036     _colorDepth = colors;
00037     
00038     _lcd_pin_reset = HIGH;
00039     wait_ms( 10 );
00040     _lcd_pin_reset = LOW;
00041     wait_ms( 10 );
00042     _lcd_pin_reset = HIGH;
00043     wait_ms( 10 );
00044     _lcd_pin_cs = HIGH;
00045     if ( _lcd_pin_bl != 0 )
00046         *_lcd_pin_bl = HIGH;
00047     else if ( _bl_pwm != 0 )
00048         *_bl_pwm = _bl_pwm_default;
00049     if ( _lcd_pin_rd != 0 )
00050         *_lcd_pin_rd = HIGH;
00051     _lcd_pin_wr = HIGH;
00052     wait_ms( 5 );
00053     
00054     Activate();
00055     
00056     short drivOut = 0;
00057     short entryMod = 0;
00058     short gateScan = 0x2700;
00059     switch ( _orientation )
00060     {
00061         case LANDSCAPE:
00062             drivOut = 0x0100;
00063             entryMod |= 0x0038;
00064             gateScan |= 0x0000;
00065             break;
00066             
00067         case LANDSCAPE_REV:
00068             drivOut = 0x0000;
00069             entryMod |= 0x0038;
00070             gateScan |= 0x8000;
00071             break;
00072             
00073         case PORTRAIT_REV:
00074             drivOut = 0x0000;
00075             entryMod |= 0x0030;
00076             gateScan |= 0x0000;
00077             break;
00078             
00079         case PORTRAIT:
00080         default:
00081             drivOut = 0x0100;
00082             entryMod |= 0x0030;
00083             gateScan |= 0x8000;
00084             break;
00085     }
00086     switch ( _colorDepth )
00087     {
00088         case RGB18:
00089             entryMod |= 0x9000;
00090             break;
00091             
00092         case RGB16:
00093         default:
00094             entryMod |= 0x1030;
00095             break;
00096     }
00097     
00098     //WriteCmdData( 0xE5, 0xFFFF ); // set SRAM internal timing
00099     WriteCmdData( 0x0001,0x0100);//0x01, drivOut ); // set Driver Output Control
00100     WriteCmdData( 0x0002,0x0700 ); // set 1 line inversion
00101     WriteCmdData( 0x0003,entryMod );//0x03, entryMod ); // set GRAM write direction and BGR=1.
00102     //WriteCmdData( 0x04, 0x0000 ); // Resize register
00103     WriteCmdData( 0x0008,0x0302 ); // set the back porch and front porch
00104     WriteCmdData( 0x0009,0x0000 ); // set non-display area refresh cycle ISC[3:0]
00105     WriteCmdData( 0x000A,0x0008 ); // FMARK function
00106     //WriteCmdData( 0x0C, 0x0000 ); // RGB interface setting
00107     //WriteCmdData( 0x0D, 0x0000 ); // Frame marker Position
00108     //WriteCmdData( 0x0F, 0x0000 ); // RGB interface polarity
00109     // ----------- Power On sequence ----------- //
00110     WriteCmdData( 0x0010,0x0790 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
00111     WriteCmdData( 0x0011,0x0005 ); // DC1[2:0], DC0[2:0], VC[2:0]
00112     WriteCmdData( 0x0012,0x0000 ); // VREG1OUT voltage
00113     WriteCmdData( 0x0013,0x000 ); // VDV[4:0] for VCOM amplitude
00114     //WriteCmdData( 0x07, 0x0001 );
00115     //wait_ms( 50 ); // Dis-charge capacitor power voltage
00116     WriteCmdData( 0x0010,0x12B0 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
00117     WriteCmdData( 0x0011,0x0007 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
00118     //wait_ms( 50 ); // Delay 50ms
00119     WriteCmdData( 0x0012,0x008C ); // 0012
00120     //wait_ms( 50 ); // Delay 50ms
00121     WriteCmdData( 0x0013,0x1700 ); // VDV[4:0] for VCOM amplitude
00122     WriteCmdData( 0x0029,0x0022 ); // 04  VCM[5:0] for VCOMH
00123     //WriteCmdData( 0x2B, 0x00FD ); // Set Frame Rate
00124     //wait_ms( 50 ); // Delay 50ms
00125     //WriteCmdData( 0x20, 0x0000 ); // GRAM horizontal Address
00126     //WriteCmdData( 0x21, 0x0000 ); // GRAM Vertical Address
00127     // ----------- Adjust the Gamma Curve ----------//
00128     WriteCmdData( 0x0030,0x0000 );
00129     WriteCmdData( 0x0031,0x0505 );
00130     WriteCmdData( 0x0032,0x0205 );
00131     WriteCmdData( 0x0035,0x0206 );
00132     WriteCmdData( 0x0036,0x0408 );
00133     WriteCmdData( 0x0037,0x0000 );
00134     WriteCmdData( 0x0038,0x0504 );
00135     WriteCmdData( 0x0039,0x0206 );
00136     WriteCmdData( 0x003C,0x0206 );
00137     WriteCmdData( 0x003D,0x0408 );
00138     //------------------ Set GRAM area ---------------//
00139     WriteCmdData( 0x50, 0x0000 ); // Horizontal GRAM Start Address
00140     WriteCmdData( 0x51, 0x00EF ); // Horizontal GRAM End Address
00141     WriteCmdData( 0x52, 0x0000 ); // Vertical GRAM Start Address
00142     WriteCmdData( 0x53, 0x013F ); // Vertical GRAM Start Address
00143     WriteCmdData( 0x60, gateScan ); // Gate Scan Line (0xA700)
00144     WriteCmdData( 0x0061,0x0001 ); // NDL,VLE, REV
00145     //WriteCmdData( 0x6A, 0x0000 ); // set scrolling line
00146     //-------------- Partial Display Control ---------//
00147     //WriteCmdData( 0x80, 0x0000 );
00148     //WriteCmdData( 0x81, 0x0000 );
00149     //WriteCmdData( 0x82, 0x0000 );
00150     //WriteCmdData( 0x83, 0x0000 );
00151     //WriteCmdData( 0x84, 0x0000 );
00152     //WriteCmdData( 0x85, 0x0000 );
00153     //-------------- Panel Control -------------------//
00154     WriteCmdData( 0x90, 0x0033 ); 
00155     //WriteCmdData( 0x92, 0x0000 );
00156     WriteCmdData( 0x0007,0x0133 ); // 262K color and display ON
00157     
00158     Deactivate();
00159 }
00160 
00161 void ILI9328_LCD::Sleep( void )
00162 {
00163     Activate();
00164     WriteCmdData( 0x10, 0x1692 ); // enter sleep mode
00165     wait_ms( 50 );
00166     LCD::Sleep();
00167     Deactivate();
00168 }
00169 
00170 void ILI9328_LCD::WakeUp( void )
00171 {
00172     Activate();
00173     WriteCmdData( 0x10, 0x1690 ); // exit sleep mode
00174     wait_ms( 50 );
00175     LCD::WakeUp();
00176     Deactivate();
00177 }
00178 
00179 void ILI9328_LCD::WriteCmd( unsigned short cmd )
00180 {
00181 
00182     unsigned short u,l;
00183     u = (cmd>>8) & 0xFF;
00184     l = cmd & 0xFF;
00185 
00186 //New
00187     _lcd_pin_cs = LOW;
00188 //
00189     _lcd_pin_rs = LOW;
00190 
00191 //New
00192     *_lcd_pin_rd = HIGH;    
00193     _lcd_pin_wr = HIGH;  
00194 //
00195     _lcd_port->write( u );
00196     pulseLow( _lcd_pin_wr );
00197 
00198     _lcd_port->write( l );
00199     pulseLow( _lcd_pin_wr );
00200 
00201 //New
00202     _lcd_pin_cs = HIGH;
00203 //
00204 }
00205 
00206 void ILI9328_LCD::WriteData( unsigned short data )
00207 {
00208     unsigned short u,l;
00209     u = (data>>8) & 0xFF;
00210     l = data & 0xFF;
00211     
00212 //New
00213     _lcd_pin_cs = LOW;
00214 //
00215     _lcd_pin_rs = HIGH;
00216 //New
00217     *_lcd_pin_rd = HIGH;    
00218     _lcd_pin_wr = HIGH;  
00219 //
00220     _lcd_port->write( u );
00221     pulseLow( _lcd_pin_wr );
00222 
00223     _lcd_port->write( l );
00224     pulseLow( _lcd_pin_wr );
00225 
00226 //New
00227     _lcd_pin_cs = HIGH;
00228 //
00229 }
00230 
00231 void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
00232 {
00233     switch ( _orientation )
00234     {
00235         case LANDSCAPE:
00236         case LANDSCAPE_REV:
00237             WriteCmdData( 0x20, y1 );
00238             WriteCmdData( 0x21, x1 );
00239             WriteCmdData( 0x50, y1 );
00240             WriteCmdData( 0x52, x1 );
00241             WriteCmdData( 0x51, y2 );
00242             WriteCmdData( 0x53, x2 );
00243             break;
00244             
00245         case PORTRAIT_REV:
00246         case PORTRAIT:
00247         default:
00248             WriteCmdData( 0x20, x1 );
00249             WriteCmdData( 0x21, y1 );
00250             WriteCmdData( 0x50, x1 );
00251             WriteCmdData( 0x52, y1 );
00252             WriteCmdData( 0x51, x2 );
00253             WriteCmdData( 0x53, y2 );
00254             break;
00255     }
00256     WriteCmd( 0x22 );
00257 }
00258 
00259 void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
00260 {
00261     unsigned char r, g, b;
00262     unsigned short clr;
00263     r = g = b = 0;
00264     if ( _colorDepth == RGB16 )
00265     {
00266         switch ( mode )
00267         {
00268             case RGB16:
00269                 WriteData( color & 0xFFFF );
00270                 break;
00271             case RGB18:
00272                 r = ( color >> 10 ) & 0xF8;
00273                 g = ( color >> 4 ) & 0xFC;
00274                 b = ( color >> 1 ) & 0x1F;
00275                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
00276                 WriteData( clr );
00277                 break;
00278             case RGB24:
00279                 r = ( color >> 16 ) & 0xF8;
00280                 g = ( color >> 8 ) & 0xFC;
00281                 b = color & 0xF8;
00282                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
00283                 WriteData( clr );
00284                 break;
00285         }
00286     }
00287     else if ( _colorDepth == RGB18 )
00288     {
00289         switch ( mode )
00290         {
00291             case RGB16:
00292                 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
00293                 g = ( color >> 3 ) & 0xFC;
00294                 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
00295                 break;
00296             case RGB18:
00297                 b = ( color << 2 ) & 0xFC;
00298                 g = ( color >> 4 ) & 0xFC;
00299                 r = ( color >> 10 ) & 0xFC;
00300                 break;
00301             case RGB24:
00302                 r = ( color >> 16 ) & 0xFC;
00303                 g = ( color >> 8 ) & 0xFC;
00304                 b = color & 0xFC;
00305                 break;
00306         }
00307         clr = ( r << 8 ) | ( g << 2 ) | ( b >> 4 );
00308         WriteData( clr );
00309         WriteData( b << 4 );
00310     }
00311 }