/*=====================================================
File Name : <dd647_TFT.ino>
Input imgDATA : <dd647_480x320.txt>
Board : <Arduino UNO>
Display : <3.2inch 480*320 TFT LCD for Mega2560>
Creation Date : <2020_6_30>
A list of works cited is below.
http://www.lcdwiki.com/zh/3.2inch_Arduino_Display-Mega2560
Datasheet : ILI9481BDS_V0.5.pdf
=====================================================*/
#define CMASK B00000011
#define CMASK2 B00100000
#define CMASK3 B00011100//WRX,DCX,CSX,
#define BMASK B00111111
#define BMASK1 B00000111
#define BMASK2 B00111000
#define DMASK B11111100
#define writeCmd8(x){ DCX_COMMAND; write8(x); DCX_DATA; }
#define write16(d) { uint8_t h = (d)>>8, l = d; write8(h); write8(l); }
#define write8(d) { PORTD = (PORTD & ~DMASK) | ((d) & DMASK); PORTC = (PORTC & ~CMASK) | ((d) & CMASK); WRX_STROBE; }
#define writeColor(d){\
PORTD = (PORTD & ~DMASK) | ((d) & DMASK);\
PORTB = (PORTB & ~BMASK) | ((d>>9) & BMASK2) | ((d>>8) & BMASK1);\
PORTC = (PORTC & CMASK3) | (d & CMASK) | ((d>>10) & CMASK2); WRX_STROBE;\
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREEN 0x07E0
#define WRX_ACTIVE digitalWrite(A2,LOW);//write control pin
#define WRX_IDLE digitalWrite(A2,HIGH);
#define DCX_COMMAND digitalWrite(A3,LOW);//data/command pin
#define DCX_DATA digitalWrite(A3,HIGH);
#define CSX_ACTIVE digitalWrite(A4,LOW);//chip selection pin
#define CSX_IDLE digitalWrite(A4,HIGH);
#define WRX_STROBE { digitalWrite(A2, LOW);digitalWrite(A2, HIGH);}
static const unsigned char lcd_font[] PROGMEM =
{
0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x1C, 0x57, 0x7D, 0x57, 0x1C,
0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
0x00, 0x18, 0x3C, 0x18, 0x00,
0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
0x00, 0x18, 0x24, 0x18, 0x00,
0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
0x30, 0x48, 0x3A, 0x06, 0x0E,
0x26, 0x29, 0x79, 0x29, 0x26,
0x40, 0x7F, 0x05, 0x05, 0x07,
0x40, 0x7F, 0x05, 0x25, 0x3F,
0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
0x7F, 0x3E, 0x1C, 0x1C, 0x08,
0x08, 0x1C, 0x1C, 0x3E, 0x7F,
0x14, 0x22, 0x7F, 0x22, 0x14,
0x5F, 0x5F, 0x00, 0x5F, 0x5F,
0x06, 0x09, 0x7F, 0x01, 0x7F,
0x00, 0x66, 0x89, 0x95, 0x6A,
0x60, 0x60, 0x60, 0x60, 0x60,
0x94, 0xA2, 0xFF, 0xA2, 0x94,
0x08, 0x04, 0x7E, 0x04, 0x08,
0x10, 0x20, 0x7E, 0x20, 0x10,
0x08, 0x08, 0x2A, 0x1C, 0x08,
0x08, 0x1C, 0x2A, 0x08, 0x08,
0x1E, 0x10, 0x10, 0x10, 0x10,
0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
0x30, 0x38, 0x3E, 0x38, 0x30,
0x06, 0x0E, 0x3E, 0x0E, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14,
0x24, 0x2A, 0x7F, 0x2A, 0x12,
0x23, 0x13, 0x08, 0x64, 0x62,
0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x08, 0x07, 0x03, 0x00,//'
0x00, 0x1C, 0x22, 0x41, 0x00,//<
0x00, 0x41, 0x22, 0x1C, 0x00,//>
0x2A, 0x1C, 0x7F, 0x1C, 0x2A,//*
0x08, 0x08, 0x3E, 0x08, 0x08,//+
0x00, 0x80, 0x70, 0x30, 0x00,//,
0x08, 0x08, 0x08, 0x08, 0x08,//-
0x00, 0x00, 0x60, 0x60, 0x00,//.
0x20, 0x10, 0x08, 0x04, 0x02,///
0x3E, 0x51, 0x49, 0x45, 0x3E,//0
0x00, 0x42, 0x7F, 0x40, 0x00,//1
0x72, 0x49, 0x49, 0x49, 0x46,//2
0x21, 0x41, 0x49, 0x4D, 0x33,//3
0x18, 0x14, 0x12, 0x7F, 0x10,
0x27, 0x45, 0x45, 0x45, 0x39,
0x3C, 0x4A, 0x49, 0x49, 0x31,
0x41, 0x21, 0x11, 0x09, 0x07,
0x36, 0x49, 0x49, 0x49, 0x36,
0x46, 0x49, 0x49, 0x29, 0x1E,
0x00, 0x00, 0x14, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41,
0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x41, 0x22, 0x14, 0x08,
0x02, 0x01, 0x59, 0x09, 0x06,
0x3E, 0x41, 0x5D, 0x59, 0x4E,
0x7C, 0x12, 0x11, 0x12, 0x7C,
0x7F, 0x49, 0x49, 0x49, 0x36,
0x3E, 0x41, 0x41, 0x41, 0x22,
0x7F, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x49, 0x49, 0x49, 0x41,
0x7F, 0x09, 0x09, 0x09, 0x01,
0x3E, 0x41, 0x41, 0x51, 0x73,
0x7F, 0x08, 0x08, 0x08, 0x7F,
0x00, 0x41, 0x7F, 0x41, 0x00,
0x20, 0x40, 0x41, 0x3F, 0x01,
0x7F, 0x08, 0x14, 0x22, 0x41,
0x7F, 0x40, 0x40, 0x40, 0x40,
0x7F, 0x02, 0x1C, 0x02, 0x7F,
0x7F, 0x04, 0x08, 0x10, 0x7F,
0x3E, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x09, 0x09, 0x09, 0x06,
0x3E, 0x41, 0x51, 0x21, 0x5E,
0x7F, 0x09, 0x19, 0x29, 0x46,
0x26, 0x49, 0x49, 0x49, 0x32,
0x03, 0x01, 0x7F, 0x01, 0x03,
0x3F, 0x40, 0x40, 0x40, 0x3F,
0x1F, 0x20, 0x40, 0x20, 0x1F,
0x3F, 0x40, 0x38, 0x40, 0x3F,
0x63, 0x14, 0x08, 0x14, 0x63,
0x03, 0x04, 0x78, 0x04, 0x03,
0x61, 0x59, 0x49, 0x4D, 0x43,
0x00, 0x7F, 0x41, 0x41, 0x41,
0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x41, 0x41, 0x41, 0x7F,
0x04, 0x02, 0x01, 0x02, 0x04,
0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x03, 0x07, 0x08, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40,
0x7F, 0x28, 0x44, 0x44, 0x38,
0x38, 0x44, 0x44, 0x44, 0x28,
0x38, 0x44, 0x44, 0x28, 0x7F,
0x38, 0x54, 0x54, 0x54, 0x18,
0x00, 0x08, 0x7E, 0x09, 0x02,
0x18, 0xA4, 0xA4, 0x9C, 0x78,
0x7F, 0x08, 0x04, 0x04, 0x78,
0x00, 0x44, 0x7D, 0x40, 0x00,
0x20, 0x40, 0x40, 0x3D, 0x00,
0x7F, 0x10, 0x28, 0x44, 0x00,
0x00, 0x41, 0x7F, 0x40, 0x00,
0x7C, 0x04, 0x78, 0x04, 0x78,
0x7C, 0x08, 0x04, 0x04, 0x78,
0x38, 0x44, 0x44, 0x44, 0x38,
0xFC, 0x18, 0x24, 0x24, 0x18,
0x18, 0x24, 0x24, 0x18, 0xFC,
0x7C, 0x08, 0x04, 0x04, 0x08,
0x48, 0x54, 0x54, 0x54, 0x24,
0x04, 0x04, 0x3F, 0x44, 0x24,
0x3C, 0x40, 0x40, 0x20, 0x7C,
0x1C, 0x20, 0x40, 0x20, 0x1C,
0x3C, 0x40, 0x30, 0x40, 0x3C,
0x44, 0x28, 0x10, 0x28, 0x44,
0x4C, 0x90, 0x90, 0x90, 0x7C,
0x44, 0x64, 0x54, 0x4C, 0x44,
0x00, 0x08, 0x36, 0x41, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02,
0x3C, 0x26, 0x23, 0x26, 0x3C,
0x1E, 0xA1, 0xA1, 0x61, 0x12,
0x3A, 0x40, 0x40, 0x20, 0x7A,
0x38, 0x54, 0x54, 0x55, 0x59,
0x21, 0x55, 0x55, 0x79, 0x41,
0x21, 0x54, 0x54, 0x78, 0x41,
0x21, 0x55, 0x54, 0x78, 0x40,
0x20, 0x54, 0x55, 0x79, 0x40,
0x0C, 0x1E, 0x52, 0x72, 0x12,
0x39, 0x55, 0x55, 0x55, 0x59,
0x39, 0x54, 0x54, 0x54, 0x59,
0x39, 0x55, 0x54, 0x54, 0x58,
0x00, 0x00, 0x45, 0x7C, 0x41,
0x00, 0x02, 0x45, 0x7D, 0x42,
0x00, 0x01, 0x45, 0x7C, 0x40,
0xF0, 0x29, 0x24, 0x29, 0xF0,
0xF0, 0x28, 0x25, 0x28, 0xF0,
0x7C, 0x54, 0x55, 0x45, 0x00,
0x20, 0x54, 0x54, 0x7C, 0x54,
0x7C, 0x0A, 0x09, 0x7F, 0x49,
0x32, 0x49, 0x49, 0x49, 0x32,
0x32, 0x48, 0x48, 0x48, 0x32,
0x32, 0x4A, 0x48, 0x48, 0x30,
0x3A, 0x41, 0x41, 0x21, 0x7A,
0x3A, 0x42, 0x40, 0x20, 0x78,
0x00, 0x9D, 0xA0, 0xA0, 0x7D,
0x39, 0x44, 0x44, 0x44, 0x39,
0x3D, 0x40, 0x40, 0x40, 0x3D,
0x3C, 0x24, 0xFF, 0x24, 0x24,
0x48, 0x7E, 0x49, 0x43, 0x66,
0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
0xFF, 0x09, 0x29, 0xF6, 0x20,
0xC0, 0x88, 0x7E, 0x09, 0x03,
0x20, 0x54, 0x54, 0x79, 0x41,
0x00, 0x00, 0x44, 0x7D, 0x41,
0x30, 0x48, 0x48, 0x4A, 0x32,
0x38, 0x40, 0x40, 0x22, 0x7A,
0x00, 0x7A, 0x0A, 0x0A, 0x72,
0x7D, 0x0D, 0x19, 0x31, 0x7D,
0x26, 0x29, 0x29, 0x2F, 0x28,
0x26, 0x29, 0x29, 0x29, 0x26,
0x30, 0x48, 0x4D, 0x40, 0x20,
0x38, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x38,
0x2F, 0x10, 0xC8, 0xAC, 0xBA,
0x2F, 0x10, 0x28, 0x34, 0xFA,
0x00, 0x00, 0x7B, 0x00, 0x00,
0x08, 0x14, 0x2A, 0x14, 0x22,
0x22, 0x14, 0x2A, 0x14, 0x08,
0xAA, 0x00, 0x55, 0x00, 0xAA,
0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x00, 0x00, 0x00, 0xFF, 0x00,
0x10, 0x10, 0x10, 0xFF, 0x00,
0x14, 0x14, 0x14, 0xFF, 0x00,
0x10, 0x10, 0xFF, 0x00, 0xFF,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x14, 0x14, 0x14, 0xFC, 0x00,
0x14, 0x14, 0xF7, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x14, 0x14, 0xF4, 0x04, 0xFC,
0x14, 0x14, 0x17, 0x10, 0x1F,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0x1F, 0x00,
0x10, 0x10, 0x10, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x1F, 0x10,
0x10, 0x10, 0x10, 0x1F, 0x10,
0x10, 0x10, 0x10, 0xF0, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0xFF, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x14,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x1F, 0x10, 0x17,
0x00, 0x00, 0xFC, 0x04, 0xF4,
0x14, 0x14, 0x17, 0x10, 0x17,
0x14, 0x14, 0xF4, 0x04, 0xF4,
0x00, 0x00, 0xFF, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0xF7, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x17, 0x14,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0xF4, 0x14,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x00, 0x00, 0x1F, 0x10, 0x1F,
0x00, 0x00, 0x00, 0x1F, 0x14,
0x00, 0x00, 0x00, 0xFC, 0x14,
0x00, 0x00, 0xF0, 0x10, 0xF0,
0x10, 0x10, 0xFF, 0x10, 0xFF,
0x14, 0x14, 0x14, 0xFF, 0x14,
0x10, 0x10, 0x10, 0x1F, 0x00,
0x00, 0x00, 0x00, 0xF0, 0x10,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0xFF,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x38, 0x44, 0x44, 0x38, 0x44,
0x7C, 0x2A, 0x2A, 0x3E, 0x14,
0x7E, 0x02, 0x02, 0x06, 0x06,
0x02, 0x7E, 0x02, 0x7E, 0x02,
0x63, 0x55, 0x49, 0x41, 0x63,
0x38, 0x44, 0x44, 0x3C, 0x04,
0x40, 0x7E, 0x20, 0x1E, 0x20,
0x06, 0x02, 0x7E, 0x02, 0x02,
0x99, 0xA5, 0xE7, 0xA5, 0x99,
0x1C, 0x2A, 0x49, 0x2A, 0x1C,
0x4C, 0x72, 0x01, 0x72, 0x4C,
0x30, 0x4A, 0x4D, 0x4D, 0x30,
0x30, 0x48, 0x78, 0x48, 0x30,
0xBC, 0x62, 0x5A, 0x46, 0x3D,
0x3E, 0x49, 0x49, 0x49, 0x00,
0x7E, 0x01, 0x01, 0x01, 0x7E,
0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
0x44, 0x44, 0x5F, 0x44, 0x44,
0x40, 0x51, 0x4A, 0x44, 0x40,
0x40, 0x44, 0x4A, 0x51, 0x40,
0x00, 0x00, 0xFF, 0x01, 0x03,
0xE0, 0x80, 0xFF, 0x00, 0x00,
0x08, 0x08, 0x6B, 0x6B, 0x08,
0x36, 0x12, 0x36, 0x24, 0x36,
0x06, 0x0F, 0x09, 0x0F, 0x06,
0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x00, 0x10, 0x10, 0x00,
0x30, 0x40, 0xFF, 0x01, 0x01,
0x00, 0x1F, 0x01, 0x01, 0x1E,
0x00, 0x19, 0x1D, 0x17, 0x12,
0x00, 0x3C, 0x3C, 0x3C, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00,
};
void start() {
delay(20);
int arr[8][15]={
{0xD0, 0x07, 0x42, 0x18, 0xff},//Power Setting
{0xD1, 0x00, 0x07, 0x10, 0xff},//VCOM Control
{0xD2, 0x01, 0x02, 0xff},//Power Setting for Normal Mode
{0xC0, 0x10, 0x3B, 0x00, 0x02, 0x11, 0xff},//Panel Driving Setting
{0xC5, 0x03,},//Frame rate and Inversion Control
{0xC8, 0x00, 0x32, 0x36, 0x45, 0x06, 0x16, 0x37, 0x75, 0x77, 0x54, 0x0C, 0x00, 0xff},//Gamma Setting
{0x36, 0x0A, 0xff},//set_address_mode-scanning direction of frame memory.
{0x3A, 0x55, 0xff},//set_pixel_format
};
CSX_ACTIVE;
writeCmd8(0x11);//Exit_sleep_mode
CSX_IDLE;
delay(20);
CSX_ACTIVE;
for(int i = 0; i<8; i++){
int j= 0;
writeCmd8(arr[i][0]);
while (arr[i][j] != 255) {
j++;
write8(arr[i][j]);
}
}
CSX_IDLE;
delay(120);
CSX_ACTIVE;
writeCmd8(0x29);//set_display_on
CSX_IDLE;
}
void reset(void) {
CSX_IDLE;
WRX_IDLE;
CSX_ACTIVE;
DCX_COMMAND;
write8(0x01);//rest command 0x01
CSX_IDLE;
}
void rect(int x1, int x2, int y1, int y2, uint16_t color){
CSX_ACTIVE;
writeCmd8(0x2A);//Set_column_address
write16(x1);
write16(x2);
writeCmd8(0x2B);//Set_page_address
write16(y1);
write16(y2);
writeCmd8(0x2C);//Write_memory_start
for (int16_t j = y1; j <= y2; j++){
for (int16_t i = x1; i <= x2; i++){
writeColor(color);
}
}
CSX_IDLE;
}
void my_string(byte *fontNo, int16_t x, int16_t y, int text_color, int back_color, int size){
int color;
while(*fontNo !=0){
unsigned char ch;
for (int i=0; i<6; i++){
if(i == 5){
ch = 0;
}else{
ch = pgm_read_byte(lcd_font+((*fontNo)*5)+i);
}
unsigned char dot;
for (int j = 0; j<8; j++){
dot = ch & (1 << j);
if(dot == 0){
color = back_color;
}else{
color = text_color;
}
rect((x+size*i), (x+size*i)+size-1, y+size*j, (y+size*j)+size-1 ,color);
}
}
fontNo++;
x = x +size*6;
}
}
void img(){ //Use the Serial monitor to enter image data
CSX_ACTIVE;
writeCmd8(0x33);//Set_scroll_area
write16(0);
write16(480);
write16(0);
CSX_IDLE;
int r=0,c=0,x=0,y=0,count1=0;
uint16_t C;
while(y < 480){
while(Serial.available() != 0){
r = Serial.read();
if(r>96 & r<103){
c = r-87;
}else if(r>47 & r<58){
c = r-48;
}else if(r == 13 | r == 10){
break;
}
switch(count1){
case 0:
C = c << 4;
count1++;
break;
case 1:
C += c;
count1++;
break;
case 2:
C += c << 12;
count1++;
break;
case 3:
C += c << 8;
rect(x, x, y, y, C);
if(x < 319){
x++;
}else{
x=0;
y++;
}
count1 = 0;
C = 0;
break;
}
}
}
}
void scroll(int area1, int area2, int s1, int s2){
CSX_ACTIVE;
writeCmd8(0x33);//Set_scroll_area
write16(area1);
write16(area2-area1);
write16(480-area2);
CSX_IDLE;
CSX_ACTIVE;
for(int i=s1; i<s2; i++){
writeCmd8(0x37);//Set_scroll_start
write16(i);
delay(10);
}
CSX_IDLE;
}
void my_math(){
int y;
for (int x=0; x < 480; x++) {
y=80*sin((double)x/25.3)+180;
rect(y, y, x, x, GREEN);
}
scroll(320,479,139,479);
}
void invert_mode(){
CSX_ACTIVE;
writeCmd8(0x21);//Enter_invert_mode
delay(2000);
writeCmd8(0x20);//Exit_invert_mode
CSX_IDLE;
}
void Set_address_mode(){
byte b[]={0x0A,0x08,0x28,0x2A,0x09,0x0A};
for(int i =0; i<6; i++){
CSX_ACTIVE;
writeCmd8(0x36);//Set_address_mode
write8(b[i]);
CSX_IDLE;
my_string("dd647",20,130,RED,BLACK,3);
delay(2000);
}
}
void setup() {
Serial.begin(115200);
CSX_IDLE//HIGH
DCX_DATA//HIGH
WRX_IDLE//HIGHTFT
DDRD |= DMASK;//UNO(0,1,2,3,4,5,6,7)-RX,DX,TFT(DB2-DB7)
DDRB |= BMASK;//UNO(8,9,10,11,12,13)-TFT(DB8.DB9,DB10,DB12,DB13,DB14)
DDRC |= B00111111;//UNO(A0,A1,A2,A3,A4,A5)-TFT(DB0,DB1,WRX,DCX,CSX,DB15)
reset();
start();
rect(0,319,0,160,GREEN);
rect(0,319,160,320,RED);
rect(0,319,320,479,BLUE);
delay(1000);
rect(0,319,0,479,BLACK);
my_math();
Set_address_mode();
rect(0,319,0,479,BLACK);
img();
invert_mode();
delay(3000);
}
void loop() {
scroll(0,480,0,479);
}