哎没什么人响应啊!我把我的触摸屏程序上传吧!
#include"touch.h"
#include"spi.h"
#include"lcd.h"
#include "systickdelay.h"
#include "stm32f10x_sdio.h"
#include "math.h"
uint8_t cnt=0;
/**********************************************************
* @brief Touch_Init 初始化触摸屏需要的端口
* @param None
* @retval None 芯片--TSC2046
*********************************************************/
void Touch_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/********* Enable GPIOC and AFIO clock *************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
/********SPI_MOSI(TDIN),CLK,CS *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_3|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/********SPI_MISO(DOUT),PEN *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/********初始化变量 EXTI_InitStructure *********/
EXTI_StructInit(&EXTI_InitStructure);
/********将EXIT线1连接到PC1 *****************/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource1);
/********配置EXTI线1上出现下降沿,则产生中断 ******/
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
// /**********设置优先级分组:先占优先级2位,从优先级2位****/
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/********** Enable EXTI1 *****************/
NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**********************************************************
* @brief EXTI_OPEN 开中断
* @param None
* @retval None
*********************************************************/
void EXTI_OPEN(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
// EXTI->IMR|=1<<1;
}
/**********************************************************
* @brief EXTI_CLOSE 关中断
* @param None
* @retval None
*********************************************************/
void EXTI_CLOSE(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=DISABLE;
EXTI_Init(&EXTI_InitStructure);
// EXTI->IMR&=~(1<<1);
}
/**********************************************************
* @brief Drow_Touch_Point 画一个触摸点,用来校准用的
* @param x : 横坐标
* y :纵坐标
* @retval None
*********************************************************/
void Drow_Touch_Point(uint8_t x,uint16_t 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);//画中心圈
}
/**********************************************************
* @brief Draw_Big_Point 画一个大点 2*2的点
* @param None
* @retval None
*********************************************************/
void Draw_Big_Point(uint8_t x,uint16_t y)
{
LCD_DrawPoint(x,y);//中心点
LCD_DrawPoint(x+1,y);
LCD_DrawPoint(x,y+1);
LCD_DrawPoint(x+1,y+1);
}
/**********************************************************
* @brief Convert_Pos 转换结果像素坐标
* @param None 根据触摸屏的校准参数来决定转换后的结果,保存在X0,Y0中
* @retval None
*********************************************************/
void Convert_Pos(void)
{
Pen_Point.X0=240-(Pen_Point.xfac*Pen_Point.X+Pen_Point.xoff);
Pen_Point.Y0=320-(Pen_Point.yfac*Pen_Point.Y+Pen_Point.yoff);
}
/**********************************************************
* @brief Touch_Adjust 触摸屏校准代码
* @param None
* @retval None
*********************************************************/
void Touch_Adjust(void)
{
uint16_t pos_temp[4][2];//坐标缓存值
float d1,d2,fac;
uint32_t tem1,tem2;
// POINT_COLOR=BLUE;
BACK_COLOR =WHITE;
LCD_Clear(WHITE);//清屏
POINT_COLOR=RED;//红色
// LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20); //画点1
//用来标记是否校准过,所以校准之前必须清掉!以免错误
Pen_Point.xfac=0;
Pen_Point.yfac=0;
Pen_Point.xoff=0;
Pen_Point.yoff=0;
while(cnt!=5)
{
pos_temp[cnt][0]=Pen_Point.X;
pos_temp[cnt][1]=Pen_Point.Y;
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=fabs(pos_temp[0][0]-pos_temp[1][0]);//x1-x2
tem2=fabs(pos_temp[0][1]-pos_temp[1][1]);//y1-y2
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//得到1,2的距离
tem1=fabs(pos_temp[2][0]-pos_temp[3][0]);//x3-x4
tem2=fabs(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=fabs(pos_temp[0][0]-pos_temp[2][0]);//x1-x3
tem2=fabs(pos_temp[0][1]-pos_temp[2][1]);//y1-y3
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//得到1,3的距离
tem1=fabs(pos_temp[1][0]-pos_temp[3][0]);//x2-x4
tem2=fabs(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||d1==0||d2==0)//不合格
{
cnt=0;
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20);
continue;
}//正确了
//对角线相等
tem1=fabs(pos_temp[1][0]-pos_temp[2][0]);//x1-x3
tem2=fabs(pos_temp[1][1]-pos_temp[2][1]);//y1-y3
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//得到1,4的距离
tem1=fabs(pos_temp[0][0]-pos_temp[3][0]);//x2-x4
tem2=fabs(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||d1==0||d2==0)//不合格
{
cnt=0;
LCD_Clear(WHITE);//清屏
Drow_Touch_Point(20,20);
continue;
}//正确了
//计算结果
Pen_Point.xfac=(float)200/(pos_temp[1][0]-pos_temp[0][0]);//得到xfac
Pen_Point.xoff=(240-Pen_Point.xfac*(pos_temp[1][0]+pos_temp[0][0]))/2;//得到xoff
Pen_Point.yfac=(float)280/(pos_temp[2][1]-pos_temp[0][1]);//得到yfac
Pen_Point.yoff=(320-Pen_Point.yfac*(pos_temp[2][1]+pos_temp[0][1]))/2;//得到yoff
POINT_COLOR=BLUE;
LCD_Clear(WHITE);//清屏
LCD_ShowString(35,110,"Touch Screen Adjust OK!");//校正完成
delay_ms(500);
LCD_Clear(WHITE);//清屏
return;//校正完成
}
}
}
/**********************************************************
* @brief Load_Drow_Dialog 显示清屏区域
* @param None
* @retval None
*********************************************************/
void Load_Drow_Dialog(void)
{
LCD_Clear(WHITE);//清屏
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(216,0,"RST");//显示清屏区域
POINT_COLOR=RED;//设置画笔蓝色
}
#ifndef _TOUCH_H
#define _TOUCH_H
#include"stm32f10x.h"
/*********笔杆结构体 **********************/
typedef struct
{
uint16_t X0;//原始坐标
uint16_t Y0;
uint16_t X; //最终/暂存坐标 即 物理坐标
uint16_t Y;
float xfac; //触摸屏校准参数
float yfac;
short xoff;
short yoff;
}Pen_Holder;
extern Pen_Holder Pen_Point;
/********* 初始化触摸屏需要的端口 *************/
void Touch_IO_Init(void);
/********* 画一个触摸点,用来校准用的 *************/
void Drow_Touch_Point(uint8_t x,uint16_t y);
/********* 触摸屏校准代码 **********************/
void Touch_Adjust(void);
/********* 关外部中断 **********************/
void EXTI_CLOSE(void);
/********* 开外部中断 **********************/
void EXTI_OPEN(void);
/*********转换结果像素坐标 **********************/
void Convert_Pos(void);
/*********显示清屏区域 **********************/
void Load_Drow_Dialog(void);
/*********画一个大点 2*2的点 **********************/
void Draw_Big_Point(uint8_t x,uint16_t y);
#endif |