1.44 tft lcd display

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

ili9328.cpp

Committer:
ttodorov
Date:
2013-06-15
Revision:
24:ac6e35658037
Parent:
23:eca4414196ca
Child:
25:6cffb758c075

File content as of revision 24:ac6e35658037:

/*
 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
 * Copyright (C)2012-2013 Todor Todorov.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to:
 *
 * Free Software Foundation, Inc.
 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
 *
 *********************************************************************/
#include "ili9328.h"
#include "helpers.h"

ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
    : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
{
    _lcd_port = DATA_PORT;
    if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
    else _lcd_pin_rd = 0;
}

void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
{
    _orientation = orientation;
    _colorDepth = colors;
    
    _lcd_pin_reset = HIGH;
    wait_ms( 50 );
    _lcd_pin_reset = LOW;
    wait_ms( 100 );
    _lcd_pin_reset = HIGH;
    wait_ms( 1000 );
    _lcd_pin_cs = HIGH;
    if ( _lcd_pin_bl != 0 )
        *_lcd_pin_bl = HIGH;
    else if ( _bl_pwm != 0 )
        *_bl_pwm = _bl_pwm_default;
    if ( _lcd_pin_rd != 0 )
        *_lcd_pin_rd = HIGH;
    _lcd_pin_wr = HIGH;
    wait_ms( 15 );
    
    Activate();
    /*
    short payload = 0;
    switch ( _orientation )
    {
        case PORTRAIT:
        default:
            payload |= 0x0020;
            break;
    }
    switch ( _colorDepth )
    {
        case RGB16:
        default:
            payload |= 0x1000;
            break;
    }
    //WriteCmdData( REG_ENTRY_MOD, payload );
    */
    
    WriteCmdData( 0xE5, 0x78F0 ); // set SRAM internal timing
    WriteCmdData( 0x01, 0x0100 ); // set Driver Output Control
    WriteCmdData( 0x02, 0x0200 ); // set 1 line inversion
    WriteCmdData( 0x03, 0x1030 ); // set GRAM write direction and BGR=1.
    WriteCmdData( 0x04, 0x0000 ); // Resize register
    WriteCmdData( 0x08, 0x0207 ); // set the back porch and front porch
    WriteCmdData( 0x09, 0x0000 ); // set non-display area refresh cycle ISC[3:0]
    WriteCmdData( 0x0A, 0x0000 ); // FMARK function
    WriteCmdData( 0x0C, 0x0000 ); // RGB interface setting
    WriteCmdData( 0x0D, 0x0000 ); // Frame marker Position
    WriteCmdData( 0x0F, 0x0000 ); // RGB interface polarity
    // ----------- Power On sequence ----------- //
    WriteCmdData( 0x10, 0x0000 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
    WriteCmdData( 0x11, 0x0007 ); // DC1[2:0], DC0[2:0], VC[2:0]
    WriteCmdData( 0x12, 0x0000 ); // VREG1OUT voltage
    WriteCmdData( 0x13, 0x0000 ); // VDV[4:0] for VCOM amplitude
    WriteCmdData( 0x07, 0x0001 );
    wait_ms( 200 ); // Dis-charge capacitor power voltage
    WriteCmdData( 0x10, 0x1690 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
    WriteCmdData( 0x11, 0x0227 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
    wait_ms( 50 ); // Delay 50ms
    WriteCmdData( 0x12, 0x000D ); // 0012
    wait_ms( 50 ); // Delay 50ms
    WriteCmdData( 0x13, 0x1200 ); // VDV[4:0] for VCOM amplitude
    WriteCmdData( 0x29, 0x000A ); // 04  VCM[5:0] for VCOMH
    WriteCmdData( 0x2B, 0x000D ); // Set Frame Rate
    wait_ms( 50 ); // Delay 50ms
    WriteCmdData( 0x20, 0x0000 ); // GRAM horizontal Address
    WriteCmdData( 0x21, 0x0000 ); // GRAM Vertical Address
    // ----------- Adjust the Gamma Curve ----------//
    WriteCmdData( 0x30, 0x0000 );
    WriteCmdData( 0x31, 0x0404 );
    WriteCmdData( 0x32, 0x0003 );
    WriteCmdData( 0x35, 0x0405 );
    WriteCmdData( 0x36, 0x0808 );
    WriteCmdData( 0x37, 0x0407 );
    WriteCmdData( 0x38, 0x0303 );
    WriteCmdData( 0x39, 0x0707 );
    WriteCmdData( 0x3C, 0x0504 );
    WriteCmdData( 0x3D, 0x0808 );
    //------------------ Set GRAM area ---------------//
    WriteCmdData( 0x50, 0x0000 ); // Horizontal GRAM Start Address
    WriteCmdData( 0x51, 0x00EF ); // Horizontal GRAM End Address
    WriteCmdData( 0x52, 0x0000 ); // Vertical GRAM Start Address
    WriteCmdData( 0x53, 0x013F ); // Vertical GRAM Start Address
    WriteCmdData( 0x60, 0xA700 ); // Gate Scan Line
    WriteCmdData( 0x61, 0x0000 ); // NDL,VLE, REV
    WriteCmdData( 0x6A, 0x0000 ); // set scrolling line
    //-------------- Partial Display Control ---------//
    WriteCmdData( 0x80, 0x0000 );
    WriteCmdData( 0x81, 0x0000 );
    WriteCmdData( 0x82, 0x0000 );
    WriteCmdData( 0x83, 0x0000 );
    WriteCmdData( 0x84, 0x0000 );
    WriteCmdData( 0x85, 0x0000 );
    //-------------- Panel Control -------------------//
    WriteCmdData( 0x90, 0x0010 ); 
    WriteCmdData( 0x92, 0x0000 );
    WriteCmdData( 0x07, 0x0133 ); // 262K color and display ON
    
    Deactivate();
}

void ILI9328_LCD::Sleep( void )
{
    //WriteCmdData( 0x10, 0x0001 ); // sleep mode: 0 = exit, 1 = enter
    LCD::Sleep();
}

void ILI9328_LCD::WakeUp( void )
{
    //WriteCmdData( 0x10, 0x0000 ); // sleep mode: 0 = exit, 1 = enter
    LCD::WakeUp();
}

void ILI9328_LCD::WriteCmd( unsigned short cmd )
{
    _lcd_pin_rs = LOW;
    _lcd_port->write( cmd );
    pulseLow( _lcd_pin_wr );
}

void ILI9328_LCD::WriteData( unsigned short data )
{
    _lcd_pin_rs = HIGH;
    _lcd_port->write( data );
    pulseLow( _lcd_pin_wr );
}

void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
{
    WriteCmdData( 0x20, x1 );
    WriteCmdData( 0x21, y1 );
    WriteCmdData( 0x50, x1 );
    WriteCmdData( 0x52, y1 );
    WriteCmdData( 0x51, x2 );
    WriteCmdData( 0x53, y2 );
    WriteCmd( 0x22 );
}

void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
{
    unsigned char r, g, b;
    unsigned short clr;
    if ( _colorDepth == RGB16 )
    {
        switch ( mode )
        {
            case RGB16:
                WriteData( color & 0xFFFF );
                break;
            case RGB18:
                r = ( color >> 10 ) & 0xF8;
                g = ( color >> 4 ) & 0xFC;
                b = ( color >> 1 ) & 0x1F;
                clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
                WriteData( clr );
                break;
            case RGB24:
                r = ( color >> 16 ) & 0xF8;
                g = ( color >> 8 ) & 0xFC;
                b = color & 0xF8;
                clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
                WriteData( clr );
                break;
        }
    }
    else if ( _colorDepth == RGB18 )
    {
        switch ( mode )
        {
            case RGB16:
                r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
                g = ( color >> 3 ) & 0xFC;
                b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
                WriteData( ( r << 8 ) | g );
                WriteData( b );
                break;
            case RGB18:
                b = ( color << 2 ) & 0xFC;
                g = ( color >> 4 ) & 0xFC;
                r = ( color >> 10 ) & 0xFC;
                WriteData( ( r << 8 ) | g );
                WriteData( b );
                break;
            case RGB24:
                r = ( color >> 16 ) & 0xFC;
                g = ( color >> 8 ) & 0xFC;
                b = color & 0xFC;
                WriteData( ( r << 8 ) | g );
                WriteData( b );
                break;
        }
    }
}