|
0.png (42.34 KB, 下載次數(shù): 133)
下載附件
2017-7-26 17:54 上傳
附件兩個(gè)代碼為xpt2046.c和xpt2046.h,是常用的電阻觸摸屏驅(qū)動(dòng)
所有資料51hei提供下載:
xpt2046.zip
(4.65 KB, 下載次數(shù): 197)
2017-7-26 17:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
單片機(jī)源程序如下:
- #include "xpt2046.h"
- #include "ili93xx.h"
- #include "stdlib.h"
- #include "math.h"
- Pen_Holder Pen_Point;//定義筆實(shí)體
- void delay_us(u32 us)
- {
- u32 time=100*us/7;
- while(--time);
- }
- //SPI寫數(shù)據(jù)
- //向7846/7843/XPT2046/UH7843/UH7846寫入1byte數(shù)據(jù)
- void ADS_Write_Byte(u8 num)
- {
- u8 count=0;
- for(count=0;count<8;count++)
- {
- if(num&0x80)
- Set_TOUCH_TDIN
- else
- Clr_TOUCH_TDIN;
- num<<=1;
- Clr_TOUCH_TCLK;//上升沿有效
- Set_TOUCH_TCLK;
- }
- }
- //SPI讀數(shù)據(jù)
- //從7846/7843/XPT2046/UH7843/UH7846讀取adc值
- u16 ADS_Read_AD(u8 CMD)
- {
- u8 count=0;
- u16 Num=0;
-
- Clr_TOUCH_TCLK;//先拉低時(shí)鐘
- Clr_TOUCH_TCS; //選中ADS7843
- ADS_Write_Byte(CMD);//發(fā)送命令字
- delay_us(6);//ADS7846的轉(zhuǎn)換時(shí)間最長(zhǎng)為6us
- Set_TOUCH_TCLK;//給1個(gè)時(shí)鐘,清除BUSY
- Clr_TOUCH_TCLK;
-
- for(count=0;count<16;count++)
- {
- Num<<=1;
- Clr_TOUCH_TCLK;//下降沿有效
- Set_TOUCH_TCLK;
- if(TOUCH_DOUT)
- Num++;
- }
-
- Num>>=4; //只有高12位有效.
- Set_TOUCH_TCS;//釋放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值,且這兩次的偏差不能超過(guò)
- //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(TOUCH_PEN==0&&t<=250)
- {
- t++;
- delay_us(10000);
- };
- Pen_Int_Set(1);//開啟中斷
- if(t>=250)
- return 0;//按下2.5s 認(rèn)為無(wú)效
- else
- return 1;
- }
- /**************************************與LCD部分有關(guān)的函數(shù)****************************************/
- //畫一個(gè)觸摸點(diǎn)
- //用來(lái)校準(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ù)來(lái)決定轉(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;
- }
- }
- //PEN中斷設(shè)置
- //啟用一個(gè)外部中斷當(dāng)觸摸屏被按下后出發(fā)外部中斷
- //外部中斷函數(shù):
- // 檢測(cè)PEN腳的一個(gè)下降沿
- // void EXTI1_IRQHandler(void)
- // {
- // Pen_Point.Key_Sta=Key_Down;//按鍵按下
- // EXTI_ClearFlag(EXTI_Line1);//清除LINE1上的中斷標(biāo)志位
- // }
- void Pen_Int_Set(u8 en)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- if(en)
- {
- EXTI_InitStructure.EXTI_Line = EXTI_Line1; //外部線路EXIT1
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //設(shè)外外部中斷模式:EXTI線路為中斷請(qǐng)求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部中斷觸發(fā)沿選擇:設(shè)置輸入線路下降沿為中斷請(qǐng)求
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能外部中斷新狀態(tài)
- EXTI_Init(&EXTI_InitStructure); //根據(jù)EXTI_InitStruct中指定的參數(shù)初始化外設(shè)EXTI寄存器
- }else{
- EXTI_InitStructure.EXTI_Line = EXTI_Line1; //外部線路EXIT1
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //設(shè)外外部中斷模式:EXTI線路為中斷請(qǐng)求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部中斷觸發(fā)沿選擇:設(shè)置輸入線路下降沿為中斷請(qǐng)求
- EXTI_InitStructure.EXTI_LineCmd = DISABLE; //關(guān)閉外部中斷新狀態(tài)
- EXTI_Init(&EXTI_InitStructure); //根據(jù)EXTI_InitStruct中指定的參數(shù)初始化外設(shè)EXTI寄存器
- }
-
- }
- //觸摸屏校準(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用來(lái)標(biāo)記是否校準(zhǔn)過(guò),所以校準(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)//不合格
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
|
|