Beta! Fully working!

Solved finally memory addressing problems.
This commit is contained in:
sumotoy 2014-05-24 01:07:33 +02:00
parent 653c157f4e
commit def2720d1b
5 changed files with 130 additions and 65 deletions

View file

@ -269,6 +269,12 @@ void TFT_ILI9163C::chipInit() {
writecommand(CMD_GAMRSEL);//Enable Gamma adj writecommand(CMD_GAMRSEL);//Enable Gamma adj
writedata(0x01); writedata(0x01);
delay(10); delay(10);
writecommand(CMD_NORML);
writecommand(0xB6);
writedata(0b11111111);//0x04
writedata(0b00000110);//0x04
writecommand(CMD_PGAMMAC);//Positive Gamma Correction Setting writecommand(CMD_PGAMMAC);//Positive Gamma Correction Setting
#if defined(__GAMMASET1) #if defined(__GAMMASET1)
writedata(0x36);//p1 writedata(0x36);//p1
@ -342,61 +348,68 @@ void TFT_ILI9163C::chipInit() {
writecommand(CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors) writecommand(CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors)
writedata(0x08);//0x0C writedata(0x08);//0x0C//0x08
writedata(0x08);//0x14 writedata(0x02);//0x14//0x08
delay(10); delay(10);
writecommand(CMD_DINVCTR);//display inversion writecommand(CMD_DINVCTR);//display inversion
writedata(0x07); writedata(0x07);
delay(10); delay(10);
writecommand(CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD writecommand(CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD
writedata(0x0A);//0x0C writedata(0x0A);//4.30 - 0x0A
writedata(0x02);//0x05 writedata(0x02);//0x05
delay(10); delay(10);
writecommand(CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL writecommand(CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL
writedata(0x02); writedata(0x02);
delay(10); delay(10);
writecommand(CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML writecommand(CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML
writedata(0x50); writedata(0x50);//0x50
writedata(0x5B); writedata(99);//0x5b
delay(10); delay(10);
writecommand(CMD_VCOMOFFS); writecommand(CMD_VCOMOFFS);
writedata(0x40); writedata(0);//0x40
delay(10); delay(10);
writecommand(CMD_CLMADRS);//Set Column Address writecommand(CMD_CLMADRS);//Set Column Address
writedata(0x00); writedata(0x00);
writedata(0X00); writedata(0X00);
writedata(0X00); writedata(0X00);
writedata(_DTA_WIDTH); writedata(_GRAMWIDTH);
writecommand(CMD_PGEADRS);//Set Page Address writecommand(CMD_PGEADRS);//Set Page Address
writedata(0x00); writedata(0x00);
writedata(0X00); writedata(0X00);
writedata(0X00); writedata(0X00);
writedata(_DTA_HEIGHT); writedata(_GRAMHEIGH);
writecommand(CMD_MADCTL);//Set Scanning Direction && Colorspace writecommand(CMD_MADCTL);//Set Scanning Direction && Colorspace
writedata(DTA_MADCTL_MX | __COLORSPC); //08 writedata(DTA_MADCTL_MX | __COLORSPC); //08
delay(10); delay(10);
writecommand(CMD_SDRVDIR);//Set Source Output Direction //writecommand(CMD_SDRVDIR);//Set Source Output Direction
writedata(0x00); //writedata(0x00);
delay(10); //delay(10);
writecommand(CMD_DISPON);//display ON writecommand(CMD_DISPON);//display ON
delay(10); delay(10);
writecommand(CMD_RAMWR);//Memory Write writecommand(CMD_RAMWR);//Memory Write
writedata16(0X0000);
writedata16(0X0000);
writedata16(0X0000);
writedata16(0X0000);
delay(10); delay(10);
fillScreen(BLACK);
setRotation(0);
} }
void TFT_ILI9163C::clearScreen(uint16_t color) { void TFT_ILI9163C::clearScreen(uint16_t color) {
homeAddress(); homeAddress();
for (int px=0;px <= (_TFTWIDTH*_TFTHEIGHT); px++){ for (int px=0;px < _GRAMSIZE; px++){
writedata16(color); writedata16(color);
} }
} }
void TFT_ILI9163C::homeAddress() { void TFT_ILI9163C::homeAddress() {
setAddrWindow(0x00,0x00,_DTA_WIDTH,_DTA_HEIGHT); setAddrWindow(0x00,0x00,_GRAMWIDTH,_GRAMHEIGH);
} }
void TFT_ILI9163C::setCursor(int16_t x, int16_t y) { void TFT_ILI9163C::setCursor(int16_t x, int16_t y) {
@ -405,27 +418,6 @@ void TFT_ILI9163C::setCursor(int16_t x, int16_t y) {
cursor_y = y; cursor_y = y;
} }
void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
writecommand(CMD_CLMADRS); // Column
writedata16(x0);
writedata16(x1);
// writedata(0x00);
// writedata(x0);
// writedata(0x00);
// writedata(x1);
writecommand(CMD_PGEADRS); // Page
writedata16(y0);
writedata16(y1);
// writedata(0x00);
// writedata(y0);
// writedata(0x00);
// writedata(y1);
writecommand(CMD_RAMWR); //Into RAM
}
void TFT_ILI9163C::pushColor(uint16_t color) { void TFT_ILI9163C::pushColor(uint16_t color) {
@ -466,8 +458,8 @@ void TFT_ILI9163C::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color
} }
void TFT_ILI9163C::fillScreen(uint16_t color) { void TFT_ILI9163C::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color); clearScreen(color);
homeAddress(); //homeAddress();
} }
// fill a rectangle // fill a rectangle
@ -492,29 +484,68 @@ uint16_t TFT_ILI9163C::Color565(uint8_t r, uint8_t g, uint8_t b) {
} }
void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
writecommand(CMD_CLMADRS); // Column
if (rotation == 0){
writedata16(x0);
writedata16(x1);
} else if (rotation == 1){
writedata16(x0+32);
writedata16(x1+32);
} else if (rotation == 2){
writedata16(x0);
writedata16(x1);
} else {
writedata16(x0);
writedata16(x1);
}
writecommand(CMD_PGEADRS); // Page
if (rotation == 0){
writedata16(y0+__OFFSET);
writedata16(y1+__OFFSET);
} else if (rotation == 1){
writedata16(y0);
writedata16(y1);
} else if (rotation == 2){
writedata16(y0);
writedata16(y1);
} else {
writedata16(y0);
writedata16(y1);
}
writecommand(CMD_RAMWR); //Into RAM
}
void TFT_ILI9163C::setRotation(uint8_t m) { void TFT_ILI9163C::setRotation(uint8_t m) {
writecommand(CMD_MADCTL); writecommand(CMD_MADCTL);
rotation = m % 4; // can't be higher than 3 rotation = m % 4; // can't be higher than 3
switch (rotation) { switch (rotation) {
case 0: case 0:
writedata(DTA_MADCTL_MX | __COLORSPC); writecommand(CMD_MADCTL);
writedata(0b00001000);
_width = _TFTWIDTH; _width = _TFTWIDTH;
_height = _TFTHEIGHT; _height = _TFTHEIGHT;//-__OFFSET;
break; break;
case 1: case 1:
writedata(DTA_MADCTL_MV | __COLORSPC); writecommand(CMD_MADCTL);
_width = _TFTHEIGHT; writedata(0b01101000);
_width = _TFTHEIGHT;//-__OFFSET;
_height = _TFTWIDTH; _height = _TFTWIDTH;
break; break;
case 2: case 2:
writedata(DTA_MADCTL_MY | __COLORSPC); writecommand(CMD_MADCTL);
writedata(0b11001000);
_width = _TFTWIDTH; _width = _TFTWIDTH;
_height = _TFTHEIGHT; _height = _TFTHEIGHT;//-__OFFSET;
break; break;
case 3: case 3:
writedata(DTA_MADCTL_MV | DTA_MADCTL_MY | DTA_MADCTL_MX | __COLORSPC); writecommand(CMD_MADCTL);
_width = _TFTHEIGHT; writedata(0b10101000);
_height = _TFTWIDTH; _width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET;
break; break;
} }
} }

View file

@ -63,6 +63,7 @@
Version: Version:
0.1a1: First release, compile correctly. Altrough not fully working! 0.1a1: First release, compile correctly. Altrough not fully working!
0.1a3: Better but still some addressing problems. 0.1a3: Better but still some addressing problems.
0.1b1: Beta! Addressing solved, now rotation works and boundaries ok.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
BugList of the current version: BugList of the current version:
@ -113,19 +114,45 @@
//ILI9163C versions------------------------ //ILI9163C versions------------------------
#if defined(__144_RED_PCB__) #if defined(__144_RED_PCB__)
#define _TFTWIDTH 128 /*
#define _TFTHEIGHT 160 This display:
http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/271422122271
This particular display has a design error! The controller has 3 pins to configure to constrain
the memory and resolution to a fixed dimension (in that case 128x128) but they leaved those pins
configured for 128x160 so there was several pixel memory addressing problems.
I solved by setup several parameters that dinamically fix the resolution as needit so below
the parameters for this diplay. If you have a strain or a correct display (can happen with chinese)
you can copy those parameters and create setup for different displays.
*/
#define _TFTWIDTH 128//the REAL W resolution of the TFT
#define _TFTHEIGHT 128//the REAL H resolution of the TFT
#define _GRAMWIDTH 128
#define _GRAMHEIGH 160
#define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH//*see note 1
#define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB
#define __GAMMASET1 //uncomment for another gamma
#define __OFFSET 32//*see note 2
#else
#define _TFTWIDTH 128//128
#define _TFTHEIGHT 160//160
#define _GRAMWIDTH 128
#define _GRAMHEIGH 160
#define _GRAMSIZE _GRAMWIDTH * _GRAMHEIGH//*see note 1
#define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB #define __COLORSPC DTA_MADCTL_BGR//DTA_MADCTL_RGB
#define __GAMMASET1 #define __GAMMASET1
#else #define __OFFSET 0
#define _TFTWIDTH 128
#define _TFTHEIGHT 160
#endif #endif
/*
Note 1: The __144_RED_PCB__ display has hardware addressing of 128 x 160
but the tft resolution it's 128 x 128 so the dram should be set correctly
Note 2: This is the offset between image in RAM and TFT. In that case 160 - 128 = 32;
*/
//----------------------------------------- //-----------------------------------------
#define BLACK 0x0000
#define WHITE 0xFFFF
#define _DTA_WIDTH _TFTWIDTH//0x7F//=128 hex
#define _DTA_HEIGHT _TFTHEIGHT//0x7F//=128 hex
//ILI9163C registers----------------------- //ILI9163C registers-----------------------
#define CMD_NOP 0x00//Non operation #define CMD_NOP 0x00//Non operation
#define CMD_SWRESET 0x01//Soft Reset #define CMD_SWRESET 0x01//Soft Reset
@ -144,7 +171,7 @@
#define CMD_NORML 0x13//Normal Display ON #define CMD_NORML 0x13//Normal Display ON
#define CMD_DINVOF 0x20//Display Inversion OFF #define CMD_DINVOF 0x20//Display Inversion OFF
#define CMD_DINVON 0x21//Display Inversion ON #define CMD_DINVON 0x21//Display Inversion ON
#define CMD_GAMMASET 0x26//Gamma Set #define CMD_GAMMASET 0x26//Gamma Set (0x01[1],0x02[2],0x04[3],0x08[4])
#define CMD_DISPOFF 0x28//Display OFF #define CMD_DISPOFF 0x28//Display OFF
#define CMD_DISPON 0x29//Display ON #define CMD_DISPON 0x29//Display ON
#define CMD_IDLEON 0x39//Idle Mode ON #define CMD_IDLEON 0x39//Idle Mode ON
@ -194,7 +221,7 @@
#define CMD_GAMRSEL 0xF2//GAM_R_SEL #define CMD_GAMRSEL 0xF2//GAM_R_SEL
#define DTA_MADCTL_MX 0x00//0x40 #define DTA_MADCTL_MX 0x40//0x40 or 00
#define DTA_MADCTL_MY 0x80 #define DTA_MADCTL_MY 0x80
#define DTA_MADCTL_MV 0x20 #define DTA_MADCTL_MV 0x20
#define DTA_MADCTL_ML 0x10 #define DTA_MADCTL_ML 0x10
@ -224,6 +251,7 @@ class TFT_ILI9163C : public Adafruit_GFX {
invertDisplay(boolean i); invertDisplay(boolean i);
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b); uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
void setBitrate(uint32_t n); void setBitrate(uint32_t n);
//void test(uint8_t rot);
private: private:

View file

@ -47,10 +47,13 @@ TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST);
void setup() { void setup() {
tft.begin(); tft.begin();
#if defined(__MK20DX128__) || defined(__MK20DX256__)
tft.setBitrate(24000000);
#endif
} }
void loop(){ void loop(){
tft.fillScreen(); tft.fillScreen(WHITE);
r[0]=r[0]+1; r[0]=r[0]+1;
r[1]=r[1]+1; r[1]=r[1]+1;
if (r[0] == 36) r[0] = 0; if (r[0] == 36) r[0] = 0;
@ -74,12 +77,12 @@ void loop(){
p2y[i] = ((tft.height())/2)+ay*500/az; p2y[i] = ((tft.height())/2)+ay*500/az;
} }
for (int i=0;i<3;i++) { for (int i=0;i<3;i++) {
tft.drawLine(p2x[i],p2y[i],p2x[i+1],p2y[i+1],RED); tft.drawLine(p2x[i],p2y[i],p2x[i+1],p2y[i+1],BLACK);
tft.drawLine(p2x[i+4],p2y[i+4],p2x[i+5],p2y[i+5],RED); tft.drawLine(p2x[i+4],p2y[i+4],p2x[i+5],p2y[i+5],BLACK);
tft.drawLine(p2x[i],p2y[i],p2x[i+4],p2y[i+4],RED); tft.drawLine(p2x[i],p2y[i],p2x[i+4],p2y[i+4],BLACK);
} }
tft.drawLine(p2x[3],p2y[3],p2x[0],p2y[0],RED); tft.drawLine(p2x[3],p2y[3],p2x[0],p2y[0],BLACK);
tft.drawLine(p2x[7],p2y[7],p2x[4],p2y[4],RED); tft.drawLine(p2x[7],p2y[7],p2x[4],p2y[4],BLACK);
tft.drawLine(p2x[3],p2y[3],p2x[7],p2y[7],RED); tft.drawLine(p2x[3],p2y[3],p2x[7],p2y[7],BLACK);
delay(50); delay(15);
} }

View file

@ -27,9 +27,10 @@ TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
//while (!Serial); //while (!Serial);
tft.setBitrate(24000000);
tft.begin(); tft.begin();
//tft.setBitrate(6000000); #if defined(__MK20DX128__) || defined(__MK20DX256__)
tft.setBitrate(24000000);
#endif
Serial.println(F("Benchmark Time (microseconds)")); Serial.println(F("Benchmark Time (microseconds)"));
Serial.print(F("Screen fill ")); Serial.print(F("Screen fill "));

View file

@ -5,5 +5,7 @@ setBrightness KEYWORD2
writeData KEYWORD2 writeData KEYWORD2
setBitrate KEYWORD2 setBitrate KEYWORD2
Color565 KEYWORD2 Color565 KEYWORD2
setBitrate KEYWORD2
clearScreen KEYWORD2