170 lines
3.4 KiB
C
170 lines
3.4 KiB
C
#include "BoardCfg.h"
|
||
/**********************************************************
|
||
行注释
|
||
内容:HAL层
|
||
**********************************************************/
|
||
void XPY2046Init()
|
||
{
|
||
GPIO_Init(GPIOA, PIN10, 1, 0, 0);
|
||
GPIO_Init(GPIOA, PIN9, 1, 0, 0);
|
||
GPIO_Init(GPIOM, PIN20, 1, 0, 0);
|
||
GPIO_Init(GPIOC, PIN7, 0, 1, 0);
|
||
}
|
||
/**
|
||
* @brief XTP DOUT线操作
|
||
*
|
||
* @param val 0/1
|
||
*/
|
||
static void XPT2046_DOUT_Write(u8 val)
|
||
{
|
||
if (val)
|
||
{
|
||
GPIO_SetBit(GPIOA, PIN10);
|
||
}
|
||
else
|
||
{
|
||
GPIO_ClrBit(GPIOA, PIN10);
|
||
}
|
||
}
|
||
/**
|
||
* @brief XTP CLK线操作
|
||
*
|
||
* @param val 0/1
|
||
*/
|
||
static void XPT2046_CLK_Write(u8 val)
|
||
{
|
||
if (val)
|
||
{
|
||
GPIO_SetBit(GPIOM, PIN20);
|
||
}
|
||
else
|
||
{
|
||
GPIO_ClrBit(GPIOM, PIN20);
|
||
}
|
||
}
|
||
/**
|
||
* @brief XTP DIN线操作
|
||
*
|
||
* @param val 0/1
|
||
*/
|
||
static u8 XPT2046_DIN_Read()
|
||
{
|
||
return GPIO_GetBit(GPIOC,PIN7);
|
||
}
|
||
/**
|
||
* @brief XTP CS线操作
|
||
*
|
||
* @param val 0/1
|
||
*/
|
||
static void XPT2046_CS_Write(u8 val)
|
||
{
|
||
if (val)
|
||
{
|
||
GPIO_SetBit(GPIOA, PIN10);
|
||
}
|
||
else
|
||
{
|
||
GPIO_ClrBit(GPIOA, PIN10);
|
||
}
|
||
}
|
||
/**
|
||
* @brief 延迟1us
|
||
* interface
|
||
* @param time
|
||
*/
|
||
static void DelayUs(u32 time)
|
||
{
|
||
for (u32 i = 0; i < time; i++)
|
||
{
|
||
for (u32 j = 0; j < 1000; j++)
|
||
{
|
||
__ASM("nop");
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
u8 CMD_RDX=0XD0;
|
||
u8 CMD_RDY=0X90;
|
||
/**********************************************************
|
||
行注释
|
||
内容:
|
||
**********************************************************/
|
||
static void TP_Write_Byte(u8 num)
|
||
{
|
||
u8 count = 0;
|
||
for (count = 0; count < 8; count++)
|
||
{
|
||
if (num & 0x80)
|
||
XPT2046_DOUT_Write(1);
|
||
else
|
||
XPT2046_DOUT_Write(0);
|
||
num <<= 1;
|
||
XPT2046_CLK_Write(0);
|
||
DelayUs(1);
|
||
XPT2046_CLK_Write(1);
|
||
}
|
||
}
|
||
|
||
u16 TP_Read_AD(u8 CMD)
|
||
{
|
||
u8 count=0;
|
||
u16 Num=0;
|
||
XPT2046_CLK_Write(0); //先拉低时钟
|
||
XPT2046_DOUT_Write(0); //拉低数据线
|
||
XPT2046_CS_Write(0); //选中触摸屏IC
|
||
TP_Write_Byte(CMD);//发送命令字
|
||
DelayUs(6);//ADS7846的转换时间最长为6us
|
||
XPT2046_CLK_Write(0);
|
||
DelayUs(1);
|
||
XPT2046_CLK_Write(1); //给1个时钟,清除BUSY
|
||
DelayUs(1);
|
||
XPT2046_CLK_Write(0);
|
||
for(count=0;count<16;count++)//读出16位数据,只有高12位有效
|
||
{
|
||
Num<<=1;
|
||
XPT2046_CLK_Write(0); //下降沿有效
|
||
DelayUs(1);
|
||
XPT2046_CLK_Write(1);
|
||
if(XPT2046_DIN_Read())Num++;
|
||
}
|
||
Num>>=4; //只有高12位有效.
|
||
XPT2046_CS_Write(1); //释放片选
|
||
return(Num);
|
||
}
|
||
#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;
|
||
}
|
||
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;//读数成功
|
||
} |