Gregor Mraczny / TFTLCD_Landtiger
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 
00026 ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RS, PinName WR, PinName DIR, PinName EN, PinName LE, BusOut* DATA_PORT,  PinName RD , PinName BL, backlight_t blType, float defaultBackLightLevel )
00027     : LCD( 240, 320, CS, RS, DIR, EN, LE, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
00028 {
00029     _lcd_port = DATA_PORT;
00030     if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
00031     else _lcd_pin_rd = 0;
00032 }
00033 
00034 void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
00035 {
00036     _orientation = orientation;
00037     _colorDepth = colors;
00038     
00039  /*   _lcd_pin_reset = HIGH;
00040     wait_ms( 50 );
00041     _lcd_pin_reset = LOW;
00042     wait_ms( 100 );
00043     _lcd_pin_reset = HIGH;
00044     wait_ms( 1000 );
00045     */
00046     _lcd_pin_cs = HIGH;
00047     if ( _lcd_pin_bl != 0 )
00048         *_lcd_pin_bl = HIGH;
00049     else if ( _bl_pwm != 0 )
00050         *_bl_pwm = _bl_pwm_default;
00051     if ( _lcd_pin_rd != 0 )
00052         *_lcd_pin_rd = HIGH;
00053     _lcd_pin_wr = HIGH;
00054     wait_ms( 50 );
00055     
00056     Activate();
00057     
00058     short drivOut = 0;
00059     short entryMod = 0;
00060     short gateScan = 0x2700;
00061     switch ( _orientation )
00062     {
00063         case LANDSCAPE:
00064             drivOut = 0x0100;
00065             entryMod |= 0x0038;
00066             gateScan |= 0x0000;
00067             break;
00068             
00069         case LANDSCAPE_REV:
00070             drivOut = 0x0000;
00071             entryMod |= 0x0038;
00072             gateScan |= 0x8000;
00073             break;
00074             
00075         case PORTRAIT_REV:
00076             drivOut = 0x0000;
00077             entryMod |= 0x0030;
00078             gateScan |= 0x0000;
00079             break;
00080             
00081         case PORTRAIT:
00082         default:
00083             drivOut = 0x0100;
00084             entryMod |= 0x0030;
00085             gateScan |= 0x8000;
00086             break;
00087     }
00088     switch ( _colorDepth )
00089     {
00090         case RGB18:
00091             entryMod |= 0x9000;
00092             break;
00093             
00094         case RGB16:
00095         default:
00096             entryMod |= 0x1000;
00097             break;
00098     } 
00099     
00100 //    WriteCmdData( 0xE5, 0x78F0 ); // set SRAM internal timing
00101     WriteCmdData( 0xE5, 0x8000 ); // set SRAM internal timing
00102     WriteCmdData( 0x00, 0x0000 ); // set Driver Output Control
00103     WriteCmdData( 0x01, drivOut ); // set Driver Output Control
00104 //    WriteCmdData( 0x02, 0x0200 ); // set 1 line inversion
00105     WriteCmdData( 0x02, 0x0700 ); // set 1 line inversion
00106     WriteCmdData( 0x03, entryMod ); // set GRAM write direction and BGR=1.
00107     WriteCmdData( 0x04, 0x0000 ); // Resize register
00108 //    WriteCmdData( 0x08, 0x0207 ); // set the back porch and front porch
00109     WriteCmdData( 0x08, 0x0202 ); // set the back porch and front porch
00110  
00111     WriteCmdData( 0x09, 0x0000 ); // set non-display area refresh cycle ISC[3:0]
00112     WriteCmdData( 0x0A, 0x0000 ); // FMARK function
00113     WriteCmdData( 0x0C, 0x0000 ); // RGB interface setting
00114     WriteCmdData( 0x0D, 0x0000 ); // Frame marker Position
00115     WriteCmdData( 0x0F, 0x0000 ); // RGB interface polarity
00116     // ----------- Power On sequence ----------- //
00117     WriteCmdData( 0x10, 0x0000 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
00118 //    WriteCmdData( 0x11, 0x0007 ); // DC1[2:0], DC0[2:0], VC[2:0]
00119     WriteCmdData( 0x11, 0x0000 ); // DC1[2:0], DC0[2:0], VC[2:0]
00120     WriteCmdData( 0x12, 0x0000 ); // VREG1OUT voltage
00121     WriteCmdData( 0x13, 0x0000 ); // VDV[4:0] for VCOM amplitude
00122 //    WriteCmdData( 0x07, 0x0001 );
00123     wait_ms( 200 ); // Dis-charge capacitor power voltage
00124  
00125 //    WriteCmdData( 0x10, 0x1690 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
00126     WriteCmdData( 0x10, 0x17B0 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
00127 //    WriteCmdData( 0x11, 0x0227 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
00128     WriteCmdData( 0x11, 0x0037 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
00129     wait_ms( 50 ); // Delay 50ms
00130  
00131 //    WriteCmdData( 0x12, 0x000D ); // 0012
00132     WriteCmdData( 0x12, 0x0138 ); // 0012
00133     wait_ms( 50 ); // Delay 50ms
00134  
00135 //    WriteCmdData( 0x13, 0x1200 ); // VDV[4:0] for VCOM amplitude
00136     WriteCmdData( 0x13, 0x1700 ); // VDV[4:0] for VCOM amplitude
00137 //    WriteCmdData( 0x29, 0x000A ); // 04  VCM[5:0] for VCOMH
00138     WriteCmdData( 0x29, 0x000D ); // 04  VCM[5:0] for VCOMH
00139     WriteCmdData( 0x2B, 0x000D ); // Set Frame Rate
00140     wait_ms( 50 ); // Delay 50ms
00141  
00142     WriteCmdData( 0x20, 0x0000 ); // GRAM horizontal Address
00143     WriteCmdData( 0x21, 0x0000 ); // GRAM Vertical Address
00144  
00145     // ----------- Adjust the Gamma Curve ----------//
00146 //    WriteCmdData( 0x30, 0x0000 );
00147     WriteCmdData( 0x30, 0x0001 );
00148 //    WriteCmdData( 0x31, 0x0404 );
00149     WriteCmdData( 0x31, 0x0606 );
00150 //    WriteCmdData( 0x32, 0x0003 );
00151     WriteCmdData( 0x32, 0x0304 );
00152 //new line
00153     WriteCmdData( 0x33, 0x0202 );
00154     WriteCmdData( 0x34, 0x0202 );
00155 //    WriteCmdData( 0x35, 0x0405 );
00156     WriteCmdData( 0x35, 0x0103 );
00157  
00158 //    WriteCmdData( 0x36, 0x0808 );
00159     WriteCmdData( 0x36, 0x011D );
00160 //    WriteCmdData( 0x37, 0x0407 );
00161     WriteCmdData( 0x37, 0x0404 );
00162 //    WriteCmdData( 0x38, 0x0303 );
00163     WriteCmdData( 0x38, 0x0404 );
00164 //    WriteCmdData( 0x39, 0x0707 );
00165     WriteCmdData( 0x39, 0x0404 );
00166 //    WriteCmdData( 0x3C, 0x0504 );
00167     WriteCmdData( 0x3C, 0x0700 );
00168 //    WriteCmdData( 0x3D, 0x0808 );
00169     WriteCmdData( 0x3D, 0x0A1F );
00170  
00171     //------------------ Set GRAM area ---------------//
00172     WriteCmdData( 0x50, 0x0000 ); // Horizontal GRAM Start Address
00173     WriteCmdData( 0x51, 0x00EF ); // Horizontal GRAM End Address
00174     WriteCmdData( 0x52, 0x0000 ); // Vertical GRAM Start Address
00175     WriteCmdData( 0x53, 0x013F ); // Vertical GRAM Start Address
00176     WriteCmdData( 0x60, gateScan ); // Gate Scan Line (0xA700)
00177 //    WriteCmdData( 0x61, 0x0000 ); // NDL,VLE, REV
00178     WriteCmdData( 0x61, 0x0001 ); // NDL,VLE, REV
00179     WriteCmdData( 0x6A, 0x0000 ); // set scrolling line
00180     //-------------- Partial Display Control ---------//
00181     WriteCmdData( 0x80, 0x0000 );
00182     WriteCmdData( 0x81, 0x0000 );
00183     WriteCmdData( 0x82, 0x0000 );
00184     WriteCmdData( 0x83, 0x0000 );
00185     WriteCmdData( 0x84, 0x0000 );
00186     WriteCmdData( 0x85, 0x0000 );
00187     //-------------- Panel Control -------------------//
00188     WriteCmdData( 0x90, 0x0010 ); 
00189     WriteCmdData( 0x92, 0x0000 );
00190 //new lines
00191     WriteCmdData( 0x93, 0x0003 );
00192     WriteCmdData( 0x95, 0x0101 );
00193     WriteCmdData( 0x97, 0x0000 );
00194     WriteCmdData( 0x98, 0x0000 );
00195  
00196 //    WriteCmdData( 0x07, 0x0133 ); // 262K color and display ON
00197     WriteCmdData( 0x07, 0x0021 ); // 262K color and display ON
00198     WriteCmdData( 0x07, 0x0031 ); // 262K color and display ON
00199     WriteCmdData( 0x07, 0x0173 ); // 262K color and display ON
00200 //new line       
00201      
00202     wait_ms( 50 ); // Delay 50ms    
00203     Deactivate();
00204 }
00205 
00206 void ILI9328_LCD::Sleep( void )
00207 {
00208     Activate();
00209     WriteCmdData( 0x10, 0x1692 ); // enter sleep mode
00210     wait_ms( 200 );
00211     LCD::Sleep();
00212     Deactivate();
00213 }
00214 
00215 void ILI9328_LCD::WakeUp( void )
00216 {
00217     Activate();
00218     WriteCmdData( 0x10, 0x1690 ); // exit sleep mode
00219     wait_ms( 200 );
00220     LCD::WakeUp();
00221     Deactivate();
00222 }
00223 
00224 void ILI9328_LCD::WriteCmd( unsigned short cmd )
00225 {    
00226     _lcd_pin_cs = LOW;
00227     _lcd_pin_rs = LOW;           // Control Reg 
00228     _lcd_pin_dir = HIGH;
00229     _lcd_pin_en = LOW;
00230     _lcd_port->write(cmd & 0xFF);
00231     _lcd_pin_le = HIGH;
00232     _lcd_pin_le = LOW;
00233     _lcd_port->write((cmd >>8) & 0xFF );
00234     pulseLow( _lcd_pin_wr ); 
00235     _lcd_pin_cs = HIGH;
00236 }
00237 
00238 void ILI9328_LCD::WriteData( unsigned short data )
00239 {
00240     _lcd_pin_cs = LOW;
00241     _lcd_pin_rs = HIGH;       // Select Data
00242     _lcd_pin_dir = HIGH;
00243     _lcd_pin_en = LOW;
00244     _lcd_port->write(data & 0xFF);
00245     _lcd_pin_le = HIGH;
00246     _lcd_pin_le = LOW;
00247     _lcd_port->write((data >> 8) & 0xFF);
00248     pulseLow( _lcd_pin_wr ); 
00249     _lcd_pin_cs = HIGH;
00250 }
00251 
00252 void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
00253 {
00254     switch ( _orientation )
00255     {
00256         case LANDSCAPE:
00257         case LANDSCAPE_REV:
00258             WriteCmdData( 0x20, y1 );
00259             WriteCmdData( 0x21, x1 );
00260             WriteCmdData( 0x50, y1 );
00261             WriteCmdData( 0x52, x1 );
00262             WriteCmdData( 0x51, y2 );
00263             WriteCmdData( 0x53, x2 );
00264             break;
00265             
00266         case PORTRAIT_REV:
00267         case PORTRAIT:
00268         default:
00269             WriteCmdData( 0x20, x1 );
00270             WriteCmdData( 0x21, y1 );
00271             WriteCmdData( 0x50, x1 );
00272             WriteCmdData( 0x52, y1 );
00273             WriteCmdData( 0x51, x2 );
00274             WriteCmdData( 0x53, y2 );
00275             break;
00276     }
00277     WriteCmd( 0x22 );
00278 }
00279 
00280 void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
00281 {
00282     unsigned char r, g, b;
00283     unsigned short clr;
00284     r = g = b = 0;
00285     if ( _colorDepth == RGB16 )
00286     {
00287         switch ( mode )
00288         {
00289             case RGB16:
00290                 WriteData( color & 0xFFFF );
00291                 break;
00292             case RGB18:
00293                 r = ( color >> 10 ) & 0xF8;
00294                 g = ( color >> 4 ) & 0xFC;
00295                 b = ( color >> 1 ) & 0x1F;
00296                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
00297                 WriteData( clr );
00298                 break;
00299             case RGB24:
00300                 r = ( color >> 16 ) & 0xF8;
00301                 g = ( color >> 8 ) & 0xFC;
00302                 b = color & 0xF8;
00303                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
00304                 WriteData( clr );
00305                 break;
00306         }
00307     }
00308     else if ( _colorDepth == RGB18 )
00309     {
00310         switch ( mode )
00311         {
00312             case RGB16:
00313                 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
00314                 g = ( color >> 3 ) & 0xFC;
00315                 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
00316                 break;
00317             case RGB18:
00318                 b = ( color << 2 ) & 0xFC;
00319                 g = ( color >> 4 ) & 0xFC;
00320                 r = ( color >> 10 ) & 0xFC;
00321                 break;
00322             case RGB24:
00323                 r = ( color >> 16 ) & 0xFC;
00324                 g = ( color >> 8 ) & 0xFC;
00325                 b = color & 0xFC;
00326                 break;
00327         }
00328         clr = ( r << 8 ) | ( g << 2 ) | ( b >> 4 );
00329         WriteData( clr );
00330         WriteData( b << 4 );
00331     }
00332 }