1.44 tft lcd display

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

Committer:
ttodorov
Date:
Tue Dec 11 18:11:14 2012 +0000
Revision:
12:d0978272a340
Parent:
10:69571adcfad5
Child:
14:8efbe7361dae
- integrated RGB16 and RGB18 color depth configuration/selection; - integrated HW rotation for the HX8340-B driver; - changed the bitmap drawing API; - TODO: no drawing of rotated or scaled bitmaps yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ttodorov 9:58b328831d0a 1 /*
ttodorov 9:58b328831d0a 2 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
ttodorov 9:58b328831d0a 3 * Copyright (C)2012 Todor Todorov.
ttodorov 9:58b328831d0a 4 *
ttodorov 9:58b328831d0a 5 * This library is free software; you can redistribute it and/or
ttodorov 9:58b328831d0a 6 * modify it under the terms of the GNU Lesser General Public
ttodorov 9:58b328831d0a 7 * License as published by the Free Software Foundation; either
ttodorov 9:58b328831d0a 8 * version 2.1 of the License, or (at your option) any later version.
ttodorov 9:58b328831d0a 9 *
ttodorov 9:58b328831d0a 10 * This library is distributed in the hope that it will be useful,
ttodorov 9:58b328831d0a 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ttodorov 9:58b328831d0a 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ttodorov 9:58b328831d0a 13 * Lesser General Public License for more details.
ttodorov 9:58b328831d0a 14 *
ttodorov 9:58b328831d0a 15 * You should have received a copy of the GNU Lesser General Public
ttodorov 9:58b328831d0a 16 * License along with this library; if not, write to:
ttodorov 9:58b328831d0a 17 *
ttodorov 9:58b328831d0a 18 * Free Software Foundation, Inc.
ttodorov 9:58b328831d0a 19 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
ttodorov 9:58b328831d0a 20 *
ttodorov 9:58b328831d0a 21 *********************************************************************/
ttodorov 9:58b328831d0a 22 #include "st7735.h"
ttodorov 9:58b328831d0a 23 #include "helpers.h"
ttodorov 9:58b328831d0a 24
ttodorov 9:58b328831d0a 25 ST7735_LCD::ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL )
ttodorov 9:58b328831d0a 26 : LCD( 128, 160, CS, RS, RESET ), _lcd_pin_scl( SCL ), _lcd_pin_sda( SDA )
ttodorov 9:58b328831d0a 27 {
ttodorov 9:58b328831d0a 28 if ( BL != NC ) _lcd_pin_bl = new DigitalOut( BL );
ttodorov 9:58b328831d0a 29 else _lcd_pin_bl = 0;
ttodorov 9:58b328831d0a 30 }
ttodorov 9:58b328831d0a 31
ttodorov 12:d0978272a340 32 void ST7735_LCD::Initialize( orientation_t orientation, colordepth_t colors )
ttodorov 9:58b328831d0a 33 {
ttodorov 9:58b328831d0a 34 _orientation = orientation;
ttodorov 12:d0978272a340 35 _colorDepth = colors;
ttodorov 9:58b328831d0a 36
ttodorov 9:58b328831d0a 37 wait_ms( 100 );
ttodorov 9:58b328831d0a 38 _lcd_pin_reset = HIGH;
ttodorov 9:58b328831d0a 39 wait_ms( 5 );
ttodorov 9:58b328831d0a 40 _lcd_pin_reset = LOW;
ttodorov 9:58b328831d0a 41 wait_ms( 15 );
ttodorov 9:58b328831d0a 42 _lcd_pin_reset = HIGH;
ttodorov 9:58b328831d0a 43 _lcd_pin_cs = HIGH;
ttodorov 9:58b328831d0a 44 _lcd_pin_rs = HIGH;
ttodorov 9:58b328831d0a 45 _lcd_pin_scl = HIGH;
ttodorov 9:58b328831d0a 46 _lcd_pin_sda = HIGH;
ttodorov 9:58b328831d0a 47 if ( _lcd_pin_bl != 0 )
ttodorov 9:58b328831d0a 48 *_lcd_pin_bl = HIGH;
ttodorov 9:58b328831d0a 49 wait_ms( 55 );
ttodorov 9:58b328831d0a 50
ttodorov 9:58b328831d0a 51 Activate();
ttodorov 9:58b328831d0a 52 WriteCmd( 0x01 ); // SW reset
ttodorov 9:58b328831d0a 53 wait_ms( 120 );
ttodorov 9:58b328831d0a 54
ttodorov 9:58b328831d0a 55 WriteCmd( 0x11 ); // sleep out
ttodorov 9:58b328831d0a 56 wait_ms( 120 );
ttodorov 9:58b328831d0a 57
ttodorov 9:58b328831d0a 58 WriteCmd( 0xB1 ); // frame control 1
ttodorov 9:58b328831d0a 59 WriteByteData( 0x01 );
ttodorov 9:58b328831d0a 60 WriteByteData( 0x2C );
ttodorov 9:58b328831d0a 61 WriteByteData( 0x2D );
ttodorov 9:58b328831d0a 62 WriteCmd( 0xB2 ); // frame control 2
ttodorov 9:58b328831d0a 63 WriteByteData( 0x01 );
ttodorov 9:58b328831d0a 64 WriteByteData( 0x2C );
ttodorov 9:58b328831d0a 65 WriteByteData( 0x2D );
ttodorov 9:58b328831d0a 66 WriteCmd( 0xB3 ); // frame control 3
ttodorov 9:58b328831d0a 67 WriteByteData( 0x01 );
ttodorov 9:58b328831d0a 68 WriteByteData( 0x2C );
ttodorov 9:58b328831d0a 69 WriteByteData( 0x2D );
ttodorov 9:58b328831d0a 70 WriteByteData( 0x01 );
ttodorov 9:58b328831d0a 71 WriteByteData( 0x2C );
ttodorov 9:58b328831d0a 72 WriteByteData( 0x2D );
ttodorov 9:58b328831d0a 73
ttodorov 9:58b328831d0a 74 WriteCmd( 0xB4 ); // column inversion
ttodorov 9:58b328831d0a 75 WriteByteData( 0x07 );
ttodorov 9:58b328831d0a 76
ttodorov 9:58b328831d0a 77 // ST7735R Power Sequence
ttodorov 9:58b328831d0a 78 WriteCmd( 0xC0 ); // power control 1
ttodorov 9:58b328831d0a 79 WriteByteData( 0xA2 );
ttodorov 9:58b328831d0a 80 WriteByteData( 0x02 );
ttodorov 9:58b328831d0a 81 WriteByteData( 0x84 );
ttodorov 9:58b328831d0a 82 WriteCmd( 0xC1 ); // power control 2
ttodorov 9:58b328831d0a 83 WriteByteData( 0xC5 );
ttodorov 9:58b328831d0a 84 WriteCmd( 0xC2 ); // power control 3
ttodorov 9:58b328831d0a 85 WriteByteData( 0x0A );
ttodorov 9:58b328831d0a 86 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 87 WriteCmd( 0xC3 ); // power control 4
ttodorov 9:58b328831d0a 88 WriteByteData( 0x8A );
ttodorov 9:58b328831d0a 89 WriteByteData( 0x2A );
ttodorov 9:58b328831d0a 90 WriteCmd( 0xC4 ); // power control 5
ttodorov 9:58b328831d0a 91 WriteByteData( 0x8A );
ttodorov 9:58b328831d0a 92 WriteByteData( 0xEE );
ttodorov 9:58b328831d0a 93
ttodorov 9:58b328831d0a 94 WriteCmd( 0xC5 ); // voltage control 1
ttodorov 9:58b328831d0a 95 WriteByteData( 0x0E );
ttodorov 9:58b328831d0a 96
ttodorov 9:58b328831d0a 97 WriteCmd( 0x36 ); //MX, MY, RGB mode
ttodorov 9:58b328831d0a 98 WriteByteData( 0xC8 );
ttodorov 9:58b328831d0a 99
ttodorov 9:58b328831d0a 100 // ST7735R Gamma Sequence
ttodorov 9:58b328831d0a 101 WriteCmd( 0xE0 ); // gamma positive
ttodorov 9:58b328831d0a 102 WriteByteData( 0x0F );
ttodorov 9:58b328831d0a 103 WriteByteData( 0x1A );
ttodorov 9:58b328831d0a 104 WriteByteData( 0x0F );
ttodorov 9:58b328831d0a 105 WriteByteData( 0x18 );
ttodorov 9:58b328831d0a 106 WriteByteData( 0x2F );
ttodorov 9:58b328831d0a 107 WriteByteData( 0x28 );
ttodorov 9:58b328831d0a 108 WriteByteData( 0x20 );
ttodorov 9:58b328831d0a 109 WriteByteData( 0x22 );
ttodorov 9:58b328831d0a 110 WriteByteData( 0x1F );
ttodorov 9:58b328831d0a 111 WriteByteData( 0x1B );
ttodorov 9:58b328831d0a 112 WriteByteData( 0x23 );
ttodorov 9:58b328831d0a 113 WriteByteData( 0x37 );
ttodorov 9:58b328831d0a 114 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 115 WriteByteData( 0x07 );
ttodorov 9:58b328831d0a 116 WriteByteData( 0x02 );
ttodorov 9:58b328831d0a 117 WriteByteData( 0x10 );
ttodorov 9:58b328831d0a 118 WriteCmd( 0xE1 ); // gamma negative
ttodorov 9:58b328831d0a 119 WriteByteData( 0x0F );
ttodorov 9:58b328831d0a 120 WriteByteData( 0x1B );
ttodorov 9:58b328831d0a 121 WriteByteData( 0x0F );
ttodorov 9:58b328831d0a 122 WriteByteData( 0x17 );
ttodorov 9:58b328831d0a 123 WriteByteData( 0x33 );
ttodorov 9:58b328831d0a 124 WriteByteData( 0x2C );
ttodorov 9:58b328831d0a 125 WriteByteData( 0x29 );
ttodorov 9:58b328831d0a 126 WriteByteData( 0x2E );
ttodorov 9:58b328831d0a 127 WriteByteData( 0x30 );
ttodorov 9:58b328831d0a 128 WriteByteData( 0x30 );
ttodorov 9:58b328831d0a 129 WriteByteData( 0x39 );
ttodorov 9:58b328831d0a 130 WriteByteData( 0x3F );
ttodorov 9:58b328831d0a 131 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 132 WriteByteData( 0x07 );
ttodorov 9:58b328831d0a 133 WriteByteData( 0x03 );
ttodorov 9:58b328831d0a 134 WriteByteData( 0x10 );
ttodorov 9:58b328831d0a 135
ttodorov 9:58b328831d0a 136 WriteCmd( 0x2A ); // set column address
ttodorov 9:58b328831d0a 137 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 138 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 139 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 140 WriteByteData( 0x7F );
ttodorov 9:58b328831d0a 141 WriteCmd( 0x2B ); // set row address
ttodorov 9:58b328831d0a 142 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 143 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 144 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 145 WriteByteData( 0x9F );
ttodorov 9:58b328831d0a 146
ttodorov 9:58b328831d0a 147 WriteCmd( 0xF0 ); // enable extensions command
ttodorov 9:58b328831d0a 148 WriteByteData( 0x01 );
ttodorov 9:58b328831d0a 149 WriteCmd( 0xF6 ); // disable ram power save mode
ttodorov 9:58b328831d0a 150 WriteByteData( 0x00 );
ttodorov 9:58b328831d0a 151
ttodorov 12:d0978272a340 152 WriteCmd( 0x3A ); // interface pixel format (color mode): 0x05 => RGB16, 0x06 => RGB18
ttodorov 12:d0978272a340 153 WriteByteData( _colorDepth == RGB16 ? 0x05 : 0x06 );
ttodorov 9:58b328831d0a 154 WriteCmd( 0x29 ); // display on
ttodorov 9:58b328831d0a 155
ttodorov 9:58b328831d0a 156 Deactivate();
ttodorov 9:58b328831d0a 157 }
ttodorov 9:58b328831d0a 158
ttodorov 9:58b328831d0a 159 void ST7735_LCD::Sleep( void )
ttodorov 9:58b328831d0a 160 {
ttodorov 9:58b328831d0a 161 Activate();
ttodorov 9:58b328831d0a 162 WriteCmd( 0x28 );
ttodorov 9:58b328831d0a 163 wait_ms( 10 );
ttodorov 9:58b328831d0a 164 WriteCmd( 0x10 );
ttodorov 9:58b328831d0a 165 wait_ms( 125 );
ttodorov 9:58b328831d0a 166 if ( _lcd_pin_bl != 0 )
ttodorov 9:58b328831d0a 167 *_lcd_pin_bl = LOW;
ttodorov 9:58b328831d0a 168 Deactivate();
ttodorov 9:58b328831d0a 169 }
ttodorov 9:58b328831d0a 170
ttodorov 9:58b328831d0a 171 void ST7735_LCD::WakeUp( void )
ttodorov 9:58b328831d0a 172 {
ttodorov 9:58b328831d0a 173 Activate();
ttodorov 9:58b328831d0a 174 WriteCmd( 0x29 );
ttodorov 9:58b328831d0a 175 wait_ms( 10 );
ttodorov 9:58b328831d0a 176 WriteCmd( 0x11 );
ttodorov 9:58b328831d0a 177 wait_ms( 125 );
ttodorov 9:58b328831d0a 178 if ( _lcd_pin_bl != 0 )
ttodorov 9:58b328831d0a 179 *_lcd_pin_bl = HIGH;
ttodorov 9:58b328831d0a 180 Deactivate();
ttodorov 9:58b328831d0a 181 }
ttodorov 9:58b328831d0a 182
ttodorov 9:58b328831d0a 183 void ST7735_LCD::WriteCmd( unsigned short cmd )
ttodorov 9:58b328831d0a 184 {
ttodorov 9:58b328831d0a 185 _lcd_pin_rs = LOW;
ttodorov 9:58b328831d0a 186 serializeByte( cmd & 0xFF );
ttodorov 9:58b328831d0a 187 }
ttodorov 9:58b328831d0a 188
ttodorov 9:58b328831d0a 189 void ST7735_LCD::WriteData( unsigned short data )
ttodorov 9:58b328831d0a 190 {
ttodorov 9:58b328831d0a 191 _lcd_pin_rs = HIGH;
ttodorov 9:58b328831d0a 192 serializeByte( ( data >> 8 ) & 0xFF );
ttodorov 9:58b328831d0a 193 serializeByte( data & 0xFF );
ttodorov 9:58b328831d0a 194 }
ttodorov 9:58b328831d0a 195
ttodorov 9:58b328831d0a 196 void ST7735_LCD::WriteByteData( unsigned char data )
ttodorov 9:58b328831d0a 197 {
ttodorov 9:58b328831d0a 198 _lcd_pin_rs = HIGH;
ttodorov 9:58b328831d0a 199 serializeByte( data );
ttodorov 9:58b328831d0a 200 }
ttodorov 9:58b328831d0a 201
ttodorov 9:58b328831d0a 202 void ST7735_LCD::SetXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
ttodorov 9:58b328831d0a 203 {
ttodorov 9:58b328831d0a 204 if ( _orientation == LANDSCAPE )
ttodorov 9:58b328831d0a 205 {
ttodorov 9:58b328831d0a 206 swap( uint16_t, x1, y1 )
ttodorov 9:58b328831d0a 207 swap( uint16_t, x2, y2 )
ttodorov 9:58b328831d0a 208 y1 = _disp_height - y1;
ttodorov 9:58b328831d0a 209 y2 = _disp_height - y2;
ttodorov 9:58b328831d0a 210 swap( uint16_t, y1, y2 )
ttodorov 9:58b328831d0a 211 }
ttodorov 9:58b328831d0a 212
ttodorov 9:58b328831d0a 213 WriteCmdData( 0x2a, x1 );
ttodorov 9:58b328831d0a 214 WriteData( x2 );
ttodorov 9:58b328831d0a 215 WriteCmdData( 0x2b, y1 );
ttodorov 9:58b328831d0a 216 WriteData( y2 );
ttodorov 9:58b328831d0a 217 WriteCmd( 0x2c );
ttodorov 9:58b328831d0a 218 }
ttodorov 9:58b328831d0a 219
ttodorov 12:d0978272a340 220 void ST7735_LCD::SetPixelColor( unsigned int color )
ttodorov 10:69571adcfad5 221 {
ttodorov 12:d0978272a340 222 unsigned char r, g, b;
ttodorov 12:d0978272a340 223 r = ( color >> 16 ) & 0xFF;
ttodorov 12:d0978272a340 224 g = ( color >> 8 ) & 0xFF;
ttodorov 12:d0978272a340 225 b = color & 0xFF;
ttodorov 12:d0978272a340 226 if ( _colorDepth == RGB16 )
ttodorov 12:d0978272a340 227 {
ttodorov 12:d0978272a340 228 unsigned short clr = ( ( ( ( r ) & 0xF8 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 0x1C ) << 3 ) | ( ( b ) >> 3 ) );
ttodorov 12:d0978272a340 229 WriteData( clr );
ttodorov 12:d0978272a340 230 }
ttodorov 12:d0978272a340 231 else
ttodorov 12:d0978272a340 232 {
ttodorov 12:d0978272a340 233 WriteByteData( r & 0xFC );
ttodorov 12:d0978272a340 234 WriteByteData( g & 0xFC );
ttodorov 12:d0978272a340 235 WriteByteData( b & 0xFC );
ttodorov 12:d0978272a340 236 }
ttodorov 10:69571adcfad5 237 }
ttodorov 10:69571adcfad5 238
ttodorov 9:58b328831d0a 239 void ST7735_LCD::serializeByte( unsigned char data )
ttodorov 9:58b328831d0a 240 {
ttodorov 9:58b328831d0a 241 for ( int i = 0; i < 8; i++ )
ttodorov 9:58b328831d0a 242 {
ttodorov 9:58b328831d0a 243 if ( data & 0x80 ) _lcd_pin_sda = HIGH;
ttodorov 9:58b328831d0a 244 else _lcd_pin_sda = LOW;
ttodorov 9:58b328831d0a 245 pulseLow( _lcd_pin_scl );
ttodorov 9:58b328831d0a 246 data = data << 1;
ttodorov 9:58b328831d0a 247 }
ttodorov 9:58b328831d0a 248 }