#define WriteSPI2(data_out) (SPI2BUF=(data_out))
#define TxBufFullSPI2() (!SPI2STATbits.SPITBE) //!SPI2STATbits.SPIRBF
GFX_COLOR _color;
volatile uint8_t LCDid;
#define ILI9340_CASET 0x2A
#define ILI9340_PASET 0x2B
#define ILI9340_RAMWR 0x2C
#define ILI9340_RAMRD 0x2E
#define ILI9340_MADCTL_MY 0x80
#define ILI9340_MADCTL_MX 0x40
#define ILI9340_MADCTL_MV 0x20
#define ILI9340_MADCTL_ML 0x10
#define ILI9340_MADCTL_RGB 0x00
#define ILI9340_MADCTL_BGR 0x08
#define ILI9340_MADCTL_MH 0x04
#define ILI9340_MADCTL 0x36
inline void LCD_RS_SetLow(void) {
switch(LCDid){
case 1:LCD1_RS_SetLow();break;
case 2:LCD2_RS_SetLow();break;
case 3:LCD3_RS_SetLow();break;
default: break;
}
}
inline void LCD_CS_SetLow(void) {// configure SPI for 16-bit mode
switch(LCDid){
case 1:LCD1_CS_SetLow();break;
case 2:LCD2_CS_SetLow();break;
case 3:LCD3_CS_SetLow();break;
default: break;
}
}
inline void LCD_RST_SetLow(void) {
switch(LCDid){
case 1:LCD1_RST_SetLow();break;
case 2:LCD2_RST_SetLow();break;
case 3:LCD3_RST_SetLow();break;
default: break;
}
}
////////////////////////////////////////////////////////////////////////////////
inline void LCD_RS_SetHigh(void) {
switch(LCDid){
case 1:LCD1_RS_SetHigh();break;
case 2:LCD2_RS_SetHigh();break;
case 3:LCD3_RS_SetHigh();break;
default: break;
}
}
inline void LCD_CS_SetHigh(void) {
switch(LCDid){
case 1:LCD1_CS_SetHigh();break;
case 2:LCD2_CS_SetHigh();break;
case 3:LCD3_CS_SetHigh();break;
default: break;
}
}
inline void LCD_RST_SetHigh(void) {
switch(LCDid){
case 1:LCD1_RST_SetHigh();break;
case 2:LCD2_RST_SetHigh();break;
case 3:LCD3_RST_SetHigh();break;
default: break;
}
}
////////////////////////////////////////////////////////////////////////////////
inline void Mode16(void) { // configure SPI for 16-bit mode
SPI2CONbits.MODE16 = 1;
}
inline void Mode8(void) { // configure SPI for 8-bit mode
SPI2CONbits.MODE16 = 0;
}
void tft_spiwrite(unsigned char c) { // Transfer to SPI
WriteSPI2(c);
while (SPI2STATbits.SPIBUSY); // wait for it to end of transaction
}
void tft_spiwrite8(unsigned char c) { // Transfer one byte c to SPI
Mode8(); // switch to 8-bit mode
while (TxBufFullSPI2());
WriteSPI2(c);
while (SPI2STATbits.SPIBUSY); // wait for it to end of transaction
Mode16(); // switch back to 16-bit mode
}
void tft_spiwrite16(unsigned short c) { // Transfer two bytes "c" to SPI
while (TxBufFullSPI2());
WriteSPI2(c);
while (SPI2STATbits.SPIBUSY); // wait for it to end of transaction
}
void tft_writecommand(unsigned char c) {
LCD_RS_SetLow();
LCD_CS_SetLow();
tft_spiwrite8(c);
LCD_CS_SetHigh();
}
void tft_writecommand16(unsigned short c) {
LCD_RS_SetLow();
LCD_CS_SetLow();
tft_spiwrite16(c);
LCD_CS_SetHigh();
}
void tft_writedata(unsigned char c) {
LCD_RS_SetHigh();
LCD_CS_SetLow();
tft_spiwrite8(c);
LCD_CS_SetHigh();
}
void tft_writedata16(unsigned short c) {
LCD_RS_SetHigh();
LCD_CS_SetLow();
tft_spiwrite16(c);
LCD_CS_SetHigh();
}
void tft_setAddrWindow(unsigned short x0, unsigned short y0, unsigned short x1, unsigned short y1) {
tft_writecommand(ILI9340_CASET); // Column addr set
tft_writedata16(x0);
tft_writedata16(x1);
tft_writecommand(ILI9340_PASET); // Row addr set
tft_writedata16(y0);
tft_writedata16(y1);
tft_writecommand(ILI9340_RAMWR); // write to RAM
}
void tft_pushColor(unsigned short color) {
LCD_RS_SetHigh();
LCD_CS_SetLow();
tft_spiwrite16(color);
LCD_CS_SetHigh();
}
void tft_begin(void) {
LCD_RST_SetLow();
LCD_RS_SetLow();
LCD_CS_SetHigh();
LCD_RST_SetHigh();
DelayMs(1);
LCD_RST_SetLow();
DelayMs(10);
LCD_RST_SetHigh();
DelayMs(120);
tft_writecommand(0x0011); //Exit Sleep
DelayMs(120);
tft_writecommand(0x00cf);
tft_writedata(0x0000); //
tft_writedata(0x00aa); //
tft_writedata(0x00b0); //
tft_writecommand(0x00EF);
tft_writedata(0x0003); //
tft_writedata(0x0080); //
tft_writedata(0x0002); //
tft_writecommand(0x00ed);
tft_writedata(0x0067); //
tft_writedata(0x0003); //
tft_writedata(0x0012); //
tft_writedata(0x0081); //
tft_writecommand(0x00e8);
tft_writedata(0x0083); //
tft_writedata(0x0011); //
tft_writedata(0x0078); //
tft_writecommand(0x00CB);
tft_writedata(0x0039); //AP[2:0]
tft_writedata(0x002c); //
tft_writedata(0x0000); //
tft_writedata(0x0034); //
tft_writedata(0x0002); //
tft_writecommand(0x00ea);
tft_writedata(0x0000); //
tft_writedata(0x0000);
tft_writecommand(0x00f7);
tft_writedata(0x0020); //
tft_writecommand(0x00CA);
tft_writedata(0x0083);
tft_writedata(0x0002);
tft_writedata(0x0000);
tft_writecommand(0x00ea);
tft_writedata(0x0000); //
tft_writedata(0x0000); //
tft_writecommand(0x00C0); //Set GVDD
tft_writedata(0x0018); //VRH[5:0] 21
tft_writecommand(0x00C1); //Set power step-up
tft_writedata(0x0011); //SAP[2:0];BT[3:0] 04
tft_writecommand(0x00C5); //set vcom
tft_writedata(0x0024); //VCM control 0x34 36
tft_writedata(0x003c); // 0x40 40
tft_writecommand(0x0036);
tft_writedata(0b01001000); // Memory Access Control //0x0048
tft_writecommand(0x00B1); // Frame Rate Control
tft_writedata(0x0000); //
tft_writedata(0x001b);
tft_writecommand(0x00B6);
tft_writedata(0x000A); //Display Function Control 0x000a
tft_writedata(0x0082);
tft_writecommand(0x00C7); //Set VCOMH/VCOML
tft_writedata(0x00b8); //VCOM Control , VMF[6:0] 0x00BC b8
tft_writecommand(0x0026); //Gamma set
tft_writedata(0x0001);
tft_writecommand(0x00F2);
tft_writedata(0x0000); //3Gamma Function Disable
tft_writecommand(0x00E0);
tft_writedata(0x001F);
tft_writedata(0x001B);
tft_writedata(0x0019); //18
tft_writedata(0x000C); //0b
tft_writedata(0x000D); //0f
tft_writedata(0x0007); //09
tft_writedata(0x0044); //46
tft_writedata(0x00A9); //B5
tft_writedata(0x002F); //37
tft_writedata(0x0008); //0A
tft_writedata(0x000D); //0C
tft_writedata(0x0013); //07
tft_writedata(0x0010); //07
tft_writedata(0x000B); //05
tft_writedata(0x0000);
tft_writecommand(0x00E1);
tft_writedata(0x0000);
tft_writedata(0x0024);
tft_writedata(0x0026); //27
tft_writedata(0x0003); //04
tft_writedata(0x0012); //10
tft_writedata(0x0008); //06
tft_writedata(0x003C); //38
tft_writedata(0x0056); //74
tft_writedata(0x0050); //48
tft_writedata(0x0007); //05
tft_writedata(0x0012); //13
tft_writedata(0x000C); //38
tft_writedata(0x002F); //38
tft_writedata(0x0034); //3A
tft_writedata(0x000F); //1F
tft_writecommand(0x003A);
tft_writedata(0x0055);
DelayMs(120);
tft_writecommand(0x002A);
tft_writedata(0x0000);
tft_writedata(0x0000);
tft_writedata(0x0000);
tft_writedata(0x00EF);
tft_writecommand(0x002B);
tft_writedata(0x0000);
tft_writedata(0x0000);
tft_writedata(0x0001);
tft_writedata(0x003F);
tft_writecommand(0x0029); //display on
tft_writecommand(0x002C); //GRAM start writing
Mode16();
}
void tft_setRotation(unsigned char m) {
unsigned char rotation;
tft_writecommand(ILI9340_MADCTL);
rotation = m % 4;