高级会员
- 积分
- 972
- 金钱
- 972
- 注册时间
- 2016-6-15
- 在线时间
- 473 小时
|
学习emWin也有1个月的时间了,从刚开始接触时的迷茫到现在的一点点进步离不开本论坛网友们的支持,在这里先感谢大家。今天分享的是自己编写的触摸屏4点校验程序(我这个人有个坏毛病就是不愿意参考别人的程序,什么都喜欢自己写),程序编写的不是很规范望见谅不过运行稳定也很实用。
#include "mym.h"
struct _pt pt;
struct _touch touch,_sur;
struct _tbase tbase;
void TOUCH_Init() //比中断IO初始化触摸片选IO
{
GPIO_InitTypeDef myGPIO;
myGPIO.GPIO_Pin =GPIO_Pin_5;
myGPIO.GPIO_Mode =GPIO_Mode_IPU;
GPIO_Init(GPIOC,&myGPIO);
myGPIO.GPIO_Pin =GPIO_Pin_1;
myGPIO.GPIO_Mode =GPIO_Mode_Out_OD;
myGPIO.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&myGPIO);
TIM2_Init();
}
UINT32 TOUCH_ReadInt(UINT8 Cmd)
{
UINT32 tmp=0;
TP_CS_CLR();
SPI1_ReadWriteByte(Cmd);Delay_5us();
tmp =SPI1_ReadWriteByte(0xff)<<8;
tmp|=SPI1_ReadWriteByte(0xff);
TP_CS_SET();
return (tmp>>4);
}
INT32 TOUCH_GetMeasureX()
{
INT32 X=0;
SPI_SetSpeed(SPI1,4); //读触摸时SPI=4.5M
X =TOUCH_ReadInt(TOUCH_READ_X);
SPI_SetSpeed(SPI1,1); //恢复18M
return (2047-X);
}
INT32 TOUCH_GetMeasureY()
{
INT32 Y=0;
SPI_SetSpeed(SPI1,4);
Y =TOUCH_ReadInt(TOUCH_READ_Y);
SPI_SetSpeed(SPI1,1);
return (Y);
}
void GetXY(struct _touch *pt) //获取触摸平均值
{
#define len 5
UINT8 i;
pt->x=pt->y=0;
for(i=0;i<len;i++)
{
pt->x +=TOUCH_GetMeasureX();
pt->y +=TOUCH_GetMeasureY();
}
pt->x /=len;
pt->y /=len;
}
void myGUI_DrawCross(UINT16 x,UINT16 y,UINT32 c) //画光标
{
GUI_SetColor(c);
GUI_SetPenSize(1);
GUI_DrawLine(x-10,y,x+10,y);
GUI_DrawLine(x,y-10,x,y+10);
}
void myGUI_MovCross(UINT16 sx,UINT16 sy,UINT16 ex,UINT16 ey,UINT32 c)//触摸时光标移动
{
GUI_SetColor(GUI_WHITE);
GUI_FillRect(0,0,239,319);
if(sx==ex)
{
if(sy<ey)
{
for(;sy<ey;sy++)
{
myGUI_DrawCross(sx,sy-1,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
Delay_ms(2);
}
myGUI_DrawCross(sx,sy-1,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
}
else
{
for(;sy>ey;sy--)
{
myGUI_DrawCross(sx,sy+1,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
Delay_ms(2);
}
myGUI_DrawCross(sx,sy+1,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
}
}
else
{
if(sx<ex)
{
for(;sx<ex;sx++)
{
myGUI_DrawCross(sx-1,sy,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
Delay_ms(2);
}
myGUI_DrawCross(sx-1,sy,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
}
else
{
for(;sx>ex;sx--)
{
myGUI_DrawCross(sx+1,sy,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
Delay_ms(2);
}
myGUI_DrawCross(sx+1,sy,GUI_WHITE);
myGUI_DrawCross(sx,sy,c);
}
}
}
void myGUI_TOUCH_Calibration() //触摸校验程序
{
UINT8 i;
UINT16 CrossPos[4][6]={{15,15,15,15,15,304},{15,304,15,304,224,304},{224,304,224,304,224,15},{224,15,0,0,0,0}};
struct _touch _touch[4];
for(i=0;i<4;i++)
{
myGUI_DrawCross(CrossPos[i][0],CrossPos[i][1],GUI_RED);
while(PEN())Delay_ms(20);
GetXY(&_touch[i]);
myGUI_MovCross(CrossPos[i][2],CrossPos[i][3],CrossPos[i][4],CrossPos[i][5],GUI_RED);
while(!PEN())Delay_ms(20);
}
GUI_SetColor(GUI_WHITE);
GUI_FillRect(0,0,239,319);
touch.x =((_touch[3].x-_touch[0].x)+(_touch[2].x-_touch[1].x)) /2;
touch.y =((_touch[1].y-_touch[0].y)+(_touch[2].y-_touch[3].y)) /2;
tbase.x =(double)touch.x /(240.0 -30.0);
tbase.y =(double)touch.y /(320.0 -30.0);
_sur.x =((_touch[0].x+_touch[1].x)/2) -(tbase.x*15.0);
_sur.y =((_touch[0].y+_touch[3].y)/2) -(tbase.y*15.0);
}
void IS_Touch(bool fg) //这里是将触摸值保存到文件系统
{
FIL file;
FRESULT res =f_open(&file,"config/touchV.txt",FA_READ);
GUI_SetColor(GUI_WHITE);
GUI_FillRect(0,0,239,319);
GetXY(&touch);
if((fg==true) || (res==FR_NO_FILE))
{
INT32 tmp,buf[4];
_tim_stop =true;
myGUI_TOUCH_Calibration();
_tim_stop =false;
buf[0] =_sur.x;
buf[1] =_sur.y;
buf[2] =(INT32)(tbase.x*10000.0);
buf[3] =(INT32)(tbase.y*10000.0);
f_close(&file);
f_open(&file,"config/touchV.txt",FA_WRITE|FA_CREATE_NEW);
f_lseek(&file,0);
f_write(&file,buf,sizeof(buf),(UINT *)&tmp);
f_close(&file);
}
else
{
INT32 tmp,buf[4];
f_lseek(&file,0);
f_read(&file,buf,sizeof(buf),(UINT *)&tmp);
f_close(&file);
_sur.x =buf[0];
_sur.y =buf[1];
tbase.x =(double)(buf[2]/10000.0);
tbase.y =(double)(buf[3]/10000.0);
}
}
void myGUI_TOUCH_Exec() //用来替换emWin中的GUI_TOUCH_Exec()函数
{
if(PEN())
{
GUI_TOUCH_StoreState(-1,-1);
}
else
{
GetXY(&touch);
pt.x =(touch.x -_sur.x) /tbase.x;
pt.y =(touch.y -_sur.y) /tbase.y;
GUI_TOUCH_StoreState(pt.x,pt.y);
}
}
//以上是完整的触摸及校验部分的程序,一下是包含的头文件
#ifndef _TOUCH_H_
#define _TOUCH_H_
#define TOUCH_READ_X 0xD0
#define TOUCH_READ_Y 0x90
struct _pt
{
INT32 x,y;
};
extern struct _pt pt;
struct _touch
{
INT32 x,y;
};
extern struct _touch touch,_sur;
struct _tbase
{
double x,y;
};
extern struct _tbase tbase;
#define PEN() (GPIOC ->IDR &(1<<5))
#define TP_CS_SET() GPIOC_SET =2
#define TP_CS_CLR() GPIOC_CLR =2
void myGUI_TOUCH_Exec(void);
void TOUCH_Init(void);
void myGUI_TOUCH_Calibration(void);
void IS_Touch(bool fg);
#endif
|
|