📄 ic-ili9320-8bit.c
字号:
DATA_BUS = d_color;
}
}
//============================================================================
void ADS7846_init(void)
{ TP_CS = 1;
TP_IRQ = 1;
TP_DCLK = 0;
TP_DIN = 0;
}
//============================================================================
unsigned int read_from_7846(void)
{ unsigned int dat=0;
unsigned char i = 0;
dat = 0;
for(i=0;i<16;i++)
{ dat=dat << 1;
delay(1);
TP_DCLK =1;
delay(1);
if((TP_DOUT) == 0)
{ dat = dat & 0xfffe; }
else
{ dat = dat | 0x0001; }
TP_DCLK= 0 ;
}
return(dat>>4);
}
//============================================================================
void send_to_7846(unsigned char dat)
{ unsigned char i = 0;
for(i=0;i<8;i++)
{ if((dat & 0x80)==0x80)
{ TP_DIN = 1;
}
else
{ TP_DIN = 0;
}
delay(1);
TP_DCLK = 1;
delay(1);
TP_DCLK = 0;
delay(1);
dat = dat << 1;
}
}
//============================================================================
unsigned int read_y_axis(void)
{ unsigned int output = 0;
TP_CS = 0;
delay(1);
send_to_7846(0x90);
output = read_from_7846();
TP_CS = 1;
return(output);
}
//============================================================================
unsigned int read_x_axis(void)
{ unsigned int output = 0;
TP_CS = 0;
delay(1);
send_to_7846(0xd0);
output=read_from_7846();
TP_CS = 1;
return(output);
}
//============================================================================
void read_cor(void)
{ if((TP_IRQ) == 0)
{ x_cor = read_x_axis();
y_cor = read_y_axis();
}
else
{ x_cor=0;
y_cor=0;
}
}
//============================================================================
//============================================================================
//============================================================================
void line(unsigned char x1,unsigned int y1,unsigned char x2, unsigned int y2)
{ int t,i,dx,dy;
if((x1==x2) && (y1==y2)) return;
if(abs(x1-x2)>abs(y1-y2))
{ if(x1>x2)
{ t=x1;
x1=x2;
x2=t;
t=y1;
y1=y2;
y2=t;
}
dy=y2-y1;
dx=x2-x1;
for(i=x1;i<=x2;i++)
{ Pixel(i,y1+((int)(i-x1)*(int)dy+dx/2)/(int)dx); }
}
else
{ if(y1>y2)
{ t=x1;
x1=x2;
x2=t;
t=y1;
y1=y2;
y2=t;
}
dy=y2-y1;
dx=x2-x1;
for(i=y1;i<=y2;i++)
{ Pixel(x1+((int)(i-y1)*(int)dx+dy/2)/(int)dy,i); }
}
}
//===========================================================================
void tp_test(void)
{ unsigned char calibrate = 1;
unsigned char new_line=1;
unsigned char touched = 0;
unsigned char new_screen=1;
long unsigned int pre_x_cor,pre_y_cor;
long unsigned int x,y;
long unsigned int x1,x2,y1,y2;
long unsigned int Tx1,Tx2,Ty1,Ty2; //new
long unsigned int x_offset,y_offset;
long unsigned int x_range,y_range;
long unsigned int timer = 0;
ADS7846_init();
//standard_test_full(0xFFFF);
standard_window(32,90,176,40,gImage_176x40);
standard_window(225,305,15,15,gImage_15x15);
delay(200);
//------------------
while(calibrate) //calibrate
{ read_cor(); //read X,Y position
x=x_cor;y=y_cor;
if(x==0 || y==0)
continue; //if X=0 or Y=0, calibrate again
delay(10);
read_cor(); //read X,Y position again
if((abs(x_cor - x)) > 5 || (abs(y_cor - y))> 5) //move less than 5 level, stabilization
continue;
if(x_cor==0 || y_cor==0) //no thouch
continue;
x_offset = x_cor+20;//-100;
y_offset = y_cor-60;//-60;
calibrate = 0;
}
clear=0xff; standard_window(225,305,15,15,CLR);clear=0x00;
delay(3000);
standard_window(0,0,15,15,gImage_15x15);
calibrate = 1; //calibrate again
while(calibrate)
{ read_cor(); //read X,Y position
x=x_cor;
y=y_cor;
if(x==0 || y==0)
continue; //if X=0 or Y=0, calibrate again
delay(10);
read_cor(); //read X,Y position again
if((abs(x_cor - x)) > 5 || (abs(y_cor - y))> 5) //move less than 5 level, stabilization
continue;
if(x_cor==0 || y_cor==0) //no thouch
continue;
x_range = (x_cor - x_offset +50);// + 110;
y_range = (y_cor - y_offset +50);// + 60;
calibrate = 0;
}
//------------------------------
clear=0xff; standard_window(32,90,176,40,CLR);
standard_window(0,0,15,15,CLR);clear=0x00;
standard_window(32,90,176,40,gImage_text);
delay(2000);
new_line = 1;
touched = 0;
while(1)
{ timer += 1;
if(timer>1000000)
{ timer = 0;
if(touched == 1)
{ Checker(0xffff,0xffff);
standard_window(32,90,176,40,gImage_text);
touched = 0;
new_screen = 1;
}
}
//-------------------
read_cor();
x=x_cor;
y=y_cor;
if(x==0 || y==0)
{ new_line = 1;
continue;
}
delay(10);
read_cor();
if((abs(x_cor - x)) > 300 || (abs(y_cor - y))> 300)
continue;
if(new_line == 1)
{
timer=0;
touched = 1;
if (new_screen == 1)
{ Checker(0xffff,0xffff);
new_screen = 0;
standard_window(0,0,240,40,gImage_240x40);//30
}
pre_x_cor = x;
pre_y_cor = y;
}
if(pre_x_cor <= x_offset)
pre_x_cor = x_offset;
if(pre_y_cor <= y_offset)
pre_y_cor = y_offset;
if(x <= x_offset)
x = x_offset;
if(y <= y_offset)
y = y_offset;
Tx2 = 238-((float)(pre_x_cor-x_offset) / ((float)x_range/240.0));
Ty2 = 320-((float)(pre_y_cor-y_offset) / ((float)y_range/320.0));
Tx1 = 238-((float)(x-x_offset) / ((float)x_range/240.0));
Ty1 = 320-((float)(y-y_offset) / ((float)y_range/320.0));
if (Tx1>240) x1=239;
if (Tx1<1) x1=1;
else x1 = Tx1;
if (Ty1>320) y1=319;
if (Ty1<=1) y1=1;
else y1 = Ty1;
if (Tx2>240) x2=239;
if (Tx2<1) x2=1;
else x2 = Tx2;
if (Ty2>320) y2=319;
if (Ty2<=1) y2=1;
else y2 = Ty2;
//======================
if(y_cor>0x00000880)//0x000006BF
{ touched = 1;
timer=0;
new_line = 1;
if(x_cor<0x00000760 && x_cor>0x00000660) //1
{// standard_test_picture(gImage_T1);
calibrate = 1;
case_value =0;
d_color = 0;
delay(500);
break;
}
if(x_cor<0x00000610 && x_cor>0x00000500) {d_color = 0xF800;} //2 R
if(x_cor<0x00000490 && x_cor>0x000003C0) {d_color = 0x07E0;} //3 G
if(x_cor<0x00000310 && x_cor>0x00000210) {d_color = 0x001F;} //4 B
if(x_cor<0x00000198 && x_cor>0x000000C6) //5 CLEAR
{ touched = 0;
Checker(0xffff,0xffff);
standard_window(0,0,240,40,gImage_240x40);//30
new_screen = 1;
}
{// standard_test_picture(gImage_T1);
calibrate = 1;
case_value =0;
d_color = 0;
delay(500);
break;
}
continue;
}
//============================
line(Tx2,Ty2,Tx1,Ty1);
pre_x_cor=x;
pre_y_cor=y;
new_line = 0;
}
}
//============================================================================
void EM_block(blk)
{ unsigned char block;
block = blk & 0x0F;
BLOCK=BLOCK & 0xf0;
BLOCK=BLOCK | block;
}
void Bmp_page(unsigned char pages)
{ switch(pages)
{ case 0: blocks=0;pointer=0x0000;adr=0x0000;break;
case 1: blocks=2;pointer=0x5800;adr=0x5800;break;
case 2: blocks=3;pointer=0x8680;adr=0x8680;break;
case 3: blocks=4;pointer=0x2680;adr=0x2680;break;
}
}
void WriteBmp(unsigned char pages)
{ unsigned char dat;
unsigned int page,seg;
Bmp_page(pages);
Set_address();
_FLASH_CS= 0;EM_block(blocks); //flash chip selected
for(page=0;page<DisplayCol;page++)
{ for(seg=0;seg<2*DisplaySeg;seg++)
{ _CS1=1;
dat=ptr[pointer++];adr++;
// DataPort_H=dat;
// DATA_BUS=dat;
// dat=ptr[pointer++];adr++;
_CS1=0;
DATA_BUS=dat;
if(adr>0xffff)
{ adr=0;pointer=0;
blocks++;EM_block(blocks);
}
}
}
_FLASH_CS=1;
}
void Checker(unsigned int param1,unsigned int param2) //Full on,off,checkerboard,bar
{ unsigned int page,seg;
Set_address();
for(page=0;page<DisplayCol;page++)
{
for(seg=0;seg<DisplaySeg;seg++)
{
LCD_DataWrite(param1);
LCD_DataWrite(param2);
}
}
}
void WriteBoundary(void) //write boundary
{ unsigned int page,seg;
Set_address();
for(page=0;page<DisplayCol;page++)
{
for(seg=0;seg<DisplaySeg;seg++)
{ if (seg==0||seg==(DisplaySeg-1)){ LCD_DataWrite(0xff); LCD_DataWrite(0xff);}//f
else if (page==0) { LCD_DataWrite(0xff); LCD_DataWrite(0xff);}
else if (page==(DisplayCol-1)) { LCD_DataWrite(0xff); LCD_DataWrite(0xff);} //f
else { LCD_DataWrite(0x00); LCD_DataWrite(0x00);}//0
}
}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -