madhu sudhana / Mbed OS newTFTLCD_

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers st7735.cpp Source File

st7735.cpp

00001 
00002 
00003 
00004 
00005 
00006 
00007 /*  Hexiwear NXP Bitmap Images
00008  *   This file contains the bitmaps for the full screen (96 by 96 pixels) NXP logo
00009  *   and a smaller 96 by 32 pixels NXP logo.
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * Redistributions of source code must retain the above copyright notice, this list
00015  * of conditions and the following disclaimer.
00016  *
00017  * Redistributions in binary form must reproduce the above copyright notice, this
00018  * list of conditions and the following disclaimer in the documentation and/or
00019  * other materials provided with the distribution.
00020  *
00021  * Neither the name of NXP, nor the names of its
00022  * contributors may be used to endorse or promote products derived from this
00023  * software without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00029  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00030  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00032  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * visit: http://www.mikroe.com and http://www.nxp.com
00037  *
00038  * get support at: http://www.mikroe.com/forum and https://community.nxp.com
00039  *
00040  * Project HEXIWEAR, 2015
00041  */
00042 
00043 /*
00044  * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
00045  * Copyright (C)2012 Todor Todorov.
00046  *
00047  * This library is free software; you can redistribute it and/or
00048  * modify it under the terms of the GNU Lesser General Public
00049  * License as published by the Free Software Foundation; either
00050  * version 2.1 of the License, or (at your option) any later version.
00051  *
00052  * This library is distributed in the hope that it will be useful,
00053  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00054  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00055  * Lesser General Public License for more details.
00056  *
00057  * You should have received a copy of the GNU Lesser General Public
00058  * License along with this library; if not, write to:
00059  *
00060  * Free Software Foundation, Inc.
00061  * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
00062  *
00063  *********************************************************************/
00064 #include "st7735.h"
00065 #include "helpers.h"
00066 
00067 ST7735_LCD::ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL, backlight_t blType, float defaultBackLightLevel )
00068     : LCD( 128, 160, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_scl( SCL ), _lcd_pin_sda( SDA )
00069 {
00070 }
00071 
00072 void ST7735_LCD::Initialize( orientation_t orientation, colordepth_t colors )
00073 {
00074     _orientation = orientation;
00075     _colorDepth = colors;
00076     
00077     wait_ms( 100 );
00078     _lcd_pin_reset = HIGH;
00079     wait_ms( 5 );
00080     _lcd_pin_reset = LOW;
00081     wait_ms( 15 );
00082     _lcd_pin_reset = HIGH;
00083     _lcd_pin_cs = HIGH;
00084     _lcd_pin_rs = HIGH;
00085     _lcd_pin_scl = HIGH;
00086     _lcd_pin_sda = HIGH;
00087     if ( _lcd_pin_bl != 0 )
00088         *_lcd_pin_bl = HIGH;
00089     else if ( _bl_pwm != 0 )
00090         *_bl_pwm = _bl_pwm_default;
00091     wait_ms( 55 );
00092     
00093     Activate();
00094     WriteCmd( 0x01 ); // SW reset
00095     wait_ms( 120 );
00096     
00097     WriteCmd( 0x11 ); // sleep out
00098     wait_ms( 120 );
00099     
00100     WriteCmd( 0xB1 ); // frame control 1
00101     WriteByteData( 0x01 );
00102     WriteByteData( 0x2C );
00103     WriteByteData( 0x2D );
00104     
00105     WriteCmd( 0xB2 ); // frame control 2
00106     WriteByteData( 0x01 );
00107     WriteByteData( 0x2C );
00108     WriteByteData( 0x2D );
00109     
00110     WriteCmd( 0xB3 ); // frame control 3
00111     WriteByteData( 0x01 );
00112     WriteByteData( 0x2C );
00113     WriteByteData( 0x2D );
00114     WriteByteData( 0x01 );
00115     WriteByteData( 0x2C );
00116     WriteByteData( 0x2D );
00117     
00118     WriteCmd( 0xB4 ); // column inversion
00119     //WriteByteData( 0x07 );
00120     WriteByteData( 0x00 );
00121 
00122     // ST7735R Power Sequence
00123     WriteCmd( 0xC0 ); // power control 1
00124     WriteByteData( 0xA2 );
00125     WriteByteData( 0x02 );
00126     WriteByteData( 0x84 );
00127     
00128     WriteCmd( 0xC1 ); // power control 2
00129     WriteByteData( 0xC5 );
00130     
00131     WriteCmd( 0xC2 ); // power control 3
00132     WriteByteData( 0x0A );
00133     WriteByteData( 0x00 );
00134     
00135     WriteCmd( 0xC3 ); // power control 4
00136     WriteByteData( 0x8A );
00137     WriteByteData( 0x2A );
00138     
00139     WriteCmd( 0xC4 ); // power control 5
00140     WriteByteData( 0x8A );
00141     WriteByteData( 0xEE );
00142     
00143     WriteCmd( 0xC5 ); // voltage control 1
00144     WriteByteData( 0x0E );
00145 
00146     // ST7735R Gamma Sequence
00147     WriteCmd( 0xE0 ); // gamma positive
00148     WriteByteData( 0x0F );
00149     WriteByteData( 0x1A );
00150     WriteByteData( 0x0F );
00151     WriteByteData( 0x18 );
00152     WriteByteData( 0x2F );
00153     WriteByteData( 0x28 );
00154     WriteByteData( 0x20 );
00155     WriteByteData( 0x22 );
00156     WriteByteData( 0x1F );
00157     WriteByteData( 0x1B );
00158     WriteByteData( 0x23 );
00159     WriteByteData( 0x37 );
00160     WriteByteData( 0x00 );
00161     WriteByteData( 0x07 );
00162     WriteByteData( 0x02 );
00163     WriteByteData( 0x10 );
00164     
00165     WriteCmd( 0xE1 ); // gamma negative
00166     WriteByteData( 0x0F );
00167     WriteByteData( 0x1B );
00168     WriteByteData( 0x0F );
00169     WriteByteData( 0x17 );
00170     WriteByteData( 0x33 );
00171     WriteByteData( 0x2C );
00172     WriteByteData( 0x29 );
00173     WriteByteData( 0x2E );
00174     WriteByteData( 0x30 );
00175     WriteByteData( 0x30 );
00176     WriteByteData( 0x39 );
00177     WriteByteData( 0x3F );
00178     WriteByteData( 0x00 );
00179     WriteByteData( 0x07 );
00180     WriteByteData( 0x03 );
00181     WriteByteData( 0x10 );
00182 
00183     WriteCmd( 0x2A ); // set column address
00184     WriteByteData( 0x00 );
00185     WriteByteData( 0x00 );
00186     WriteByteData( 0x00 );
00187     WriteByteData( 0x7F );
00188     
00189     WriteCmd( 0x2B ); // set row address
00190     WriteByteData( 0x00 );
00191     WriteByteData( 0x00 );
00192     WriteByteData( 0x00 );
00193     WriteByteData( 0x9F );
00194 
00195     WriteCmd( 0xF0 ); // enable extensions command
00196     WriteByteData( 0x01 );
00197     
00198     WriteCmd( 0xF6 ); // disable ram power save mode
00199     WriteByteData( 0x00 );
00200 
00201     WriteCmd( 0x3A ); // interface pixel format (color mode): 0x05 => RGB16, 0x06 => RGB18
00202     WriteByteData( _colorDepth == RGB16 ? 0x05 : 0x06 );
00203 
00204     WriteCmd( 0x36 ); //MX, MY, RGB mode
00205     switch ( _orientation )
00206     {
00207         case LANDSCAPE: WriteByteData( 0x6C ); break;
00208         case PORTRAIT_REV: WriteByteData( 0xDC ); break;
00209         case LANDSCAPE_REV: WriteByteData( 0xB8 ); break;
00210         case PORTRAIT:
00211         default: WriteByteData( 0x08 ); break;
00212     }
00213     
00214     WriteCmd( 0x29 ); // display on
00215 
00216     Deactivate();
00217 }
00218 
00219 void ST7735_LCD::Sleep( void )
00220 {
00221     Activate();
00222     WriteCmd( 0x28 );
00223     wait_ms( 10 );
00224     WriteCmd( 0x10 );
00225     wait_ms( 125 );
00226     LCD::Sleep();
00227     Deactivate();
00228 }
00229 
00230 void ST7735_LCD::WakeUp( void )
00231 {
00232     Activate();
00233     WriteCmd( 0x29 );
00234     wait_ms( 10 );
00235     WriteCmd( 0x11 );
00236     wait_ms( 125 );
00237     LCD::WakeUp();
00238     Deactivate();
00239 }
00240 
00241 void ST7735_LCD::WriteCmd( unsigned short cmd )
00242 {
00243     _lcd_pin_rs = LOW;
00244     serializeByte( cmd & 0xFF );
00245 }
00246 
00247 void ST7735_LCD::WriteData( unsigned short data )
00248 {
00249     _lcd_pin_rs = HIGH;
00250     serializeByte( ( data >> 8 ) & 0xFF );
00251     serializeByte( data & 0xFF );
00252 }
00253 
00254 void ST7735_LCD::WriteByteData( unsigned char data )
00255 {
00256     _lcd_pin_rs = HIGH;
00257     serializeByte( data );
00258 }
00259 
00260 void ST7735_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
00261 {
00262     WriteCmdData( 0x2a, x1 );
00263     WriteData( x2 );
00264     WriteCmdData( 0x2b, y1 );
00265     WriteData( y2 );
00266     WriteCmd( 0x2c );
00267 }
00268 
00269 void ST7735_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
00270 {
00271     unsigned char r = 0, g = 0, b = 0;
00272     unsigned short clr;
00273     if ( _colorDepth == RGB16 )
00274     {
00275         switch ( mode )
00276         {
00277             case RGB16:
00278                 WriteData( color & 0xFFFF );
00279                 break;
00280             case RGB18:
00281                 r = ( color >> 10 ) & 0xF8;
00282                 g = ( color >> 4 ) & 0xFC;
00283                 b = ( color >> 1 ) & 0x1F;
00284                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
00285                 WriteData( clr );
00286                 break;
00287             case RGB24:
00288                 r = ( color >> 16 ) & 0xF8;
00289                 g = ( color >> 8 ) & 0xFC;
00290                 b = color & 0xF8;
00291                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
00292                 WriteData( clr );
00293                 break;
00294         }
00295     }
00296     else if ( _colorDepth == RGB18 )
00297     {
00298         switch ( mode )
00299         {
00300             case RGB16:
00301                 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
00302                 g = ( color >> 3 ) & 0xFC;
00303                 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
00304                 break;
00305             case RGB18:
00306                 b = ( color << 2 ) & 0xFC;
00307                 g = ( color >> 4 ) & 0xFC;
00308                 r = ( color >> 10 ) & 0xFC;
00309                 break;
00310             case RGB24:
00311                 r = ( color >> 16 ) & 0xFC;
00312                 g = ( color >> 8 ) & 0xFC;
00313                 b = color & 0xFC;
00314                 break;
00315         }
00316         WriteByteData( r );
00317         WriteByteData( g );
00318         WriteByteData( b );
00319     }
00320 }
00321 
00322 void ST7735_LCD::serializeByte( unsigned char data )
00323 {
00324     for ( int i = 0; i < 8; i++ )
00325     {
00326         if ( data & 0x80 ) _lcd_pin_sda = HIGH;
00327         else _lcd_pin_sda = LOW;
00328         pulseLow( _lcd_pin_scl );
00329         data = data << 1;
00330     }
00331 }
00332