大家好,又见面了,我是你们的朋友全栈君。
GT911手册及驱动程序(安卓、MTK).rar-嵌入式文档类资源-CSDN下载 https://download.csdn.net/download/qasxc78563/15117948
参考:STM32F103驱动GT911 – DarkBright – 博客园 https://www.cnblogs.com/DarkBright/p/10730346.html
GT911 与主机接口共有 6 PIN,分别为:VDD、GND、SCL、SDA、INT、RESET。
这里用P11做中断脚,P13做复位脚,P31为SDA,P32为SCL
TouchPannels.c
#define _TOUCH_PANNEL_C_
#include "TouchPannel.h"
#ifdef TouchPannelEn
void GT911_INT_Init(void)
{
SetAP1_1M(0);//设置P11输出0
}
void GT911_INT(U8 cmd)
{
if(cmd) SetAP1_1M(1);
else SetAP1_1M(0);
}
//中断设置
void GT911_INT_Change(void)
{
GetAP1_1M();//读引脚值
EA = 1; //总中断开关
EX0 = 1; //允许外部中断0
IT0 = 1; //外部中断0的触发方式
SetINTInput();
}
void GT911_RST_Init(void)
{
SetAP1_3M(0);
}
void GT911_RST(U8 cmd)
{
if(cmd) SetAP1_3M(1);
else SetAP1_3M(0);
}
void TP_I2C_Read(U16 addr,U8*pData,U8 len)
{
U8 i=0;
I2CStart();
I2CWrite(CTP_SLAVE_ADDR); //写地址
ACKCheck();
I2CWrite((U8)(addr>>8)); //
ACKCheck();
I2CWrite((U8)(addr&0XFF)); //
ACKCheck();
I2CStop();
I2CStart();
I2CWrite(CTP_SLAVE_ADDR|0x01); // 读地址
ACKCheck();
for(i=0;i<len;i++)
{
pData[i]=I2CRead();
AckSend();
//printf("d=%x",pData[i]);
}
pData[i]=I2CRead();
NACKSend();
I2CStop();
}
U8 TP_WR_Reg(U16 reg,U8 *buf,U8 len)
{
U8 i;
U8 ret=0;
I2CStart();
I2CWrite(CTP_SLAVE_ADDR);
ACKCheck();
I2CWrite(reg>>8);
ACKCheck();
I2CWrite(reg&0XFF);
ACKCheck();
for(i=0;i<len;i++)
{
I2CWrite(buf[i]);
ret=ACKCheck();
if(ret)break;
}
I2CStop();
return ret;
}
U8 CODE GT911_Cfg[] ={
\
0x41,0x00,0x06,0x00,0x08,0x0A,0x05,0x00,0x01,0x0F,\
0x28,0x0F,0x50,0x32,0x03,0x05,0x00,0x00,0xFB,0x03,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x30,0xAA,\
0x1F,0x1C,0xD6,0x09,0x00,0x00,0x00,0x9A,0x33,0x25,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x19,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x01,0x04,0x05,0x06,0x07,0x08,0x09,\
0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x14,0x15,0x16,0x17,\
0x18,0x19,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x14,0x13,0x12,0x11,0x10,0x0F,0x0E,0x0D,\
0x0C,0x0A,0x08,0x07,0x06,0x04,0x02,0x00,0x19,0x1B,\
0x1C,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,\
0x27,0x28,0x29,0x2A,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x63,0x01};
//驱动写入配置
U8 GT911_Send_Cfg(U8 mode)
{
U8 buf[2];
U8 i=0;
buf[0]=0;
buf[1]=mode;
for(i=0;i<sizeof(GT911_Cfg);i++)buf[0]+=GT911_Cfg[i];
buf[0]=(~buf[0])+1;
TP_WR_Reg(GT_CFGS_REG,(U8*)GT911_Cfg,sizeof(GT911_Cfg));
TP_WR_Reg(GT_CHECK_REG,buf,2);
return 0;
}
//GT911初始化
void InitialTouchPannelDevice(void)
{
U8 tmp[4]={
0};
//printfStr("InitialTouchPannelDevice");
GT911_RST_Init();
GT911_INT_Init();
GT911_RST(0);
GT911_INT(1);
DelayMs(1);
GT911_RST(1);
DelayMs(6);
GT911_INT(0);
DelayMs(55);
GT911_INT_Change();
DelayMs(50);
TP_I2C_Read(GT_PID_REG, tmp, 4);
tmp[0] = 0x02;
TP_WR_Reg(GT_CTRL_REG, tmp, 1);
TP_I2C_Read(GT_CFGS_REG, tmp, 1);
if(tmp[0] < 0x60){
//printf("Default Ver:0x%X\r\n",tmp[0]);
GT911_Send_Cfg(1);
}
DelayMs(10);
tmp[0] = 0x00;
TP_WR_Reg(GT_CTRL_REG, tmp, 1);
}
//读取一个点的坐标
BOOL GetPointValue()
{
U8 tmp[1]={
0};
TP_I2C_Read(0x8140, g_touchPointInfor, 0x40);
tmp[0] = g_touchPointInfor[0x0E];
printf("tmp[0]=%x", tmp[0]);
if((tmp[0]&0x80) && ((tmp[0]&0x0f)>0)){
if(tmp[0]==0x81)
{
//GetPointData(tmp[0]&0x0f, g_touchPointInfor);
g_curPoint.xPos = g_touchPointInfor[0x11]<<8 | g_touchPointInfor[0x10] ;
g_curPoint.yPos = g_touchPointInfor[0x13]<<8 | g_touchPointInfor[0x12];
printf("x1=%d",g_curPoint.xPos);
printf("y1=%d",g_curPoint.yPos);
}
}
tmp[0] = 0;
//每次读取后要重新写寄存器
TP_WR_Reg(0x814E, &tmp, 1);
return 1;
}
//读取5个点的坐标
#if 0
void GetPointData(U8 cnt, U8 *Pdata)
{
Pdata=Pdata;
switch(cnt){
case 5:
printf("x5=%d",Pdata[0x31]<<8 | Pdata[0x30]);
printf("y5=%d",Pdata[0x33]<<8 | Pdata[0x32]);
case 4:
printf("x4=%d",Pdata[0x29]<<8 | Pdata[0x28]);
printf("y4=%d",Pdata[0x2B]<<8 | Pdata[0x2A]);
case 3:
printf("x3=%d",Pdata[0x21]<<8 | Pdata[0x20]);
printf("y3=%d",Pdata[0x23]<<8 | Pdata[0x22]);
case 2:
printf("x2=%d",Pdata[0x19]<<8 | Pdata[0x18]);
printf("y2=%d",Pdata[0x1B]<<8 | Pdata[0x1A]);
case 1:
printf("x1=%d",Pdata[0x11]<<8 | Pdata[0x10]);
printf("y1=%d",Pdata[0x13]<<8 | Pdata[0x12]);
break;
default:
break;
}
}
#endif
#endif
TouchPannel.h
#ifndef _TOUCH_PANNEL_H__
#define _TOUCH_PANNEL_H__
#ifdef _TOUCH_PANNEL_C_
#define _TOUCH_PANNEL_EXTERN_
#else
#define _TOUCH_PANNEL_EXTERN_ extern
#endif
#define TouchPannelEn
#define I2C_EN ENABLE
#define TP_X_MAX_PIXEL 800
#define TP_Y_MAX_PIXEL 480
UCHAR XDATA g_touchPointInfor[0x40];//存放数据信息
#define CTP_SLAVE_ADDR 0x28
//GT911 部分寄存器定义
#define GT_CTRL_REG 0X8040 //GT911控制寄存器
#define GT_CFGS_REG 0X8047 //GT911配置起始地址寄存器
#define GT_CHECK_REG 0X80FF //GT911校验和寄存器
#define GT_PID_REG 0X8140 //GT911产品ID 寄存器
#define GT_GSTID_REG 0X814E //GT911当前触摸情况
//中断脚设置
#define SetINTInput() GetAP1_1M() //读取引脚值
//SDA,SCL
#define GetSda() GetAP3_1M()
#define SetSda(x) SetAP3_1M(x) //设置引脚值
#define SetScl(x) SetAP3_2M(x)
#define SetSdaOutput() SetSda(1)
#define SetSdaInput() GetSda()
#define SetSclOutput() SetScl(1)
typedef unsigned char UCHAR;
typedef unsigned char BYTE;
typedef unsigned char UINT8;
typedef unsigned char U8;
typedef unsigned int U16;
typedef unsigned long U32;
#define DATA volatile data
#define BDATA volatile bdata
#define IDATA volatile idata
#define PDATA volatile pdata
#define XDATA volatile xdata
#define CODE volatile code
typedef struct _POINT
{
U16 xPos;
U16 yPos;
}POINT;
U8 XDATA g_ucTPFlags;
POINT XDATA g_curPoint;
POINT XDATA g_prePoint;
BOOL g_bTPIntFlag;
_TOUCH_PANNEL_EXTERN_ void DelayI2c(U16 delayTime);
_TOUCH_PANNEL_EXTERN_ void I2CStart(void);
_TOUCH_PANNEL_EXTERN_ void I2CStop(void);
_TOUCH_PANNEL_EXTERN_ FLAG ACKCheck(void);
_TOUCH_PANNEL_EXTERN_ void NACKSend(void);
_TOUCH_PANNEL_EXTERN_ void I2CWrite(U8 tempdata);
_TOUCH_PANNEL_EXTERN_ U8 I2CRead(void);
_TOUCH_PANNEL_EXTERN_ void AckSend(void);
_TOUCH_PANNEL_EXTERN_ void InitialTouchPannelDevice(void);
_TOUCH_PANNEL_EXTERN_ BOOL GetPointValue();
I2C.C
#include "TouchPannel.h"
#if I2C_EN
/*********************************************************** void DelayI2c(U16 delayTime) ************************************************************/
void DelayI2c(U16 delayTime)
{
while(delayTime)
{
delayTime = delayTime - 1;
}
}
/********************************************************** void I2CStart(void) ***********************************************************/
void I2CStart(void)
{
SetSdaOutput();
SetSclOutput();
DelayI2c(5); //___
SetSda(1); // |
SetScl(1); // |___SDA
DelayI2c(20); //____
SetSda(0); // |
DelayI2c(20); // |__SCL
SetScl(0);
DelayI2c(20);
}
/*********************************************************** void I2CStop(void) ************************************************************/
void I2CStop(void)
{
SetSdaOutput(); // ___SDA
SetSda(0); // |
SetScl(1); //___|
DelayI2c(20); //_______SCL
SetSda(1); //
DelayI2c(20); //
}
/*********************************************************** FLAG ACKCheck(void) ************************************************************/
FLAG ACKCheck(void)
{
U8 loop = 200;
SetSdaInput();
DelayI2c(2);
SetScl(1);
do
{
DelayI2c(10);
if(GetSda() == 0)
{
SetScl(0);
return(0); //这里说明有ACK信号
}
else
{
if(loop == 1)
{
SetScl(0);
printfStr("Ack Error!");
return(1);
}
}
}while(--loop) ;
return 1;
}
/*********************************************************** void NACKSend(void) ************************************************************/
void NACKSend(void)
{
SetSdaOutput();
SetSda(1); //"发送非应答位"
DelayI2c(10);
SetScl(1);
DelayI2c(10);
SetScl(0);
DelayI2c(10);
SetSda(1);
}
void AckSend(void)
{
SetScl(0);
DelayI2c(10); //延时5us
SetSda(0);
DelayI2c(10); //延时5us
SetScl(1);
DelayI2c(10); //延时5us
SetScl(0);
DelayI2c(10); //延时5us
}
/*********************************************************** void I2CWrite(U8 tempdata) ************************************************************/
void I2CWrite(U8 tempdata)
{
U8 XDATA num; //"发送数据位数"
SetSdaOutput();
for(num = 0;num < 8;num ++)
{
SetSda((tempdata >> (7-num)) & 1); //"先发高位"
DelayI2c(20);
SetScl(1); //下降缘 读取
DelayI2c(20);
SetScl(0);
DelayI2c(20);
}
SetSda(1);
}
/*********************************************************** U8 I2CRead(void) ************************************************************/
U8 I2CRead(void)
{
U8 XDATA temp = 0xff;
U8 XDATA num; //"接收数据位数"
SetSdaInput();
for(num = 0;num < 8;num ++)
{
SetScl(1);
DelayI2c(20);
temp = temp << 1;//"先接收高位"
if(GetSda() == 1)
{
temp = temp | 0x01;
}
if(GetSda() == 0)
{
temp = temp & 0xfe;
}
SetScl(0);
DelayI2c(20);
}
return (temp);
}
#endif
Interrup.c
void IRQ_Int1Server(void) interrupt IntSource_INT1
{
#ifdef TouchPannelEn
g_bTPIntFlag=1;
#endif
//GetPointValue();
INT1IrqFlagClear();
}
main.c
void main(void)
{
InitialTouchPannelDevice();
///DelayMs(50);
GetPointValue();
while(1)
{
if(g_bTPIntFlag==1)
{
GetPointValue();
g_bTPIntFlag=0;
}
//DelayMs(50);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/147837.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...