Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TFTLCD by
ili9328.cpp
- Committer:
- nameless129
- Date:
- 2015-07-12
- Revision:
- 28:45e8aa1fc420
- Parent:
- 27:26491d710e72
- Child:
- 30:1e9905bdfd15
File content as of revision 28:45e8aa1fc420:
/*
* 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 drivOut = 0;
short entryMod = 0;
short gateScan = 0x2700;
switch ( _orientation )
{
case LANDSCAPE:
drivOut = 0x0100;
entryMod |= 0x0038;
gateScan |= 0x0000;
break;
case LANDSCAPE_REV:
drivOut = 0x0000;
entryMod |= 0x0038;
gateScan |= 0x8000;
break;
case PORTRAIT_REV:
drivOut = 0x0000;
entryMod |= 0x0030;
gateScan |= 0x0000;
break;
case PORTRAIT:
default:
drivOut = 0x0100;
entryMod |= 0x0030;
gateScan |= 0x8000;
break;
}
switch ( _colorDepth )
{
case RGB18:
entryMod |= 0x9000;
break;
case RGB16:
default:
entryMod |= 0x1000;
break;
}
WriteCmdData( ILI932X_DRIV_OUT_CTRL, drivOut ); // set Driver Output Control
WriteCmdData( ILI932X_DRIV_WAV_CTRL, 0x0700 ); // set 1 line inversion
WriteCmdData( ILI932X_ENTRY_MOD, entryMod ); // set GRAM write direction and BGR=1.
WriteCmdData( ILI932X_RESIZE_CTRL, 0x0000 ); // Resize register
WriteCmdData( ILI932X_DISP_CTRL2, 0x0202 ); // set the back porch and front porch
WriteCmdData( ILI932X_DISP_CTRL3, 0x0000 ); // set non-display area refresh cycle ISC[3:0]
WriteCmdData( ILI932X_DISP_CTRL4, 0x0000 ); // FMARK function
WriteCmdData( ILI932X_RGB_DISP_IF_CTRL1, 0x0000 ); // RGB interface setting
WriteCmdData( ILI932X_FRM_MARKER_POS, 0x0000 ); // Frame marker Position
WriteCmdData( ILI932X_RGB_DISP_IF_CTRL2, 0x0000 ); // RGB interface polarity
// ----------- Power On sequence ----------- //
WriteCmdData( ILI932X_POW_CTRL1, 0x0000 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
WriteCmdData( ILI932X_POW_CTRL2, 0x0007 ); // DC1[2:0], DC0[2:0], VC[2:0]
WriteCmdData( ILI932X_POW_CTRL3, 0x0000 ); // VREG1OUT voltage
WriteCmdData( ILI932X_POW_CTRL4, 0x0000 ); // VDV[4:0] for VCOM amplitude
wait_ms( 200 ); // Dis-charge capacitor power voltage
WriteCmdData( ILI932X_POW_CTRL1, 0x1690 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
WriteCmdData( ILI932X_POW_CTRL2, 0x0227 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
wait_ms( 50 ); // Delay 50ms
WriteCmdData( ILI932X_POW_CTRL3, 0x001A ); // 0012
wait_ms( 50 ); // Delay 50ms
WriteCmdData( ILI932X_POW_CTRL4, 0x1800 ); // VDV[4:0] for VCOM amplitude
WriteCmdData( ILI932X_POW_CTRL7, 0x002A ); // 04 VCM[5:0] for VCOMH
wait_ms( 50 ); // Delay 50ms
WriteCmdData( ILI932X_GRAM_HOR_AD, 0x0000 ); // GRAM horizontal Address
WriteCmdData( ILI932X_GRAM_VER_AD, 0x0000 ); // GRAM Vertical Address
// ----------- Adjust the Gamma Curve ----------//
WriteCmdData( ILI932X_GAMMA_CTRL1, 0x0000 );
WriteCmdData( ILI932X_GAMMA_CTRL2, 0x0000 );
WriteCmdData( ILI932X_GAMMA_CTRL3, 0x0000 );
WriteCmdData( ILI932X_GAMMA_CTRL4, 0x0206 );
WriteCmdData( ILI932X_GAMMA_CTRL5, 0x0808 );
WriteCmdData( ILI932X_GAMMA_CTRL6, 0x0007 );
WriteCmdData( ILI932X_GAMMA_CTRL7, 0x0201 );
WriteCmdData( ILI932X_GAMMA_CTRL8, 0x0000 );
WriteCmdData( ILI932X_GAMMA_CTRL9, 0x0000 );
WriteCmdData( ILI932X_GAMMA_CTRL10, 0x0000 );
//------------------ Set GRAM area ---------------//
WriteCmdData( ILI932X_HOR_END_AD, 0x00EF ); // Horizontal GRAM End Address
WriteCmdData( ILI932X_VER_START_AD, 0x0000 ); // Vertical GRAM Start Address
WriteCmdData( ILI932X_VER_END_AD, 0x013F ); // Vertical GRAM Start Address
WriteCmdData( ILI932X_GATE_SCAN_CTRL1, gateScan ); // Gate Scan Line (0xA700)
WriteCmdData( ILI932X_GATE_SCAN_CTRL2, 0x0003 ); // NDL,VLE, REV
WriteCmdData( ILI932X_GATE_SCAN_CTRL3, 0x0000 ); // set scrolling line
//-------------- Panel Control -------------------//
WriteCmdData( ILI932X_PANEL_IF_CTRL1, 0x0010 );
WriteCmdData( ILI932X_PANEL_IF_CTRL2, 0x0000 );
WriteCmdData( ILI932X_PANEL_IF_CTRL4, 0X1100 );
WriteCmdData( ILI932X_DISP_CTRL1, 0x0133 ); // 262K color and display ON
Deactivate();
}
void ILI9328_LCD::Sleep( void )
{
Activate();
WriteCmdData( 0x10, 0x1692 ); // enter sleep mode
wait_ms( 200 );
LCD::Sleep();
Deactivate();
}
void ILI9328_LCD::WakeUp( void )
{
Activate();
WriteCmdData( 0x10, 0x1690 ); // exit sleep mode
wait_ms( 200 );
LCD::WakeUp();
Deactivate();
}
//fix to 8bit mode
void ILI9328_LCD::WriteCmd( unsigned short cmd )
{
_lcd_pin_rs = LOW;
_lcd_port->write( 0 );
pulseLow( _lcd_pin_wr );
_lcd_port->write( cmd );
pulseLow( _lcd_pin_wr );
}
//fix to 8bit mode
void ILI9328_LCD::WriteData( unsigned short data )
{
_lcd_pin_rs = HIGH;
_lcd_port->write( data>>8 );
pulseLow( _lcd_pin_wr );
_lcd_port->write( data&0xff );
pulseLow( _lcd_pin_wr );
}
void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
{
switch ( _orientation )
{
case LANDSCAPE:
case LANDSCAPE_REV:
WriteCmdData( 0x20, y1 );
WriteCmdData( 0x21, x1 );
WriteCmdData( 0x50, y1 );
WriteCmdData( 0x52, x1 );
WriteCmdData( 0x51, y2 );
WriteCmdData( 0x53, x2 );
break;
case PORTRAIT_REV:
case PORTRAIT:
default:
WriteCmdData( 0x20, x1 );
WriteCmdData( 0x21, y1 );
WriteCmdData( 0x50, x1 );
WriteCmdData( 0x52, y1 );
WriteCmdData( 0x51, x2 );
WriteCmdData( 0x53, y2 );
break;
}
WriteCmd( 0x22 );
}
void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
{
unsigned char r, g, b;
unsigned short clr;
r = g = b = 0;
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 );
break;
case RGB18:
b = ( color << 2 ) & 0xFC;
g = ( color >> 4 ) & 0xFC;
r = ( color >> 10 ) & 0xFC;
break;
case RGB24:
r = ( color >> 16 ) & 0xFC;
g = ( color >> 8 ) & 0xFC;
b = color & 0xFC;
break;
}
clr = ( r << 8 ) | ( g << 2 ) | ( b >> 4 );
WriteData( clr );
WriteData( b << 4 );
}
}
