Forked from TFTLCD_8bit by Thiha Electronics
Dependents: GR-PEACH_TFTLCD_8bit
Fork of TFTLCD_8bit by
Revision 32:155abe4126e3, committed 2014-12-03
- Comitter:
- ThihaElectronics
- Date:
- Wed Dec 03 16:35:25 2014 +0000
- Parent:
- 31:9ca104de2f74
- Commit message:
- sorry if i broken any of your code by recent check in. please enlighten me how to unlink library
Changed in this revision
ili9328.cpp | Show annotated file Show diff for this revision Revisions of this file |
ili9328.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 9ca104de2f74 -r 155abe4126e3 ili9328.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ili9328.cpp Wed Dec 03 16:35:25 2014 +0000 @@ -0,0 +1,311 @@ +/* + * 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( 0xE5, 0x78F0 ); // set SRAM internal timing + WriteCmdData( 0x01, drivOut ); // set Driver Output Control + WriteCmdData( 0x02, 0x0200 ); // set 1 line inversion + WriteCmdData( 0x03, entryMod ); // 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, gateScan ); // Gate Scan Line (0xA700) + 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 ) +{ + 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(); +} + +void ILI9328_LCD::WriteCmd( unsigned short cmd ) +{ + + unsigned short u,l; + u = (cmd>>8) & 0xFF; + l = cmd & 0xFF; + +//New + _lcd_pin_cs = LOW; +// + _lcd_pin_rs = LOW; + +//New + *_lcd_pin_rd = HIGH; + _lcd_pin_wr = HIGH; +// + _lcd_port->write( u ); + pulseLow( _lcd_pin_wr ); + + _lcd_port->write( l ); + pulseLow( _lcd_pin_wr ); + +//New + _lcd_pin_cs = HIGH; +// +} + +void ILI9328_LCD::WriteData( unsigned short data ) +{ + unsigned short u,l; + u = (data>>8) & 0xFF; + l = data & 0xFF; + +//New + _lcd_pin_cs = LOW; +// + _lcd_pin_rs = HIGH; +//New + *_lcd_pin_rd = HIGH; + _lcd_pin_wr = HIGH; +// + _lcd_port->write( u ); + pulseLow( _lcd_pin_wr ); + + _lcd_port->write( l ); + pulseLow( _lcd_pin_wr ); + +//New + _lcd_pin_cs = HIGH; +// +} + +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 ); + } +}
diff -r 9ca104de2f74 -r 155abe4126e3 ili9328.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ili9328.h Wed Dec 03 16:35:25 2014 +0000 @@ -0,0 +1,188 @@ +/** \file ili9328.h + * \brief mbed LCD driver for displays with the ILI9328 controller. + * \copyright GNU Public License, v2. or later + * + * This library is based on the Arduino/chipKIT UTFT library by Henning + * Karlsen, http://henningkarlsen.com/electronics/library.php?id=52 + * + * 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 + * + *********************************************************************/ +#ifndef TFTLCD_ILI9328_H +#define TFTLCD_ILI9328_H + +#include "lcd_base.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Represents a LCD instance. + * + * This is the utility class, through which the display can be manipulated + * and graphics objects can be shown to the user. A known display, which + * works with this library is the INANBO-T24-ILI9328-V11 - a RGB TFT + * with 240x320 pixels resolution and 65K/262K colors, using 8/16-bit interface. + * + * The display works with a supply voltage of 2.8-3.3 volts for both logic and + * backlight. It can be driven in 8bit or 16bit interface mode. (Current + * version of the driver works only in 16bit mode for now.) + * + * How to use: + * \code + * // include the library, this will also pull in the header for the provided fonts + * #include "ili9328.h" + * + * // prepare the data bus for writing commands and pixel data + * BusOut dataBus( p30, p29, p28, p27, p26, p25, p24, p23, p22, p21, p20, p19, p18, p17, p16, p15 ); // 16 pins + * // create the lcd instance + * ILI9328_LCD lcd( p14, p13, p12, p11, &dataBus ); // control pins and data bus + * + * int main() + * { + * // initialize display - place it in standard portrait mode and set background to black and + * // foreground to white color. + * lcd.Initialize(); + * // set current font to the smallest 8x12 pixels font. + * lcd.SetFont( Font8x12 ); + * // print something on the screen + * lcd.Print( "Hello, World!", CENTER, 25 ); // align text to center horizontally and use starndard colors + * + * while ( 1 ) { } + * } + * + * \endcode + * \version 0.1 + * \author Todor Todorov + */ +class ILI9328_LCD : public LCD +{ +public: + /** Creates a new instance of the class. + * + * \param CS Pin for the ChipSelect signal. + * \param RESET Pin for the RESET line. + * \param RS Pin for the RS signal. + * \param WR Pin for the WR signal. + * \param DATA_PORT Address of the data bus for transfer of commands and pixel data. + * \param BL Pin for controlling the backlight. By default not used. + * \param RD Pin for the RD signal. This line is not needed by the driver, so if you would like to + * use the pin on the mbed for something else, just pull-up the respective pin on the LCD high, + * and do not assign a value to this parameter when creating the controller instance. + * \param blType The backlight type, the default is to utilize the pin - if supplied - as a simple on/off switch + * \param defaultBacklightLevel If using PWM to control backlight, this would be the default brightness in percent after LCD initialization. + */ + ILI9328_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL = NC, PinName RD = NC, backlight_t blType = Constant, float defaultBackLightLevel = 1.0 ); + + /** Initialize display. + * + * Wakes up the display from sleep, initializes power parameters. + * This function must be called first, befor any painting on the + * display is done, otherwise the positioning of graphical elements + * will not work properly and any paynt operation will not be visible + * or produce garbage. + * + * \param oritentation The display orientation, landscape is default. + * \param colors The correct color depth to use for the pixel data. Value is disregarded. + */ + virtual void Initialize( orientation_t orientation = LANDSCAPE, colordepth_t colors = RGB16 ); + + /** Puts the display to sleep. + * + * When the display is in sleep mode, its power consumption is + * minimized. Before new pixel data can be written to the display + * memory, the controller needs to be brought out of sleep mode. + * \sa #WakeUp( void ); + * \remarks The result of this operation might not be exactly as + * expected. Putting the display to sleep will cause the + * controller to switch to the standard color of the LCD, + * so depending on whether the display is normally white, + * or normally dark, the screen might or might not go + * dark. Additional power saving can be achieved, if + * the backlight of the used display is not hardwired on + * the PCB and can be controlled via the BL pin. + */ + virtual void Sleep( void ); + + /** Wakes up the display from sleep mode. + * + * This function needs to be called before any other, when the + * display has been put into sleep mode by a previois call to + * #Sleep( void ). + */ + virtual void WakeUp( void ); + +protected: + /** Sends a command to the display. + * + * \param cmd The display command. + * \remarks Commands are controller-specific and this function needs to + * be implemented separately for each available controller. + */ + virtual void WriteCmd( unsigned short cmd ); + + /** Sends pixel data to the display. + * + * \param data The display data. + * \remarks Sending data is controller-specific and this function needs to + * be implemented separately for each available controller. + */ + virtual void WriteData( unsigned short data ); + + /** Assigns a chunk of the display memory to receive data. + * + * When data is sent to the display after this function completes, the opertion will + * start from the begining of the assigned address (pixel position) and the pointer + * will be automatically incremented so that the next data write operation will continue + * with the next pixel from the memory block. If more data is written than available + * pixels, at the end of the block the pointer will jump back to its beginning and + * commence again, until the next address change command is sent to the display. + * + * \param x1 The X coordinate of the pixel at the beginning of the block. + * \param y1 The Y coordinate of the pixel at the beginning of the block. + * \param x2 The X coordinate of the pixel at the end of the block. + * \param y2 The Y coordinate of the pixel at the end of the block. + * \remarks Addressing commands are controller-specific and this function needs to be + * implemented separately for each available controller. + */ + virtual void SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 ); + + /** Sets the color of the pixel at the address pointer of the controller. + * + * This function is to be provided by each implementation separately in + * order to account for different color depth used by the controller. + * \param color The color of the pixel. + * \param mode The depth (palette) of the color. + */ + virtual void SetPixelColor( unsigned int color, colordepth_t mode = RGB24 ); + +private: + DigitalOut _lcd_pin_wr; + BusOut* _lcd_port; + DigitalOut* _lcd_pin_bl; + DigitalOut* _lcd_pin_rd; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* TFTLCD_ILI9328_H */