0.png (42.34 KB, 下載次數: 133)
下載附件
2017-7-26 17:54 上傳
附件兩個代碼為xpt2046.c和xpt2046.h,是常用的電阻觸摸屏驅動
所有資料51hei提供下載:
xpt2046.zip
(4.65 KB, 下載次數: 197)
2017-7-26 17:54 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
單片機源程序如下:
- #include "xpt2046.h"
- #include "ili93xx.h"
- #include "stdlib.h"
- #include "math.h"
- Pen_Holder Pen_Point;//定義筆實體
- void delay_us(u32 us)
- {
- u32 time=100*us/7;
- while(--time);
- }
- //SPI寫數據
- //向7846/7843/XPT2046/UH7843/UH7846寫入1byte數據
- 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讀數據
- //從7846/7843/XPT2046/UH7843/UH7846讀取adc值
- u16 ADS_Read_AD(u8 CMD)
- {
- u8 count=0;
- u16 Num=0;
-
- Clr_TOUCH_TCLK;//先拉低時鐘
- Clr_TOUCH_TCS; //選中ADS7843
- ADS_Write_Byte(CMD);//發送命令字
- delay_us(6);//ADS7846的轉換時間最長為6us
- Set_TOUCH_TCLK;//給1個時鐘,清除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);
- }
- //讀取一個坐標值
- //連續讀取READ_TIMES次數據,對這些數據升序排列,
- //然后去掉最低和最高LOST_VAL個數,取平均值
- #define READ_TIMES 15 //讀取次數
- #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;
- }
- //帶濾波的坐標讀取
- //最小值不能少于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;//讀數失敗
- *x=xtemp;
- *y=ytemp;
- return 1;//讀數成功
- }
- //2次讀取ADS7846,連續讀取2次有效的AD值,且這兩次的偏差不能超過
- //50,滿足條件,則認為讀數正確,否則讀數錯誤.
- //該函數能大大提高準確度
- #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內
- &&((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;
- }
- }
- //讀取一次坐標值
- //僅僅讀取一次,知道PEN松開才返回!
- u8 Read_TP_Once(void)
- {
- u8 t=0;
- Pen_Int_Set(0);//關閉中斷
- 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 認為無效
- else
- return 1;
- }
- /**************************************與LCD部分有關的函數****************************************/
- //畫一個觸摸點
- //用來校準用的
- 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);//畫中心圈
- }
- //畫一個大點
- //2*2的點
- void Draw_Big_Point(u8 x,u16 y)
- {
- LCD_DrawPoint(x,y);//中心點
- LCD_DrawPoint(x+1,y);
- LCD_DrawPoint(x,y+1);
- LCD_DrawPoint(x+1,y+1);
- }
- /***************************************************************************************************/
- //轉換結果
- //根據觸摸屏的校準參數來決定轉換后的結果,保存在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中斷設置
- //啟用一個外部中斷當觸摸屏被按下后出發外部中斷
- //外部中斷函數:
- // 檢測PEN腳的一個下降沿
- // void EXTI1_IRQHandler(void)
- // {
- // Pen_Point.Key_Sta=Key_Down;//按鍵按下
- // EXTI_ClearFlag(EXTI_Line1);//清除LINE1上的中斷標志位
- // }
- 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; //設外外部中斷模式:EXTI線路為中斷請求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部中斷觸發沿選擇:設置輸入線路下降沿為中斷請求
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能外部中斷新狀態
- EXTI_Init(&EXTI_InitStructure); //根據EXTI_InitStruct中指定的參數初始化外設EXTI寄存器
- }else{
- EXTI_InitStructure.EXTI_Line = EXTI_Line1; //外部線路EXIT1
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //設外外部中斷模式:EXTI線路為中斷請求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部中斷觸發沿選擇:設置輸入線路下降沿為中斷請求
- EXTI_InitStructure.EXTI_LineCmd = DISABLE; //關閉外部中斷新狀態
- EXTI_Init(&EXTI_InitStructure); //根據EXTI_InitStruct中指定的參數初始化外設EXTI寄存器
- }
-
- }
- //觸摸屏校準代碼
- //得到四個校準參數
- void Touch_Adjust(void)
- {
- u16 pos_temp[4][2];//坐標緩存值
- 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);//畫點1
- Pen_Point.Key_Sta=Key_Up;//消除觸發信號
- Pen_Point.xfac=0;//xfac用來標記是否校準過,所以校準之前必須清掉!以免錯誤
- 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);//畫點2
- break;
- case 2:
- LCD_Clear(WHITE);//清屏
- Drow_Touch_Point(20,300);//畫點3
- break;
- case 3:
- LCD_Clear(WHITE);//清屏
- Drow_Touch_Point(220,300);//畫點4
- break;
- case 4: //全部四個點已經得到
- //對邊相等
- 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;
- }//正確了
-
- //對角線相等
- 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)//不合格
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
|