1. 程式人生 > >【原理】從零編寫ILI9341驅動全過程(基於Arduino)

【原理】從零編寫ILI9341驅動全過程(基於Arduino)

最近在淘寶入手了一塊ILI9341彩色螢幕,支援320x240解析度。之前一直很好奇這類微控制器驅動的彩色螢幕的原理,就打算自己寫一個驅動,從電流層面操控ILI9341螢幕。話不多說,我們開始吧( ̄▽ ̄)~*

1.ILI9341晶片和ILI9341驅動板

首先這裡要明確兩個概念,ILI9341晶片和ILI9341驅動板。

ILI9341晶片是ilitek釋出的液晶驅動晶片,是這個樣子的:

而淘寶上的ILI9341驅動板是把ILI9341晶片、螢幕和針腳焊接在一起的電路板,它可能是這個樣子的:

 

也可能是這個樣子的:

 

 還可能是這個樣子的:

 

沒錯,不同的廠家可以製造不同形狀,不同介面的ILI9341驅動板,但他們上面都有ILI9341晶片,所以我們可以用相同的方法操作它。

2.如何操作它呢?

這是ILI9341驅動板的背面,我在上面做了些標註,應該會方便理解些。我們只要控制這些針腳的通電與否(高電平與低電平),就能夠獲得操控這塊螢幕的“完整許可權”!那這些針腳的定義是什麼呢?我們一個一個看:

左上角有五個最重要的針腳,分別是LCD_RST、LCD_CS、LCD_RS、LCD_WR和LCD_RD:(直接用文字寫不能冒號對齊,沒辦法,開個程式碼框( ̄▽ ̄)~*)

LCD_RST : 即LCD Reset,用於在通電之後復位,初始化整個模組。

LCD_CS  : 即LCD Chip Select,用於多個晶片之間的片選操作。由於這塊驅動板只有一個可用的晶片,所以一般該針腳不通電。

LCD_RS  : 又稱D/CX訊號線,用於切換寫命令(Command)和寫資料(Data),當對顯示屏寫命令(Command)時,應該讓針腳不通電,當對顯示屏寫資料(Data)時,應該讓針腳通電。

LCD_WR  : 寫使能。當LCD_WR不通電,並且LCD_RD通電時,資料傳輸方向為寫入。

LCD_RD  : 讀使能。

左下角的針腳負責供電,不細講。

右上角的LCD_D0到LCD_D7是資料腳,通過控制它的通電與否來傳輸8個位元,也就是8個0或1。這種方式可以傳輸一個最小值0,最大值255的數字,我們用它來傳輸所有命令和資料。

右下角的SD_SS,SD_DI,SD_D0,SD_SCK適用於控制SD卡讀寫的,不屬於ILI9341的範疇,我們先不討論。

那麼,如何操作它呢?這張圖能夠很清楚的說明:(下面用拉低代表示不通電,拉高表示通電,這樣術語會更加標準)

 

  • 微控制器開機,ILI9341驅動板接收到電流,開始進入待命狀態
  • 拉低LCD_CS片選訊號,選擇對ILI9341晶片傳送命令
  • 通過拉高拉低LCD_D0到LCD_D7資料腳,來表示二進位制資料
  • 拉低拉高LCD_RS針腳來告訴機器這是一個命令,還是一個數據
  • 拉低LCD_WR,進行寫使能(可以理解為按下回車鍵,把LCD_D0到D7的資料傳送出去)

這就是傳送一個命令或者資料的方法。二進位制,十進位制和十六進位制的轉換和表達先直接略過,如果要展開,那可能可以出一本書了( • ̀ω•́ )✧,關於LCD_D0到D7腳應該傳送什麼,ILITEK在設計ILI9341時就已經規定好了,中文文件在此:

接下來,就是,愉快的,編碼時間啦( • ̀ω•́ )✧!!!

3.相容性設定

不知剛才你有沒有注意到資料腳是從LCD_D2開始的?那是因為Arduino Uno開發板的第0和1腳是USB針腳,不能被使用,只能從第2個針腳開始設計:

那我們在程式設計時要用到LCD_D0和LCD_D1時,就必須寫成8和9。另外不同機器腳位也不一樣,所以我用巨集定義來簡化程式:

#define LCD_D0   8
#define LCD_D1   9
#define LCD_D2   2
#define LCD_D3   3
#define LCD_D4   4
#define LCD_D5   5
#define LCD_D6   6
#define LCD_D7   7
#define LCD_RD   A0
#define LCD_WR   A1
#define LCD_RS   A2
#define LCD_CS   A3
#define LCD_RST  A4

這樣即解決了LCD_D0和LCD_D1腳的問題,還搞定的不同開發板的相容性問題。由於#define是在預編譯階段生成的,所以不會影響程式碼執行的速度。

4.傳送命令和資料

呼叫這塊螢幕的方法很明確,就是寫入2進位制數字。通過設計廠商提供的命令表傳送相應的2進制命令和資料,實現操控。這樣做的好處是無論你使用的是什麼機器,什麼驅動板,只要實現了LcdWriteCommand()和LcdWriteData()兩個函式,就可以實現對螢幕的完全控制。

你當然可以用最直接的辦法去控制引腳,比如digitalWrite():

void LcdWriteCommand(unsigned char d){
  //Write Command Mode On
  digitalWrite(LCD_RS,LOW);
  //Write Datas to LCD_D0 to LCD_D7
  digitalWrite(LCD_D0,d%2);
  d = d >> 1;
  digitalWrite(LCD_D1,d%2);
  d = d >> 1;
  digitalWrite(LCD_D2,d%2);
  d = d >> 1;
  digitalWrite(LCD_D3,d%2);
  d = d >> 1;
  digitalWrite(LCD_D4,d%2);
  d = d >> 1;
  digitalWrite(LCD_D5,d%2);
  d = d >> 1;
  digitalWrite(LCD_D6,d%2);
  d = d >> 1;
  digitalWrite(LCD_D7,d%2);
  d = d >> 1;
  //Enable Datas
  digitalWrite(LCD_WR,LOW);
  digitalWrite(LCD_WR,HIGH);
}

void LcdWriteData(unsigned char d){
  //Write Data Mode On
  digitalWrite(LCD_RS,HIGH);
  //Write Datas to LCD_D0 to LCD_D7
  digitalWrite(LCD_D0,d%2);
  d = d >> 1;
  digitalWrite(LCD_D1,d%2);
  d = d >> 1;
  digitalWrite(LCD_D2,d%2);
  d = d >> 1;
  digitalWrite(LCD_D3,d%2);
  d = d >> 1;
  digitalWrite(LCD_D4,d%2);
  d = d >> 1;
  digitalWrite(LCD_D5,d%2);
  d = d >> 1;
  digitalWrite(LCD_D6,d%2);
  d = d >> 1;
  digitalWrite(LCD_D7,d%2);
  d = d >> 1;
  //Enable Datas
  digitalWrite(LCD_WR,LOW);
  digitalWrite(LCD_WR,HIGH);
}

但是這樣做的話,速度嘛。。。看看這個,你就不會想用digitalWrite了:

 

 微控制器中,速度為王,我們還是直接改Register吧:

void LcdWriteCommand(unsigned char d){
  //Write Command Mode On
  fastDigitalWriteLOW(LCD_RS);
  //Write Datas to LCD_D0 to LCD_D7
  PORTD = (PORTD & B00000011) | ((d) & B11111100); 
  PORTB = (PORTB & B11111100) | ((d) & B00000011); 
  //Enable Datas
  fastDigitalWriteLOW(LCD_WR);
  fastDigitalWriteHIGH(LCD_WR);
}

void LcdWriteData(unsigned char d){
  //Write Command Mode On
  fastDigitalWriteHIGH(LCD_RS);
  //Write Datas to LCD_D0 to LCD_D7
  PORTD = (PORTD & B00000011) | ((d) & B11111100);
  PORTB = (PORTB & B11111100) | ((d) & B00000011);
  //Enable Datas
  fastDigitalWriteLOW(LCD_WR);
  fastDigitalWriteHIGH(LCD_WR);
}

這段程式碼中,我用了巨集定義來實現fastDigitalWriteHIGH()和fastDigitalWriteLOW(),這兩個定義能避免函式的棧呼叫。其實用行內函數來寫也可以實現:

inline void fastDigitalWriteHIGH(int Pin){
  *(portOutputRegister(digitalPinToPort(Pin)))|=digitalPinToBitMask(Pin);
  return;
}

但我就是喜歡巨集定義,而且巨集定義行數少些。

另外你可能會疑惑:什麼是PORTB和PORTD?

PORTB其實就是針腳8-13,PORTD其實就是針腳0-7:

 

 

 假如你有一個這樣的二進位制數:

00000100

把他轉換成十進位制:

4     

再把它賦值給PORTD

PORTD = 4;

你就會發現針腳2通電了(圖中連線到左邊第一個紅色燈泡):

 

 

 這就是PORTD的真正意義,它使用一個從0到255的數,記錄針腳0到7的通電情況。

那我們為什麼不用digitalWrite(),而是要用PORTB和PORTD呢?因為快啊( ̄▽ ̄)~*

5.Enjoy!

我們剛剛實現了LcdWriteCommand()和LcdWriteData()兩個函式,現在,我們就可以實現對螢幕的完全控制了!

首先,先執行一段初始化命令:

  //Initialize Data Pins
  pinMode(LCD_D0,OUTPUT);
  pinMode(LCD_D1,OUTPUT);
  pinMode(LCD_D2,OUTPUT);
  pinMode(LCD_D3,OUTPUT);
  pinMode(LCD_D4,OUTPUT);
  pinMode(LCD_D5,OUTPUT);
  pinMode(LCD_D6,OUTPUT);
  pinMode(LCD_D7,OUTPUT);

  //Initialize Command Pins
  pinMode(LCD_RD,OUTPUT);
  pinMode(LCD_WR,OUTPUT);
  pinMode(LCD_RS,OUTPUT);
  pinMode(LCD_CS,OUTPUT);
  pinMode(LCD_RST,OUTPUT);
  digitalWrite(LCD_CS,LOW);
  digitalWrite(LCD_RD,HIGH);
  digitalWrite(LCD_WR,LOW);

  //Reset
  digitalWrite(LCD_RST,HIGH);
  delay(5);
  digitalWrite(LCD_RST,LOW);
  delay(15);
  digitalWrite(LCD_RST,HIGH);
  delay(15);

  LcdWriteCommand(0xCB);
  LcdWriteData(0x39);
  LcdWriteData(0x2C);
  LcdWriteData(0x00);
  LcdWriteData(0x34);
  LcdWriteData(0x02);

  LcdWriteCommand(0xCF);
  LcdWriteData(0x00);
  LcdWriteData(0XC1);
  LcdWriteData(0X30);

  LcdWriteCommand(0xE8);
  LcdWriteData(0x85);
  LcdWriteData(0x00);
  LcdWriteData(0x78);

  LcdWriteCommand(0xEA);
  LcdWriteData(0x00);
  LcdWriteData(0x00);
 
  LcdWriteCommand(0xED);
  LcdWriteData(0x64);
  LcdWriteData(0x03);
  LcdWriteData(0X12);
  LcdWriteData(0X81);

  LcdWriteCommand(0xF7);
  LcdWriteData(0x20);
  
  LcdWriteCommand(0xC0);    //Power control 
  LcdWriteData(0x23);   //VRH[5:0] 
 
  LcdWriteCommand(0xC1);    //Power control 
  LcdWriteData(0x10);   //SAP[2:0];BT[3:0] 

  LcdWriteCommand(0xC5);    //VCM control 
  LcdWriteData(0x3e);   //Contrast
  LcdWriteData(0x28); 
 
  LcdWriteCommand(0xC7);    //VCM control2 
  LcdWriteData(0x86);   //--
 
  LcdWriteCommand(0x36);    // Memory Access Control 
  LcdWriteData(0x48);   

  LcdWriteCommand(0x3A);
  LcdWriteData(0x55);

  LcdWriteCommand(0xB1);
  LcdWriteData(0x00);
  LcdWriteData(0x18);
 
  LcdWriteCommand(0xB6);    // Display Function Control 
  LcdWriteData(0x08);
  LcdWriteData(0x82);
  LcdWriteData(0x27);

  LcdWriteCommand(0x11);    //Exit Sleep 
  delay(120);
        
  LcdWriteCommand(0x29);    //Display on 
  LcdWriteCommand(0x2c);

這麼多!不要怕,原樣複製過去執行就好。這段命令是按照ILITEK設計文件中的規則傳送的,用於初始化螢幕。執行完這段命令之後,我們就可以開始發自己的命令了。

我們試著來清一下屏。清屏就是指定一塊區域,然後給螢幕每一個畫素點的顏色為白色,這樣就好了。

首先定義我們要寫入的區域,這裡就是從(0,0)寫入到(239,319):

int x1 = 0;
int x2 = 239;
int y1 = 0;
int y2 = 319;

接著通知螢幕我們要寫入的區域的X座標的起始、終止位置(命令0x2a):

LcdWriteCommand(0x2a);

然後傳送X座標的起始位置(x1),和X座標的終止位置(x2)。我們的機器一次只能傳送八位數字,但八位數字最大隻能表示255,所以我們要分兩次傳送,先發送前八位,再發送後八位:

LcdWriteData(x1>>8);
LcdWriteData(x1);
LcdWriteData(x2>>8);
LcdWriteData(x2);

Y座標也是一樣,只是把通知命令改成0x2b:

LcdWriteCommand(0x2b);
LcdWriteData(y1>>8);
LcdWriteData(y1);
LcdWriteData(y2>>8);
LcdWriteData(y2);

接著,我們傳送開始寫入的命令(0x2c),告訴螢幕我要開始傳送畫素了:

LcdWriteCommand(0x2c);

最後,傳送所有畫素的顏色資料(Data)。裡面的RGB()巨集定義是我在上一篇文章實現的。另外,因為是資料,所以我們要使用LcdWriteData():

#define RGB(r,g,b) ((b&31)+((g&63)<<5)+((r&31)<<11))

for(int i=y1;i<=y2;i++){
  for(int j=x1;j<=x2;j++){
    LcdWriteData(RGB(31,63,31)>>8);
    LcdWriteData(RGB(31,63,31));
  }
}

儲存,下載。

刷屏完整程式碼:

// Breakout/Arduino UNO pin usage:
// LCD Data Bit :   7   6   5   4   3   2   1   0
// Uno dig. pin :   7   6   5   4   3   2   9   8
// Uno port/pin : PD7 PD6 PD5 PD4 PD3 PD2 PB1 PB0
// Mega dig. pin:  29  28  27  26  25  24  23  22
#define LCD_D0   8
#define LCD_D1   9
#define LCD_D2   2
#define LCD_D3   3
#define LCD_D4   4
#define LCD_D5   5
#define LCD_D6   6
#define LCD_D7   7
#define LCD_RD   A0
#define LCD_WR   A1
#define LCD_RS   A2
#define LCD_CS   A3
#define LCD_RST  A4
#define fastDigitalWriteHIGH(Pin) *(portOutputRegister(digitalPinToPort(Pin)))|=digitalPinToBitMask(Pin)  //Faster digitalWrite(Pin,HIGH);
#define fastDigitalWriteLOW(Pin) *(portOutputRegister(digitalPinToPort(Pin)))&=~digitalPinToBitMask(Pin)  //Faster digitalWrite(Pin,LOW);
#define RGB(r,g,b) ((b&31)+((g&63)<<5)+((r&31)<<11))

void LcdWriteCommand(unsigned char d){
  //Write Command Mode On
  fastDigitalWriteLOW(LCD_RS);
  //Write Datas to LCD_D0 to LCD_D7
  PORTD = (PORTD & B00000011) | ((d) & B11111100); 
  PORTB = (PORTB & B11111100) | ((d) & B00000011); 
  //Enable Datas
  fastDigitalWriteLOW(LCD_WR);
  fastDigitalWriteHIGH(LCD_WR);
}

void LcdWriteData(unsigned char d){
  //Write Command Mode On
  fastDigitalWriteHIGH(LCD_RS);
  //Write Datas to LCD_D0 to LCD_D7
  PORTD = (PORTD & B00000011) | ((d) & B11111100);
  PORTB = (PORTB & B11111100) | ((d) & B00000011);
  //Enable Datas
  fastDigitalWriteLOW(LCD_WR);
  fastDigitalWriteHIGH(LCD_WR);
}

void setup(){
  //Initialize Data Pins
  pinMode(LCD_D0,OUTPUT);
  pinMode(LCD_D1,OUTPUT);
  pinMode(LCD_D2,OUTPUT);
  pinMode(LCD_D3,OUTPUT);
  pinMode(LCD_D4,OUTPUT);
  pinMode(LCD_D5,OUTPUT);
  pinMode(LCD_D6,OUTPUT);
  pinMode(LCD_D7,OUTPUT);

  //Initialize Command Pins
  pinMode(LCD_RD,OUTPUT);
  pinMode(LCD_WR,OUTPUT);
  pinMode(LCD_RS,OUTPUT);
  pinMode(LCD_CS,OUTPUT);
  pinMode(LCD_RST,OUTPUT);
  digitalWrite(LCD_CS,LOW);
  digitalWrite(LCD_RD,HIGH);
  digitalWrite(LCD_WR,LOW);

  //Reset
  digitalWrite(LCD_RST,HIGH);
  delay(5);
  digitalWrite(LCD_RST,LOW);
  delay(15);
  digitalWrite(LCD_RST,HIGH);
  delay(15);

  LcdWriteCommand(0xCB);
  LcdWriteData(0x39);
  LcdWriteData(0x2C);
  LcdWriteData(0x00);
  LcdWriteData(0x34);
  LcdWriteData(0x02);

  LcdWriteCommand(0xCF);
  LcdWriteData(0x00);
  LcdWriteData(0XC1);
  LcdWriteData(0X30);

  LcdWriteCommand(0xE8);
  LcdWriteData(0x85);
  LcdWriteData(0x00);
  LcdWriteData(0x78);

  LcdWriteCommand(0xEA);
  LcdWriteData(0x00);
  LcdWriteData(0x00);
 
  LcdWriteCommand(0xED);
  LcdWriteData(0x64);
  LcdWriteData(0x03);
  LcdWriteData(0X12);
  LcdWriteData(0X81);

  LcdWriteCommand(0xF7);
  LcdWriteData(0x20);
  
  LcdWriteCommand(0xC0);    //Power control 
  LcdWriteData(0x23);   //VRH[5:0] 
 
  LcdWriteCommand(0xC1);    //Power control 
  LcdWriteData(0x10);   //SAP[2:0];BT[3:0] 

  LcdWriteCommand(0xC5);    //VCM control 
  LcdWriteData(0x3e);   //Contrast
  LcdWriteData(0x28); 
 
  LcdWriteCommand(0xC7);    //VCM control2 
  LcdWriteData(0x86);   //--
 
  LcdWriteCommand(0x36);    // Memory Access Control 
  LcdWriteData(0x48);   

  LcdWriteCommand(0x3A);
  LcdWriteData(0x55);

  LcdWriteCommand(0xB1);
  LcdWriteData(0x00);
  LcdWriteData(0x18);
 
  LcdWriteCommand(0xB6);    // Display Function Control 
  LcdWriteData(0x08);
  LcdWriteData(0x82);
  LcdWriteData(0x27);

  LcdWriteCommand(0x11);    //Exit Sleep 
  delay(120);
        
  LcdWriteCommand(0x29);    //Display on 
  LcdWriteCommand(0x2c);

  //Set Writing Area
  int x1 = 0;
  int x2 = 239;
  int y1 = 0;
  int y2 = 319;
  LcdWriteCommand(0x2a);
  LcdWriteData(x1>>8);
  LcdWriteData(x1);
  LcdWriteData(x2>>8);
  LcdWriteData(x2);
  LcdWriteCommand(0x2b);
  LcdWriteData(y1>>8);
  LcdWriteData(y1);
  LcdWriteData(y2>>8);
  LcdWriteData(y2);

  //Start Writing
  LcdWriteCommand(0x2c);
  for(int i=y1;i<=y2;i++){
    for(int j=x1;j<=x2;j++){
      LcdWriteData(RGB(31,63,31)>>8);
      LcdWriteData(RGB(31,63,31));
    }
  }
}

void loop(){
  
}

接下來的路線就很簡單了,把指定區域的命令(0x2a,0x2b,0x2c)分裝成LcdOpenWindow()函式,再實現LcdFill()函式,一個完整的ILI9341驅動就完成了:

#define LCD_D0   8
#define LCD_D1   9
#define LCD_D2   2
#define LCD_D3   3
#define LCD_D4   4
#define LCD_D5   5
#define LCD_D6   6
#define LCD_D7   7
#define LCD_RD   A0
#define LCD_WR   A1
#define LCD_RS   A2
#define LCD_CS   A3
#define LCD_RST  A4
#define fastDigitalWriteHIGH(Pin) *(portOutputRegister(digitalPinToPort(Pin)))|=digitalPinToBitMask(Pin)  //Faster digitalWrite(Pin,HIGH);
#define fastDigitalWriteLOW(Pin) *(portOutputRegister(digitalPinToPort(Pin)))&=~digitalPinToBitMask(Pin)  //Faster digitalWrite(Pin,LOW);
#define RGB(r,g,b) ((b&31)+((g&63)<<5)+((r&31)<<11))

void LcdWriteCommand(unsigned char d){
  //Write Command Mode On
  fastDigitalWriteLOW(LCD_RS);
  //Write Datas to LCD_D0 to LCD_D7
  PORTD = (PORTD & B00000011) | ((d) & B11111100); 
  PORTB = (PORTB & B11111100) | ((d) & B00000011); 
  //Enable Datas
  fastDigitalWriteLOW(LCD_WR);
  fastDigitalWriteHIGH(LCD_WR);
}

void LcdWriteData(unsigned char d){
  //Write Command Mode On
  fastDigitalWriteHIGH(LCD_RS);
  //Write Datas to LCD_D0 to LCD_D7
  PORTD = (PORTD & B00000011) | ((d) & B11111100);
  PORTB = (PORTB & B11111100) | ((d) & B00000011);
  //Enable Datas
  fastDigitalWriteLOW(LCD_WR);
  fastDigitalWriteHIGH(LCD_WR);
}

void LcdInit(void){
  //Initialize Data Pins
  pinMode(LCD_D0,OUTPUT);
  pinMode(LCD_D1,OUTPUT);
  pinMode(LCD_D2,OUTPUT);
  pinMode(LCD_D3,OUTPUT);
  pinMode(LCD_D4,OUTPUT);
  pinMode(LCD_D5,OUTPUT);
  pinMode(LCD_D6,OUTPUT);
  pinMode(LCD_D7,OUTPUT);

  //Initialize Command Pins
  pinMode(LCD_RD,OUTPUT);
  pinMode(LCD_WR,OUTPUT);
  pinMode(LCD_RS,OUTPUT);
  pinMode(LCD_CS,OUTPUT);
  pinMode(LCD_RST,OUTPUT);
  digitalWrite(LCD_CS,LOW);
  digitalWrite(LCD_RD,HIGH);
  digitalWrite(LCD_WR,LOW);

  //Reset
  digitalWrite(LCD_RST,HIGH);
  delay(5);
  digitalWrite(LCD_RST,LOW);
  delay(15);
  digitalWrite(LCD_RST,HIGH);
  delay(15);

  LcdWriteCommand(0xCB);
  LcdWriteData(0x39);
  LcdWriteData(0x2C);
  LcdWriteData(0x00);
  LcdWriteData(0x34);
  LcdWriteData(0x02);

  LcdWriteCommand(0xCF);
  LcdWriteData(0x00);
  LcdWriteData(0XC1);
  LcdWriteData(0X30);

  LcdWriteCommand(0xE8);
  LcdWriteData(0x85);
  LcdWriteData(0x00);
  LcdWriteData(0x78);

  LcdWriteCommand(0xEA);
  LcdWriteData(0x00);
  LcdWriteData(0x00);
 
  LcdWriteCommand(0xED);
  LcdWriteData(0x64);
  LcdWriteData(0x03);
  LcdWriteData(0X12);
  LcdWriteData(0X81);

  LcdWriteCommand(0xF7);
  LcdWriteData(0x20);
  
  LcdWriteCommand(0xC0);    //Power control 
  LcdWriteData(0x23);   //VRH[5:0] 
 
  LcdWriteCommand(0xC1);    //Power control 
  LcdWriteData(0x10);   //SAP[2:0];BT[3:0] 

  LcdWriteCommand(0xC5);    //VCM control 
  LcdWriteData(0x3e);   //Contrast
  LcdWriteData(0x28); 
 
  LcdWriteCommand(0xC7);    //VCM control2 
  LcdWriteData(0x86);   //--
 
  LcdWriteCommand(0x36);    // Memory Access Control 
  LcdWriteData(0x48);   

  LcdWriteCommand(0x3A);
  LcdWriteData(0x55);

  LcdWriteCommand(0xB1);
  LcdWriteData(0x00);
  LcdWriteData(0x18);
 
  LcdWriteCommand(0xB6);    // Display Function Control 
  LcdWriteData(0x08);
  LcdWriteData(0x82);
  LcdWriteData(0x27);

  LcdWriteCommand(0x11);    //Exit Sleep 
  delay(120);
        
  LcdWriteCommand(0x29);    //Display on 
  LcdWriteCommand(0x2c);
}

void LcdOpenWindow(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2){
  LcdWriteCommand(0x2a);
  LcdWriteData(x1>>8);
  LcdWriteData(x1);
  LcdWriteData(x2>>8);
  LcdWriteData(x2);
  LcdWriteCommand(0x2b);
  LcdWriteData(y1>>8);
  LcdWriteData(y1);
  LcdWriteData(y2>>8);
  LcdWriteData(y2);
  LcdWriteCommand(0x2c);
}

void LcdFill(int x,int y,int width,int height,unsigned int color)
{
  LcdOpenWindow(x,y,x+width-1,y+height-1);
  for(int i=y;i<y+height;i++){
    for(int j=x;j<x+width;j++){
      LcdWriteData(color>>8);
      LcdWriteData(color);
    }
  }
}

void setup(){
  LcdInit();
  LcdFill(0,0,239,319,RGB(31,63,31));
  LcdFill(10,10,100,100,RGB(31,0,0));
  LcdFill(20,20,110,110,RGB(0,63,0));
  LcdFill(30,30,120,120,RGB(0,0,31));
}

void loop(){
  
}

 

 都看到這了,還不點個贊嗎?(✪ω