📄 ads7846.c
字号:
Uart_WriteByte(PORT1,(char)data);
}
CHR Bin2BCD(CHR num);
unsigned char Ts_Loc_Get(GUI_HID_STATE state);
#define putw(a) Wr_Word_Uart(a)
void Ads7846_Test(void)
{
GUI_HID_STATE stat;
char str[10];
char key = 0;
char key1 = 0;
{
void TFT_Clear(char);
void Test_DrawAllBox(char,char);
TFT_Clear(0x00);
Test_DrawAllBox(1,0x03);
}
putw(0x55aa);
putw(Ts_MinMax.Xmin);
putw(Ts_MinMax.Xmid);
putw(Ts_MinMax.Xmax);
putw(Ts_MinMax.Ymin);
putw(Ts_MinMax.Ymax);
putw(0xaa55);
while(1)
{
if (GUI_TOUCH_GetState(&stat))
{
TS_State.Pressed = 0;
key = Ts_Loc_Get(stat);
if (key != 0)
{
if (key != key1)
{
key1 = key;
}
else
{
key_rngset(((word)key)<<8);
// str[0] = key/10 +'0';
// str[1] = key%10 +'0';
// str[2] = 0;
// Lcd_Draw_Rect_Fill(0, 0, 799, 60, 0);
// text(str, FONT_ASC_24x48,0x11, NULL, 0xe0, 0, 0);
bellcnt = 0x10;
// Insert_Byte(Bin2BCD(key),8);
}
}
}
}
}
//#define MAX_XDIR_KEY_NUM 10
//#define MAX_YDIR_KEY_NUM 10
#if (MODEL_VER == ROYAL_9INCH)
#define MAX_XDIR_KEY_NUM 10
#define MAX_YDIR_KEY_NUM 10
#else
#define MAX_XDIR_KEY_NUM 10
#define MAX_YDIR_KEY_NUM 13
#endif
#define X_INC1 ((Ts_MinMax.Xmax-Ts_MinMax.Xmin)/MAX_XDIR_KEY_NUM)
//#define X_INC1 ((Ts_MinMax.Xmid-Ts_MinMax.Xmin)/5)
#define Y_INC1 ((Ts_MinMax.Ymax-Ts_MinMax.Ymin)/MAX_YDIR_KEY_NUM)
#define X_INC2 ((Ts_MinMax.Xmax-Ts_MinMax.Xmid)/5)
#define Y_INC2 ((YMAX1-YMIN1)/10)
unsigned char Ts_Loc_Get(GUI_HID_STATE state)
{
unsigned char idx = 1;
unsigned int x1=0,y1=0;
unsigned int x_loc,y_loc;
unsigned int i,j;
unsigned int x,y;
unsigned int x_inc,y_inc;
x = state.x;
y = state.y;
x_loc = Ts_MinMax.Xmin;
y_loc = Ts_MinMax.Ymin;
// if ( (x<XMIN1)||(x>XMAX2)||(y<YMIN1)||(y>YMAX1))
// return(0);
#if (MODEL_VER == ROYAL_9INCH) /*ROYAL is 9 inch*/
for (i=0;i<MAX_XDIR_KEY_NUM;i++) /*10 * 10 key matrix*/
{
for(j=0;j<MAX_YDIR_KEY_NUM;j++)
{
if ((x>x_loc)&&(x<(x_loc+X_INC1))&&(y>y_loc)&&(y<(y_loc+Y_INC1)))
{
return(Ts_Key_Cvt_Tbl[idx-1]);
}
idx ++;
y_loc += Y_INC1;
}
x_loc += X_INC1;
y_loc = Ts_MinMax.Ymin;
}
#else /*WD3-HK is 12 inch*/
for (i=0;i<MAX_YDIR_KEY_NUM;i++) /*10 * 10 key matrix*/
{
for(j=0;j<MAX_XDIR_KEY_NUM;j++)
{
if ((x>x_loc)&&(x<(x_loc+X_INC1))&&(y>y_loc)&&(y<(y_loc+Y_INC1)))
{
// return(Ts_Key_Cvt_Tbl[idx-1]);
return(idx);
}
idx ++;
x_loc += X_INC1;
}
y_loc += Y_INC1;
x_loc = Ts_MinMax.Xmin;
}
#endif
return(0);
#if 0
x_loc = Ts_MinMax.Xmid;
y_loc = Ts_MinMax.Ymin;
for (i=0;i<5;i++) /*10 * 10 key matrix*/
{
for(j=0;j<10;j++)
{
if ((x>x_loc)&&(x<(x_loc+X_INC2))&&(y>y_loc)&&(y<(y_loc+Y_INC1)))
{
return(Ts_Key_Cvt_Tbl[idx-1]);
}
idx ++;
y_loc += Y_INC1;
}
x_loc += X_INC2;
y_loc = Ts_MinMax.Ymin;
}
return(0);
#endif
}
int Fl_Ts_Depress = 0;
int Fl_Ts_Cali = 0;
void Get_TsLocByUart(WORD *x, WORD *y)
{
disint();
Fl_Ts_Cali = 1;
Fl_Ts_Depress = 0;
enint();
while(!Fl_Ts_Depress);
*x = TS_State.x;
*y = TS_State.y;
Fl_Ts_Cali = 0;
Fl_Ts_Depress = 0;
}
/*
* 因为触摸屏的非线性,所以需要对其参数进行校准,
* 现在的样机程序暂时校准最左,最右和中间三个点,
* 这样可以得到Xmin,Xmax,Xmid 和Ymin,Ymax,Ymid
* 几个参数
*/
void Ts_MinMax_Define(void)
{
WORD tmp1,tmp2;
BYTE step = 0;
BOOL loop = true;
#if 0
return;
#endif
while(loop)
{
TFT_Clear(0x00); /*Clear screen first*/
switch(step)
{
case 0: /*Locate the left side position*/
Lcd_Draw_Circle_Fill(5,5,5,0x03); /*Draw a red point*/
text("Please press this point",FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 10);
Get_TsLocByUart(&tmp1,&tmp2);
Ts_MinMax.Xmin = tmp1;
Ts_MinMax.Ymin = tmp2;
step ++;
break;
case 1: /*Middle position detection*/
#if (MODEL_VER == ROYAL_9INCH)
Lcd_Draw_Circle_Fill(795,475,5,0x03); /*Draw a red point*/
text("Please depress this point",FONT_ASC_9x17, 0x11, NULL, 0xe0, 550, 460);
#else
Lcd_Draw_Circle_Fill(795,595,5,0x03); /*Draw a red point*/
text("Please depress this point",FONT_ASC_9x17, 0x11, NULL, 0xe0, 550, 580);
#endif
Get_TsLocByUart(&tmp1,&tmp2);
Ts_MinMax.Xmax = tmp1;
Ts_MinMax.Ymax = tmp2;
loop = false; /*finished the location*/
break;
}
}
return;
loop = true;
int1ic = 0x00; /*Disable the TS interrupt*/
while(loop)
{
TFT_Clear(0x00); /*Clear screen first*/
switch(step)
{
case 0: /*Locate the left side position*/
Lcd_Draw_Circle_Fill(5,5,5,0x03); /*Draw a red point*/
text("Please press this point",FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 10);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
if(!(ADS7846_INT_PIN))
// if((tmp1 < TS_DFT_XMIN) && ( tmp2 < TS_DFT_YMIN))
{
Ts_MinMax.Xmin = tmp1;
Ts_MinMax.Ymin = tmp2;
step ++;
}
// else
// {
// bellcnt = 0xfe;
// text("Error, please try again",FONT_ASC_12x24, 0x11, NULL, 0xe0, 10, 240);
// text("Press screen for quit",FONT_ASC_12x24, 0x11, NULL, 0xe0, 10, 280);
// Get_TsLocByManual(&tmp1,&tmp2); /*Get key*/
// }
{
WORD k;
for(k=0;k<0xfffe;k++);
while(!ADS7846_INT_PIN);
}
break;
case 1: /*Middle position detection*/
Lcd_Draw_Circle_Fill(798,478,5,0x03); /*Draw a red point*/
text("Please depress this point",FONT_ASC_9x17, 0x11, NULL, 0xe0, 350, 460);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
// if(tmp1 < TS_DFT_XMID)
if(!(ADS7846_INT_PIN))
{
Ts_MinMax.Xmid = tmp1;
step ++;
}
// else
// {
// bellcnt = 0xfe;
// text("Error, please try again",FONT_ASC_12x24, 0x11, NULL, 0xe0, 10, 240);
// text("Press screen for quit",FONT_ASC_12x24, 0x11, NULL, 0xe0, 10, 280);
// Get_TsLocByManual(&tmp1,&tmp2); /*Get key*/
// }
{
WORD k;
for(k=0;k<0xfffe;k++);
while(!ADS7846_INT_PIN);
}
break;
case 2:
Lcd_Draw_Circle_Fill(795,475,5,0x03); /*Draw a red point*/
text("Please depress this point",FONT_ASC_9x17, 0x11, NULL, 0xe0, 650, 460);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
Get_TsLocByManual(&tmp1,&tmp2);
// if((tmp1 < TS_DFT_XMAX)&&(tmp2<TS_DFT_YMAX))
{
Ts_MinMax.Xmax = tmp1;
Ts_MinMax.Ymax = tmp2;
step ++;
loop = false; /*finished the location*/
}
// else
// {
// bellcnt = 0xfe;
// text("Error, please try again",FONT_ASC_12x24, 0x11, NULL, 0xe0, 10, 240);
// text("Press screen for quit",FONT_ASC_12x24, 0x11, NULL, 0xe0, 10, 280);
// Get_TsLocByManual(&tmp1,&tmp2); /*Get key*/
// }
break;
}
}
TFT_Clear(0x00);
bellcnt = 0xfe; /*Success*/
int1ic = 0x23; /*Enable the TS interrupt*/
}
/*=======================================================================
* Get the TS input direction by manual -- not by interrupt mode
* Note: Before call this function, make sure the TS pen interrupt
* is masked.
*======================================================================*/
void Get_TsLocByManual(WORD *x, WORD *y)
{
GUI_HID_STATE stat;
while(1)
{
while(ADS7846_INT_PIN); /*Wait the TS interrupt*/
GUI_TOUCH_Exec(); /*Check the input value*/
if (GUI_TOUCH_GetState(&stat))
{
TS_State.Pressed = 0;
*x = stat.x;
*y = stat.y;
return;
}
}
}
void Ts_FX_Test(void)
{
char i;
char ch;
char Uart_ReadByte(char,char *);
i = 0;
while(++i<100)
{
Uart_WriteByte(PORT0,i);
Uart_WriteByte(PORT1,i);
}
i = 0;
while(1)
{
if (Uart_ReadByte(PORT1,&ch)==OK)
{
Uart_WriteByte(PORT0,ch);
Insert_Byte(ch, i);
i +=2;
if (i>8)
i = 0;
}
}
}
#define TS_FX_DATA_IN_START_HEAD 0xb1 /*press the TS*/
#define TS_FX_DATA_IN_START_TAIL 0xba
#define TS_DATA_IN_IDLE 0x00
#define TS_DATA_IN_INPUT 0x01
void Ts_Fx_Data_Input(char data)
{
static BYTE ts_in_cntr = 0;
static BYTE ts_in_flag = 0;
static BYTE tmp_Data[10];
WORD key;
WORD x,y;
switch(ts_in_flag)
{
case TS_DATA_IN_IDLE: /*idel mode*/
if(data == TS_FX_DATA_IN_START_HEAD)
{
ts_in_flag = TS_DATA_IN_INPUT;
ts_in_cntr = 0;
}
break;
case TS_DATA_IN_INPUT: /*during the input mode*/
tmp_Data[ts_in_cntr] = data;
ts_in_cntr ++;
if(ts_in_cntr==5)
{
ts_in_cntr = 0;
ts_in_flag = TS_DATA_IN_IDLE;
if(data == TS_FX_DATA_IN_START_TAIL)
{
TS_State.y = (WORD)((WORD)tmp_Data[0] * 128 + (WORD)tmp_Data[1]);
TS_State.x = (WORD)((WORD)tmp_Data[2] * 128 + (WORD)tmp_Data[3]);
Fl_Ts_Depress = 1;
if (!Fl_Ts_Cali)
{
bellcnt = 0x03;
key = Ts_Loc_Get(TS_State); /*Get the key input index no*/
// if (key == key_bak) /*2 times is same, store it */
{
key_rngset(key<<8);
TS_State.Key_Off = 0;
#undef TS_DEBUG_INPUT
#ifdef TS_DEBUG_INPUT
{
char str[20];
char ch;
char kk = key;
ch=(kk>>4)&0x0f;
str[0] = ch+0x30;
if(ch>9)
str[0]+=7;
ch=kk&0x0f;
str[1]=ch+0x30;
if(ch>9)
str[1]+=7;
str[2] = 0;
TFT_Clear(0x00); /*Clear screen first*/
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 10);
{
void LWordtoAsc( CHR *asc_str, DWORD num, CHR len);
LWordtoAsc(str, TS_State.x,4);
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 30);
LWordtoAsc(str, TS_State.y,4);
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 50);
LWordtoAsc(str, Ts_MinMax.Xmin,4);
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 70);
LWordtoAsc(str, Ts_MinMax.Xmax,4);
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 90);
LWordtoAsc(str, Ts_MinMax.Ymin,4);
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 110);
LWordtoAsc(str, Ts_MinMax.Ymax,4);
text(str,FONT_ASC_9x17, 0x11, NULL, 0xe0, 0, 130);
}
}
#endif
}
// else
{
// key_bak = key; /*Use the new key*/
}
}
}
}
break;
default:
ts_in_flag = TS_DATA_IN_IDLE;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -