for Arduino TFT LCD Screen 160x128

Dependents:   TFTLCDSCREEN Pong_ILI9163C

Fork of TFT_ILI9163C by _ peu605

Committer:
peu605
Date:
Thu Jan 22 12:16:31 2015 +0000
Revision:
1:c271e7e2e330
Parent:
0:f90a4405ef98
Child:
2:6c1fadae252f
enabled to use mbed general SPI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peu605 0:f90a4405ef98 1 /*
peu605 0:f90a4405ef98 2 ILI9163C - A fast SPI driver for TFT that use Ilitek ILI9163C.
peu605 0:f90a4405ef98 3
peu605 0:f90a4405ef98 4 Features:
peu605 0:f90a4405ef98 5 - Very FAST!, expecially with Teensy 3.x where uses DMA SPI.
peu605 0:f90a4405ef98 6 - It uses just 4 or 5 wires.
peu605 0:f90a4405ef98 7 - Compatible at command level with Adafruit display series so it's easy to adapt existing code.
peu605 0:f90a4405ef98 8 - It uses the standard Adafruit_GFX Library (you need to install).
peu605 0:f90a4405ef98 9
peu605 0:f90a4405ef98 10 Background:
peu605 0:f90a4405ef98 11 I got one of those displays from a chinese ebay seller but unfortunatly I cannot get
peu605 0:f90a4405ef98 12 any working library so I decided to hack it. ILI9163C looks pretty similar to other
peu605 0:f90a4405ef98 13 display driver but it uses it's own commands so it's tricky to work with it unlsess you
peu605 0:f90a4405ef98 14 carefully fight with his gigantic and not so clever datasheet.
peu605 0:f90a4405ef98 15 My display it's a 1.44"", 128x128 that suppose to substitute Nokia 5110 LCD and here's the
peu605 0:f90a4405ef98 16 first confusion! Many sellers claim that it's compatible with Nokia 5110 (that use a philips
peu605 0:f90a4405ef98 17 controller) but the only similarity it's the pin names since that this one it's color and
peu605 0:f90a4405ef98 18 have totally different controller that's not compatible.
peu605 0:f90a4405ef98 19 http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/141196897388
peu605 0:f90a4405ef98 20 http://www.elecrow.com/144-128x-128-tft-lcd-with-spi-interface-p-855.html
peu605 0:f90a4405ef98 21 Pay attention that can drive different resolutions and your display can be
peu605 0:f90a4405ef98 22 160*128 or whatever, also there's a strain of this display with a black PCB that a friend of mine
peu605 0:f90a4405ef98 23 got some weeks ago and need some small changes in library to get working.
peu605 0:f90a4405ef98 24 If you look at TFT_ILI9163C.h file you can add your modifications and let me know so I
peu605 0:f90a4405ef98 25 can include for future versions.
peu605 0:f90a4405ef98 26
peu605 0:f90a4405ef98 27 Code Optimizations:
peu605 0:f90a4405ef98 28 The purpose of this library it's SPEED. I have tried to use hardware optimized calls
peu605 0:f90a4405ef98 29 where was possible and results are quite good for most applications, actually nly filled circles
peu605 0:f90a4405ef98 30 are still a bit slow. Many SPI call has been optimized by reduce un-needed triggers to RS and CS
peu605 0:f90a4405ef98 31 lines. Of course it can be improved so feel free to add suggestions.
peu605 0:f90a4405ef98 32 -------------------------------------------------------------------------------
peu605 0:f90a4405ef98 33 Copyright (c) 2014, .S.U.M.O.T.O.Y., coded by Max MC Costa.
peu605 0:f90a4405ef98 34
peu605 0:f90a4405ef98 35 TFT_ILI9163C Library is free software: you can redistribute it and/or modify
peu605 0:f90a4405ef98 36 it under the terms of the GNU General Public License as published by
peu605 0:f90a4405ef98 37 the Free Software Foundation, either version 3 of the License, or
peu605 0:f90a4405ef98 38 (at your option) any later version.
peu605 0:f90a4405ef98 39
peu605 0:f90a4405ef98 40 TFT_ILI9163C Library is distributed in the hope that it will be useful,
peu605 0:f90a4405ef98 41 but WITHOUT ANY WARRANTY; without even the implied warranty of
peu605 0:f90a4405ef98 42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
peu605 0:f90a4405ef98 43 GNU General Public License for more details.
peu605 0:f90a4405ef98 44
peu605 0:f90a4405ef98 45 You should have received a copy of the GNU General Public License
peu605 0:f90a4405ef98 46 along with Foobar. If not, see <http://www.gnu.org/licenses/>.
peu605 0:f90a4405ef98 47 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
peu605 0:f90a4405ef98 48 This file needs the following Libraries:
peu605 0:f90a4405ef98 49
peu605 0:f90a4405ef98 50 Adafruit_GFX by Adafruit:
peu605 0:f90a4405ef98 51 https://github.com/adafruit/Adafruit-GFX-Library
peu605 0:f90a4405ef98 52 Remember to update GFX library often to have more features with this library!
peu605 0:f90a4405ef98 53 From this version I'm using my version of Adafruit_GFX library:
peu605 0:f90a4405ef98 54 https://github.com/sumotoy/Adafruit-GFX-Library
peu605 0:f90a4405ef98 55 It has faster char rendering and some small little optimizations but you can
peu605 0:f90a4405ef98 56 choose one of the two freely since are both fully compatible.
peu605 0:f90a4405ef98 57 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
peu605 0:f90a4405ef98 58 Special Thanks:
peu605 0:f90a4405ef98 59 Thanks Adafruit for his Adafruit_GFX!
peu605 0:f90a4405ef98 60 Thanks to Paul Stoffregen for his beautiful Teensy3 and DMA SPI.
peu605 0:f90a4405ef98 61
peu605 0:f90a4405ef98 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
peu605 0:f90a4405ef98 63 Version:
peu605 0:f90a4405ef98 64 0.1a1: First release, compile correctly. Altrough not fully working!
peu605 0:f90a4405ef98 65 0.1a3: Better but still some addressing problems.
peu605 0:f90a4405ef98 66 0.1b1: Beta! Addressing solved, now rotation works and boundaries ok.
peu605 0:f90a4405ef98 67 0.2b1: Cleaned up.
peu605 0:f90a4405ef98 68 0.2b3: Added 2.2" Red PCB parameters
peu605 0:f90a4405ef98 69 0.2b4: Bug fixes, added colorSpace (for future send image)
peu605 0:f90a4405ef98 70 0.2b5: Cleaning
peu605 0:f90a4405ef98 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
peu605 0:f90a4405ef98 72 BugList of the current version:
peu605 0:f90a4405ef98 73
peu605 0:f90a4405ef98 74 - Actually no scroll commands (only in release will be included).
peu605 0:f90a4405ef98 75 */
peu605 0:f90a4405ef98 76
peu605 0:f90a4405ef98 77 /**
peu605 0:f90a4405ef98 78 * TFT_ILI9163C library for ST Nucleo F411RE
peu605 0:f90a4405ef98 79 *
peu605 0:f90a4405ef98 80 * @author Copyright (c) 2014, .S.U.M.O.T.O.Y., coded by Max MC Costa
peu605 0:f90a4405ef98 81 * https://github.com/sumotoy/TFT_ILI9163C
peu605 0:f90a4405ef98 82 *
peu605 0:f90a4405ef98 83 * @author modified by masuda, Masuda Naika
peu605 0:f90a4405ef98 84 */
peu605 0:f90a4405ef98 85
peu605 0:f90a4405ef98 86 #ifndef _TFT_ILI9163CLIB_H_
peu605 0:f90a4405ef98 87 #define _TFT_ILI9163CLIB_H_
peu605 0:f90a4405ef98 88
peu605 0:f90a4405ef98 89 #include "mbed.h"
peu605 0:f90a4405ef98 90
peu605 0:f90a4405ef98 91 #include <Adafruit_GFX.h>
peu605 0:f90a4405ef98 92
peu605 1:c271e7e2e330 93 #if defined(STM32F411xE)
peu605 1:c271e7e2e330 94 #define __F411RE__
peu605 1:c271e7e2e330 95 #define __F411RE_DMA__ // use DMA
peu605 1:c271e7e2e330 96 #endif
peu605 0:f90a4405ef98 97
peu605 0:f90a4405ef98 98 //----- Define here witch display you own
peu605 0:f90a4405ef98 99 #define __144_RED_PCB__ //128x128
peu605 0:f90a4405ef98 100 //#define __22_RED_PCB__ //240x320
peu605 0:f90a4405ef98 101 //#define __144_AITENDO_PCB__ //128x128
peu605 0:f90a4405ef98 102 //---------------------------------------
peu605 0:f90a4405ef98 103
peu605 0:f90a4405ef98 104 //ILI9163C versions------------------------
peu605 0:f90a4405ef98 105 #if defined(__144_RED_PCB__)
peu605 0:f90a4405ef98 106 /*
peu605 0:f90a4405ef98 107 This display:
peu605 0:f90a4405ef98 108 http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/271422122271
peu605 0:f90a4405ef98 109 This particular display has a design error! The controller has 3 pins to configure to constrain
peu605 0:f90a4405ef98 110 the memory and resolution to a fixed dimension (in that case 128x128) but they leaved those pins
peu605 0:f90a4405ef98 111 configured for 128x160 so there was several pixel memory addressing problems.
peu605 0:f90a4405ef98 112 I solved by setup several parameters that dinamically fix the resolution as needit so below
peu605 0:f90a4405ef98 113 the parameters for this diplay. If you have a strain or a correct display (can happen with chinese)
peu605 0:f90a4405ef98 114 you can copy those parameters and create setup for different displays.
peu605 0:f90a4405ef98 115 */
peu605 0:f90a4405ef98 116 #define _TFTWIDTH 128 //the REAL W resolution of the TFT
peu605 0:f90a4405ef98 117 #define _TFTHEIGHT 128 //the REAL H resolution of the TFT
peu605 0:f90a4405ef98 118 #define _GRAMWIDTH 128
peu605 0:f90a4405ef98 119 #define _GRAMHEIGH 160
peu605 0:f90a4405ef98 120 // #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH //*see note 1
peu605 0:f90a4405ef98 121 #define _GRAMSIZE _TFTWIDTH * _TFTHEIGHT //this is enough to fill visible area
peu605 0:f90a4405ef98 122 #define __COLORSPC 1 // 1:GBR - 0:RGB
peu605 0:f90a4405ef98 123 #define __GAMMASET1 //uncomment for another gamma
peu605 0:f90a4405ef98 124 #define __OFFSET (_GRAMHEIGH - _TFTHEIGHT) // 32 *see note 2
peu605 0:f90a4405ef98 125 //Tested!
peu605 0:f90a4405ef98 126
peu605 0:f90a4405ef98 127 #elif defined (__22_RED_PCB__)
peu605 0:f90a4405ef98 128 /*
peu605 0:f90a4405ef98 129 Like this one:
peu605 0:f90a4405ef98 130 http://www.ebay.it/itm/2-2-Serial-SPI-TFT-LCD-Display-Module-240x320-Chip-ILI9340C-PCB-Adapter-SD-Card-/281304733556
peu605 0:f90a4405ef98 131 Not tested!
peu605 0:f90a4405ef98 132 */
peu605 0:f90a4405ef98 133 #define _TFTWIDTH 240 //the REAL W resolution of the TFT
peu605 0:f90a4405ef98 134 #define _TFTHEIGHT 320 //the REAL H resolution of the TFT
peu605 0:f90a4405ef98 135 #define _GRAMWIDTH 240
peu605 0:f90a4405ef98 136 #define _GRAMHEIGH 320
peu605 0:f90a4405ef98 137 #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH
peu605 0:f90a4405ef98 138 #define __COLORSPC 1 // 1:GBR - 0:RGB
peu605 0:f90a4405ef98 139 #define __GAMMASET1 //uncomment for another gamma
peu605 0:f90a4405ef98 140 #define __OFFSET 0
peu605 0:f90a4405ef98 141
peu605 0:f90a4405ef98 142 #elif defined(__144_AITENDO_PCB__)
peu605 0:f90a4405ef98 143 /*
peu605 0:f90a4405ef98 144 This display:
peu605 0:f90a4405ef98 145 http://www.aitendo.com/product/3857
peu605 0:f90a4405ef98 146 M014C9163SPI
peu605 0:f90a4405ef98 147 */
peu605 0:f90a4405ef98 148 #define _TFTWIDTH 128 //the REAL W resolution of the TFT
peu605 0:f90a4405ef98 149 #define _TFTHEIGHT 128 //the REAL H resolution of the TFT
peu605 0:f90a4405ef98 150 #define _GRAMWIDTH 128
peu605 0:f90a4405ef98 151 #define _GRAMHEIGH 128
peu605 0:f90a4405ef98 152 #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH
peu605 0:f90a4405ef98 153 #define __COLORSPC 1 // 1:GBR - 0:RGB
peu605 0:f90a4405ef98 154 #define __GAMMASET1 //uncomment for another gamma
peu605 0:f90a4405ef98 155 #define __OFFSET 0
peu605 0:f90a4405ef98 156
peu605 0:f90a4405ef98 157 #else
peu605 0:f90a4405ef98 158 #define _TFTWIDTH 128 //128
peu605 0:f90a4405ef98 159 #define _TFTHEIGHT 160 //160
peu605 0:f90a4405ef98 160 #define _GRAMWIDTH 128
peu605 0:f90a4405ef98 161 #define _GRAMHEIGH 160
peu605 0:f90a4405ef98 162 #define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH
peu605 0:f90a4405ef98 163 #define __COLORSPC 1 // 1:GBR - 0:RGB
peu605 0:f90a4405ef98 164 #define __GAMMASET1
peu605 0:f90a4405ef98 165 #define __OFFSET 0
peu605 0:f90a4405ef98 166 #endif
peu605 0:f90a4405ef98 167 /*
peu605 0:f90a4405ef98 168 Note 1: The __144_RED_PCB__ display has hardware addressing of 128 x 160
peu605 0:f90a4405ef98 169 but the tft resolution it's 128 x 128 so the dram should be set correctly
peu605 0:f90a4405ef98 170
peu605 0:f90a4405ef98 171 Note 2: This is the offset between image in RAM and TFT. In that case 160 - 128 = 32;
peu605 0:f90a4405ef98 172 */
peu605 0:f90a4405ef98 173 //--------- Keep out hands from here!-------------
peu605 0:f90a4405ef98 174
peu605 0:f90a4405ef98 175 #define BLACK 0x0000
peu605 0:f90a4405ef98 176 #define WHITE 0xFFFF
peu605 0:f90a4405ef98 177
peu605 0:f90a4405ef98 178 //ILI9163C registers-----------------------
peu605 0:f90a4405ef98 179 #define CMD_NOP 0x00//Non operation
peu605 0:f90a4405ef98 180 #define CMD_SWRESET 0x01//Soft Reset
peu605 0:f90a4405ef98 181 #define CMD_SLPIN 0x10//Sleep ON
peu605 0:f90a4405ef98 182 #define CMD_SLPOUT 0x11//Sleep OFF
peu605 0:f90a4405ef98 183 #define CMD_PTLON 0x12//Partial Mode ON
peu605 0:f90a4405ef98 184 #define CMD_NORML 0x13//Normal Display ON
peu605 0:f90a4405ef98 185 #define CMD_DINVOF 0x20//Display Inversion OFF
peu605 0:f90a4405ef98 186 #define CMD_DINVON 0x21//Display Inversion ON
peu605 0:f90a4405ef98 187 #define CMD_GAMMASET 0x26//Gamma Set (0x01[1],0x02[2],0x04[3],0x08[4])
peu605 0:f90a4405ef98 188 #define CMD_DISPOFF 0x28//Display OFF
peu605 0:f90a4405ef98 189 #define CMD_DISPON 0x29//Display ON
peu605 0:f90a4405ef98 190 #define CMD_IDLEON 0x39//Idle Mode ON
peu605 0:f90a4405ef98 191 #define CMD_IDLEOF 0x38//Idle Mode OFF
peu605 0:f90a4405ef98 192 #define CMD_CLMADRS 0x2A//Column Address Set
peu605 0:f90a4405ef98 193 #define CMD_PGEADRS 0x2B//Page Address Set
peu605 0:f90a4405ef98 194
peu605 0:f90a4405ef98 195 #define CMD_RAMWR 0x2C//Memory Write
peu605 0:f90a4405ef98 196 #define CMD_RAMRD 0x2E//Memory Read
peu605 0:f90a4405ef98 197 #define CMD_CLRSPACE 0x2D//Color Space : 4K/65K/262K
peu605 0:f90a4405ef98 198 #define CMD_PARTAREA 0x30//Partial Area
peu605 0:f90a4405ef98 199 #define CMD_VSCLLDEF 0x33//Vertical Scroll Definition
peu605 0:f90a4405ef98 200 #define CMD_TEFXLON 0x34//Tearing Effect Line ON
peu605 0:f90a4405ef98 201 #define CMD_TEFXLOF 0x35//Tearing Effect Line OFF
peu605 0:f90a4405ef98 202 #define CMD_MADCTL 0x36//Memory Access Control
peu605 0:f90a4405ef98 203
peu605 0:f90a4405ef98 204 #define CMD_PIXFMT 0x3A//Interface Pixel Format
peu605 0:f90a4405ef98 205 #define CMD_FRMCTR1 0xB1//Frame Rate Control (In normal mode/Full colors)
peu605 0:f90a4405ef98 206 #define CMD_FRMCTR2 0xB2//Frame Rate Control(In Idle mode/8-colors)
peu605 0:f90a4405ef98 207 #define CMD_FRMCTR3 0xB3//Frame Rate Control(In Partial mode/full colors)
peu605 0:f90a4405ef98 208 #define CMD_DINVCTR 0xB4//Display Inversion Control
peu605 0:f90a4405ef98 209 #define CMD_RGBBLK 0xB5//RGB Interface Blanking Porch setting
peu605 0:f90a4405ef98 210 #define CMD_DFUNCTR 0xB6//Display Fuction set 5
peu605 0:f90a4405ef98 211 #define CMD_SDRVDIR 0xB7//Source Driver Direction Control
peu605 0:f90a4405ef98 212 #define CMD_GDRVDIR 0xB8//Gate Driver Direction Control
peu605 0:f90a4405ef98 213
peu605 0:f90a4405ef98 214 #define CMD_PWCTR1 0xC0//Power_Control1
peu605 0:f90a4405ef98 215 #define CMD_PWCTR2 0xC1//Power_Control2
peu605 0:f90a4405ef98 216 #define CMD_PWCTR3 0xC2//Power_Control3
peu605 0:f90a4405ef98 217 #define CMD_PWCTR4 0xC3//Power_Control4
peu605 0:f90a4405ef98 218 #define CMD_PWCTR5 0xC4//Power_Control5
peu605 0:f90a4405ef98 219 #define CMD_VCOMCTR1 0xC5//VCOM_Control 1
peu605 0:f90a4405ef98 220 #define CMD_VCOMCTR2 0xC6//VCOM_Control 2
peu605 0:f90a4405ef98 221 #define CMD_VCOMOFFS 0xC7//VCOM Offset Control
peu605 0:f90a4405ef98 222 #define CMD_PGAMMAC 0xE0//Positive Gamma Correction Setting
peu605 0:f90a4405ef98 223 #define CMD_NGAMMAC 0xE1//Negative Gamma Correction Setting
peu605 0:f90a4405ef98 224 #define CMD_GAMRSEL 0xF2//GAM_R_SEL
peu605 0:f90a4405ef98 225
peu605 0:f90a4405ef98 226
peu605 0:f90a4405ef98 227 class TFT_ILI9163C : public Adafruit_GFX, public SPI {
peu605 0:f90a4405ef98 228
peu605 0:f90a4405ef98 229 public:
peu605 0:f90a4405ef98 230
peu605 0:f90a4405ef98 231 TFT_ILI9163C(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc, PinName reset);
peu605 0:f90a4405ef98 232 TFT_ILI9163C(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc);
peu605 0:f90a4405ef98 233
peu605 0:f90a4405ef98 234 void begin(void),
peu605 0:f90a4405ef98 235 setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),//graphic Addressing
peu605 0:f90a4405ef98 236 setCursor(int16_t x,int16_t y),//char addressing
peu605 0:f90a4405ef98 237 pushColor(uint16_t color),
peu605 0:f90a4405ef98 238 clearScreen(uint16_t color=0x0000),//same as fillScreen
peu605 0:f90a4405ef98 239 setRotation(uint8_t r);
peu605 0:f90a4405ef98 240
peu605 0:f90a4405ef98 241 virtual void fillScreen(uint16_t color=0x0000),
peu605 0:f90a4405ef98 242 drawPixel(int16_t x, int16_t y, uint16_t color),
peu605 0:f90a4405ef98 243 drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
peu605 0:f90a4405ef98 244 drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
peu605 0:f90a4405ef98 245 fillRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color),
peu605 0:f90a4405ef98 246 invertDisplay(bool i);
peu605 0:f90a4405ef98 247
peu605 0:f90a4405ef98 248 uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
peu605 0:f90a4405ef98 249 void setBitrate(uint32_t n);
peu605 0:f90a4405ef98 250
peu605 0:f90a4405ef98 251 private:
peu605 0:f90a4405ef98 252 uint8_t _Mactrl_Data;//container for the memory access control data
peu605 0:f90a4405ef98 253 uint8_t _colorspaceData;
peu605 0:f90a4405ef98 254 void colorSpace(uint8_t cspace);
peu605 0:f90a4405ef98 255 void writecommand(uint8_t c);
peu605 0:f90a4405ef98 256 void writedata(uint8_t d);
peu605 0:f90a4405ef98 257 void writedata16(uint16_t d);
peu605 0:f90a4405ef98 258 void writedata32(uint16_t d1, uint16_t d2);
peu605 0:f90a4405ef98 259 void writedata16burst(uint16_t d, int32_t len);
peu605 0:f90a4405ef98 260 void waitSpiFree();
peu605 0:f90a4405ef98 261 void chipInit();
peu605 0:f90a4405ef98 262 bool boundaryCheck(int16_t x,int16_t y);
peu605 0:f90a4405ef98 263 void homeAddress();
peu605 0:f90a4405ef98 264
peu605 0:f90a4405ef98 265 DigitalOut _cs;
peu605 0:f90a4405ef98 266 DigitalOut _dc;
peu605 0:f90a4405ef98 267 PinName _resetPinName;
peu605 0:f90a4405ef98 268
peu605 1:c271e7e2e330 269 #if defined(__F411RE__)
peu605 0:f90a4405ef98 270 SPI_TypeDef *spi_ptr;
peu605 0:f90a4405ef98 271 #if defined(__F411RE_DMA__)
peu605 0:f90a4405ef98 272 DMA_HandleTypeDef hdma;
peu605 0:f90a4405ef98 273 #endif
peu605 1:c271e7e2e330 274 #endif
peu605 1:c271e7e2e330 275
peu605 0:f90a4405ef98 276 };
peu605 0:f90a4405ef98 277 #endif