📄 touch.c
字号:
/*-------------------------------------------------------------------*/
/* include files */
/*---------------------------------------------------------------- --*/
#include "..\..\com\lpc_lib_touch\touch.h"
//#include "..\..\com\lpc_lib_mylcd\ascii.h"
/*-------------------------------------------------------------------*/
/* extern function/variable declare */
/*-------------------------------------------------------------------*/
void irq_eint0_svr(void); //中断服务子程序
extern irq_eint0(void); //中断子程序的入口地址
/*-------------------------------------------------------------------*/
/* global variable define */
/*-------------------------------------------------------------------*/
/* An array of perfect input screen points used to obtain a first pass */
/* calibration matrix good enough to collect calibration samples. */
//用于校正的在液晶上的三个点。根据实际情况来定点
POINT perfectScreenSample[3] = {
{ 0,0 },
{ 0, 0 },
{ 0, 0 }
} ;
/* An array of perfect display points used to obtain a first pass */
/* calibration matrix good enough to collect calibration samples. */
//数据采集的那三个点对应的触摸屏的位置
POINT perfectDisplaySample[3] = {
{ 50, 50 },
{ 270, 50 },
{ 160, 190 }
} ;
//转换中要用到的数据
MATRIX matrix={-29480,550,7103290,0,-22540,5474000,21574};
MATRIX matrix_init={-29480,550,7103290,0,-22540,5474000,21574};
Rectangle_rang enter={0};
Rectangle_rang cancle={0};
//触摸屏点的位置
POINT touch_data={0,0};
//INT8U jiaoing=0;
INT8U count=0;
INT8U bit_jiao=1;
/*
***********************************************************************
函数声明
***********************************************************************
*/
/*
INT8U spi_data_receive_7843();
void spi_data_send_7843(INT8U data_send);
INT8U get_value(INT8U com);
void touch_init();
POINT touch_read();
POINT mytouch_get_key(void);
INT8U mytouch_moni_key();
*/
/*
********************************************************************
* name : io1_set
* func : 触摸屏的初始化,设置7843的各个端口,除了SPI口
* para : none
* ret : none
* modify:
* comment:
********************************************************************
*/
void io1_set(INT8U on_off)
{
if(on_off)
{
//P0.18
//set IO1_7843=1
IOSET0|=(0x01<<29);
}
else
{
//P0.18
//set IO1_7843=0
IOCLR0|=(0x01<<29);
}
}
/*
********************************************************************
* name : touch_init
* func : 触摸屏的初始化,设置7843的各个端口,除了SPI口
* para : none
* ret : none
* modify:
* comment:
********************************************************************
*/
void cs_set(INT8U on_off)
{
if(on_off)
{
//P0.27
//set cs_7843=1
IOSET0|=(0x01<<27);
}
else
{
//P0.27
//set cs_7843=0
IOCLR0|=(0x01<<27);
}
}
/*
********************************************************************
* name : touch_init
* func : 触摸屏的初始化,设置7843的各个端口,除了SPI口
* para : none
* ret : none
* modify:
* comment:
********************************************************************
*/
void touch_init()
{
INT32U i;
POINT data;
//设7843的BUSY,CS,IO1
i=PINSEL1;
i&=(~_7843_BUSY_CS_IO1_MASK);
i |= _7843_BUSY_CS_IO1;
PINSEL1 = i;
//cs->p0.27,io1->p0.29 为输出口,
IODIR0|=0x28000000;
//busy->p0.28为输入口
IODIR0&=0xefffffff;
//这0.16为输入口
IODIR0&=0xFFFEFFFF;
//P0.16为GPIO口
i=PINSEL1;
i&=(~P0_16_GPIO_MASK);
i |=P0_16_GPIO;
PINSEL1 = i;
//P0.16为EINT0口
i=PINSEL1;
i&=(~P0_16_EINT0_MASK);
i |=P0_16_EINT0;
PINSEL1 = i;
io1_set(1);
PINSEL1&=0xffffff3f;
IODIR0|=0x00800000;
IOCLR0|=0x00800000;
// for(i=0;i<20;i++);
cs_set(0);
spi_init(1, 1, 1);
SPI1_SPDR = 0x98;
while( 0==(SPI1_SPSR & 0x80) );
SPI1_SPDR=0x00;
while(0==(SPI1_SPSR & 0x80));
cs_set(1);
// while(IOPIN0 & 0x00010000 ==0);
//设置外部中断0地址等
EXTINT=0x01;
VICVectCntl0 = 0x2e; //14th channel 101110
VICVectAddr0 = (INT32U)irq_eint0;
VICIntEnable |= 0x00004000;
}
/*
********************************************************************
* name : irq_eint0_svr
* func : 外部中断0服务子程序
* para : none
* ret : none
* modify:
* comment:
********************************************************************
*/
void irq_eint0_svr(void)
{
INT8U j,data_receive;
INT32U i;
POINT touch1={0,0},touch2={0xff,0xff},lcd;
for(i=0;i<50;i++);
IOCLR0|=(0x01<<29);
IOCLR0|=(0x01<<27);
PINSEL1 &= 0xfffffffc;
IODIR0 |= 0x00010000;
IOCLR0 |= 0x1<<16;
spi_init(1, 1, 1);
while( (touch1.x!=touch2.x) && (touch1.y!=touch2.y) )
{
touch2=touch1;
SPI1_SPCR = SPCR_SPI_595;
SPI1_SPDR = 0x98;
while( 0==(SPI1_SPSR & 0x80) );
SPI1_SPCR = SPCR_SPI_5951;
SPI1_SPDR = 0x00;
while( 0==(SPI1_SPSR & 0x80) );
touch1.y=SPI1_SPDR;
SPI1_SPCR = SPCR_SPI_595;
SPI1_SPDR = 0xd8;
while( 0==(SPI1_SPSR & 0x80) );
SPI1_SPCR = SPCR_SPI_5951;
SPI1_SPDR = 0x00;
while( 0==(SPI1_SPSR & 0x80) );
touch1.x=SPI1_SPDR;
}
touch_data=touch2;
// mycls(0xe0);
getDisplayPoint1(&lcd,&touch_data,&matrix);
myLCD_display_mouse_1(lcd.x,lcd.y);
IOSET0|=(0x01<<29);
IOSET0 |= 0x1<<16;
// for(i=0;i<20;i++);
IODIR0 &= ~(0x1<<16);
// i=IOPIN0;
while(( IOPIN0 & 0x00010000 )==0);
i = PINSEL1;
i &= (~P0_16_EINT0_MASK);
i |=P0_16_EINT0;
PINSEL1 = i;
// while(IOPIN0 & 0x00010000 ==0);
for(i=0;i<1000;i++);
EXTINT=0x01; //清中断声明
VICVectAddr=0; //复位中断地址
IOSET0|=(0x01<<27);
}
/*
********************************************************************
* name : mytouch_get_key
* func : 用于给外部函数读触摸屏点的位置
* para : none
* ret : 触摸屏点的位置
* modify:
* comment:
********************************************************************
*/
POINT mytouch_get_key(void)
{
POINT data;
data=touch_data;
touch_data.x=0;
touch_data.y=0;
return data;
}
/*
********************************************************************
* name : mytouch_moni_key
* func : 判断是否按下触摸屏
* para : none
* ret : 是否按下触摸屏
* modify:
* comment:
********************************************************************
*/
INT8U mytouch_moni_key()
{
if(touch_data.x==0&&touch_data.y==0)
{
return 0;
}
else
{
touch_data.x=0;
touch_data.y=0;
return 1;
}
}
/*
*********************************************************************
* name : disp_x
* func : 用于显示校正点(x)的图标
* para : x:图标的x轴位置 ,y:图标y轴的位置 ,col:图标的颜色
* modify:
* comment:
*********************************************************************
*/
void disp_x(INT16U x,INT8U y,INT8U col)
{
INT16U x_x;
INT8U x_y;
//画叉的第一画 "\"
x_x=x-jiao_zheng_length/2;
x_y=y+jiao_zheng_length/2;
myposi_piexl_start(x_x,x_y);
x_x=x+jiao_zheng_length/2;
x_y=y-jiao_zheng_length/2;
myposi_piexl_end(x_x,x_y);
myLCD_line(col);
//画叉的第二画 "/"
x_x=x-jiao_zheng_length/2;
x_y=y-jiao_zheng_length/2;
myposi_piexl_start(x_x,x_y);
x_x=x+jiao_zheng_length/2;
x_y=y+jiao_zheng_length/2;
myposi_piexl_end(x_x,x_y);
myLCD_line(col);
}
/*
*********************************************************************
* name : conversion_touch_to_lcd
* func : 用于触摸屏的坐标转为液晶的坐标
* para : *touch->为要输入触摸屏的坐标点,*lcd->为要输出的液晶的坐标点
* modify:
* comment:
*********************************************************************
*/
void display_an(INT8U en_can)
{
Rectangle_rang xy;
if(en_can)
{
xy=enter;
}
else
{
xy=cancle;
}
myposi_piexl_start(xy.x_start,xy.y_start);
myposi_piexl_end(xy.x_end,xy.y_end);
if(en_can)
{
// myLCD_disp_picture(enter_data);
}
else
{
// myLCD_disp_picture(enter_cancle);
}
}
/*
*********************************************************************
* name : conversion_touch_to_lcd
* func : 用于触摸屏的坐标转为液晶的坐标
* para : *touch->为要输入触摸屏的坐标点,*lcd->为要输出的液晶的坐标点
* modify:
* comment:
*********************************************************************
*/
POINT getDisplayPoint(POINT touch)
{
POINT lcd;
getDisplayPoint1(&lcd,&touch,&matrix);
return lcd;
}
/*
*********************************************************************
* name : which_rectangle
* func : 看data是否在所在矩形的范围内
* para : 矩形范围为 x_start~x_end y_start~y_end data:为触摸点
的位置
* ret : 触摸点是否在规定位置上
* modify:
* comment:
*********************************************************************
*/
INT8U which_rectangle(Rectangle_rang xy,POINT data)
{
if((data.x<xy.x_end)&&(data.x>xy.x_start)&&(data.y>xy.y_start)&&(data.y<xy.y_end))
{
return 1;
}
else
{
return 0;
}
}
/*
*********************************************************************
* name : jiao_init
* func :
* para :
* ret :
* modify:
* comment:
*********************************************************************
*/
void jiao_init()
{
matrix=matrix_init;
}
/*
*********************************************************************
* name : mylcd_touch_jiao
* func : 校正程序
* para : xxxx
* modify:
* comment:bit_jiao 为校正完一个点的标志位,count 为校正的第几个
data 为接收到的数据
*********************************************************************
*/
void mylcd_touch_jiao()
{
INT8U over=0;
POINT lcd;
count=0;
bit_jiao=1;
//display_an(0);
// display_an(0);
while(1)
{
if(bit_jiao)
{
mycls(jiao_back); //清屏
disp_x(perfectDisplaySample[count].x,perfectDisplaySample[count].y,jiao_col);
bit_jiao=0;
}
// jiaoing=1;
if(!(touch_data.x==0 && touch_data.y==0)) //接收到有效数据时,保存数据
{
// if(which_rectangle(enter,touch_data))
// {
perfectScreenSample[count++]=touch_data;
bit_jiao=1;
if(count>=3)
{
over=1;
break;
}
// }
// else if(which_rectangle(cancle,touch_data))
// {
// over=0;
// break;
// }
myposi_set(10,10);
myLCD_printf(" ( %d ,%d ) ",touch_data.x,touch_data.y );
/*
getDisplayPoint1(&lcd,&touch_data,&matrix);
myposi_set(12,10);
myLCD_printf(" ( %d ,%d ) ",lcd.x,lcd.y );
*/
// myLCD_display_mouse_1(lcd.x,lcd.y);
touch_data.x=0;
touch_data.y=0;
}
//可以在这补一个按钮来退出校正程序
}
if(over)
{
//计算校正程序中用到的 matrix 的值
setCalibrationMatrix(&perfectDisplaySample[0],&perfectScreenSample[0],&matrix);
/*
myposi_set(0,0);
myLCD_printf("AN=%ld BN=%ld\n ",matrix.An,matrix.Bn);
myLCD_printf("CN=%ld DN=%ld\n",matrix.Cn,matrix.Dn);
myLCD_printf("EN=%ld FN=%ld\n ",matrix.En,matrix.Fn);
myLCD_printf("DIVIVED=%ld \n",matrix.Divider);
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -