TFTLCD with FastIO

Fork of TFTLCD by en 129

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hx8340bs.cpp Source File

hx8340bs.cpp

00001 /*
00002  * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
00003  * Copyright (C)2012 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 "hx8340bs.h"
00023 #include "helpers.h"
00024 
00025 HX8340S_LCD::HX8340S_LCD( PinName CS, PinName RESET, PinName SCL, PinName SDI, PinName BL, backlight_t blType, float defaultBackLightLevel )
00026     : LCD( 176, 220, CS, NC, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_scl( SCL ), _lcd_pin_sdi( SDI )
00027 {
00028 }
00029 
00030 void HX8340S_LCD::Initialize( orientation_t orientation, colordepth_t colors )
00031 {
00032     _orientation = orientation;
00033     _colorDepth = colors;
00034     
00035     wait_ms( 100 );
00036     _lcd_pin_reset = HIGH;
00037     wait_ms( 5 );
00038     _lcd_pin_reset = LOW;
00039     wait_ms( 15 );
00040     _lcd_pin_reset = HIGH;
00041     _lcd_pin_cs = HIGH;
00042     _lcd_pin_scl = HIGH;
00043     _lcd_pin_sdi = HIGH;
00044     if ( _lcd_pin_bl != 0 )
00045         *_lcd_pin_bl = HIGH;
00046     else if ( _bl_pwm != 0 )
00047         *_bl_pwm = _bl_pwm_default;
00048     wait_ms( 55 );
00049     
00050     Activate();
00051     WriteCmd( 0xC1 ); // SETEXTCMD
00052     WriteByteData( 0xFF );
00053     WriteByteData( 0x83 );
00054     WriteByteData( 0x40 );
00055     
00056     WriteCmd( 0x11 ); // SLPOUT
00057     wait_ms( 160 );
00058     
00059     WriteCmd( 0xCA );
00060     WriteByteData( 0x70 );
00061     WriteByteData( 0x00 );
00062     WriteByteData( 0xD9 );
00063     
00064     WriteCmd( 0xB0 ); // SETOSC
00065     WriteByteData( 0x01 );
00066     WriteByteData( 0x11 );
00067 
00068     WriteCmd( 0xC9 );
00069     WriteByteData( 0x90 );
00070     WriteByteData( 0x49 );
00071     WriteByteData( 0x10 );
00072     WriteByteData( 0x28 );
00073     WriteByteData( 0x28 );
00074     WriteByteData( 0x10 );
00075     WriteByteData( 0x00 );
00076     WriteByteData( 0x06 );
00077     wait_ms( 20 );
00078     
00079     WriteCmd( 0xC2 ); // SETGAMMAP
00080     WriteByteData( 0x60 );
00081     WriteByteData( 0x71 );
00082     WriteByteData( 0x01 );
00083     WriteByteData( 0x0E );
00084     WriteByteData( 0x05 );
00085     WriteByteData( 0x02 );
00086     WriteByteData( 0x09 );
00087     WriteByteData( 0x31 );
00088     WriteByteData( 0x0A );
00089 
00090     WriteCmd( 0xc3 ); // SETGAMMAN
00091     WriteByteData( 0x67 );
00092     WriteByteData( 0x30 );
00093     WriteByteData( 0x61 );
00094     WriteByteData( 0x17 );
00095     WriteByteData( 0x48 );
00096     WriteByteData( 0x07 );
00097     WriteByteData( 0x05 );
00098     WriteByteData( 0x33 );
00099     wait_ms( 10 );
00100     
00101     WriteCmd( 0xB5 ); // SETPWCTR5
00102     WriteByteData( 0x35 );
00103     WriteByteData( 0x20 );
00104     WriteByteData( 0x45 );
00105 
00106     WriteCmd( 0xB4 ); // SETPWCTR4
00107     WriteByteData( 0x33 );
00108     WriteByteData( 0x25 );
00109     WriteByteData( 0x4c );
00110     wait_ms( 10 );
00111     
00112     WriteCmd( 0x3A ); // COLMOD == color depth: 0x05 => 16bit, 0x06 => 18bit
00113     WriteByteData( _colorDepth == RGB16 ? 0x05 : 0x06 );
00114     
00115     WriteCmd( 0x36 ); // MADCTL
00116     switch ( _orientation )
00117     {
00118         case LANDSCAPE: WriteByteData( 0xB8 ); break;
00119         case PORTRAIT_REV: WriteByteData( 0xDC ); break;
00120         case LANDSCAPE_REV: WriteByteData( 0x6C ); break;
00121         case PORTRAIT:
00122         default: WriteByteData( 0x08 ); break;
00123     }
00124     
00125     WriteCmd( 0x29 ); // DISPON
00126     wait_ms( 10 );
00127     
00128     ClearXY();
00129     Deactivate();
00130 }
00131 
00132 void HX8340S_LCD::Sleep( void )
00133 {
00134     Activate();
00135     WriteCmd( 0x28 );
00136     wait_ms( 10 );
00137     WriteCmd( 0x10 );
00138     wait_ms( 125 );
00139     LCD::Sleep();
00140     Deactivate();
00141 }
00142 
00143 void HX8340S_LCD::WakeUp( void )
00144 {
00145     Activate();
00146     WriteCmd( 0x29 );
00147     wait_ms( 10 );
00148     WriteCmd( 0x11 );
00149     wait_ms( 125 );
00150     LCD::WakeUp();
00151     Deactivate();
00152 }
00153 
00154 void HX8340S_LCD::WriteCmd( unsigned short cmd )
00155 {
00156     _lcd_pin_sdi = LOW;
00157     pulseLow( _lcd_pin_scl );
00158     serializeByte( cmd & 0xFF );
00159 }
00160 
00161 void HX8340S_LCD::WriteData( unsigned short data )
00162 {
00163     _lcd_pin_sdi = HIGH;
00164     pulseLow( _lcd_pin_scl );
00165     serializeByte( ( data >> 8 ) & 0xFF );
00166     _lcd_pin_sdi = HIGH;
00167     pulseLow( _lcd_pin_scl );
00168     serializeByte( data & 0xFF );
00169 }
00170 
00171 void HX8340S_LCD::WriteByteData( unsigned char data )
00172 {
00173     _lcd_pin_sdi = HIGH;
00174     pulseLow( _lcd_pin_scl );
00175     serializeByte( data );
00176 }
00177 
00178 void HX8340S_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
00179 {
00180     WriteCmdData( 0x2A, x1 );  // CASET
00181     WriteData( x2 );
00182     WriteCmdData( 0x2B, y1 );  // PASET
00183     WriteData( y2 );
00184     WriteCmd( 0x2C ); // RAMWR
00185 }
00186 
00187 void HX8340S_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
00188 {
00189     unsigned char r = 0, g = 0, b = 0;
00190     unsigned short clr;
00191     if ( _colorDepth == RGB16 )
00192     {
00193         switch ( mode )
00194         {
00195             case RGB16:
00196                 WriteData( color & 0xFFFF );
00197                 break;
00198             case RGB18:
00199                 r = ( color >> 10 ) & 0xF8;
00200                 g = ( color >> 4 ) & 0xFC;
00201                 b = ( color >> 1 ) & 0x1F;
00202                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
00203                 WriteData( clr );
00204                 break;
00205             case RGB24:
00206                 r = ( color >> 16 ) & 0xF8;
00207                 g = ( color >> 8 ) & 0xFC;
00208                 b = color & 0xF8;
00209                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
00210                 WriteData( clr );
00211                 break;
00212         }
00213     }
00214     else if ( _colorDepth == RGB18 )
00215     {
00216         switch ( mode )
00217         {
00218             case RGB16:
00219                 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
00220                 g = ( color >> 3 ) & 0xFC;
00221                 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
00222                 break;
00223             case RGB18:
00224                 b = ( color << 2 ) & 0xFC;
00225                 g = ( color >> 4 ) & 0xFC;
00226                 r = ( color >> 10 ) & 0xFC;
00227                 break;
00228             case RGB24:
00229                 r = ( color >> 16 ) & 0xFC;
00230                 g = ( color >> 8 ) & 0xFC;
00231                 b = color & 0xFC;
00232                 break;
00233         }
00234         WriteByteData( r );
00235         WriteByteData( g );
00236         WriteByteData( b );
00237     }
00238 }
00239 
00240 void HX8340S_LCD::serializeByte( unsigned char data )
00241 {
00242     for ( int i = 0; i < 8; i++ )
00243     {
00244         if ( data & 0x80 ) _lcd_pin_sdi = HIGH;
00245         else _lcd_pin_sdi = LOW;
00246         pulseLow( _lcd_pin_scl );
00247         data = data << 1;
00248     }
00249 }