触摸屏实验.docx
《触摸屏实验.docx》由会员分享,可在线阅读,更多相关《触摸屏实验.docx(7页珍藏版)》请在冰点文库上搜索。
触摸屏实验
#include"sys.h"
#include"usart.h"
#include"delay.h"
#include"led.h"
#include"beep.h"
#include"key.h"
#include"exti.h"
#include"wdg.h"
#include"timer.h"
#include"tpad.h"
#include"oled.h"
#include"lcd.h"
#include"usmart.h"
#include"rtc.h"
#include"wkup.h"
#include"adc.h"
#include"dac.h"
#include"dma.h"
#include"24cxx.h"
#include"flash.h"
#include"rs485.h"
#include"can.h"
#include"touch.h"
voidLoad_Drow_Dialog(void)
{
LCD_Clear(WHITE);//清屏
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(lcddev.width-24,0,200,16,16,"RST");//显示清屏区域
POINT_COLOR=RED;//设置画笔蓝色
}
//////////////////////////////////////////////////////////////////////////////////电容触摸屏专有部分
//画水平线
//x0,y0:
坐标
//len:
线长度
//color:
颜色
voidgui_draw_hline(u16x0,u16y0,u16len,u16color)
{
if(len==0)return;
LCD_Fill(x0,y0,x0+len-1,y0,color);
}
//画实心圆
//x0,y0:
坐标
//r:
半径
//color:
颜色
voidgui_fill_circle(u16x0,u16y0,u16r,u16color)
{
u32i;
u32imax=((u32)r*707)/1000+1;
u32sqmax=(u32)r*(u32)r+(u32)r/2;
u32x=r;
gui_draw_hline(x0-r,y0,2*r,color);
for(i=1;i<=imax;i++)
{
if((i*i+x*x)>sqmax)//drawlinesfromoutside
{
if(x>imax)
{
gui_draw_hline(x0-i+1,y0+x,2*(i-1),color);
gui_draw_hline(x0-i+1,y0-x,2*(i-1),color);
}
x--;
}
//drawlinesfrominside(center)
gui_draw_hline(x0-x,y0+i,2*x,color);
gui_draw_hline(x0-x,y0-i,2*x,color);
}
}
//两个数之差的绝对值
//x1,x2:
需取差值的两个数
//返回值:
|x1-x2|
u16my_abs(u16x1,u16x2)
{
if(x1>x2)returnx1-x2;
elsereturnx2-x1;
}
//画一条粗线
//(x1,y1),(x2,y2):
线条的起始坐标
//size:
线条的粗细程度
//color:
线条的颜色
voidlcd_draw_bline(u16x1,u16y1,u16x2,u16y2,u8size,u16color){
u16t;
intxerr=0,yerr=0,delta_x,delta_y,distance;
intincx,incy,uRow,uCol;
if(x1delta_x=x2-x1;//计算坐标增量
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1;//设置单步方向
elseif(delta_x==0)incx=0;//垂直线
else{incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
elseif(delta_y==0)incy=0;//水平线
else{incy=-1;delta_y=-delta_y;}
if(delta_x>delta_y)distance=delta_x;//选取基本增量坐标轴elsedistance=delta_y;
for(t=0;t<=distance+1;t++)//画线输出
{
gui_fill_circle(uRow,uCol,size,color);//画点
xerr+=delta_x;
yerr+=delta_y;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
////////////////////////////////////////////////////////////////////////////////
//5个触控点的颜色
constu16
POINT_COLOR_TBL[OTT_MAX_TOUCH]={RED,GREEN,BLUE,BROWN,GRED};
//电阻触摸屏测试函数
voidrtp_test(void)
{
u8key;
u8i=0;
while
(1)
{
key=KEY_Scan(0);
tp_dev.scan(0);
if(tp_dev.sta&TP_PRES_DOWN)//触摸屏被按下
{
if(tp_dev.x[0]{
if(tp_dev.x[0]>(lcddev.width-
24)&&tp_dev.y[0]<16)Load_Drow_Dialog();//清除
elseTP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],RED);
//画图
}
}elsedelay_ms(10);//没有按键按下的时候
if(key==KEY_RIGHT)//KEY_RIGHT按下,则执行校准程序
{
LCD_Clear(WHITE);//清屏
TP_Adjust();//屏幕校准
TP_Save_Adjdata();
Load_Drow_Dialog();
}
i++;
if(i%20==0)LED0=!
LED0;
}
}
//电容触摸屏测试函数
voidctp_test(void)
{
u8t=0;
u8i=0;
u16lastpos[5][2];//最后一次的数据
while
(1)
{
tp_dev.scan(0);
for(t=0;t{
if((tp_dev.sta)&(1<{
if(tp_dev.x[t]{
if(lastpos[t][0]==0XFFFF)
{
lastpos[t][0]=tp_dev.x[t];
lastpos[t][1]=tp_dev.y[t];
}
lcd_draw_bline(lastpos[t][0],lastpos[t][1],tp_dev.x[t],tp_dev.y[t],2,POINT_COLOR_TBL[t]);//画线
lastpos[t][0]=tp_dev.x[t];
lastpos[t][1]=tp_dev.y[t];
if(tp_dev.x[t]>(lcddev.width-
24)&&tp_dev.y[t]<16)
{
Load_Drow_Dialog();//清除
}
}
}elselastpos[t][0]=0XFFFF;
}
delay_ms(5);i++;
if(i%20==0)LED0=!
LED0;
}
}
intmain(void)
{
Stm32_Clock_Init(9);//系统时钟设置
uart_init(72,9600);//串口初始化为9600
delay_init(72);//延时初始化
LED_Init();//初始化与LED连接的硬件接口
LCD_Init();//初始化LCD
usmart_dev.init(72);//初始化USMART
KEY_Init();//按键初始化
tp_dev.init();//触摸屏初始化
POINT_COLOR=RED;//设置字体为红色
LCD_ShowString(60,50,200,16,16,"WarShipSTM32");
LCD_ShowString(60,70,200,16,16,"TOUCHTEST");
LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(60,110,200,16,16,"2014/2/12");
if(tp_dev.touchtype!
=0XFF)LCD_ShowString(60,130,200,16,16,"PressKEY0toAdjust");//电阻屏才显示
delay_ms(1500);
Load_Drow_Dialog();
if(tp_dev.touchtype&0X80)ctp_test();//电容屏测试
elsertp_test();//电阻屏测试
}