A modifiedversion of TFTLCD by Todor Todorov with ultra-fast operation for SSD1289 controller. TODO: speed-up fonts, add my own fonts Can work out-of-the-box with ST Nucleo F401RE

Dependents:   TFT_320QVT_Window_Drag_Demo TFT_320QVT_HelloWorld

Fork of TFTLCD by Todor Todorov

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers st7735.cpp Source File

st7735.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 "st7735.h"
00023 #include "helpers.h"
00024 
00025 ST7735_LCD::ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL, backlight_t blType, float defaultBackLightLevel )
00026     : LCD( 128, 160, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_scl( SCL ), _lcd_pin_sda( SDA )
00027 {
00028 }
00029 
00030 void ST7735_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_rs = HIGH;
00043     _lcd_pin_scl = HIGH;
00044     _lcd_pin_sda = 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     wait_ms( 55 );
00050     
00051     Activate();
00052     WriteCmd( 0x01 ); // SW reset
00053     wait_ms( 120 );
00054     
00055     WriteCmd( 0x11 ); // sleep out
00056     wait_ms( 120 );
00057     
00058     WriteCmd( 0xB1 ); // frame control 1
00059     WriteByteData( 0x01 );
00060     WriteByteData( 0x2C );
00061     WriteByteData( 0x2D );
00062     
00063     WriteCmd( 0xB2 ); // frame control 2
00064     WriteByteData( 0x01 );
00065     WriteByteData( 0x2C );
00066     WriteByteData( 0x2D );
00067     
00068     WriteCmd( 0xB3 ); // frame control 3
00069     WriteByteData( 0x01 );
00070     WriteByteData( 0x2C );
00071     WriteByteData( 0x2D );
00072     WriteByteData( 0x01 );
00073     WriteByteData( 0x2C );
00074     WriteByteData( 0x2D );
00075     
00076     WriteCmd( 0xB4 ); // column inversion
00077     //WriteByteData( 0x07 );
00078     WriteByteData( 0x00 );
00079 
00080     // ST7735R Power Sequence
00081     WriteCmd( 0xC0 ); // power control 1
00082     WriteByteData( 0xA2 );
00083     WriteByteData( 0x02 );
00084     WriteByteData( 0x84 );
00085     
00086     WriteCmd( 0xC1 ); // power control 2
00087     WriteByteData( 0xC5 );
00088     
00089     WriteCmd( 0xC2 ); // power control 3
00090     WriteByteData( 0x0A );
00091     WriteByteData( 0x00 );
00092     
00093     WriteCmd( 0xC3 ); // power control 4
00094     WriteByteData( 0x8A );
00095     WriteByteData( 0x2A );
00096     
00097     WriteCmd( 0xC4 ); // power control 5
00098     WriteByteData( 0x8A );
00099     WriteByteData( 0xEE );
00100     
00101     WriteCmd( 0xC5 ); // voltage control 1
00102     WriteByteData( 0x0E );
00103 
00104     // ST7735R Gamma Sequence
00105     WriteCmd( 0xE0 ); // gamma positive
00106     WriteByteData( 0x0F );
00107     WriteByteData( 0x1A );
00108     WriteByteData( 0x0F );
00109     WriteByteData( 0x18 );
00110     WriteByteData( 0x2F );
00111     WriteByteData( 0x28 );
00112     WriteByteData( 0x20 );
00113     WriteByteData( 0x22 );
00114     WriteByteData( 0x1F );
00115     WriteByteData( 0x1B );
00116     WriteByteData( 0x23 );
00117     WriteByteData( 0x37 );
00118     WriteByteData( 0x00 );
00119     WriteByteData( 0x07 );
00120     WriteByteData( 0x02 );
00121     WriteByteData( 0x10 );
00122     
00123     WriteCmd( 0xE1 ); // gamma negative
00124     WriteByteData( 0x0F );
00125     WriteByteData( 0x1B );
00126     WriteByteData( 0x0F );
00127     WriteByteData( 0x17 );
00128     WriteByteData( 0x33 );
00129     WriteByteData( 0x2C );
00130     WriteByteData( 0x29 );
00131     WriteByteData( 0x2E );
00132     WriteByteData( 0x30 );
00133     WriteByteData( 0x30 );
00134     WriteByteData( 0x39 );
00135     WriteByteData( 0x3F );
00136     WriteByteData( 0x00 );
00137     WriteByteData( 0x07 );
00138     WriteByteData( 0x03 );
00139     WriteByteData( 0x10 );
00140 
00141     WriteCmd( 0x2A ); // set column address
00142     WriteByteData( 0x00 );
00143     WriteByteData( 0x00 );
00144     WriteByteData( 0x00 );
00145     WriteByteData( 0x7F );
00146     
00147     WriteCmd( 0x2B ); // set row address
00148     WriteByteData( 0x00 );
00149     WriteByteData( 0x00 );
00150     WriteByteData( 0x00 );
00151     WriteByteData( 0x9F );
00152 
00153     WriteCmd( 0xF0 ); // enable extensions command
00154     WriteByteData( 0x01 );
00155     
00156     WriteCmd( 0xF6 ); // disable ram power save mode
00157     WriteByteData( 0x00 );
00158 
00159     WriteCmd( 0x3A ); // interface pixel format (color mode): 0x05 => RGB16, 0x06 => RGB18
00160     WriteByteData( _colorDepth == RGB16 ? 0x05 : 0x06 );
00161 
00162     WriteCmd( 0x36 ); //MX, MY, RGB mode
00163     switch ( _orientation )
00164     {
00165         case LANDSCAPE: WriteByteData( 0x6C ); break;
00166         case PORTRAIT_REV: WriteByteData( 0xDC ); break;
00167         case LANDSCAPE_REV: WriteByteData( 0xB8 ); break;
00168         case PORTRAIT:
00169         default: WriteByteData( 0x08 ); break;
00170     }
00171     
00172     WriteCmd( 0x29 ); // display on
00173 
00174     Deactivate();
00175 }
00176 
00177 void ST7735_LCD::Sleep( void )
00178 {
00179     Activate();
00180     WriteCmd( 0x28 );
00181     wait_ms( 10 );
00182     WriteCmd( 0x10 );
00183     wait_ms( 125 );
00184     LCD::Sleep();
00185     Deactivate();
00186 }
00187 
00188 void ST7735_LCD::WakeUp( void )
00189 {
00190     Activate();
00191     WriteCmd( 0x29 );
00192     wait_ms( 10 );
00193     WriteCmd( 0x11 );
00194     wait_ms( 125 );
00195     LCD::WakeUp();
00196     Deactivate();
00197 }
00198 
00199 void ST7735_LCD::WriteCmd( unsigned short cmd )
00200 {
00201     _lcd_pin_rs = LOW;
00202     serializeByte( cmd & 0xFF );
00203 }
00204 
00205 void ST7735_LCD::WriteData( unsigned short data )
00206 {
00207     _lcd_pin_rs = HIGH;
00208     serializeByte( ( data >> 8 ) & 0xFF );
00209     serializeByte( data & 0xFF );
00210 }
00211 
00212 void ST7735_LCD::WriteByteData( unsigned char data )
00213 {
00214     _lcd_pin_rs = HIGH;
00215     serializeByte( data );
00216 }
00217 
00218 void ST7735_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
00219 {
00220     WriteCmdData( 0x2a, x1 );
00221     WriteData( x2 );
00222     WriteCmdData( 0x2b, y1 );
00223     WriteData( y2 );
00224     WriteCmd( 0x2c );
00225 }
00226 
00227 void ST7735_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
00228 {
00229     unsigned char r = 0, g = 0, b = 0;
00230     unsigned short clr;
00231     if ( _colorDepth == RGB16 )
00232     {
00233         switch ( mode )
00234         {
00235             case RGB16:
00236                 WriteData( color & 0xFFFF );
00237                 break;
00238             case RGB18:
00239                 r = ( color >> 10 ) & 0xF8;
00240                 g = ( color >> 4 ) & 0xFC;
00241                 b = ( color >> 1 ) & 0x1F;
00242                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
00243                 WriteData( clr );
00244                 break;
00245             case RGB24:
00246                 r = ( color >> 16 ) & 0xF8;
00247                 g = ( color >> 8 ) & 0xFC;
00248                 b = color & 0xF8;
00249                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
00250                 WriteData( clr );
00251                 break;
00252         }
00253     }
00254     else if ( _colorDepth == RGB18 )
00255     {
00256         switch ( mode )
00257         {
00258             case RGB16:
00259                 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
00260                 g = ( color >> 3 ) & 0xFC;
00261                 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
00262                 break;
00263             case RGB18:
00264                 b = ( color << 2 ) & 0xFC;
00265                 g = ( color >> 4 ) & 0xFC;
00266                 r = ( color >> 10 ) & 0xFC;
00267                 break;
00268             case RGB24:
00269                 r = ( color >> 16 ) & 0xFC;
00270                 g = ( color >> 8 ) & 0xFC;
00271                 b = color & 0xFC;
00272                 break;
00273         }
00274         WriteByteData( r );
00275         WriteByteData( g );
00276         WriteByteData( b );
00277     }
00278 }
00279 
00280 void ST7735_LCD::serializeByte( unsigned char data )
00281 {
00282     for ( int i = 0; i < 8; i++ )
00283     {
00284         if ( data & 0x80 ) _lcd_pin_sda = HIGH;
00285         else _lcd_pin_sda = LOW;
00286         pulseLow( _lcd_pin_scl );
00287         data = data << 1;
00288     }
00289 }