Components and supplies
ILI9341 2.8 TFT LCD SD
Jumper wires (generic)
Arduino Mega 2560
Project description
Code
The Program
arduino
EAsy code with basic commands
1//Screeen 2#include <ILI9341_kbv.h> 3ILI9341_kbv tft; 4#define vali0 0 5 6 7 8 9 10//COLORS 11#define BLACK 0x0000 12#define BLUE 0x001F 13#define RED 0xF800 14#define GREEN 0x07E0 15#define CYAN 0x07FF 16#define MAGENTA 0xF81F 17#define YELLOW 0xFFE0 18#define WHITE 0xFFFF 19 20void setup() 21 { 22 23 tft.begin(); 24 tft.setRotation(1);// Landscape 25 tft.fillScreen(TFT_PINK);// set color iitial screen 26 27 } 28 29 30 31 32 33 34void loop(){ 35 36 //Some text to see on the screen 37 tft.setCursor(0, 0); 38 tft.fillScreen(TFT_BLUE); 39 tft.setTextColor(YELLOW); 40 tft.setTextSize(2); 41 tft.print("YELLOW"); 42 tft.setTextSize(4); 43 tft.setTextColor(RED); 44 tft.setTextSize(3); 45 tft.print("RED"); 46 tft.setCursor(50, 50); 47 tft.setTextColor(BLACK); 48 tft.setTextSize(8); 49 tft.print("TEST"); 50 delay(3000); 51 52 53 54 55} 56
The Program
arduino
EAsy code with basic commands
1//Screeen 2#include <ILI9341_kbv.h> 3ILI9341_kbv tft; 4#define vali0 0 5 6 7 8 9 10//COLORS 11#define BLACK 0x0000 12#define BLUE 0x001F 13#define RED 0xF800 14#define GREEN 0x07E0 15#define CYAN 0x07FF 16#define MAGENTA 0xF81F 17#define YELLOW 0xFFE0 18#define WHITE 0xFFFF 19 20void setup() 21 { 22 23 tft.begin(); 24 tft.setRotation(1);// Landscape 25 tft.fillScreen(TFT_PINK);// set color iitial screen 26 27 } 28 29 30 31 32 33 34void loop(){ 35 36 //Some text to see on the screen 37 tft.setCursor(0, 0); 38 tft.fillScreen(TFT_BLUE); 39 tft.setTextColor(YELLOW); 40 tft.setTextSize(2); 41 tft.print("YELLOW"); 42 tft.setTextSize(4); 43 tft.setTextColor(RED); 44 tft.setTextSize(3); 45 tft.print("RED"); 46 tft.setCursor(50, 50); 47 tft.setTextColor(BLACK); 48 tft.setTextSize(8); 49 tft.print("TEST"); 50 delay(3000); 51 52 53 54 55} 56
The Modify libray
arduino
REMEMBER: Allways make a copy of the original before installing this one
1 2// EDITED TO WORK ON THE ARDUINO MEGA 3 4 5#if defined(__AVR_ATmega328P__) && defined(ILI9488_KBV_H_) 6//#define USE_SERIAL_COMPLEX //optimised C code for Uno, Xmega, ... 7#endif 8#if defined(__STM32F1__) 9#define DMA__STM32F1__ //special feature of MAPLE CORE 10#endif 11//#define MY_BLUEPILL 12// MAPLE core has SPI.write(block, n) 13// ST core has only got SPI.transfer(block, n) 14// SAMD core might have SPI.transfer(block, n) 15// SAM core has got SPI.transfer(block, n) 16// so it is probably safest to have a moderate stack buffer and use transfer 17 18//ST core is ok for ILI9341 but not for ST7735X 19 20#define CD_COMMAND PIN_LOW(CD_PORT, CD_PIN) 21#define CD_DATA PIN_HIGH(CD_PORT, CD_PIN) 22#define CD_OUTPUT PIN_OUTPUT(CD_PORT, CD_PIN) 23#define CS_ACTIVE PIN_LOW(CS_PORT, CS_PIN); 24#define CS_IDLE PIN_HIGH(CS_PORT, CS_PIN); 25#define CS_OUTPUT PIN_OUTPUT(CS_PORT, CS_PIN) 26#define RESET_ACTIVE PIN_LOW(RESET_PORT, RESET_PIN) 27#define RESET_IDLE PIN_HIGH(RESET_PORT, RESET_PIN) 28#define RESET_OUTPUT PIN_OUTPUT(RESET_PORT, RESET_PIN) 29#define SD_ACTIVE PIN_LOW(SD_PORT, SD_PIN) 30#define SD_IDLE PIN_HIGH(SD_PORT, SD_PIN) 31#define SD_OUTPUT PIN_OUTPUT(SD_PORT, SD_PIN) 32 // bit-bang macros for SDIO 33#define SCK_LO PIN_LOW(SPI_PORT, SCK_PIN) 34#define SCK_HI PIN_HIGH(SPI_PORT, SCK_PIN) 35#define SCK_OUT PIN_OUTPUT(SPI_PORT, SCK_PIN) 36#define MOSI_LO PIN_LOW(SPI_PORT, MOSI_PIN) 37#define MOSI_HI PIN_HIGH(SPI_PORT, MOSI_PIN) 38#define MOSI_OUT PIN_OUTPUT(SPI_PORT, MOSI_PIN) 39#define MOSI_IN PIN_INPUT(SPI_PORT, MOSI_PIN) 40#define LED_LO PIN_LOW(LED_PORT, LED_PIN) 41#define LED_HI PIN_HIGH(LED_PORT, LED_PIN) 42#define LED_OUT PIN_OUTPUT(LED_PORT, LED_PIN) 43 44#if defined(USE_SERIAL_COMPLEX) 45#include "serial_complex.h" 46#elif defined(__AVR_ATxmega32A4U__) || defined(__AVR_ATxmega128A4U__) 47#include "serial_xmega.h" 48#define xchg8(x) xchg8_1(x) 49#define WriteCmd(x) { CD_COMMAND; xchg8_1(x); CD_DATA; } 50#define wait_ms(ms) delay(ms) 51#define write16(x) { write16_N(x, 1); } 52#define write24(x) { write24_N(x, 1); } 53#define WriteData(x) { write16(x); } 54 55#else 56 57#include <SPI.h> //include before write16() macro 58#warning Using Arduino SPI methods 59 60#if defined(NINEBITS) 61#define xchg8(x) readbits(8) 62#define WriteCmd(x) { SDIO_OUTMODE(); MOSI_LO; SCK_HI; SCK_LO; write_8(x); } 63#define WriteDat8(x) { MOSI_HI; SCK_HI; SCK_LO; write_8(x); } 64#define INIT() { CS_IDLE; RESET_IDLE; SETDDR; } 65#define SDIO_INMODE() MOSI_IN;SCK_OUT //no braces 66#define SDIO_OUTMODE() {MOSI_OUT;SCK_OUT;} 67#else 68#define xchg8(x) xchg8_1(x) 69#define WriteCmd(x) { CD_COMMAND; xchg8_1(x); CD_DATA; } 70#define INIT() { CS_IDLE; RESET_IDLE; SETDDR; SPI.begin(); SPI.beginTransaction(settings); } 71#define SDIO_INMODE() SPI.endTransaction(); MOSI_IN;SCK_OUT //no braces 72#define SDIO_OUTMODE() {MOSI_OUT;SCK_OUT;SPI.beginTransaction(settings);} 73#endif 74 75#define wait_ms(ms) delay(ms) 76#define write16(x) { write16_N(x, 1); } 77#define write24(x) { write24_N(x, 1); } 78#define WriteData(x) { write16(x); } 79 80static uint8_t spibuf[16]; 81 82#if 0 83#else 84#define CD_PIN 48 //DC 85#define CS_PIN 53 86#define RESET_PIN 49 87#define SD_PIN 47 //sd-cs 88#define MOSI_PIN 51 89#define SCK_PIN 52 90#endif 91 92#define SETDDR { CS_OUTPUT; CD_OUTPUT; RESET_OUTPUT; PIN_HIGH(SD_PORT, SD_PIN); PIN_OUTPUT(SD_PORT, SD_PIN); } 93 94#define PIN_LOW(p, b) digitalWrite(b, LOW) 95#define PIN_HIGH(p, b) digitalWrite(b, HIGH) 96#define PIN_OUTPUT(p, b) pinMode(b, OUTPUT) 97#define PIN_INPUT(p, b) pinMode(b, INPUT_PULLUP) 98#define PIN_READ(p, b) digitalRead(b) 99 100#define FLUSH_IDLE { CS_IDLE; } 101 102static SPISettings settings(8000000, MSBFIRST, SPI_MODE0); 103 104static inline void write_8(uint8_t val) 105{ 106 for (uint8_t i = 0; i < 8; i++) { //send command 107 if (val & 0x80) MOSI_HI; 108 else MOSI_LO; 109 SCK_HI; 110 SCK_LO; 111 val <<= 1; 112 } 113} 114 115static inline uint8_t xchg8_1(uint8_t x) 116{ 117#if defined(DMA__STM32F1__) 118 uint8_t ret; 119 SPI.dmaTransfer(&x, &ret, 1); 120 return ret; 121#else 122 return SPI.transfer(x); 123#endif 124} 125 126static uint32_t readbits(uint8_t bits) 127{ 128 uint32_t ret = 0; 129 while (bits--) { 130 ret <<= 1; 131 if (PIN_READ(SPI_PORT, MOSI_PIN)) 132 ret++; 133 SCK_HI; 134 SCK_LO; 135 } 136 return ret; 137} 138 139static inline void write16_N(uint16_t color, int16_t n) 140{ 141#if defined(NINEBITS) 142 uint8_t hi = color >> 8, lo = color; 143 while (n-- > 0) { 144 WriteDat8(hi); 145 WriteDat8(lo); 146 } 147#elif defined(ESP8266) || defined(ESP32) 148 uint8_t hilo[2]; 149 hilo[0] = color >> 8; 150 hilo[1] = color; 151 SPI.writePattern(hilo, 2, (uint32_t)n); 152#elif defined(DMA__STM32F1__) 153 SPI.setDataSize (SPI_CR1_DFF); // Set SPI 16bit mode 154 SPI.dmaSend(&color, n, 0); 155 SPI.setDataSize (0); 156#elif defined(__STM32F1__) 157 uint8_t buf[64]; 158 int cnt = (n > 32) ? 32 : n; 159 uint8_t *p = buf; 160 while (cnt--) { *p++ = color >> 8; *p++ = color; } 161 while (n > 0) { 162 cnt = (n > 32) ? 32 : n; 163 SPI.write(buf, cnt << 1); 164 n -= cnt; 165 } 166#elif 1 167 uint8_t buf[64]; 168 while (n > 0) { 169 uint8_t *p = buf; 170 int cnt = (n > 32) ? 32 : n; 171 while (cnt--) { *p++ = color >> 8; *p++ = color; } 172 cnt = (n > 32) ? 32 : n; 173 SPI.transfer(buf, cnt << 1); 174 n -= cnt; 175 } 176#else 177 uint8_t hi = color >> 8, lo = color; 178 while (n-- > 0) { 179 SPI.transfer(hi); 180 SPI.transfer(lo); 181 } 182#endif 183} 184 185static inline void write24_N(uint16_t color, int16_t n) 186{ 187#if defined(NINEBITS) 188 uint8_t r = color >> 8, g = (color >> 3), b = color << 3; 189 while (n-- > 0) { 190 WriteDat8(r); 191 WriteDat8(g); 192 WriteDat8(b); 193 } 194#elif defined(ESP8266) || defined(ESP32) 195 uint8_t rgb[3]; 196 rgb[0] = color >> 8; 197 rgb[1] = color >> 3; 198 rgb[2] = color << 3; 199 SPI.writePattern(rgb, 3, (uint32_t)n); 200#else 201 uint8_t r = color >> 8, g = (color >> 3), b = color << 3; 202 while (n-- > 0) { 203 SPI.transfer(r); 204 SPI.transfer(g); 205 SPI.transfer(b); 206 } 207#endif 208} 209 210static inline void write8_block(uint8_t * block, int16_t n, uint8_t bigend = 0) 211{ 212#if defined(NINEBITS) 213 while (n-- > 0) WriteDat8(*block++); 214#elif defined(ESP8266) || defined(ESP32) 215 SPI.writeBytes(block, (uint32_t)n); 216#elif defined(DMA__STM32F1__) 217 SPI.dmaSend(block, n, 1); 218#elif defined(__STM32F1__) 219 SPI.write(block, (uint32_t)n); 220#elif defined(__SAMD21G18A__) 221 Sercom *s = SERCOM1; 222 if (bigend) { 223 n >>= 1; 224 while (n--) { 225 uint8_t l = *block++, h = *block++; 226 while (s->SPI.INTFLAG.bit.DRE == 0) ; 227 s->SPI.DATA.bit.DATA = h; 228 while (s->SPI.INTFLAG.bit.DRE == 0) ; 229 s->SPI.DATA.bit.DATA = l; 230 } 231 } 232 else { 233 while (n--) { 234 while (s->SPI.INTFLAG.bit.DRE == 0) ; 235 s->SPI.DATA.bit.DATA = *block++; 236 } 237 } 238 while (s->SPI.INTFLAG.bit.TXC == 0) ; 239 s->SPI.DATA.bit.DATA; 240 s->SPI.DATA.bit.DATA; 241#else 242 SPI.transfer(block, n); 243#endif 244} 245 246#if defined(__SAMD21G18A__) 247static inline void spi_pattern(uint8_t * txbuf, uint16_t * rxbuf, int16_t n, int16_t rpt = 0) 248{ 249 Sercom *s = SERCOM1; 250 uint8_t cmd, reply; 251 do { 252 for (int i = n; i--; ) { 253 cmd = (txbuf == NULL) ? 0 : *txbuf++; 254 while (s->SPI.INTFLAG.bit.DRE == 0) ; 255 s->SPI.DATA.bit.DATA = cmd; 256 if (s->SPI.INTFLAG.bit.RXC) { 257 reply = s->SPI.DATA.bit.DATA; 258 if (rxbuf) *rxbuf++ = reply; 259 } 260 } 261 } while (--rpt > 0); 262 while (s->SPI.INTFLAG.bit.TXC == 0) ; 263 reply = s->SPI.DATA.bit.DATA; 264 if (rxbuf) *rxbuf++ = reply; 265} 266#endif 267 268#endif 269
Downloadable files
connections
connections
Connections
Connections
Connections
Connections
Connections
Connections
Connections
Connections
connections
connections
Comments
Only logged in users can leave comments
roger_marin
0 Followers
•0 Projects
0
0