?? touch.c
字號(hào):
#include "touch.h"
#include "lcd.h"
#include "delay.h"
#include "stdlib.h"
#include "math.h"
#include "24cxx.h"
//////////////////////////////////////////////////////////////////////////////////
//本程序只供學(xué)習(xí)使用,未經(jīng)作者許可,不得用于其它任何用途
//Mini STM32開發(fā)板
//ADS7843/7846/UH7843/7846/XPT2046/TSC2046 驅(qū)動(dòng)函數(shù)
//正點(diǎn)原子@ALIENTEK
//技術(shù)論壇:www.openedv.com
//修改日期:2010/6/13
//版本:V1.0
//版權(quán)所有,盜版必究。
//Copyright(C) 正點(diǎn)原子 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
Pen_Holder Pen_Point;//定義筆實(shí)體
//SPI寫數(shù)據(jù)
//向7843寫入1byte數(shù)據(jù)
void ADS_Write_Byte(u8 num)
{
u8 count=0;
for(count=0;count<8;count++)
{
if(num&0x80)TDIN=1;
else TDIN=0;
num<<=1;
TCLK=0;//上升沿有效
TCLK=1;
}
}
//SPI讀數(shù)據(jù)
//從7846/7843/XPT2046/UH7843/UH7846讀取adc值
u16 ADS_Read_AD(u8 CMD)
{
u8 count=0;
u16 Num=0;
TCLK=0;//先拉低時(shí)鐘
TCS=0; //選中ADS7843
ADS_Write_Byte(CMD);//發(fā)送命令字
delay_us(6);//ADS7846的轉(zhuǎn)換時(shí)間最長(zhǎng)為6us
TCLK=1;//給1個(gè)時(shí)鐘,清除BUSY
TCLK=0;
for(count=0;count<16;count++)
{
Num<<=1;
TCLK=0;//下降沿有效
TCLK=1;
if(DOUT)Num++;
}
Num>>=4; //只有高12位有效.
TCS=1;//釋放ADS7843
return(Num);
}
//讀取一個(gè)坐標(biāo)值
//連續(xù)讀取READ_TIMES次數(shù)據(jù),對(duì)這些數(shù)據(jù)升序排列,
//然后去掉最低和最高LOST_VAL個(gè)數(shù),取平均值
#define READ_TIMES 15 //讀取次數(shù)
#define LOST_VAL 5 //丟棄值
u16 ADS_Read_XY(u8 xy)
{
u16 i, j;
u16 buf[READ_TIMES];
u16 sum=0;
u16 temp;
for(i=0;i<READ_TIMES;i++)
{
buf[i]=ADS_Read_AD(xy);
}
for(i=0;i<READ_TIMES-1; i++)//排序
{
for(j=i+1;j<READ_TIMES;j++)
{
if(buf[i]>buf[j])//升序排列
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
sum=0;
for(i=LOST_VAL;i<READ_TIMES-LOST_VAL;i++)sum+=buf[i];
temp=sum/(READ_TIMES-2*LOST_VAL);
return temp;
}
//帶濾波的坐標(biāo)讀取
//最小值不能少于100.
u8 Read_ADS(u16 *x,u16 *y)
{
u16 xtemp,ytemp;
xtemp=ADS_Read_XY(CMD_RDX);
ytemp=ADS_Read_XY(CMD_RDY);
if(xtemp<100||ytemp<100)return 0;//讀數(shù)失敗
*x=xtemp;
*y=ytemp;
return 1;//讀數(shù)成功
}
//2次讀取ADS7846,連續(xù)讀取2次有效的AD值,且這兩次的偏差不能超過
//50,滿足條件,則認(rèn)為讀數(shù)正確,否則讀數(shù)錯(cuò)誤.
//該函數(shù)能大大提高準(zhǔn)確度
#define ERR_RANGE 50 //誤差范圍
u8 Read_ADS2(u16 *x,u16 *y)
{
u16 x1,y1;
u16 x2,y2;
u8 flag;
flag=Read_ADS(&x1,&y1);
if(flag==0)return(0);
flag=Read_ADS(&x2,&y2);
if(flag==0)return(0);
if(((x2<=x1&&x1<x2+ERR_RANGE)||(x1<=x2&&x2<x1+ERR_RANGE))//前后兩次采樣在+-50內(nèi)
&&((y2<=y1&&y1<y2+ERR_RANGE)||(y1<=y2&&y2<y1+ERR_RANGE)))
{
*x=(x1+x2)/2;
*y=(y1+y2)/2;
return 1;
}else return 0;
}
//讀取一次坐標(biāo)值
//僅僅讀取一次,知道PEN松開才返回!
u8 Read_TP_Once(void)
{
u8 t=0;
Pen_Int_Set(0);//關(guān)閉中斷
Pen_Point.Key_Sta=Key_Up;
Read_ADS2(&Pen_Point.X,&Pen_Point.Y);
while(PEN==0&&t<=250)
{
t++;
delay_ms(10);
};
Pen_Int_Set(1);//開啟中斷
if(t>=250)return 0;//按下2.5s 認(rèn)為無效
else return 1;
}
//////////////////////////////////////////////////
//與LCD部分有關(guān)的函數(shù)
//畫一個(gè)觸摸點(diǎn)
//用來校準(zhǔn)用的
void Drow_Touch_Point(u8 x,u16 y)
{
LCD_DrawLine(x-12,y,x+13,y);//橫線
LCD_DrawLine(x,y-12,x,y+13);//豎線
LCD_DrawPoint(x+1,y+1);
LCD_DrawPoint(x-1,y+1);
LCD_DrawPoint(x+1,y-1);
LCD_DrawPoint(x-1,y-1);
Draw_Circle(x,y,6);//畫中心圈
}
//畫一個(gè)大點(diǎn)
//2*2的點(diǎn)
void Draw_Big_Point(u8 x,u16 y)
{
LCD_DrawPoint(x,y);//中心點(diǎn)
LCD_DrawPoint(x+1,y);
LCD_DrawPoint(x,y+1);
LCD_DrawPoint(x+1,y+1);
}
//////////////////////////////////////////////////
//轉(zhuǎn)換結(jié)果
//根據(jù)觸摸屏的校準(zhǔn)參數(shù)來決定轉(zhuǎn)換后的結(jié)果,保存在X0,Y0中
void Convert_Pos(void)
{
if(Read_ADS2(&Pen_Point.X,&Pen_Point.Y))
{
Pen_Point.X0=Pen_Point.xfac*Pen_Point.X+Pen_Point.xoff;
Pen_Point.Y0=Pen_Point.yfac*Pen_Point.Y+Pen_Point.yoff;
}
}
//中斷,檢測(cè)到PEN腳的一個(gè)下降沿.
//置位Pen_Point.Key_Sta為按下狀態(tài)
//中斷線0線上的中斷檢測(cè)
void EXTI1_IRQHandler(void)
{
Pen_Point.Key_Sta=Key_Down;//按鍵按下
EXTI->PR=1<<1; //清除LINE1上的中斷標(biāo)志位
}
//PEN中斷設(shè)置
void Pen_Int_Set(u8 en)
{
if(en)EXTI->IMR|=1<<1; //開啟line1上的中斷
else EXTI->IMR&=~(1<<1); //關(guān)閉line1上的中斷
}
//////////////////////////////////////////////////////////////////////////
//此部分涉及到使用外部EEPROM,如果沒有外部EEPROM,屏蔽此部分即可
#ifdef ADJ_SAVE_ENABLE
//保存在EEPROM里面的地址區(qū)間基址,占用13個(gè)字節(jié)(RANGE:SAVE_ADDR_BASE~SAVE_ADDR_BASE+12)
#define SAVE_ADDR_BASE 40
//保存校準(zhǔn)參數(shù)
void Save_Adjdata(void)
{
s32 temp;
//保存校正結(jié)果!
temp=Pen_Point.xfac*100000000;//保存x校正因素
AT24CXX_WriteLenByte(SAVE_ADDR_BASE,temp,4);
temp=Pen_Point.yfac*100000000;//保存y校正因素
AT24CXX_WriteLenByte(SAVE_ADDR_BASE+4,temp,4);
//保存x偏移量
AT24CXX_WriteLenByte(SAVE_ADDR_BASE+8,Pen_Point.xoff,2);
//保存y偏移量
AT24CXX_WriteLenByte(SAVE_ADDR_BASE+10,Pen_Point.yoff,2);
temp=AT24CXX_ReadOneByte(SAVE_ADDR_BASE+12);
temp&=0XF0;
temp|=0X0A;//標(biāo)記校準(zhǔn)過了
AT24CXX_WriteOneByte(SAVE_ADDR_BASE+12,temp);
}
//得到保存在EEPROM里面的校準(zhǔn)值
//返回值:1,成功獲取數(shù)據(jù)
// 0,獲取失敗,要重新校準(zhǔn)
u8 Get_Adjdata(void)
{
s32 tempfac;
tempfac=AT24CXX_ReadOneByte(52);//第五十二字節(jié)的第四位用來標(biāo)記是否校準(zhǔn)過!
if((tempfac&0X0F)==0X0A)//觸摸屏已經(jīng)校準(zhǔn)過了
{
tempfac=AT24CXX_ReadLenByte(40,4);
Pen_Point.xfac=(float)tempfac/100000000;//得到x校準(zhǔn)參數(shù)
tempfac=AT24CXX_ReadLenByte(44,4);
Pen_Point.yfac=(float)tempfac/100000000;//得到y(tǒng)校準(zhǔn)參數(shù)
//得到x偏移量
tempfac=AT24CXX_ReadLenByte(48,2);
Pen_Point.xoff=tempfac;
//得到y(tǒng)偏移量
tempfac=AT24CXX_ReadLenByte(50,2);
Pen_Point.yoff=tempfac;
return 1;
}
return 0;
}
#endif
//觸摸屏校準(zhǔn)代碼
//得到四個(gè)校準(zhǔn)參數(shù)
void Touch_Adjust(void)
{
u16 pos_temp[4][2];//坐標(biāo)緩存值
u8 cnt=0;
u16 d1,d2;
u32 tem1,tem2;
float fac;
cnt=0;
POINT_COLOR=BLUE;
BACK_COLOR =WHITE;
LCD_Clear(WHITE);//清屏
POINT_COLOR=RED;//紅色
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20);//畫點(diǎn)1
Pen_Point.Key_Sta=Key_Up;//消除觸發(fā)信號(hào)
Pen_Point.xfac=0;//xfac用來標(biāo)記是否校準(zhǔn)過,所以校準(zhǔn)之前必須清掉!以免錯(cuò)誤
while(1)
{
if(Pen_Point.Key_Sta==Key_Down)//按鍵按下了
{
if(Read_TP_Once())//得到單次按鍵值
{
pos_temp[cnt][0]=Pen_Point.X;
pos_temp[cnt][1]=Pen_Point.Y;
cnt++;
}
switch(cnt)
{
case 1:
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(220,20);//畫點(diǎn)2
break;
case 2:
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,300);//畫點(diǎn)3
break;
case 3:
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(220,300);//畫點(diǎn)4
break;
case 4: //全部四個(gè)點(diǎn)已經(jīng)得到
//對(duì)邊相等
tem1=abs(pos_temp[0][0]-pos_temp[1][0]);//x1-x2
tem2=abs(pos_temp[0][1]-pos_temp[1][1]);//y1-y2
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//得到1,2的距離
tem1=abs(pos_temp[2][0]-pos_temp[3][0]);//x3-x4
tem2=abs(pos_temp[2][1]-pos_temp[3][1]);//y3-y4
tem1*=tem1;
tem2*=tem2;
d2=sqrt(tem1+tem2);//得到3,4的距離
fac=(float)d1/d2;
if(fac<0.95||fac>1.05||d1==0||d2==0)//不合格
{
cnt=0;
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20);
continue;
}
tem1=abs(pos_temp[0][0]-pos_temp[2][0]);//x1-x3
tem2=abs(pos_temp[0][1]-pos_temp[2][1]);//y1-y3
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//得到1,3的距離
tem1=abs(pos_temp[1][0]-pos_temp[3][0]);//x2-x4
tem2=abs(pos_temp[1][1]-pos_temp[3][1]);//y2-y4
tem1*=tem1;
tem2*=tem2;
d2=sqrt(tem1+tem2);//得到2,4的距離
fac=(float)d1/d2;
if(fac<0.95||fac>1.05)//不合格
{
cnt=0;
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20);
continue;
}//正確了
//對(duì)角線相等
tem1=abs(pos_temp[1][0]-pos_temp[2][0]);//x1-x3
tem2=abs(pos_temp[1][1]-pos_temp[2][1]);//y1-y3
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//得到1,4的距離
tem1=abs(pos_temp[0][0]-pos_temp[3][0]);//x2-x4
tem2=abs(pos_temp[0][1]-pos_temp[3][1]);//y2-y4
tem1*=tem1;
tem2*=tem2;
d2=sqrt(tem1+tem2);//得到2,3的距離
fac=(float)d1/d2;
if(fac<0.95||fac>1.05)//不合格
{
cnt=0;
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20);
continue;
}//正確了
//計(jì)算結(jié)果
Pen_Point.xfac=(float)200/(pos_temp[1][0]-pos_temp[0][0]);//得到xfac
Pen_Point.xoff=(240-Pen_Point.xfac*(pos_temp[1][0]+pos_temp[0][0]))/2;//得到xoff
Pen_Point.yfac=(float)280/(pos_temp[2][1]-pos_temp[0][1]);//得到y(tǒng)fac
Pen_Point.yoff=(320-Pen_Point.yfac*(pos_temp[2][1]+pos_temp[0][1]))/2;//得到y(tǒng)off
POINT_COLOR=BLUE;
LCD_Clear(WHITE);//清屏
LCD_ShowString(35,110,"Touch Screen Adjust OK!");//校正完成
delay_ms(1000);
LCD_Clear(WHITE);//清屏
return;//校正完成
}
}
}
}
//外部中斷初始化函數(shù)
void Touch_Init(void)
{
//注意,時(shí)鐘使能之后,對(duì)GPIO的操作才有效
//所以上拉之前,必須使能時(shí)鐘.才能實(shí)現(xiàn)真正的上拉輸出
RCC->APB2ENR|=1<<4; //PC時(shí)鐘使能
RCC->APB2ENR|=1<<0; //開啟輔助時(shí)鐘
GPIOC->CRL&=0XFFFF0000;//PC0~3
GPIOC->CRL|=0X00003883;
GPIOC->CRH&=0XFF0FFFFF;//PC13
GPIOC->CRH|=0X00300000;//PC13推挽輸出
GPIOC->ODR|=0X200f; //PC0~3 13 全部上拉
Read_ADS(&Pen_Point.X,&Pen_Point.Y);//第一次讀取初始化
MY_NVIC_Init(2,0,EXTI1_IRQChannel,2);
RCC->APB2ENR|=0x01; //使能io復(fù)用時(shí)鐘
AFIO->EXTICR[0]|=0X0020; //EXTI13映射到PC1
EXTI->IMR|=1<<1; //開啟line1上的中斷
EXTI->EMR|=1<<1; //不屏蔽line1上的事件
EXTI->FTSR|=1<<1; //line1上事件下降沿觸發(fā)
#ifdef ADJ_SAVE_ENABLE
AT24CXX_Init();//初始化24CXX
if(Get_Adjdata())return;//已經(jīng)校準(zhǔn)
else //未校準(zhǔn)?
{
LCD_Clear(WHITE);//清屏
Touch_Adjust(); //屏幕校準(zhǔn)
Save_Adjdata();
}
Get_Adjdata();
#else
LCD_Clear(WHITE);//清屏
Touch_Adjust(); //屏幕校準(zhǔn),帶自動(dòng)保存
#endif
// printf("Pen_Point.xfac:%f\n",Pen_Point.xfac);
// printf("Pen_Point.yfac:%f\n",Pen_Point.yfac);
// printf("Pen_Point.xoff:%d\n",Pen_Point.xoff);
// printf("Pen_Point.yoff:%d\n",Pen_Point.yoff);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -