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.
ILI9341.cpp
00001 /* NeatGUI Library 00002 * Copyright (c) 2013 Neil Thiessen 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "ILI9341.h" 00018 00019 ILI9341::ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc) : Display(240, 320), m_SPI(mosi, miso, sclk), m_CS(cs), m_DC(dc) 00020 { 00021 //Set the control pins to a known state 00022 m_CS = 1; 00023 m_DC = 1; 00024 00025 //Set the SPI format to 8-bit 00026 m_SPI.format(8, 3); 00027 00028 //Set the SPI frequency to 10MHz 00029 m_SPI.frequency(10000000); 00030 } 00031 00032 bool ILI9341::open() 00033 { 00034 writeCommand(0x01); 00035 wait_ms(5); 00036 00037 writeCommand(0xCF); 00038 writeData8(0x00); 00039 writeData8(0x8B); 00040 writeData8(0X30); 00041 00042 writeCommand(0xED); 00043 writeData8(0x67); 00044 writeData8(0x03); 00045 writeData8(0X12); 00046 writeData8(0X81); 00047 00048 writeCommand(0xE8); 00049 writeData8(0x85); 00050 writeData8(0x10); 00051 writeData8(0x7A); 00052 00053 writeCommand(0xCB); 00054 writeData8(0x39); 00055 writeData8(0x2C); 00056 writeData8(0x00); 00057 writeData8(0x34); 00058 writeData8(0x02); 00059 00060 writeCommand(0xF7); 00061 writeData8(0x20); 00062 00063 writeCommand(0xEA); 00064 writeData8(0x00); 00065 writeData8(0x00); 00066 00067 writeCommand(0xC0); /* Power control */ 00068 writeData8(0x1B); /* VRH[5:0] */ 00069 00070 writeCommand(0xC1); /* Power control */ 00071 writeData8(0x10); /* SAP[2:0];BT[3:0] */ 00072 00073 writeCommand(0xC5); /* VCM control */ 00074 writeData8(0x3F); 00075 writeData8(0x3C); 00076 00077 writeCommand(0xC7); /* VCM control2 */ 00078 writeData8(0XB7); 00079 00080 writeCommand(0x36); /* Memory Access Control */ 00081 writeData8(0x08); 00082 00083 writeCommand(0x3A); 00084 writeData8(0x55); 00085 00086 writeCommand(0xB1); 00087 writeData8(0x00); 00088 writeData8(0x1B); 00089 00090 writeCommand(0xB6); /* Display Function Control */ 00091 writeData8(0x0A); 00092 writeData8(0xA2); 00093 00094 00095 writeCommand(0xF2); /* 3Gamma Function Disable */ 00096 writeData8(0x00); 00097 00098 writeCommand(0x26); /* Gamma curve selected */ 00099 writeData8(0x01); 00100 00101 writeCommand(0xE0); /* Set Gamma */ 00102 writeData8(0x0F); 00103 writeData8(0x2A); 00104 writeData8(0x28); 00105 writeData8(0x08); 00106 writeData8(0x0E); 00107 writeData8(0x08); 00108 writeData8(0x54); 00109 writeData8(0XA9); 00110 writeData8(0x43); 00111 writeData8(0x0A); 00112 writeData8(0x0F); 00113 writeData8(0x00); 00114 writeData8(0x00); 00115 writeData8(0x00); 00116 writeData8(0x00); 00117 00118 writeCommand(0XE1); /* Set Gamma */ 00119 writeData8(0x00); 00120 writeData8(0x15); 00121 writeData8(0x17); 00122 writeData8(0x07); 00123 writeData8(0x11); 00124 writeData8(0x06); 00125 writeData8(0x2B); 00126 writeData8(0x56); 00127 writeData8(0x3C); 00128 writeData8(0x05); 00129 writeData8(0x10); 00130 writeData8(0x0F); 00131 writeData8(0x3F); 00132 writeData8(0x3F); 00133 writeData8(0x0F); 00134 00135 writeCommand(0x11); /* Exit Sleep */ 00136 wait_ms(120); 00137 writeCommand(0x29); /* Display on */ 00138 00139 //Probe for the ILI9341 using a Zero Length Transfer 00140 /*if (!m_I2C.write(m_Addr, NULL, 0)) { 00141 //Init sequence for 128x64 OLED module 00142 writeCommand(CMD_DISPLAYOFF); 00143 writeCommand(CMD_SETDISPLAYCLOCKDIV); 00144 writeCommand(0x80); 00145 writeCommand(CMD_SETMULTIPLEX); 00146 writeCommand(0x3F); 00147 writeCommand(CMD_SETDISPLAYOFFSET); 00148 writeCommand(0x0); 00149 writeCommand(CMD_SETSTARTLINE | 0x0); 00150 writeCommand(CMD_CHARGEPUMP); 00151 writeCommand(CMD_CHARGEPUMPON); 00152 writeCommand(CMD_MEMORYMODE); 00153 writeCommand(0x00); 00154 writeCommand(CMD_SEGREMAP | 0x1); 00155 writeCommand(CMD_COMSCANDEC); 00156 writeCommand(CMD_SETCOMPINS); 00157 writeCommand(0x12); 00158 writeCommand(CMD_SETCONTRAST); 00159 writeCommand(0xCF); 00160 writeCommand(CMD_SETPRECHARGE); 00161 writeCommand(0xF1); 00162 writeCommand(CMD_SETVCOMDETECT); 00163 writeCommand(0x40); 00164 writeCommand(CMD_DISPLAYALLON_RESUME); 00165 writeCommand(CMD_NORMALDISPLAY); 00166 00167 //Return success 00168 return true; 00169 } else { 00170 //Return failure 00171 return false; 00172 }*/ 00173 00174 return true; 00175 } 00176 00177 void ILI9341::flush() 00178 { 00179 //Select low col 0, hi col 0, line 0 00180 //writeCommand(CMD_SETLOWCOLUMN | 0x0); 00181 //writeCommand(CMD_SETHIGHCOLUMN | 0x0); 00182 //writeCommand(CMD_SETSTARTLINE | 0x0); 00183 00184 //Make sure the first byte in the buffer is the control byte 00185 //m_Buffer[0] = CONTROL_DATA; 00186 00187 //Write the buffer 00188 //m_I2C.write(m_Addr, m_Buffer, 1025); 00189 } 00190 00191 Display::State ILI9341::state() 00192 { 00193 //Return the base class's state 00194 return Display::state(); 00195 } 00196 00197 void ILI9341::state(State s) 00198 { 00199 //Check what the requested state is 00200 if (s == Display::DISPLAY_ON) { 00201 //Turn the display on 00202 //writeCommand(CMD_DISPLAYON); 00203 } else if (s == Display::DISPLAY_OFF) { 00204 //Turn the display off 00205 //writeCommand(CMD_DISPLAYOFF); 00206 } 00207 00208 //Update the base class 00209 Display::state(s); 00210 } 00211 00212 void ILI9341::drawPixel(int x, int y, unsigned int c) 00213 { 00214 //Range check the pixel 00215 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) 00216 return; 00217 00218 //TODO: Clean up this code!!! 00219 00220 //Set the column 00221 writeCommand(0x2A); 00222 writeData16(x); 00223 writeData16(x); 00224 00225 //Set the page 00226 writeCommand(0x2B); 00227 writeData16(y); 00228 writeData16(y); 00229 00230 //Don't know what this does... 00231 writeCommand(0x2C); 00232 00233 //Woohoo, send the pixel!!! 00234 writeData16(c); 00235 00236 //TODO: Clean up this code!!! 00237 } 00238 00239 void ILI9341::writeCommand(char command) 00240 { 00241 //Pull DC low for Command 00242 m_DC = 0; 00243 00244 //Pull CS low to select the TFT 00245 m_CS = 0; 00246 00247 //Write the command byte 00248 m_SPI.write(command); 00249 00250 //Pull CS high to end the transfer 00251 m_CS = 1; 00252 } 00253 00254 void ILI9341::writeData8(char data) 00255 { 00256 //Pull DC high for Data 00257 m_DC = 1; 00258 00259 //Pull CS low to select the TFT 00260 m_CS = 0; 00261 00262 //Write the data byte 00263 m_SPI.write(data); 00264 00265 //Pull CS high to end the transfer 00266 m_CS = 1; 00267 } 00268 00269 void ILI9341::writeData16(unsigned short data) 00270 { 00271 //Pull DC high for Data 00272 m_DC = 1; 00273 00274 //Pull CS low to select the TFT 00275 m_CS = 0; 00276 00277 //Write the 2 data bytes 00278 m_SPI.write((char)(data >> 8)); 00279 m_SPI.write((char)data); 00280 00281 //Pull CS high to end the transfer 00282 m_CS = 1; 00283 }
Generated on Tue Jul 12 2022 21:38:03 by
1.7.2