欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
電阻觸摸屏的校準(zhǔn)程序V1.0版--灼灼其華
[打印本頁(yè)]
作者:
jialinx
時(shí)間:
2015-9-14 18:49
標(biāo)題:
電阻觸摸屏的校準(zhǔn)程序V1.0版--灼灼其華
本帖最后由 jialinx 于 2015-9-14 18:51 編輯
/* 頭文件 touch.h */
#ifndef TOUCH_H
#define TOUCH_H
/* 添加驅(qū)動(dòng)文件 */
//#include "Diver.h"
typedef unsigned short int uint16_t ;
typedef unsigned char uint8_t;
typedef short int int16_t;
/* 觸摸屏必要數(shù)據(jù)結(jié)構(gòu)體 */
typedef struct
{
/* 得到觸摸屏X通道AD數(shù)據(jù)的方法 */
uint16_t (* const Get_XChannel)(void);
/* 得到觸摸屏Y通道AD數(shù)據(jù)的方法 */
uint16_t (* const Get_YChannel)(void);
/* 延時(shí)函數(shù) */
void (*Delay_ms)(uint16_t);
/* 查看觸摸屏是否被按下 返回0表示未按下 否則被按下 */
uint8_t (*Get_StatusFlag)(void);
void (*Clear_PendingBit)(void);
/* 在液晶屏上顯示十字號(hào)函數(shù) */
void (*DispH)(uint16_t x ,uint16_t y);
/* 液晶屏的X=觸摸屏的X*TP_Xk+TP_Xoffset Y坐標(biāo)也同理*/
float Xk;
float Yk;
float Xoffset;
float Yoffset;
}Tuch_TypeDef;
/* 坐標(biāo)結(jié)構(gòu)體 */
typedef struct
{
uint16_t TP_X;
uint16_t TP_Y;
}Touch_CoorDinate;
#define Calibration_x1 50
#define Calibration_y1 50
#define Calibration_x2 50
#define Calibration_y2 190
#define Calibration_x3 270
#define Calibration_y3 190
#define Calibration_x4 270
#define Calibration_y4 50
/* 得到液晶屏的坐標(biāo) 返回值0 坐標(biāo)靠譜 否則不靠譜 */
uint8_t Touch_GetCoordinate(Touch_CoorDinate *TP_COOR);
/* 觸摸屏校準(zhǔn) 返回0 校準(zhǔn)成功 否則失敗 */
uint8_t TP_Calibration(void);
#endif // TOUCH_H_INCLUDED
復(fù)制代碼
/* 源文件 touch.c */
/*
名稱(chēng):電阻觸摸屏的校準(zhǔn)程序V1.0版
作者:灼灼其華
移植的時(shí)候只要給 Touch_Structure 這個(gè)變量賦初值 就可以了
Get_XChannel 得到X通道的數(shù)據(jù)的方法
Get_YChannel 得到Y(jié)通道數(shù)據(jù)的方法
Delay_ms 毫秒級(jí)延時(shí)函數(shù)
Get_StatusFlag 檢測(cè)觸摸屏被按下的方法 0 沒(méi)有按下 1 已結(jié)按下了
Clear_PendingBit 清除觸摸屏按下標(biāo)志的方法
DispH 在液晶屏上顯示十字號(hào)的方法
移植之前要先實(shí)現(xiàn)這幾個(gè)簡(jiǎn)單的函數(shù)/方法
*/
#include "touch.h"
#define N 10
/* 保存點(diǎn)擊一次觸摸屏 采集的數(shù)據(jù) */
static uint16_t TP_Coordinate[2][10];
/* 給Touch_Structure 變量賦初值 */
static Tuch_TypeDef Touch_Structure;
#define R 50
/* 觸摸屏數(shù)據(jù)濾波 */
uint16_t TP_Data_Processing(uint16_t *pData,uint8_t Length)
{
uint8_t i,j,Cnt=0,Max=0;
uint16_t Number,cc;
for(i=0;i<Length;i++)
{
for(j=0;j<Length;j++)
{
if(i==j)
continue;
if(pData[i]>pData[j])
cc=pData[i]-pData[j];
else
cc=pData[j]-pData[i];
if(cc<R)
{
Cnt++;
}
}
if(Cnt>Max)
{
Max=Cnt;
Number=pData[i];
}
Cnt=0;
}
return Number;
}
/* 求二階行列式的值 */
int16_t Get_Determinant_2(int16_t a11,int16_t a12,int16_t a21,int16_t a22)
{
return (a11*a22-a21*a12);
}
/* 解二元一次方程 前兩列保存的是系數(shù) */
uint8_t Solve_Equations_2(int16_t(*ptr)[3],float *k,float *off)
{
int16_t D,D1,D2;
D=Get_Determinant_2(ptr[0][0],ptr[0][1],ptr[1][0],ptr[1][1]);
if(D==0)
return 1;
D1=Get_Determinant_2(ptr[0][2],ptr[0][1],ptr[1][2],ptr[1][1]);
D2=Get_Determinant_2(ptr[0][0],ptr[0][2],ptr[1][0],ptr[1][2]);
*k=D1/(float)D;
*off=D2/(float)D;
return 0;
}
/* 求絕對(duì)值 */
uint16_t Get_ABS(uint16_t x,uint16_t y)
{
if(x>y)
return x-y;
else
return y-x;
}
// 允許的誤差 */
#define CCMAX 50
/* 得到觸摸屏的坐標(biāo) 并且濾波 返回0 表示坐標(biāo)靠譜 否則是不靠譜的 */
uint8_t TP_GetCoorDinate(Touch_CoorDinate *TP_COOR)
{
uint8_t bit;
uint16_t TP_XCoor_1,TP_YCoor_1;
uint16_t TP_XCoor_2,TP_YCoor_2;
/* 第一次采集數(shù)據(jù) */
for(bit=0;bit<N;bit++)
{
/* 采集10次 X 數(shù)據(jù) */
TP_Coordinate[0][bit]=Touch_Structure.Get_XChannel();
/* 采集10次 Y 數(shù)據(jù) */
TP_Coordinate[1][bit]=Touch_Structure.Get_YChannel();
}
TP_XCoor_1=TP_Data_Processing(TP_Coordinate[0],N);
TP_YCoor_1=TP_Data_Processing(TP_Coordinate[1],N);
/* 第二次采集數(shù)據(jù) */
for(bit=0;bit<N;bit++)
{
/* 采集10次 X 數(shù)據(jù) */
TP_Coordinate[0][bit]=Touch_Structure.Get_XChannel();
/* 采集10次 Y 數(shù)據(jù) */
TP_Coordinate[1][bit]=Touch_Structure.Get_YChannel();
}
TP_XCoor_2=TP_Data_Processing(TP_Coordinate[0],N);
TP_YCoor_2=TP_Data_Processing(TP_Coordinate[1],N);
/* 兩次采集的數(shù)據(jù)之差不能超過(guò)CCMAX 否則認(rèn)為數(shù)據(jù)不靠譜 */
if( Get_ABS(TP_XCoor_1 , TP_XCoor_2) >CCMAX )
return 1;
if( Get_ABS(TP_YCoor_1 , TP_YCoor_2) >CCMAX )
return 1;
/* 取兩次采集數(shù)據(jù)的平均值 */
TP_COOR->TP_X=(TP_XCoor_1>>1)+(TP_XCoor_2>>1);
TP_COOR->TP_Y=(TP_YCoor_1>>1)+(TP_YCoor_2>>1);
}
/* 校準(zhǔn)誤差 */
#define CCMA 100
/* 觸摸屏校準(zhǔn) */
uint8_t TP_Calibration(void)
{
Touch_CoorDinate TP_COOR[4];
int16_t arr[2][3];
uint16_t CC,CC1,CC2;
/* 校準(zhǔn)第一個(gè)點(diǎn) */
Touch_Structure.Delay_ms(500);
Touch_Structure.DispH(Calibration_x1,Calibration_y1);
Touch_Structure.Clear_PendingBit();
while(1)
{
if(Touch_Structure.Get_StatusFlag())
{
Touch_Structure.Clear_PendingBit();
if(TP_GetCoorDinate(TP_COOR))
{
/* 顯示重新點(diǎn)擊 */
}
else
break;
}
}
/* 校準(zhǔn)第二個(gè)點(diǎn) */
Touch_Structure.Delay_ms(500);
Touch_Structure.DispH(Calibration_x2,Calibration_y2);
Touch_Structure.Clear_PendingBit();
while(1)
{
if(Touch_Structure.Get_StatusFlag())
{
Touch_Structure.Clear_PendingBit();
if(TP_GetCoorDinate(TP_COOR+1))
{
/* 顯示重新點(diǎn)擊 */
}
else
break;
}
}
/* 校準(zhǔn)第三個(gè)點(diǎn) */
Touch_Structure.Delay_ms(500);
Touch_Structure.DispH(Calibration_x3,Calibration_y3);
Touch_Structure.Clear_PendingBit();
while(1)
{
if(Touch_Structure.Get_StatusFlag())
{
Touch_Structure.Clear_PendingBit();
if(TP_GetCoorDinate(TP_COOR+2))
{
/* 顯示重新點(diǎn)擊 */
}
else
break;
}
}
/* 校準(zhǔn)第四個(gè)點(diǎn) */
Touch_Structure.Delay_ms(500);
Touch_Structure.DispH(Calibration_x4,Calibration_y4);
Touch_Structure.Clear_PendingBit();
while(1)
{
if(Touch_Structure.Get_StatusFlag())
{
Touch_Structure.Clear_PendingBit();
if(TP_GetCoorDinate(TP_COOR+3))
{
/* 顯示重新點(diǎn)擊 */
}
else
break;
}
}
/* 檢查數(shù)據(jù)是否準(zhǔn)確 */
CC1=Get_ABS(TP_COOR[0].TP_X , TP_COOR[3].TP_X);
CC2=Get_ABS(TP_COOR[2].TP_X , TP_COOR[1].TP_X);
CC=Get_ABS(CC1,CC2);
if(CC>CCMA)
return 1;
CC1=Get_ABS(TP_COOR[0].TP_Y , TP_COOR[1].TP_Y);
CC2=Get_ABS(TP_COOR[2].TP_Y , TP_COOR[3].TP_Y);
CC=Get_ABS(CC1,CC2);
if(CC>CCMA)
return 1;
/* 解方程求 TP_Xk TP_Yk TP_Xoffset TP_Yoffset */
/* 觸摸屏坐標(biāo)*K+offset =液晶屏坐標(biāo) */
arr[0][0]=TP_COOR[0].TP_X; arr[0][1]=1; arr[0][2]=Calibration_x1;
arr[1][0]=TP_COOR[2].TP_X; arr[1][1]=1; arr[1][2]=Calibration_x3;
if(Solve_Equations_2(arr , &Touch_Structure.Xk , &Touch_Structure.Xoffset))
{
/* 顯示方程無(wú)解 數(shù)據(jù)錯(cuò)誤 */
return 1;
}
arr[0][0]=TP_COOR[1].TP_Y; arr[0][1]=1; arr[0][2]=Calibration_y2;
arr[1][0]=TP_COOR[3].TP_Y; arr[1][1]=1; arr[1][2]=Calibration_y4;
if(Solve_Equations_2(arr , &Touch_Structure.Yk , &Touch_Structure.Yoffset))
{
/* 顯示方程無(wú)解 數(shù)據(jù)錯(cuò)誤 */
return 1;
}
return 0;
}
/* 得到液晶屏的坐標(biāo) 返回值0 坐標(biāo)靠譜 否則不靠譜 */
uint8_t Touch_GetCoordinate(Touch_CoorDinate *TP_COOR)
{
if(TP_GetCoorDinate(TP_COOR))
return 1;
/* 數(shù)據(jù)一般不會(huì)得到極值 */
/*
if(TP_COOR->TP_X==0||TP_COOR->TP_X==4095 ||TP_COOR->TP_Y==0 ||TP_COOR->TP_Y==4095)
return 1;
*/
TP_COOR->TP_X=TP_COOR->TP_X*Touch_Structure.Xk+Touch_Structure.Xoffset;
TP_COOR->TP_Y=TP_COOR->TP_Y*Touch_Structure.Yk+Touch_Structure.Yoffset;
return 0;
}
復(fù)制代碼
作者:
緣分五月lck
時(shí)間:
2015-9-15 13:13
挺不錯(cuò)的呀。。。
作者:
lyl_420819
時(shí)間:
2017-5-16 20:13
好資料,下載學(xué)習(xí),謝謝分享。
作者:
lyl_420819
時(shí)間:
2017-5-16 20:29
學(xué)習(xí)了,謝謝分享。
作者:
liangzz
時(shí)間:
2017-5-17 16:46
謝謝分享。!
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1