for Arduino TFT LCD Screen 160x128

Dependents:   TFTLCDSCREEN Pong_ILI9163C

Fork of TFT_ILI9163C by _ peu605

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TFT_ILI9163C_BASE.h Source File

TFT_ILI9163C_BASE.h

00001 #include "mbed.h"
00002 #include <Adafruit_GFX.h>
00003 
00004 
00005 //----- Define here witch display you own
00006 #define __144_RED_PCB__ //128x128
00007 //#define __22_RED_PCB__    //240x320
00008 //#define __144_AITENDO_PCB__   //128x128
00009 //---------------------------------------
00010 
00011 //ILI9163C versions------------------------
00012 #if defined(__144_RED_PCB__)
00013 /*
00014 This display:
00015 http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/271422122271
00016 This particular display has a design error! The controller has 3 pins to configure to constrain
00017 the memory and resolution to a fixed dimension (in that case 128x128) but they leaved those pins
00018 configured for 128x160 so there was several pixel memory addressing problems.
00019 I solved by setup several parameters that dinamically fix the resolution as needit so below
00020 the parameters for this diplay. If you have a strain or a correct display (can happen with chinese)
00021 you can copy those parameters and create setup for different displays.
00022 */
00023     #define _TFTWIDTH       128     //the REAL W resolution of the TFT 128
00024     #define _TFTHEIGHT      160     //the REAL H resolution of the TFT
00025     #define _GRAMWIDTH      128
00026     #define _GRAMHEIGH      160
00027 //  #define _GRAMSIZE       _GRAMWIDTH * _GRAMHEIGH //*see note 1
00028     #define _GRAMSIZE       _TFTWIDTH * _TFTHEIGHT  //this is enough to fill visible area
00029     #define __COLORSPC      1   // 1:GBR - 0:RGB
00030     #define __GAMMASET1     //uncomment for another gamma
00031     #define __OFFSET        (_GRAMHEIGH - _TFTHEIGHT)   // 32 *see note 2
00032     //Tested!
00033 
00034 #elif defined (__22_RED_PCB__)
00035 /*
00036 Like this one:
00037 http://www.ebay.it/itm/2-2-Serial-SPI-TFT-LCD-Display-Module-240x320-Chip-ILI9340C-PCB-Adapter-SD-Card-/281304733556
00038 Not tested!
00039 */
00040     #define _TFTWIDTH       240     //the REAL W resolution of the TFT
00041     #define _TFTHEIGHT      320     //the REAL H resolution of the TFT
00042     #define _GRAMWIDTH      240
00043     #define _GRAMHEIGH      320
00044     #define _GRAMSIZE       _GRAMWIDTH * _GRAMHEIGH
00045     #define __COLORSPC      1   // 1:GBR - 0:RGB
00046     #define __GAMMASET1     //uncomment for another gamma
00047     #define __OFFSET        0
00048     
00049 #elif defined(__144_AITENDO_PCB__)
00050 /*
00051 This display:
00052 http://www.aitendo.com/product/3857
00053 M014C9163SPI
00054 */
00055     #define _TFTWIDTH       128     //the REAL W resolution of the TFT  128
00056     #define _TFTHEIGHT      160     //the REAL H resolution of the TFT
00057     #define _GRAMWIDTH      128
00058     #define _GRAMHEIGH      128
00059     #define _GRAMSIZE       _GRAMWIDTH * _GRAMHEIGH
00060     #define __COLORSPC      1   // 1:GBR - 0:RGB
00061     #define __GAMMASET1     //uncomment for another gamma
00062     #define __OFFSET        0
00063     
00064 #else
00065     #define _TFTWIDTH       128     //128
00066     #define _TFTHEIGHT      160     //160
00067     #define _GRAMWIDTH      128
00068     #define _GRAMHEIGH      160
00069     #define _GRAMSIZE       _GRAMWIDTH * _GRAMHEIGH
00070     #define __COLORSPC      1   // 1:GBR - 0:RGB
00071     #define __GAMMASET1
00072     #define __OFFSET        0
00073 #endif
00074 /*
00075     Note 1: The __144_RED_PCB__ display has hardware addressing of 128 x 160
00076     but the tft resolution it's 128 x 128 so the dram should be set correctly
00077     
00078     Note 2: This is the offset between image in RAM and TFT. In that case 160 - 128 = 32;
00079 */
00080 //--------- Keep out hands from here!-------------
00081 // Color definitions
00082 #define BLACK           0x0000
00083 #define BLUE            0x001F
00084 #define RED             0xF800
00085 #define GREEN           0x07E0
00086 #define CYAN            0x07FF
00087 #define MAGENTA         0xF81F
00088 #define YELLOW          0xFFE0  
00089 #define WHITE           0xFFFF
00090 #define TRANSPARENT     -1
00091 
00092 //ILI9163C registers-----------------------
00093 #define CMD_NOP         0x00//Non operation
00094 #define CMD_SWRESET     0x01//Soft Reset
00095 #define CMD_SLPIN       0x10//Sleep ON
00096 #define CMD_SLPOUT      0x11//Sleep OFF
00097 #define CMD_PTLON       0x12//Partial Mode ON
00098 #define CMD_NORML       0x13//Normal Display ON
00099 #define CMD_DINVOF      0x20//Display Inversion OFF
00100 #define CMD_DINVON      0x21//Display Inversion ON
00101 #define CMD_GAMMASET    0x26//Gamma Set (0x01[1],0x02[2],0x04[3],0x08[4])
00102 #define CMD_DISPOFF     0x28//Display OFF
00103 #define CMD_DISPON      0x29//Display ON
00104 #define CMD_IDLEON      0x39//Idle Mode ON
00105 #define CMD_IDLEOF      0x38//Idle Mode OFF
00106 #define CMD_CLMADRS     0x2A//Column Address Set
00107 #define CMD_PGEADRS     0x2B//Page Address Set
00108 
00109 #define CMD_RAMWR       0x2C//Memory Write
00110 #define CMD_RAMRD       0x2E//Memory Read
00111 #define CMD_CLRSPACE    0x2D//Color Space : 4K/65K/262K
00112 #define CMD_PARTAREA    0x30//Partial Area
00113 #define CMD_VSCLLDEF    0x33//Vertical Scroll Definition
00114 #define CMD_TEFXLON     0x34//Tearing Effect Line ON
00115 #define CMD_TEFXLOF     0x35//Tearing Effect Line OFF
00116 #define CMD_MADCTL      0x36//Memory Access Control
00117 #define CMD_VSSTADRS    0x37//Vertical Scrolling Start address
00118 
00119 #define CMD_PIXFMT      0x3A//Interface Pixel Format
00120 #define CMD_FRMCTR1     0xB1//Frame Rate Control (In normal mode/Full colors)
00121 #define CMD_FRMCTR2     0xB2//Frame Rate Control(In Idle mode/8-colors)
00122 #define CMD_FRMCTR3     0xB3//Frame Rate Control(In Partial mode/full colors)
00123 #define CMD_DINVCTR     0xB4//Display Inversion Control
00124 #define CMD_RGBBLK      0xB5//RGB Interface Blanking Porch setting
00125 #define CMD_DFUNCTR     0xB6//Display Fuction set 5
00126 #define CMD_SDRVDIR     0xB7//Source Driver Direction Control
00127 #define CMD_GDRVDIR     0xB8//Gate Driver Direction Control 
00128 
00129 #define CMD_PWCTR1      0xC0//Power_Control1
00130 #define CMD_PWCTR2      0xC1//Power_Control2
00131 #define CMD_PWCTR3      0xC2//Power_Control3
00132 #define CMD_PWCTR4      0xC3//Power_Control4
00133 #define CMD_PWCTR5      0xC4//Power_Control5
00134 #define CMD_VCOMCTR1    0xC5//VCOM_Control 1
00135 #define CMD_VCOMCTR2    0xC6//VCOM_Control 2
00136 #define CMD_VCOMOFFS    0xC7//VCOM Offset Control
00137 #define CMD_PGAMMAC     0xE0//Positive Gamma Correction Setting
00138 #define CMD_NGAMMAC     0xE1//Negative Gamma Correction Setting
00139 #define CMD_GAMRSEL     0xF2//GAM_R_SEL
00140 
00141 
00142 class TFT_ILI9163C_BASE : public Adafruit_GFX, public SPI {
00143 
00144  public:
00145 
00146     TFT_ILI9163C_BASE(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc, PinName reset);
00147     TFT_ILI9163C_BASE(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc);
00148 
00149     void        begin(void),
00150                 setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),//graphic Addressing
00151                 setCursor(int16_t x,int16_t y),//char addressing
00152                 pushColor(uint16_t color),
00153                 clearScreen(uint16_t color=0x0000),//same as fillScreen
00154                 setRotation(uint8_t r);
00155                 
00156     void        display(bool onOff),
00157                 sleepMode(bool mode),
00158                 defineScrollArea(uint16_t taf,uint16_t bfa),
00159                 scroll(uint16_t ssa);
00160 
00161     virtual void    fillScreen(uint16_t color=0x0000),
00162                     drawPixel(int16_t x, int16_t y, uint16_t color),
00163                     drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
00164                     drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
00165                     fillRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color),
00166                     invertDisplay(bool i);
00167                 
00168   uint16_t      Color565(uint8_t r, uint8_t g, uint8_t b);
00169   void          setBitrate(uint32_t n); 
00170 
00171  protected:
00172  
00173     DigitalOut _cs;
00174     DigitalOut _dc;
00175     PinName _resetPinName;
00176 
00177  private:
00178 
00179     virtual void    init(PinName cs, PinName dc);
00180     virtual void    selectSlave();
00181     virtual void    deselectSlave();
00182     virtual void    setCommandMode();
00183     virtual void    setDataMode();
00184     virtual void    writecommand(uint8_t c);
00185     virtual void    writedata(uint8_t d);
00186     virtual void    writedata16(uint16_t d);
00187     virtual void    writedata32(uint16_t d1, uint16_t d2);
00188     virtual void    writedata16burst(uint16_t d, int32_t len);
00189  
00190     uint8_t     _Mactrl_Data;//container for the memory access control data
00191     uint8_t     _colorspaceData;
00192     uint8_t     sleep;
00193     void        colorSpace(uint8_t cspace);
00194     void        chipInit();
00195     bool        boundaryCheck(int16_t x,int16_t y);
00196     void        homeAddress();
00197     
00198 };