Go Down

Topic: 2.8" tft touch shield help needed (Read 1 time) previous topic - next topic

camolas

Hi,

I bouget this 2.8" tft touch shield http://www.ebay.es/itm/2-8-TFT-LCD-for-Arduino-Atmega168-328-/160519609750?pt=LH_DefaultDomain_0&hash=item255fb6dd96#ht_1857wt_647 i think is a good starting shield for a beginer like me cames with sample code and real compatible pins just plug in and play kinde.
What i need help is on writing orientation this the sample code writes in vertical
and i wont "horisontal" write.

sample code writes                            the way that i wont to write
  _______________                    _______________________
|                        |                      |                                         |                                            |                          |                                        |
|  ARDUINO.CC     |                    | ARDUINO.CC    CAMOLAS    |
|                        |                      |                                          |
|    CAMOLAS        |                    |                       2012           |
|                        |                      |                                          |
|        2012          |                     |                                          |
|_______________|                   |_______________________|




camolas

#1
Mar 12, 2012, 07:40 pm Last Edit: Mar 12, 2012, 07:44 pm by camolas Reason: 1
1sr part:

Code: [Select]
#include "Font.h"
#include "digitalWriteFast.h"
#define DB10  2
#define DB11  3
#define DB12  4
#define DB13  5
#define DB14  6
#define DB15  7
#define DB16  8
#define DB17  9
#define CS 11
#define RS 13
#define WR 12
#define RD A0 //use as analog pin in digital mode. allows easy upgrade to mega. as pin14 is different
#define RESET 10
#define lcd_Data  

#define red 0xf800
#define green 0x07e0
#define blue 0x001f
#define black 0x0000
#define white 0xffff
#define gray 0x8c51
#define yellow 0xFFE0
#define cyan 0x07FF
#define purple 0xF81F

const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A1;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A3;                  // z-axis (only on 3-axis models)

//int xpin = 0;     // variable to read the value from the analog pin 0
//int ypin = 0;     // variable to read the value from the analog pin 0
//int zpin = 0;     // variable to read the value from the analog pin 0
char value2[4] ;
unsigned int colorc[]={red,green,blue,black,white,gray,yellow,cyan,purple};

void setup()
{
 char c;
 pinMode(0,OUTPUT);
 pinMode(1,OUTPUT);
  pinMode(DB10,OUTPUT);
 pinMode(DB11,OUTPUT);
 pinMode(DB12,OUTPUT);
 pinMode(DB13,OUTPUT);
 pinMode(DB14,OUTPUT);
 pinMode(DB15,OUTPUT);
 pinMode(DB16,OUTPUT);
 pinMode(DB17,OUTPUT);
 pinMode(RESET,OUTPUT);
 pinMode(CS,OUTPUT);
 pinMode(WR,OUTPUT);
 pinMode(RS,OUTPUT);
 pinMode(RD,OUTPUT);  
 lcd_Init();

 Serial.begin(1152000);
 pinMode(groundpin, OUTPUT);
 pinMode(powerpin, OUTPUT);
 digitalWrite(groundpin, LOW);
 digitalWrite(powerpin, HIGH);
 
 lcd_Clear(yellow);
 delay(1);
 lcd_Clear(green);
 delay(1);
 for(byte y=0; y<100;y++)
 lcd_Circle(120, 120, y,purple);
 
 lcd_Line(0,0, 190,310,white);
 lcd_Line(190,310, 0,0,white);
 
  lcd_Str(0,150,"          Arduino TFT (profounddevices)          ",white,blue);
  lcd_Str(0,0,"      Atmega328  Example                ",yellow,black);
delay(1000000);// pause her efor reset
}
void charcodetable()
{
 char c;
lcd_Str(0,0," 0123456789ABCDEF",white,red);    
 c=0x30;
 for(int y=0;y<10;y++)
   lcd_Char(0,y+1,c++,white,red);    
 c=0x41;
 for(int y=10;y<16;y++)
   lcd_Char(0,y+1,c++,white,red);    
// disp_ANKFont();
 for(int y=0;y<204;y+=12)
   for(int x=0;x<102;x++)
     lcd_Pixel(x,y,black);
 for(int x=5;x<102;x+=6)
   for(int y=0;y<204;y++)
     lcd_Pixel(x,y,black);
}
 void loop()
{
  itoa (xpin, value2, 10);

  lcd_Str(10,180,'Text Value :'+value2,white,blue);
}
 
void SET_LOW(int pin)
{
 digitalWriteFast2(pin,LOW);
}

void SET_HIGH(int pin)
{
 digitalWriteFast2(pin,HIGH);
}

extern void Trans_Dat(unsigned int index, unsigned int data)
{
unsigned char low8  = lowByte(index);
unsigned char high8 = highByte(index);

digitalWriteFast2(CS,LOW);//SET_LOW(CS);
digitalWriteFast2(RS,LOW);//SET_LOW(RS);
digitalWriteFast2(WR,HIGH);//SET_HIGH(WR);

       Write_Data(high8);
     
digitalWriteFast2(WR,LOW);//SET_LOW(WR);
       
digitalWriteFast2(WR,HIGH); //SET_HIGH(WR);
Write_Data(low8);
   
digitalWriteFast2(WR,LOW);//SET_LOW(WR);
       
digitalWriteFast2(WR,HIGH); //SET_HIGH(WR);

digitalWriteFast2(RS,HIGH); //SET_HIGH(RS);
       
low8  = lowByte(data);
high8 = highByte(data);

Write_Data(high8);
//delayMicroseconds(5);
//PORTD = high8;
       digitalWriteFast2(WR,LOW); // SET_LOW(WR);
 // delayMicroseconds(5);
       digitalWriteFast2(WR,HIGH) //SET_HIGH(WR);
Write_Data(low8);
//delayMicroseconds(5);
digitalWriteFast2(WR,LOW);// SET_LOW(WR);
      // delayMicroseconds(5);
digitalWriteFast2(WR,HIGH) ;//SET_HIGH(WR);
//delayMicroseconds(5);
digitalWriteFast2 (CS,HIGH); //SET_HIGH(CS);
}

void Write_Data(unsigned char data)
{// made pin specific as port is not the same on arduino and mega
//  PORTD=data<<2; //FASTERWAY/ WHY BECAUSE PIN IS NOW KNOWN ALWAYS. IT IS HARDCODED IN COMPILE

if (bitRead(data,0)){digitalWriteFast2(DB10,HIGH);} else{digitalWriteFast2(DB10,LOW);}// digitalWriteFast2(DB16,bitRead(data,6));
if (bitRead(data,2)){digitalWriteFast2(DB12,HIGH);} else{digitalWriteFast2(DB12,LOW);}// digitalWriteFast2(DB17,bitRead(data,6));
if (bitRead(data,4)){digitalWriteFast2(DB14,HIGH);} else{digitalWriteFast2(DB14,LOW);}// digitalWriteFast2(DB16,bitRead(data,6));
if (bitRead(data,6)){digitalWriteFast2(DB16,HIGH);} else{digitalWriteFast2(DB16,LOW);}// digitalWriteFast2(DB17,bitRead(data,6));
if (bitRead(data,1)){digitalWriteFast2(DB11,HIGH);} else{digitalWriteFast2(DB11,LOW);}// digital
if (bitRead(data,3)){digitalWriteFast2(DB13,HIGH);} else{digitalWriteFast2(DB13,LOW);}// digitalWriteFast2(DB17,bitRead(data,6));
if (bitRead(data,5)){digitalWriteFast2(DB15,HIGH);} else{digitalWriteFast2(DB15,LOW);}// digitalWriteFast2(DB16,bitRead(data,6));
if (bitRead(data,7)){digitalWriteFast2(DB17,HIGH);} else{digitalWriteFast2(DB17,LOW);}// digitalWriteFast2(DB17,bitRead(data,6));
}

void lcd_Init(void)
{
digitalWriteFast2(RD,HIGH); //SET_HIGH(RD);
digitalWriteFast2 (RD,HIGH); //SET_HIGH(RESET);
delay(3);
digitalWriteFast2(RESET,LOW); //SET_LOW(RESET);
delay(15);
digitalWriteFast2(RESET,HIGH);//SET_HIGH(RESET);
delay(20);

Trans_Dat(0x00E3, 0x3008);
Trans_Dat(0x00E7, 0x0012);
Trans_Dat(0x00EF, 0x1231);
Trans_Dat(0x0001, 0x0100);
Trans_Dat(0x0002, 0x0700); // set line inversion
// Trans_Dat(0x0003, 0b0001000000011000); // ???
Trans_Dat(0x0003, 0b0001000000110000); // ???
Trans_Dat(0x0004, 0x0000);
Trans_Dat(0x0008, 0x0207);
Trans_Dat(0x0009, 0x0000);
Trans_Dat(0x000A, 0x0000);
Trans_Dat(0x000C, 0x0000);
Trans_Dat(0x000D, 0x0000);
Trans_Dat(0x000F, 0x0000);
Trans_Dat(0x0010, 0x0000);
Trans_Dat(0x0011, 0x0007);
Trans_Dat(0x0012, 0x0000); // VREG1OUT voltage
Trans_Dat(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
delay(200);
Trans_Dat(0x0010, 0x1490); // SAP, BT[3:0], AP[2:0], DSTB, SLP, STB
Trans_Dat(0x0011, 0x0227);
delay(50); // Delayms 50ms
Trans_Dat(0x0012, 0x001c); // External reference voltage= Vci;
delay(50); // Delayms 50ms
Trans_Dat(0x0013, 0x0A00); // R13=0F00 when R12=009E;VDV[4:0] for VCOM amplitude
Trans_Dat(0x0029, 0x000F); // R29=0019 when R12=009E;VCM[5:0] for VCOMH//0012//
Trans_Dat(0x002B, 0x000D); // Frame Rate = 91Hz
delay(50);
Trans_Dat(0x0030, 0x0000);
Trans_Dat(0x0031, 0x0203);
Trans_Dat(0x0032, 0x0001);
Trans_Dat(0x0035, 0x0205);
Trans_Dat(0x0036, 0x030C);
Trans_Dat(0x0037, 0x0607);
Trans_Dat(0x0038, 0x0405);
Trans_Dat(0x0039, 0x0707);
Trans_Dat(0x003C, 0x0502);
Trans_Dat(0x003D, 0x1008);
       lcd_ClearWindow();
Trans_Dat(0x0060, 0xA700); // Gate Scan Line
Trans_Dat(0x0061, 0x0001);
Trans_Dat(0x006A, 0x0000);
Trans_Dat(0x0080, 0x0000);
Trans_Dat(0x0081, 0x0000);
Trans_Dat(0x0082, 0x0000);
Trans_Dat(0x0083, 0x0000);
Trans_Dat(0x0084, 0x0000);
Trans_Dat(0x0085, 0x0000);
Trans_Dat(0x0090, 0x0010);
Trans_Dat(0x0092, 0x0600); //0x0000
Trans_Dat(0x0093, 0x0003);
Trans_Dat(0x0095, 0x0110);
Trans_Dat(0x0097, 0x0000);
Trans_Dat(0x0098, 0x0000);
Trans_Dat(0x0007, 0x0133);
}

#define ENDCOL     240 // X
#define ENDROW     320 // Y
#define XChar  (int)((ENDCOL) / 6)
#define YLine  (int)((ENDROW) / 12)

void lcd_Clear(unsigned int Color)
{
 unsigned long i;
 lcd_SetX(0);
 lcd_SetY(0);
 lcd_ClearWindow();
 for(i=0;i<320UL*240UL;i++)
   Trans_Dat(0x0022,Color);
}

void lcd_ClearWindow(void)
{
 lcd_SetWindow(0,239,0,319);
}

void lcd_SetWindow(unsigned int HSA,unsigned int HEA,unsigned int VSA,unsigned int VEA)
{
 Trans_Dat(0x0050, HSA); // ??(Horizontal) G
 Trans_Dat(0x0051, HEA); // ??(Horizontal)
 Trans_Dat(0x0052, VSA); // ??(Vertical)
 Trans_Dat(0x0053, VEA); // ??(Vertical)
}  





camolas

#2
Mar 12, 2012, 07:43 pm Last Edit: Mar 12, 2012, 07:45 pm by camolas Reason: 1
2s part:

Code: [Select]
void lcd_SetX(unsigned int x) //??X
{
 Trans_Dat(0x0020, x);
}  

void lcd_SetY(unsigned int y) //??Y
{
 Trans_Dat(0x0021, y);
}  

void lcd_Color(unsigned int Color)
{
 Trans_Dat(0x0022,Color);
}

void lcd_Pixel(unsigned int Xpos, unsigned int Ypos, unsigned int Color){

if((Xpos<ENDCOL) && (Ypos<ENDROW)){
Trans_Dat(0x0020, Xpos); // ????
Trans_Dat(0x0021, Ypos);
Trans_Dat(0x0022, Color);
}
}

#define abs(a) (((a)>0) ? (a) : -(a))
void lcd_Line(int x0, int y0, int x1, int y1, unsigned int Color)
{
int steep, t;
int deltax, deltay, error;
int x, y;
int ystep;

y0=ENDROW-y0 -1;
y1=ENDROW-y1 -1;
steep = (abs(y1 - y0) > abs(x1 - x0));
if(steep){
t = x0; x0 = y0; y0 = t;
t = x1; x1 = y1; y1 = t;
}
if(x0 > x1) {
t = x0; x0 = x1; x1 = t;
t = y0; y0 = y1; y1 = t;
}
deltax = x1 - x0;
deltay = abs(y1 - y0);
error = 0;
y = y0;
if(y0 < y1) ystep = 1; else ystep = -1;
for(x=x0; x<=x1; x++) {
if(steep) lcd_Pixel(y,x,Color); else lcd_Pixel(y,x,Color);
error += deltay;
if((error << 1) >= deltax) {
y += ystep;
error -= deltax;
}
}
}
void lcd_Circle(int x0, int y0, int r, unsigned int color)
{
int x = r;
int y = 0;
int F = -2 * r + 3;

while(x >= y){
lcd_Pixel(x0+x, y0+y, color);
lcd_Pixel(x0-x, y0+y, color);
lcd_Pixel(x0+x, y0-y, color);
lcd_Pixel(x0-x, y0-y, color);
lcd_Pixel(x0+y, y0+x, color);
lcd_Pixel(x0-y, y0+x, color);
lcd_Pixel(x0+y, y0-x, color);
lcd_Pixel(x0-y, y0-x, color);
if(F >= 0){
x--;
F -= 4 * x;
}
y++;
F += 4 * y + 2;
}
}

void lcd_Char(unsigned  char X, unsigned char Y, unsigned char C, unsigned int Color1, unsigned int Color2){
 unsigned char FontMask;
 unsigned int DrawX,DrawY;
 unsigned char FontData;
 
 DrawX=X*6;
 DrawY=Y*12;
 lcd_SetX(DrawX);
 lcd_SetY(DrawY);
 lcd_SetWindow(DrawX,DrawX+5,DrawY,DrawY+11);
 for(unsigned char y=0;y<12;y++)
 {
   FontMask=0b10000000;
   if(C<0xa1)
     FontData=pgm_read_byte_near(ANKFont[C-0x20]+y);
   else
   //  FontData=pgm_read_byte_near(HalfKanaFont[C-0xa1]+y);
   Serial.print("C=");
   Serial.print(C,HEX);
   Serial.print(" C2=");
   Serial.println(C-0xa1,HEX);
   for(unsigned char x=0;x<6;x++)
   {
     if(FontData&FontMask)
       lcd_Color(Color1);
     else
       lcd_Color(Color2);
     FontMask>>=1;
   }
 }

}

void lcd_CharZ(unsigned  char X, unsigned char Y, unsigned char utf8, unsigned int Color1, unsigned int Color2){
 unsigned char FontMask;
 unsigned int DrawX,DrawY;
 unsigned char FontData;
 unsigned char fp=0;
 
 utf8-=0x90;
 DrawX=X*6;
 DrawY=Y*12;
 lcd_SetX(DrawX);
 lcd_SetY(DrawY);
 lcd_SetWindow(DrawX,DrawX+11,DrawY,DrawY+11);
 for(unsigned char y=0;y<6;y++)
 {
   FontMask=0b10000000;
   FontData=pgm_read_byte_near(KanjiFont[utf8]+ fp++);
   for(unsigned char x=0;x<8;x++)
   {
     if(FontData&FontMask)
       lcd_Color(Color1);
     else
       lcd_Color(Color2);
     FontMask>>=1;
   }
   
   FontMask=0b10000000;
   FontData=pgm_read_byte_near(KanjiFont[utf8]+ fp++);
   for(unsigned char x=0;x<4;x++)
   {
     if(FontData&FontMask)
       lcd_Color(Color1);
     else
       lcd_Color(Color2);
     FontMask>>=1;
   }
   
   FontMask=0b1000;
   for(unsigned char x=0;x<4;x++)
   {
     if(FontData&FontMask)
       lcd_Color(Color1);
     else
       lcd_Color(Color2);
     FontMask>>=1;
   }
   
   FontMask=0b10000000;
   FontData=pgm_read_byte_near(KanjiFont[utf8]+ fp++);
   for(unsigned char x=0;x<8;x++)
   {
     if(FontData&FontMask)
       lcd_Color(Color1);
     else
       lcd_Color(Color2);
     FontMask>>=1;
   }
 }
}

void lcd_Str(int colum, int line, char *s, unsigned int Color1, unsigned int Color2)
{
 while (*s)
 {
   if(*s==0xffef)
   {
     Serial.println("ef-mode");
     for(int a=0;a<3;a++)
     {
       Serial.print("CharCode=");
       Serial.println(*(s+a),HEX);
     }
     if(*(s+1)==0xffbd && *(s+2)>=0xffa1)
     {
       Serial.println("BBBBB");
       Serial.println(*(s+2),HEX);
       lcd_Char(colum,line,*(s+2),Color1,Color2);
       colum++;
     }
     else        
     {
       Serial.println("CCCCC");
       lcd_CharZ(colum,line,*(s+2),Color1,Color2);
       colum=+2;
     }
     s+=3;
   } else
   {
     Serial.println("normal-mode");
     lcd_Char(colum++, line, *s++, Color1, Color2);
   }
   if(colum >= XChar)
   {
     line++;
     colum = 0;
     if(line >= YLine) line = 0;
   }
 }
}


The code is to big to post in a single one sow i had to breack it in 2.

Thanks and sory form the long post

camolas

Hi,

Any ideas how to work this TFT ST7781 out?

Thanks,

Paulo

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy