配置GT9157触摸屏,获取触摸位置
触摸IC为GT9157
1.配置触摸屏引脚
VDD | SCL | SDA | RST | INT | GND |
电源 | I2C 时钟 | I2C数据 | 屏幕复位 | 屏幕触摸信号 | 地 |
static void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*I2C Periph clock enable*/
RCC_APB1PeriphClockCmd(GTP_I2C_CLK, ENABLE);
/*GTP_I2C_SCL_GPIO_CLK and GTP_I2C_SDA_GPIO_CLK Periph clock enable */
RCC_AHB1PeriphClockCmd(GTP_I2C_SCL_GPIO_CLK | GTP_I2C_SDA_GPIO_CLK|GTP_RST_GPIO_CLK|GTP_INT_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//配置中断
/*配置SCL引脚 */
GPIO_InitStructure.GPIO_Pin = GTP_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GTP_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
/*配置SDA引脚 */
GPIO_InitStructure.GPIO_Pin = GTP_I2C_SDA_PIN;
GPIO_Init(GTP_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
/*RST */
GPIO_InitStructure.GPIO_Pin = GTP_RST_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GTP_RST_GPIO_PORT, &GPIO_InitStructure);
/*INT */
GPIO_InitStructure.GPIO_Pin = GTP_INT_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //设置为下拉,方便初始化
GPIO_Init(GTP_INT_GPIO_PORT, &GPIO_InitStructure);
}
void BSP_EXTI_INT(void)
{
/*Configure the I/O in input mode using GPIO_Init()*/
/*Select the input source pin for the EXTI line using SYSCFG_EXTILineConfig()
SYSCFG APB clock must be enabled to get write access to SYSCFG_EXTICRx
registers using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(GTP_INT_EXTI_PORTSOURCE,GTP_INT_EXTI_PINSOURCE);
/*Select the mode(interrupt, event) and configure the trigger
selection (Rising, falling or both) using EXTI_Init()*/
EXTI_InitTypeDef EXTI_Init_Struct;
EXTI_Init_Struct.EXTI_Line = EXTI_Line13;
EXTI_Init_Struct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_Init_Struct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init_Struct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_Init_Struct);
/*Configure NVIC IRQ channel mapped to the EXTI line using NVIC_Init()*/
NVIC_InitTypeDef NVIC_Init_Struct;
NVIC_Init_Struct.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_Init_Struct);
}
void I2C_ResetChip(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*!< Configure INT */
GPIO_InitStructure.GPIO_Pin = GTP_INT_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //设置为下拉,方便初始化
GPIO_Init(GTP_INT_GPIO_PORT, &GPIO_InitStructure);
/*初始化GT9157,rst为高电平,int为低电平,则gt9157的设备地址被配置为0xBA*/
/*复位为低电平,为初始化做准备*/
GPIO_ResetBits (GTP_RST_GPIO_PORT,GTP_RST_GPIO_PIN);
Delay(0x0FFFFF);
/*拉高一段时间,进行初始化*/
GPIO_SetBits (GTP_RST_GPIO_PORT,GTP_RST_GPIO_PIN);
Delay(0x0FFFFF);
/*重新把INT引脚设置为浮空输入模式*/
/*!< Configure INT */
GPIO_InitStructure.GPIO_Pin = GTP_INT_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GTP_INT_GPIO_PORT, &GPIO_InitStructure);
BSP_EXTI_INT();
}
void I2C_Touch_Init(void)
{
I2C_GPIO_Config();
I2C_ResetChip();
}
2.编写软件I2C读写函数
/*IIC软件时序*/
static void i2c_Delay(void)
{
int i;
for (i = 0; i < 10*5; i++);
}
void i2c_Start(void)
{
I2C_SDA_1();
I2C_SCL_1();
i2c_Delay();
I2C_SDA_0();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
}
void i2c_Stop(void)
{
I2C_SDA_0();
I2C_SCL_1();
i2c_Delay();
I2C_SDA_1();
}
void i2c_SendByte(uint8_t _ucByte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if (_ucByte & 0x80)
{
I2C_SDA_1();
}
else
{
I2C_SDA_0();
}
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
if (i == 7)
{
I2C_SDA_1();
}
_ucByte <<= 1;
i2c_Delay();
}
}
uint8_t i2c_ReadByte(void)
{
uint8_t i;
uint8_t value;
value = 0;
for (i = 0; i < 8; i++)
{
value <<= 1;
I2C_SCL_1();
i2c_Delay();
if (I2C_SDA_READ())
{
value++;
}
I2C_SCL_0();
i2c_Delay();
}
return value;
}
uint8_t i2c_WaitAck(void)
{
uint8_t re;
I2C_SDA_1();
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
if (I2C_SDA_READ())
{
re = 1;
}
else
{
re = 0;
}
I2C_SCL_0();
i2c_Delay();
return re;
}
void i2c_Ack(void)
{
I2C_SDA_0();
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
I2C_SDA_1();
}
void i2c_NAck(void)
{
I2C_SDA_1();
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
}
/*IIC读写函数*/
#define I2C_DIR_WR 0 /* 写*/
#define I2C_DIR_RD 1 /* 读*/
uint32_t I2C_ReadBytes(uint8_t ClientAddr,uint8_t* pBuffer, uint16_t NumByteToRead)
{
i2c_Start();
i2c_SendByte(ClientAddr | I2C_DIR_RD);
if (i2c_WaitAck() != 0)
{
}
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
i2c_NAck();
i2c_Stop();
}
*pBuffer = i2c_ReadByte();
pBuffer++;
NumByteToRead--;
i2c_Ack();
}
i2c_Stop();
return 0; /* 执行成功 */
}
uint32_t I2C_WriteBytes(uint8_t ClientAddr,uint8_t* pBuffer, uint8_t NumByteToWrite)
{
i2c_Stop();
i2c_Start();
i2c_SendByte(ClientAddr | I2C_DIR_WR);
if (i2c_WaitAck() == 0)
{
}
while(NumByteToWrite--)
{
i2c_SendByte(*pBuffer);
if (i2c_WaitAck() != 0)
{
}
pBuffer++;
}
i2c_Stop();
return 0;
}
3.移植gt9xx.c
将编写好的软件IIC读写函数 加入到gt9xx.c中的I2C_Transfer( struct i2c_msg *msgs,int num)函数中
static int I2C_Transfer( struct i2c_msg *msgs,int num)
{
int im = 0;
int ret = 0;
GTP_DEBUG_FUNC();
for (im = 0; ret == 0 && im != num; im++)
{
if ((msgs[im].flags&I2C_M_RD)) //根据flag判断是读数据还是写数据
{
ret = I2C_ReadBytes(msgs[im].addr, msgs[im].buf, msgs[im].len); //IIC读取数据
} else
{
ret = I2C_WriteBytes(msgs[im].addr, msgs[im].buf, msgs[im].len); //IIC写入数据
}
}
if(ret)
return ret;
return im; //正常完成的传输结构个数
}
Goodix_TS_Work_Func(void) 函数是主要处理函数,获取触摸坐标
4.配置INT中断处理函数,在中断处调用Goodix_TS_Work_Func()
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13)==SET)
{
EXTI_ClearITPendingBit(EXTI_Line13);
Goodix_TS_Work_Func();
}
}
5 .触摸屏幕四个角,串口打印出坐标