Caiou
Dependents: LCD_Proj LCD_imagetest LCD_Proj_rtc
Fork of DmTftLibrary by
DmTftIli9341.cpp@14:2db7065edbec, 2015-10-12 (annotated)
- Committer:
- displaymodule
- Date:
- Mon Oct 12 08:35:35 2015 +0000
- Revision:
- 14:2db7065edbec
- Parent:
- 0:d6ff5fa503e8
- Child:
- 15:0c36e018a6cc
Important updates, compatible with the new mbed library?more platforms support. Recommended updates.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
displaymodule | 0:d6ff5fa503e8 | 1 | /********************************************************************************************** |
displaymodule | 0:d6ff5fa503e8 | 2 | Copyright (c) 2014 DisplayModule. All rights reserved. |
displaymodule | 0:d6ff5fa503e8 | 3 | |
displaymodule | 0:d6ff5fa503e8 | 4 | Redistribution and use of this source code, part of this source code or any compiled binary |
displaymodule | 0:d6ff5fa503e8 | 5 | based on this source code is permitted as long as the above copyright notice and following |
displaymodule | 0:d6ff5fa503e8 | 6 | disclaimer is retained. |
displaymodule | 0:d6ff5fa503e8 | 7 | |
displaymodule | 0:d6ff5fa503e8 | 8 | DISCLAIMER: |
displaymodule | 0:d6ff5fa503e8 | 9 | THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES |
displaymodule | 0:d6ff5fa503e8 | 10 | NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE. |
displaymodule | 0:d6ff5fa503e8 | 11 | ********************************************************************************************/ |
displaymodule | 0:d6ff5fa503e8 | 12 | |
displaymodule | 14:2db7065edbec | 13 | //Tested on NUCLEO-F401RE, LPCXpresso11U68, LPCXpresso824-MAX platform. |
displaymodule | 14:2db7065edbec | 14 | |
displaymodule | 0:d6ff5fa503e8 | 15 | #include "DmTftIli9341.h" |
displaymodule | 0:d6ff5fa503e8 | 16 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 17 | DmTftIli9341::DmTftIli9341(uint8_t cs, uint8_t dc) |
displaymodule | 14:2db7065edbec | 18 | :DmTftBase(240, 320) |
displaymodule | 0:d6ff5fa503e8 | 19 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 14:2db7065edbec | 20 | DmTftIli9341::DmTftIli9341(PinName cs, PinName dc, PinName mosi, PinName miso, PinName clk) |
displaymodule | 14:2db7065edbec | 21 | :DmTftBase(240, 320), spi(mosi, miso, clk) |
displaymodule | 0:d6ff5fa503e8 | 22 | #endif |
displaymodule | 14:2db7065edbec | 23 | { |
displaymodule | 0:d6ff5fa503e8 | 24 | _cs = cs; |
displaymodule | 0:d6ff5fa503e8 | 25 | _dc = dc; |
displaymodule | 0:d6ff5fa503e8 | 26 | } |
displaymodule | 0:d6ff5fa503e8 | 27 | |
displaymodule | 0:d6ff5fa503e8 | 28 | DmTftIli9341::~DmTftIli9341() { |
displaymodule | 0:d6ff5fa503e8 | 29 | #if defined (DM_TOOLCHAIN_MBED) |
displaymodule | 0:d6ff5fa503e8 | 30 | delete _pinCS; |
displaymodule | 0:d6ff5fa503e8 | 31 | delete _pinDC; |
displaymodule | 0:d6ff5fa503e8 | 32 | |
displaymodule | 0:d6ff5fa503e8 | 33 | _pinCS = NULL; |
displaymodule | 0:d6ff5fa503e8 | 34 | _pinDC = NULL; |
displaymodule | 0:d6ff5fa503e8 | 35 | #endif |
displaymodule | 0:d6ff5fa503e8 | 36 | } |
displaymodule | 0:d6ff5fa503e8 | 37 | |
displaymodule | 0:d6ff5fa503e8 | 38 | void DmTftIli9341::writeBus(uint8_t data) { |
displaymodule | 0:d6ff5fa503e8 | 39 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 40 | SPCR = _spiSettings; // SPI Control Register |
displaymodule | 0:d6ff5fa503e8 | 41 | SPDR = data; // SPI Data Register |
displaymodule | 0:d6ff5fa503e8 | 42 | while(!(SPSR & _BV(SPIF))); // SPI Status Register Wait for transmission to finish |
displaymodule | 0:d6ff5fa503e8 | 43 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 14:2db7065edbec | 44 | spi.write(data); |
displaymodule | 0:d6ff5fa503e8 | 45 | #endif |
displaymodule | 0:d6ff5fa503e8 | 46 | } |
displaymodule | 0:d6ff5fa503e8 | 47 | |
displaymodule | 0:d6ff5fa503e8 | 48 | void DmTftIli9341::sendCommand(uint8_t index) { |
displaymodule | 0:d6ff5fa503e8 | 49 | cbi(_pinDC, _bitmaskDC); |
displaymodule | 0:d6ff5fa503e8 | 50 | writeBus(index); |
displaymodule | 0:d6ff5fa503e8 | 51 | } |
displaymodule | 0:d6ff5fa503e8 | 52 | |
displaymodule | 0:d6ff5fa503e8 | 53 | void DmTftIli9341::send8BitData(uint8_t data) { |
displaymodule | 0:d6ff5fa503e8 | 54 | sbi(_pinDC, _bitmaskDC); |
displaymodule | 0:d6ff5fa503e8 | 55 | writeBus(data); |
displaymodule | 0:d6ff5fa503e8 | 56 | } |
displaymodule | 0:d6ff5fa503e8 | 57 | |
displaymodule | 0:d6ff5fa503e8 | 58 | void DmTftIli9341::sendData(uint16_t data) { |
displaymodule | 0:d6ff5fa503e8 | 59 | uint8_t dh = data>>8; |
displaymodule | 0:d6ff5fa503e8 | 60 | uint8_t dl = data&0xff; |
displaymodule | 0:d6ff5fa503e8 | 61 | |
displaymodule | 0:d6ff5fa503e8 | 62 | sbi(_pinDC, _bitmaskDC); |
displaymodule | 0:d6ff5fa503e8 | 63 | writeBus(dh); |
displaymodule | 0:d6ff5fa503e8 | 64 | writeBus(dl); |
displaymodule | 0:d6ff5fa503e8 | 65 | } |
displaymodule | 0:d6ff5fa503e8 | 66 | |
displaymodule | 0:d6ff5fa503e8 | 67 | void DmTftIli9341::setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { |
displaymodule | 0:d6ff5fa503e8 | 68 | sendCommand(0x2A); // Set Column |
displaymodule | 0:d6ff5fa503e8 | 69 | sendData(x0); |
displaymodule | 0:d6ff5fa503e8 | 70 | sendData(x1); |
displaymodule | 0:d6ff5fa503e8 | 71 | |
displaymodule | 0:d6ff5fa503e8 | 72 | sendCommand(0x2B); // Set Page |
displaymodule | 0:d6ff5fa503e8 | 73 | sendData(y0); |
displaymodule | 0:d6ff5fa503e8 | 74 | sendData(y1); |
displaymodule | 0:d6ff5fa503e8 | 75 | |
displaymodule | 0:d6ff5fa503e8 | 76 | sendCommand(0x2c); |
displaymodule | 0:d6ff5fa503e8 | 77 | } |
displaymodule | 0:d6ff5fa503e8 | 78 | |
displaymodule | 0:d6ff5fa503e8 | 79 | void DmTftIli9341::init(void) { |
displaymodule | 0:d6ff5fa503e8 | 80 | setTextColor(BLACK, WHITE); |
displaymodule | 0:d6ff5fa503e8 | 81 | #if defined (DM_TOOLCHAIN_ARDUINO) |
displaymodule | 0:d6ff5fa503e8 | 82 | _pinCS = portOutputRegister(digitalPinToPort(_cs)); |
displaymodule | 0:d6ff5fa503e8 | 83 | _bitmaskCS = digitalPinToBitMask(_cs); |
displaymodule | 0:d6ff5fa503e8 | 84 | _pinDC = portOutputRegister(digitalPinToPort(_dc)); |
displaymodule | 0:d6ff5fa503e8 | 85 | _bitmaskDC = digitalPinToBitMask(_dc); |
displaymodule | 0:d6ff5fa503e8 | 86 | pinMode(_cs,OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 87 | pinMode(_dc,OUTPUT); |
displaymodule | 0:d6ff5fa503e8 | 88 | |
displaymodule | 0:d6ff5fa503e8 | 89 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 90 | |
displaymodule | 0:d6ff5fa503e8 | 91 | SPI.begin(); |
displaymodule | 0:d6ff5fa503e8 | 92 | SPI.setClockDivider(SPI_CLOCK_DIV2); // 8 MHz (full! speed!) |
displaymodule | 0:d6ff5fa503e8 | 93 | SPI.setBitOrder(MSBFIRST); |
displaymodule | 0:d6ff5fa503e8 | 94 | SPI.setDataMode(SPI_MODE0); |
displaymodule | 0:d6ff5fa503e8 | 95 | _spiSettings = SPCR; |
displaymodule | 0:d6ff5fa503e8 | 96 | #elif defined (DM_TOOLCHAIN_MBED) |
displaymodule | 14:2db7065edbec | 97 | _pinCS = new DigitalOut(_cs); |
displaymodule | 14:2db7065edbec | 98 | _pinDC = new DigitalOut(_dc); |
displaymodule | 0:d6ff5fa503e8 | 99 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 14:2db7065edbec | 100 | |
displaymodule | 14:2db7065edbec | 101 | spi.format(8,0); |
displaymodule | 14:2db7065edbec | 102 | spi.frequency(8000000); // Max SPI speed for display is 10 and for 17 for LPC15xx |
displaymodule | 0:d6ff5fa503e8 | 103 | #endif |
displaymodule | 0:d6ff5fa503e8 | 104 | cbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 105 | delay(135); // This much delay needed?? |
displaymodule | 0:d6ff5fa503e8 | 106 | |
displaymodule | 0:d6ff5fa503e8 | 107 | // ILI9341 init |
displaymodule | 0:d6ff5fa503e8 | 108 | sendCommand(0x11); |
displaymodule | 0:d6ff5fa503e8 | 109 | delay(120); |
displaymodule | 0:d6ff5fa503e8 | 110 | |
displaymodule | 0:d6ff5fa503e8 | 111 | sendCommand(0xCF); |
displaymodule | 0:d6ff5fa503e8 | 112 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 113 | send8BitData(0xc3); |
displaymodule | 0:d6ff5fa503e8 | 114 | send8BitData(0X30); |
displaymodule | 0:d6ff5fa503e8 | 115 | |
displaymodule | 0:d6ff5fa503e8 | 116 | sendCommand(0xED); |
displaymodule | 0:d6ff5fa503e8 | 117 | send8BitData(0x64); |
displaymodule | 0:d6ff5fa503e8 | 118 | send8BitData(0x03); |
displaymodule | 0:d6ff5fa503e8 | 119 | send8BitData(0X12); |
displaymodule | 0:d6ff5fa503e8 | 120 | send8BitData(0X81); |
displaymodule | 0:d6ff5fa503e8 | 121 | |
displaymodule | 0:d6ff5fa503e8 | 122 | sendCommand(0xE8); |
displaymodule | 0:d6ff5fa503e8 | 123 | send8BitData(0x85); |
displaymodule | 0:d6ff5fa503e8 | 124 | send8BitData(0x10); |
displaymodule | 0:d6ff5fa503e8 | 125 | send8BitData(0x79); |
displaymodule | 0:d6ff5fa503e8 | 126 | |
displaymodule | 0:d6ff5fa503e8 | 127 | sendCommand(0xCB); |
displaymodule | 0:d6ff5fa503e8 | 128 | send8BitData(0x39); |
displaymodule | 0:d6ff5fa503e8 | 129 | send8BitData(0x2C); |
displaymodule | 0:d6ff5fa503e8 | 130 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 131 | send8BitData(0x34); |
displaymodule | 0:d6ff5fa503e8 | 132 | send8BitData(0x02); |
displaymodule | 0:d6ff5fa503e8 | 133 | |
displaymodule | 0:d6ff5fa503e8 | 134 | sendCommand(0xF7); |
displaymodule | 0:d6ff5fa503e8 | 135 | send8BitData(0x20); |
displaymodule | 0:d6ff5fa503e8 | 136 | |
displaymodule | 0:d6ff5fa503e8 | 137 | sendCommand(0xEA); |
displaymodule | 0:d6ff5fa503e8 | 138 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 139 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 140 | |
displaymodule | 0:d6ff5fa503e8 | 141 | sendCommand(0xC0); //Power control |
displaymodule | 0:d6ff5fa503e8 | 142 | send8BitData(0x22); //VRH[5:0] |
displaymodule | 0:d6ff5fa503e8 | 143 | |
displaymodule | 0:d6ff5fa503e8 | 144 | sendCommand(0xC1); //Power control |
displaymodule | 0:d6ff5fa503e8 | 145 | send8BitData(0x11); //SAP[2:0];BT[3:0] |
displaymodule | 0:d6ff5fa503e8 | 146 | |
displaymodule | 0:d6ff5fa503e8 | 147 | sendCommand(0xC5); //VCM control |
displaymodule | 0:d6ff5fa503e8 | 148 | send8BitData(0x3d); |
displaymodule | 0:d6ff5fa503e8 | 149 | send8BitData(0x20); |
displaymodule | 0:d6ff5fa503e8 | 150 | |
displaymodule | 0:d6ff5fa503e8 | 151 | sendCommand(0xC7); //VCM control2 |
displaymodule | 0:d6ff5fa503e8 | 152 | send8BitData(0xAA); //0xB0 |
displaymodule | 0:d6ff5fa503e8 | 153 | |
displaymodule | 0:d6ff5fa503e8 | 154 | sendCommand(0x36); // Memory Access Control |
displaymodule | 0:d6ff5fa503e8 | 155 | send8BitData(0x08); |
displaymodule | 0:d6ff5fa503e8 | 156 | |
displaymodule | 0:d6ff5fa503e8 | 157 | sendCommand(0x3A); |
displaymodule | 0:d6ff5fa503e8 | 158 | send8BitData(0x55); |
displaymodule | 0:d6ff5fa503e8 | 159 | |
displaymodule | 0:d6ff5fa503e8 | 160 | sendCommand(0xB1); |
displaymodule | 0:d6ff5fa503e8 | 161 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 162 | send8BitData(0x13); |
displaymodule | 0:d6ff5fa503e8 | 163 | |
displaymodule | 0:d6ff5fa503e8 | 164 | sendCommand(0xB6); // Display Function Control |
displaymodule | 0:d6ff5fa503e8 | 165 | send8BitData(0x0A); |
displaymodule | 0:d6ff5fa503e8 | 166 | send8BitData(0xA2); |
displaymodule | 0:d6ff5fa503e8 | 167 | |
displaymodule | 0:d6ff5fa503e8 | 168 | sendCommand(0xF6); |
displaymodule | 0:d6ff5fa503e8 | 169 | send8BitData(0x01); |
displaymodule | 0:d6ff5fa503e8 | 170 | send8BitData(0x30); |
displaymodule | 0:d6ff5fa503e8 | 171 | |
displaymodule | 0:d6ff5fa503e8 | 172 | sendCommand(0xF2); // 3Gamma Function Disable |
displaymodule | 0:d6ff5fa503e8 | 173 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 174 | |
displaymodule | 0:d6ff5fa503e8 | 175 | sendCommand(0x26); //Gamma curve selected |
displaymodule | 0:d6ff5fa503e8 | 176 | send8BitData(0x01); |
displaymodule | 0:d6ff5fa503e8 | 177 | |
displaymodule | 0:d6ff5fa503e8 | 178 | sendCommand(0xE0); //Set Gamma |
displaymodule | 0:d6ff5fa503e8 | 179 | send8BitData(0x0F); |
displaymodule | 0:d6ff5fa503e8 | 180 | send8BitData(0x3F); |
displaymodule | 0:d6ff5fa503e8 | 181 | send8BitData(0x2F); |
displaymodule | 0:d6ff5fa503e8 | 182 | send8BitData(0x0C); |
displaymodule | 0:d6ff5fa503e8 | 183 | send8BitData(0x10); |
displaymodule | 0:d6ff5fa503e8 | 184 | send8BitData(0x0A); |
displaymodule | 0:d6ff5fa503e8 | 185 | send8BitData(0x53); |
displaymodule | 0:d6ff5fa503e8 | 186 | send8BitData(0XD5); |
displaymodule | 0:d6ff5fa503e8 | 187 | send8BitData(0x40); |
displaymodule | 0:d6ff5fa503e8 | 188 | send8BitData(0x0A); |
displaymodule | 0:d6ff5fa503e8 | 189 | send8BitData(0x13); |
displaymodule | 0:d6ff5fa503e8 | 190 | send8BitData(0x03); |
displaymodule | 0:d6ff5fa503e8 | 191 | send8BitData(0x08); |
displaymodule | 0:d6ff5fa503e8 | 192 | send8BitData(0x03); |
displaymodule | 0:d6ff5fa503e8 | 193 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 194 | |
displaymodule | 0:d6ff5fa503e8 | 195 | sendCommand(0XE1); //Set Gamma |
displaymodule | 0:d6ff5fa503e8 | 196 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 197 | send8BitData(0x00); |
displaymodule | 0:d6ff5fa503e8 | 198 | |
displaymodule | 0:d6ff5fa503e8 | 199 | send8BitData(0x10); |
displaymodule | 0:d6ff5fa503e8 | 200 | send8BitData(0x03); |
displaymodule | 0:d6ff5fa503e8 | 201 | send8BitData(0x0F); |
displaymodule | 0:d6ff5fa503e8 | 202 | send8BitData(0x05); |
displaymodule | 0:d6ff5fa503e8 | 203 | send8BitData(0x2C); |
displaymodule | 0:d6ff5fa503e8 | 204 | send8BitData(0xA2); |
displaymodule | 0:d6ff5fa503e8 | 205 | send8BitData(0x3F); |
displaymodule | 0:d6ff5fa503e8 | 206 | send8BitData(0x05); |
displaymodule | 0:d6ff5fa503e8 | 207 | send8BitData(0x0E); |
displaymodule | 0:d6ff5fa503e8 | 208 | send8BitData(0x0C); |
displaymodule | 0:d6ff5fa503e8 | 209 | send8BitData(0x37); |
displaymodule | 0:d6ff5fa503e8 | 210 | send8BitData(0x3C); |
displaymodule | 0:d6ff5fa503e8 | 211 | send8BitData(0x0F); |
displaymodule | 0:d6ff5fa503e8 | 212 | |
displaymodule | 0:d6ff5fa503e8 | 213 | sendCommand(0x11); //Exit Sleep |
displaymodule | 0:d6ff5fa503e8 | 214 | delay(120); |
displaymodule | 0:d6ff5fa503e8 | 215 | sendCommand(0x29); //Display on |
displaymodule | 0:d6ff5fa503e8 | 216 | delay(50); |
displaymodule | 0:d6ff5fa503e8 | 217 | sbi(_pinCS, _bitmaskCS); |
displaymodule | 0:d6ff5fa503e8 | 218 | clearScreen(); |
displaymodule | 0:d6ff5fa503e8 | 219 | } |
displaymodule | 0:d6ff5fa503e8 | 220 | |
displaymodule | 0:d6ff5fa503e8 | 221 | /********************************************************************************************************* |
displaymodule | 0:d6ff5fa503e8 | 222 | END FILE |
displaymodule | 0:d6ff5fa503e8 | 223 | *********************************************************************************************************/ |
displaymodule | 0:d6ff5fa503e8 | 224 | |
displaymodule | 0:d6ff5fa503e8 | 225 |