最近在淘寶入手了一塊ILI9341彩色屏幕,支持320x240分辨率。以前一直很好奇這類單片機驅動的彩色屏幕的原理,就打算本身寫一個驅動,從電流層面操控ILI9341屏幕。話很少說,咱們開始吧( ̄▽ ̄)~*git
首先這裏要明確兩個概念,ILI9341芯片和ILI9341驅動板。編程
ILI9341芯片是ilitek發佈的液晶驅動芯片,是這個樣子的:函數
而淘寶上的ILI9341驅動板是把ILI9341芯片、屏幕和針腳焊接在一塊兒的電路板,它多是這個樣子的:oop
也多是這個樣子的:ui
還多是這個樣子的:編碼
沒錯,不一樣的廠家能夠製造不一樣形狀,不一樣接口的ILI9341驅動板,但他們上面都有ILI9341芯片,因此咱們能夠用相同的方法操做它。spa
這是ILI9341驅動板的背面,我在上面作了些標註,應該會方便理解些。咱們只要控制這些針腳的通電與否(高電平與低電平),就可以得到操控這塊屏幕的「完整權限」!那這些針腳的定義是什麼呢?咱們一個一個看:設計
左上角有五個最重要的針腳,分別是LCD_RST、LCD_CS、LCD_RS、LCD_WR和LCD_RD:(直接用文字寫不能冒號對齊,沒辦法,開個代碼框( ̄▽ ̄)~*)3d
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 : 讀使能。
左下角的針腳負責供電,不細講。code
右上角的LCD_D0到LCD_D7是數據腳,經過控制它的通電與否來傳輸8個比特,也就是8個0或1。這種方式能夠傳輸一個最小值0,最大值255的數字,咱們用它來傳輸全部命令和數據。
右下角的SD_SS,SD_DI,SD_D0,SD_SCK適用於控制SD卡讀寫的,不屬於ILI9341的範疇,咱們先不討論。
那麼,如何操做它呢?這張圖可以很清楚的說明:(下面用拉低表明示不通電,拉高表示通電,這樣術語會更加標準)
這就是發送一個命令或者數據的方法。二進制,十進制和十六進制的轉換和表達先直接略過,若是要展開,那可能能夠出一本書了( • ̀ω•́ )✧,關於LCD_D0到D7腳應該發送什麼,ILITEK在設計ILI9341時就已經規定好了,中文文檔在此:
接下來,就是,愉快的,編碼時間啦( • ̀ω•́ )✧!!!
不知剛纔你有沒有注意到數據腳是從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是在預編譯階段生成的,因此不會影響代碼運行的速度。
調用這塊屏幕的方法很明確,就是寫入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呢?由於快啊( ̄▽ ̄)~*
咱們剛剛實現了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(){ }
都看到這了,還不點個贊嗎?(✪ω✪)