复制
收藏
提问
全网

#include "touch.h" #include "lcd.h" #include "delay.h" #include "stdlib.h" #include "math.h" #include "24cxx.h" ////////////////////////////////////////////////////////////////////////////////// _m_tp_dev tp_dev= { TP_Init, TP_Scan, TP_Adjust, 0, 0, 0, 0, 0, 0, 0, 0, }; //ĬÈÏΪtouchtype=0µÄÊý¾Ý. u8 CMD_RDX=0XD0; u8 CMD_RDY=0X90; //SPIдÊý¾Ý //Ïò´¥ÃþÆÁICдÈë1byteÊý¾Ý //num:ҪдÈëµÄÊý¾Ý void TP_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¶ÁÊý¾Ý //´Ó´¥ÃþÆÁIC¶ÁÈ¡adcÖµ //CMD:Ö¸Áî //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý u16 TP_Read_AD(u8 CMD) { u8 count=0; u16 Num=0; TCLK=0; //ÏÈÀ­µÍʱÖÓ TDIN=0; //À­µÍÊý¾ÝÏß TCS=0; //Ñ¡Öд¥ÃþÆÁIC TP_Write_Byte(CMD);//·¢ËÍÃüÁî×Ö delay_us(6);//ADS7846µÄת»»Ê±¼ä×Ϊ6us TCLK=0; delay_us(1); TCLK=1; //¸ø1¸öʱÖÓ£¬Çå³ýBUSY TCLK=0; for(count=0;count<16;count++)//¶Á³ö16λÊý¾Ý,Ö»Óиß12λÓÐЧ { Num<<=1; TCLK=0; //ϽµÑØÓÐЧ TCLK=1; if(DOUT)Num++; } Num>>=4; //Ö»Óиß12λÓÐЧ. TCS=1; //ÊÍ·ÅƬѡ return(Num); } //¶ÁÈ¡Ò»¸ö×ø±êÖµ(x»òÕßy) //Á¬Ðø¶ÁÈ¡READ_TIMES´ÎÊý¾Ý,¶ÔÕâЩÊý¾ÝÉýÐòÅÅÁÐ, //È»ºóÈ¥µô×îµÍºÍ×î¸ßLOST_VAL¸öÊý,ȡƽ¾ùÖµ //xy:Ö¸ÁCMD_RDX/CMD_RDY£© //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý #define READ_TIMES 5 //¶ÁÈ¡´ÎÊý #define LOST_VAL 1 //¶ªÆúÖµ u16 TP_Read_XOY(u8 xy) { u16 i, j; u16 buf[READ_TIMES]; u16 sum=0; u16 temp; for(i=0;i<READ_TIMES;i++)buf[i]=TP_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; } //¶ÁÈ¡x,y×ø±ê //×îСֵ²»ÄÜÉÙÓÚ100. //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ u8 TP_Read_XY(u16 *x,u16 *y) { u16 xtemp,ytemp; xtemp=TP_Read_XOY(CMD_RDX); ytemp=TP_Read_XOY(CMD_RDY); //if(xtemp<100||ytemp<100)return 0;//¶ÁÊýʧ°Ü *x=xtemp; *y=ytemp; return 1;//¶ÁÊý³É¹¦ } //Á¬Ðø2´Î¶ÁÈ¡´¥ÃþÆÁIC,ÇÒÕâÁ½´ÎµÄÆ«²î²»Äܳ¬¹ý //ERR_RANGE,Âú×ãÌõ¼þ,ÔòÈÏΪ¶ÁÊýÕýÈ·,·ñÔò¶ÁÊý´íÎó. //¸Ãº¯ÊýÄÜ´ó´óÌá¸ß׼ȷ¶È //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ #define ERR_RANGE 50 //Îó²î·¶Î§ u8 TP_Read_XY2(u16 *x,u16 *y) { u16 x1,y1; u16 x2,y2; u8 flag; flag=TP_Read_XY(&x1,&y1); if(flag==0)return(0); flag=TP_Read_XY(&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; } ////////////////////////////////////////////////////////////////////////////////// //ÓëLCD²¿·ÖÓйصĺ¯Êý //»­Ò»¸ö´¥Ãþµã //ÓÃÀ´Ð£×¼ÓÃµÄ //x,y:×ø±ê //color:ÑÕÉ« void TP_Drow_Touch_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; 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); LCD_Draw_Circle(x,y,6);//»­ÖÐÐÄȦ } //»­Ò»¸ö´óµã(2*2µÄµã) //x,y:×ø±ê //color:ÑÕÉ« void TP_Draw_Big_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; LCD_DrawPoint(x,y);//ÖÐÐĵã LCD_DrawPoint(x+1,y); LCD_DrawPoint(x,y+1); LCD_DrawPoint(x+1,y+1); } ////////////////////////////////////////////////////////////////////////////////// //´¥Ãþ°´¼üɨÃè //tp:0,ÆÁÄ»×ø±ê;1,ÎïÀí×ø±ê(У׼µÈÌØÊⳡºÏÓÃ) //·µ»ØÖµ:µ±Ç°´¥ÆÁ״̬. //0,´¥ÆÁÎÞ´¥Ãþ;1,´¥ÆÁÓд¥Ãþ u8 TP_Scan(u8 tp) { if(PEN==0)//Óа´¼ü°´Ï { if(tp)TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]);//¶ÁÈ¡ÎïÀí×ø±ê else if(TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]))//¶ÁÈ¡ÆÁÄ»×ø±ê { tp_dev.x[0]=tp_dev.xfac*tp_dev.x[0]+tp_dev.xoff;//½«½á¹ûת»»ÎªÆÁÄ»×ø±ê tp_dev.y[0]=tp_dev.yfac*tp_dev.y[0]+tp_dev.yoff; } if((tp_dev.sta&

63d1c1573
3个月前
全网
文档
学术
百科
知识库
时间不限
自动

理解问题#include "touch.h" #include "lcd.h" #include "delay.h" #include "stdlib.h" #include "math.h" #include "24cxx.h" ////////////////////////////////////////////////////////////////////////////////// _m_tp_dev tp_dev= { TP_Init, TP_Scan, TP_Adjust, 0, 0, 0, 0, 0, 0, 0, 0, }; //ĬÈÏΪtouchtype=0µÄÊý¾Ý. u8 CMD_RDX=0XD0; u8 CMD_RDY=0X90; //SPIдÊý¾Ý //Ïò´¥ÃþÆÁICдÈë1byteÊý¾Ý //num:ҪдÈëµÄÊý¾Ý void TP_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¶ÁÊý¾Ý //´Ó´¥ÃþÆÁIC¶ÁÈ¡adcÖµ //CMD:Ö¸Áî //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý u16 TP_Read_AD(u8 CMD) { u8 count=0; u16 Num=0; TCLK=0; //ÏÈÀ­µÍʱÖÓ TDIN=0; //À­µÍÊý¾ÝÏß TCS=0; //Ñ¡Öд¥ÃþÆÁIC TP_Write_Byte(CMD);//·¢ËÍÃüÁî×Ö delay_us(6);//ADS7846µÄת»»Ê±¼ä×Ϊ6us TCLK=0; delay_us(1); TCLK=1; //¸ø1¸öʱÖÓ£¬Çå³ýBUSY TCLK=0; for(count=0;count<16;count++)//¶Á³ö16λÊý¾Ý,Ö»Óиß12λÓÐЧ { Num<<=1; TCLK=0; //ϽµÑØÓÐЧ TCLK=1; if(DOUT)Num++; } Num>>=4; //Ö»Óиß12λÓÐЧ. TCS=1; //ÊÍ·ÅƬѡ return(Num); } //¶ÁÈ¡Ò»¸ö×ø±êÖµ(x»òÕßy) //Á¬Ðø¶ÁÈ¡READ_TIMES´ÎÊý¾Ý,¶ÔÕâЩÊý¾ÝÉýÐòÅÅÁÐ, //È»ºóÈ¥µô×îµÍºÍ×î¸ßLOST_VAL¸öÊý,ȡƽ¾ùÖµ //xy:Ö¸ÁCMD_RDX/CMD_RDY£© //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý #define READ_TIMES 5 //¶ÁÈ¡´ÎÊý #define LOST_VAL 1 //¶ªÆúÖµ u16 TP_Read_XOY(u8 xy) { u16 i, j; u16 buf[READ_TIMES]; u16 sum=0; u16 temp; for(i=0;i<READ_TIMES;i++)buf[i]=TP_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; } //¶ÁÈ¡x,y×ø±ê //×îСֵ²»ÄÜÉÙÓÚ100. //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ u8 TP_Read_XY(u16 *x,u16 *y) { u16 xtemp,ytemp; xtemp=TP_Read_XOY(CMD_RDX); ytemp=TP_Read_XOY(CMD_RDY); //if(xtemp<100||ytemp<100)return 0;//¶ÁÊýʧ°Ü *x=xtemp; *y=ytemp; return 1;//¶ÁÊý³É¹¦ } //Á¬Ðø2´Î¶ÁÈ¡´¥ÃþÆÁIC,ÇÒÕâÁ½´ÎµÄÆ«²î²»Äܳ¬¹ý //ERR_RANGE,Âú×ãÌõ¼þ,ÔòÈÏΪ¶ÁÊýÕýÈ·,·ñÔò¶ÁÊý´íÎó. //¸Ãº¯ÊýÄÜ´ó´óÌá¸ß׼ȷ¶È //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ #define ERR_RANGE 50 //Îó²î·¶Î§ u8 TP_Read_XY2(u16 *x,u16 *y) { u16 x1,y1; u16 x2,y2; u8 flag; flag=TP_Read_XY(&x1,&y1); if(flag==0)return(0); flag=TP_Read_XY(&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; } ////////////////////////////////////////////////////////////////////////////////// //ÓëLCD²¿·ÖÓйصĺ¯Êý //»­Ò»¸ö´¥Ãþµã //ÓÃÀ´Ð£×¼ÓÃµÄ //x,y:×ø±ê //color:ÑÕÉ« void TP_Drow_Touch_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; 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); LCD_Draw_Circle(x,y,6);//»­ÖÐÐÄȦ } //»­Ò»¸ö´óµã(2*2µÄµã) //x,y:×ø±ê //color:ÑÕÉ« void TP_Draw_Big_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; LCD_DrawPoint(x,y);//ÖÐÐĵã LCD_DrawPoint(x+1,y); LCD_DrawPoint(x,y+1); LCD_DrawPoint(x+1,y+1); } ////////////////////////////////////////////////////////////////////////////////// //´¥Ãþ°´¼üɨÃè //tp:0,ÆÁÄ»×ø±ê;1,ÎïÀí×ø±ê(У׼µÈÌØÊⳡºÏÓÃ) //·µ»ØÖµ:µ±Ç°´¥ÆÁ״̬. //0,´¥ÆÁÎÞ´¥Ãþ;1,´¥ÆÁÓд¥Ãþ u8 TP_Scan(u8 tp) { if(PEN==0)//Óа´¼ü°´Ï { if(tp)TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]);//¶ÁÈ¡ÎïÀí×ø±ê else if(TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]))//¶ÁÈ¡ÆÁÄ»×ø±ê { tp_dev.x[0]=tp_dev.xfac*tp_dev.x[0]+tp_dev.xoff;//½«½á¹ûת»»ÎªÆÁÄ»×ø±ê tp_dev.y[0]=tp_dev.yfac*tp_dev.y[0]+tp_dev.yoff; } if((tp_dev.sta&

已完成理解#include "touch.h" #include "lcd.h" #include "delay.h" #include "stdlib.h" #include "math.h" #include "24cxx.h" ////////////////////////////////////////////////////////////////////////////////// _m_tp_dev tp_dev= { TP_Init, TP_Scan, TP_Adjust, 0, 0, 0, 0, 0, 0, 0, 0, }; //ĬÈÏΪtouchtype=0µÄÊý¾Ý. u8 CMD_RDX=0XD0; u8 CMD_RDY=0X90; //SPIдÊý¾Ý //Ïò´¥ÃþÆÁICдÈë1byteÊý¾Ý //num:ҪдÈëµÄÊý¾Ý void TP_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¶ÁÊý¾Ý //´Ó´¥ÃþÆÁIC¶ÁÈ¡adcÖµ //CMD:Ö¸Áî //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý u16 TP_Read_AD(u8 CMD) { u8 count=0; u16 Num=0; TCLK=0; //ÏÈÀ­µÍʱÖÓ TDIN=0; //À­µÍÊý¾ÝÏß TCS=0; //Ñ¡Öд¥ÃþÆÁIC TP_Write_Byte(CMD);//·¢ËÍÃüÁî×Ö delay_us(6);//ADS7846µÄת»»Ê±¼ä×Ϊ6us TCLK=0; delay_us(1); TCLK=1; //¸ø1¸öʱÖÓ£¬Çå³ýBUSY TCLK=0; for(count=0;count<16;count++)//¶Á³ö16λÊý¾Ý,Ö»Óиß12λÓÐЧ { Num<<=1; TCLK=0; //ϽµÑØÓÐЧ TCLK=1; if(DOUT)Num++; } Num>>=4; //Ö»Óиß12λÓÐЧ. TCS=1; //ÊÍ·ÅƬѡ return(Num); } //¶ÁÈ¡Ò»¸ö×ø±êÖµ(x»òÕßy) //Á¬Ðø¶ÁÈ¡READ_TIMES´ÎÊý¾Ý,¶ÔÕâЩÊý¾ÝÉýÐòÅÅÁÐ, //È»ºóÈ¥µô×îµÍºÍ×î¸ßLOST_VAL¸öÊý,ȡƽ¾ùÖµ //xy:Ö¸ÁCMD_RDX/CMD_RDY£© //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý #define READ_TIMES 5 //¶ÁÈ¡´ÎÊý #define LOST_VAL 1 //¶ªÆúÖµ u16 TP_Read_XOY(u8 xy) { u16 i, j; u16 buf[READ_TIMES]; u16 sum=0; u16 temp; for(i=0;i<READ_TIMES;i++)buf[i]=TP_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; } //¶ÁÈ¡x,y×ø±ê //×îСֵ²»ÄÜÉÙÓÚ100. //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ u8 TP_Read_XY(u16 *x,u16 *y) { u16 xtemp,ytemp; xtemp=TP_Read_XOY(CMD_RDX); ytemp=TP_Read_XOY(CMD_RDY); //if(xtemp<100||ytemp<100)return 0;//¶ÁÊýʧ°Ü *x=xtemp; *y=ytemp; return 1;//¶ÁÊý³É¹¦ } //Á¬Ðø2´Î¶ÁÈ¡´¥ÃþÆÁIC,ÇÒÕâÁ½´ÎµÄÆ«²î²»Äܳ¬¹ý //ERR_RANGE,Âú×ãÌõ¼þ,ÔòÈÏΪ¶ÁÊýÕýÈ·,·ñÔò¶ÁÊý´íÎó. //¸Ãº¯ÊýÄÜ´ó´óÌá¸ß׼ȷ¶È //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ #define ERR_RANGE 50 //Îó²î·¶Î§ u8 TP_Read_XY2(u16 *x,u16 *y) { u16 x1,y1; u16 x2,y2; u8 flag; flag=TP_Read_XY(&x1,&y1); if(flag==0)return(0); flag=TP_Read_XY(&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; } ////////////////////////////////////////////////////////////////////////////////// //ÓëLCD²¿·ÖÓйصĺ¯Êý //»­Ò»¸ö´¥Ãþµã //ÓÃÀ´Ð£×¼ÓÃµÄ //x,y:×ø±ê //color:ÑÕÉ« void TP_Drow_Touch_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; 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); LCD_Draw_Circle(x,y,6);//»­ÖÐÐÄȦ } //»­Ò»¸ö´óµã(2*2µÄµã) //x,y:×ø±ê //color:ÑÕÉ« void TP_Draw_Big_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; LCD_DrawPoint(x,y);//ÖÐÐĵã LCD_DrawPoint(x+1,y); LCD_DrawPoint(x,y+1); LCD_DrawPoint(x+1,y+1); } ////////////////////////////////////////////////////////////////////////////////// //´¥Ãþ°´¼üɨÃè //tp:0,ÆÁÄ»×ø±ê;1,ÎïÀí×ø±ê(У׼µÈÌØÊⳡºÏÓÃ) //·µ»ØÖµ:µ±Ç°´¥ÆÁ״̬. //0,´¥ÆÁÎÞ´¥Ãþ;1,´¥ÆÁÓд¥Ãþ u8 TP_Scan(u8 tp) { if(PEN==0)//Óа´¼ü°´Ï { if(tp)TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]);//¶ÁÈ¡ÎïÀí×ø±ê else if(TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]))//¶ÁÈ¡ÆÁÄ»×ø±ê { tp_dev.x[0]=tp_dev.xfac*tp_dev.x[0]+tp_dev.xoff;//½«½á¹ûת»»ÎªÆÁÄ»×ø±ê tp_dev.y[0]=tp_dev.yfac*tp_dev.y[0]+tp_dev.yoff; } if((tp_dev.sta&

展开阅读网页

这段代码是一个嵌入式系统中用于触摸屏操作的C语言程序。它包含了触摸屏初始化、读取触摸点坐标、以及在LCD上绘制触摸点的功能。下面是对代码的简要解释:

  1. 定义了一个触摸屏设备结构体 _m_tp_dev,包含了初始化、扫描、校准等函数指针和一些变量。

  2. 定义了两个宏 CMD_RDXCMD_RDY,用于读取触摸点的X和Y坐标。

  3. 定义了 TP_Write_Byte 函数,用于通过SPI协议向触摸屏发送命令。

  4. 定义了 TP_Read_AD 函数,用于从触摸屏读取模拟信号转换为数字信号的值。

  5. 定义了 TP_Read_XOY 函数,用于读取X或Y坐标的值,并通过多次读取取平均值来减少误差。

  6. 定义了 TP_Read_XY 函数,用于读取触摸点的X和Y坐标,如果坐标值小于100则认为没有触摸。

  7. 定义了 TP_Read_XY2 函数,用于两次读取触摸点的坐标,如果两次读取的坐标相近,则认为触摸有效。

  8. 定义了 TP_Drow_Touch_Point 函数,用于在LCD上绘制触摸点。

  9. 定义了 TP_Draw_Big_Point 函数,用于在LCD上绘制较大的触摸点。

  10. 定义了 TP_Scan 函数,用于扫描触摸点,如果检测到触摸则读取坐标,并进行坐标校准。

代码中有一些宏定义和函数调用没有给出,例如 PENLCD_DrawLineLCD_DrawPointLCD_Draw_Circle 等,这些可能是在其他头文件或源文件中定义的。

此外,代码中有一些注释是用中文写的,这对于理解代码的功能和逻辑很有帮助。但是,代码的最后一部分被截断了,可能是因为复制粘贴时出现了问题。如果需要完整的代码分析或帮助,请提供完整的代码。

更专业一些
转笔记
专业
#include "touch.h" #include "lcd.h" #include "delay.h" #include "stdlib.h" #include "math.h" #include "24cxx.h" ////////////////////////////////////////////////////////////////////////////////// _m_tp_dev tp_dev= { TP_Init, TP_Scan, TP_Adjust, 0, 0, 0, 0, 0, 0, 0, 0, }; //ĬÈÏΪtouchtype=0µÄÊý¾Ý. u8 CMD_RDX=0XD0; u8 CMD_RDY=0X90; //SPIдÊý¾Ý //Ïò´¥ÃþÆÁICдÈë1byteÊý¾Ý //num:ҪдÈëµÄÊý¾Ý void TP_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¶ÁÊý¾Ý //´Ó´¥ÃþÆÁIC¶ÁÈ¡adcÖµ //CMD:Ö¸Áî //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý u16 TP_Read_AD(u8 CMD) { u8 count=0; u16 Num=0; TCLK=0; //ÏÈÀ­µÍʱÖÓ TDIN=0; //À­µÍÊý¾ÝÏß TCS=0; //Ñ¡Öд¥ÃþÆÁIC TP_Write_Byte(CMD);//·¢ËÍÃüÁî×Ö delay_us(6);//ADS7846µÄת»»Ê±¼ä×Ϊ6us TCLK=0; delay_us(1); TCLK=1; //¸ø1¸öʱÖÓ£¬Çå³ýBUSY TCLK=0; for(count=0;count<16;count++)//¶Á³ö16λÊý¾Ý,Ö»Óиß12λÓÐЧ { Num<<=1; TCLK=0; //ϽµÑØÓÐЧ TCLK=1; if(DOUT)Num++; } Num>>=4; //Ö»Óиß12λÓÐЧ. TCS=1; //ÊÍ·ÅƬѡ return(Num); } //¶ÁÈ¡Ò»¸ö×ø±êÖµ(x»òÕßy) //Á¬Ðø¶ÁÈ¡READ_TIMES´ÎÊý¾Ý,¶ÔÕâЩÊý¾ÝÉýÐòÅÅÁÐ, //È»ºóÈ¥µô×îµÍºÍ×î¸ßLOST_VAL¸öÊý,ȡƽ¾ùÖµ //xy:Ö¸ÁCMD_RDX/CMD_RDY£© //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý #define READ_TIMES 5 //¶ÁÈ¡´ÎÊý #define LOST_VAL 1 //¶ªÆúÖµ u16 TP_Read_XOY(u8 xy) { u16 i, j; u16 buf[READ_TIMES]; u16 sum=0; u16 temp; for(i=0;i<READ_TIMES;i++)buf[i]=TP_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; } //¶ÁÈ¡x,y×ø±ê //×îСֵ²»ÄÜÉÙÓÚ100. //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ u8 TP_Read_XY(u16 *x,u16 *y) { u16 xtemp,ytemp; xtemp=TP_Read_XOY(CMD_RDX); ytemp=TP_Read_XOY(CMD_RDY); //if(xtemp<100||ytemp<100)return 0;//¶ÁÊýʧ°Ü *x=xtemp; *y=ytemp; return 1;//¶ÁÊý³É¹¦ } //Á¬Ðø2´Î¶ÁÈ¡´¥ÃþÆÁIC,ÇÒÕâÁ½´ÎµÄÆ«²î²»Äܳ¬¹ý //ERR_RANGE,Âú×ãÌõ¼þ,ÔòÈÏΪ¶ÁÊýÕýÈ·,·ñÔò¶ÁÊý´íÎó. //¸Ãº¯ÊýÄÜ´ó´óÌá¸ß׼ȷ¶È //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ #define ERR_RANGE 50 //Îó²î·¶Î§ u8 TP_Read_XY2(u16 *x,u16 *y) { u16 x1,y1; u16 x2,y2; u8 flag; flag=TP_Read_XY(&x1,&y1); if(flag==0)return(0); flag=TP_Read_XY(&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; } ////////////////////////////////////////////////////////////////////////////////// //ÓëLCD²¿·ÖÓйصĺ¯Êý //»­Ò»¸ö´¥Ãþµã //ÓÃÀ´Ð£×¼ÓÃµÄ //x,y:×ø±ê //color:ÑÕÉ« void TP_Drow_Touch_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; 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); LCD_Draw_Circle(x,y,6);//»­ÖÐÐÄȦ } //»­Ò»¸ö´óµã(2*2µÄµã) //x,y:×ø±ê //color:ÑÕÉ« void TP_Draw_Big_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; LCD_DrawPoint(x,y);//ÖÐÐĵã LCD_DrawPoint(x+1,y); LCD_DrawPoint(x,y+1); LCD_DrawPoint(x+1,y+1); } ////////////////////////////////////////////////////////////////////////////////// //´¥Ãþ°´¼üɨÃè //tp:0,ÆÁÄ»×ø±ê;1,ÎïÀí×ø±ê(У׼µÈÌØÊⳡºÏÓÃ) //·µ»ØÖµ:µ±Ç°´¥ÆÁ״̬. //0,´¥ÆÁÎÞ´¥Ãþ;1,´¥ÆÁÓд¥Ãþ u8 TP_Scan(u8 tp) { if(PEN==0)//Óа´¼ü°´Ï { if(tp)TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]);//¶ÁÈ¡ÎïÀí×ø±ê else if(TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]))//¶ÁÈ¡ÆÁÄ»×ø±ê { tp_dev.x[0]=tp_dev.xfac*tp_dev.x[0]+tp_dev.xoff;//½«½á¹ûת»»ÎªÆÁÄ»×ø±ê tp_dev.y[0]=tp_dev.yfac*tp_dev.y[0]+tp_dev.yoff; } if((tp_dev.sta&
不在提醒

更专业内容正在努力生成中
知识树
#include "touch.h" #include "lcd.h" #include "delay.h" #include "stdlib.h" #include "math.h" #include "24cxx.h" ////////////////////////////////////////////////////////////////////////////////// _m_tp_dev tp_dev= { TP_Init, TP_Scan, TP_Adjust, 0, 0, 0, 0, 0, 0, 0, 0, }; //ĬÈÏΪtouchtype=0µÄÊý¾Ý. u8 CMD_RDX=0XD0; u8 CMD_RDY=0X90; //SPIдÊý¾Ý //Ïò´¥ÃþÆÁICдÈë1byteÊý¾Ý //num:ҪдÈëµÄÊý¾Ý void TP_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¶ÁÊý¾Ý //´Ó´¥ÃþÆÁIC¶ÁÈ¡adcÖµ //CMD:Ö¸Áî //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý u16 TP_Read_AD(u8 CMD) { u8 count=0; u16 Num=0; TCLK=0; //ÏÈÀ­µÍʱÖÓ TDIN=0; //À­µÍÊý¾ÝÏß TCS=0; //Ñ¡Öд¥ÃþÆÁIC TP_Write_Byte(CMD);//·¢ËÍÃüÁî×Ö delay_us(6);//ADS7846µÄת»»Ê±¼ä×Ϊ6us TCLK=0; delay_us(1); TCLK=1; //¸ø1¸öʱÖÓ£¬Çå³ýBUSY TCLK=0; for(count=0;count<16;count++)//¶Á³ö16λÊý¾Ý,Ö»Óиß12λÓÐЧ { Num<<=1; TCLK=0; //ϽµÑØÓÐЧ TCLK=1; if(DOUT)Num++; } Num>>=4; //Ö»Óиß12λÓÐЧ. TCS=1; //ÊÍ·ÅƬѡ return(Num); } //¶ÁÈ¡Ò»¸ö×ø±êÖµ(x»òÕßy) //Á¬Ðø¶ÁÈ¡READ_TIMES´ÎÊý¾Ý,¶ÔÕâЩÊý¾ÝÉýÐòÅÅÁÐ, //È»ºóÈ¥µô×îµÍºÍ×î¸ßLOST_VAL¸öÊý,ȡƽ¾ùÖµ //xy:Ö¸ÁCMD_RDX/CMD_RDY£© //·µ»ØÖµ:¶Áµ½µÄÊý¾Ý #define READ_TIMES 5 //¶ÁÈ¡´ÎÊý #define LOST_VAL 1 //¶ªÆúÖµ u16 TP_Read_XOY(u8 xy) { u16 i, j; u16 buf[READ_TIMES]; u16 sum=0; u16 temp; for(i=0;i<READ_TIMES;i++)buf[i]=TP_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; } //¶ÁÈ¡x,y×ø±ê //×îСֵ²»ÄÜÉÙÓÚ100. //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ u8 TP_Read_XY(u16 *x,u16 *y) { u16 xtemp,ytemp; xtemp=TP_Read_XOY(CMD_RDX); ytemp=TP_Read_XOY(CMD_RDY); //if(xtemp<100||ytemp<100)return 0;//¶ÁÊýʧ°Ü *x=xtemp; *y=ytemp; return 1;//¶ÁÊý³É¹¦ } //Á¬Ðø2´Î¶ÁÈ¡´¥ÃþÆÁIC,ÇÒÕâÁ½´ÎµÄÆ«²î²»Äܳ¬¹ý //ERR_RANGE,Âú×ãÌõ¼þ,ÔòÈÏΪ¶ÁÊýÕýÈ·,·ñÔò¶ÁÊý´íÎó. //¸Ãº¯ÊýÄÜ´ó´óÌá¸ß׼ȷ¶È //x,y:¶ÁÈ¡µ½µÄ×ø±êÖµ //·µ»ØÖµ:0,ʧ°Ü;1,³É¹¦¡£ #define ERR_RANGE 50 //Îó²î·¶Î§ u8 TP_Read_XY2(u16 *x,u16 *y) { u16 x1,y1; u16 x2,y2; u8 flag; flag=TP_Read_XY(&x1,&y1); if(flag==0)return(0); flag=TP_Read_XY(&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; } ////////////////////////////////////////////////////////////////////////////////// //ÓëLCD²¿·ÖÓйصĺ¯Êý //»­Ò»¸ö´¥Ãþµã //ÓÃÀ´Ð£×¼ÓÃµÄ //x,y:×ø±ê //color:ÑÕÉ« void TP_Drow_Touch_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; 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); LCD_Draw_Circle(x,y,6);//»­ÖÐÐÄȦ } //»­Ò»¸ö´óµã(2*2µÄµã) //x,y:×ø±ê //color:ÑÕÉ« void TP_Draw_Big_Point(u16 x,u16 y,u16 color) { POINT_COLOR=color; LCD_DrawPoint(x,y);//ÖÐÐĵã LCD_DrawPoint(x+1,y); LCD_DrawPoint(x,y+1); LCD_DrawPoint(x+1,y+1); } ////////////////////////////////////////////////////////////////////////////////// //´¥Ãþ°´¼üɨÃè //tp:0,ÆÁÄ»×ø±ê;1,ÎïÀí×ø±ê(У׼µÈÌØÊⳡºÏÓÃ) //·µ»ØÖµ:µ±Ç°´¥ÆÁ״̬. //0,´¥ÆÁÎÞ´¥Ãþ;1,´¥ÆÁÓд¥Ãþ u8 TP_Scan(u8 tp) { if(PEN==0)//Óа´¼ü°´Ï { if(tp)TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]);//¶ÁÈ¡ÎïÀí×ø±ê else if(TP_Read_XY2(&tp_dev.x[0],&tp_dev.y[0]))//¶ÁÈ¡ÆÁÄ»×ø±ê { tp_dev.x[0]=tp_dev.xfac*tp_dev.x[0]+tp_dev.xoff;//½«½á¹ûת»»ÎªÆÁÄ»×ø±ê tp_dev.y[0]=tp_dev.yfac*tp_dev.y[0]+tp_dev.yoff; } if((tp_dev.sta&
如何使用SPI通信读取触摸数据?
TP_Read_AD函数的作用是什么?
如何通过LCD显示触摸点?
在线客服