7 inch capcitive touch screen with GT911? - Page 1
Forum section: electronics / general

author: seyedsaeed
author_rank: Regular Contributor
timestamp: June 02, 2020, 09:56:26 am
content: HiI use gt911 library But not working rightCode:#include "GT911.h"GT911_Dev Dev_Now,Dev_Backup;uint8_t s_GT911_CfgParams[]={0x52,0x20,0x03,0xE0,0x01,0x0A,0x3D,0x00,0x01,0x08,0x28,0x08,0x5A,0x3C,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x1A,0x1E,0x14,0x8C,0x2E,0x0E,0x71,0x73,0x9D,0x0A,0x00,0x00,0x01,0xCA,0x03,0x2D,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0x1E,0x94,0xD5,0x02,0x07,0x00,0x00,0x04,0x95,0x55,0x00,0x89,0x62,0x00,0x7F,0x71,0x00,0x78,0x82,0x00,0x72,0x95,0x00,0x72,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,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,0x14,0x16,0x18,0x1A,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2A,0x29,0x28,0x26,0x24,0x22,0x21,0x20,0x1F,0x1E,0x1D,0x1C,0x18,0x16,0x14,0x13,0x12,0x10,0x0F,0x0C,0x0A,0x08,0x06,0x04,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE5,0x00/*//old0x00,0x20,0x03,0xE0,0x01,0x05,0x0D,0x00,0x01,0x08,0x1E,0x08,0x64,0x46,0x03,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x18,0x1A,0x1E,0x14,0x8B,0x2B,0x0E,0x71,0x73,0xB2,0x04,0x00,0x00,0x00,0x01,0x02,0x1D,0x00,0x01,0x00,0x00,0x00,0x03,0x64,0x32,0x00,0x00,0x00,0x50,0xA0,0x94,0xD5,0x02,0x07,0x00,0x00,0x00,0x9D,0x55,0x00,0x8D,0x62,0x00,0x80,0x71,0x00,0x74,0x82,0x00,0x6B,0x95,0x00,0x6B,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,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,0x14,0x16,0x18,0x1A,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0F,0x10,0x12,0x13,0x16,0x18,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x24,0x26,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x67,0x01*/};//uint16_t Color_table[]={Black,Red,Yellow,Blue,Green};void GT911_INT_Config(void){/*GPIO_InitTypeDef GPIO_InitStructure;EXTI_InitTypeDef EXTI_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(CT_INT_CLK,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);GPIO_InitStructure.GPIO_Pin = CT_INT_GPIO_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_Init(CT_INT_GPIO_PORT, &GPIO_InitStructure);GPIO_EXTILineConfig(CT_INT_EXTI_PORTSOURCE,CT_INT_EXTI_PINSOURCE);EXTI_InitStructure.EXTI_Line=CT_INT_EXTI_LINE;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_Init(&EXTI_InitStructure);NVIC_InitStructure.NVIC_IRQChannel = CT_INT_EXTI_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);*/}//void CT_INT_EXTI_IRQ(void)//{//Dev_Now.Touch = 1;/*EXTI_ClearITPendingBit(CT_INT_EXTI_LINE);EXTI_ClearFlag(CT_INT_EXTI_LINE);*///}void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){Dev_Now.Touch = 1;}void GT911_Reset_Sequence(){/*GPIO_InitTypeDef GPIO_InitStruct = {0};__HAL_RCC_GPIOB_CLK_ENABLE();GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Pin = CT_RES_GPIO_PIN;GPIO_Init(CT_RES_GPIO_PORT, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = CT_INT_GPIO_PIN;GPIO_Init(CT_INT_GPIO_PORT, &GPIO_InitStructure);GPIO_ResetBits(CT_RES_GPIO_PORT,CT_RES_GPIO_PIN);//RST PB1delay_ms(100);GPIO_ResetBits(CT_INT_GPIO_PORT,CT_INT_GPIO_PIN);//INT PB13delay_ms(100);GPIO_SetBits(CT_RES_GPIO_PORT,CT_RES_GPIO_PIN);//RST PB1delay_ms(200);GT911_INT_Config();delay_ms(400);*/}void GT911_RD_Reg(uint16_t reg,uint8_t *buf,uint8_t len){uint8_t i;CT_I2C_Start();CT_I2C_Send_Byte(CT_CMD_WR);CT_I2C_Wait_Ack();CT_I2C_Send_Byte(reg>>8);CT_I2C_Wait_Ack();CT_I2C_Send_Byte(reg&0XFF);CT_I2C_Wait_Ack();CT_I2C_Stop();CT_I2C_Start();CT_I2C_Send_Byte(CT_CMD_RD);CT_I2C_Wait_Ack();for(i=0;i<len;i++){buf=CT_I2C_Read_Byte(i==(len-1)?0:1);}CT_I2C_Stop();}uint8_t GT911_WR_Reg(uint16_t reg,uint8_t *buf,uint8_t len){uint8_t i;uint8_t ret=0;CT_I2C_Start();CT_I2C_Send_Byte(CT_CMD_WR);CT_I2C_Wait_Ack();CT_I2C_Send_Byte(reg>>8);CT_I2C_Wait_Ack();CT_I2C_Send_Byte(reg&0XFF);CT_I2C_Wait_Ack();for(i=0;i<len;i++){CT_I2C_Send_Byte(buf);ret=CT_I2C_Wait_Ack();if(ret)break;}CT_I2C_Stop();return ret;}uint8_t GT911_ReadStatue(void){uint8_t buf;GT911_RD_Reg(GT911_PRODUCT_ID_REG, (uint8_t *)&buf, 3);GT911_RD_Reg(GT911_CONFIG_REG, (uint8_t *)&buf, 1);if ((buf==0xff) && (buf==0xff) && (buf==0xff) && (buf==0xff)){}//printf("TouchPad_ID:%c,%c,%c\r\nTouchPad_Config_Version:%d\r\n",buf,buf,buf,buf);//HAL_UART_Transmit(return buf;}uint16_t GT911_ReadFirmwareVersion(void){uint8_t buf;GT911_RD_Reg(GT911_FIRMWARE_VERSION_REG, buf, 2);//printf("FirmwareVersion:%2x\r\n",(((uint16_t)buf << 8) + buf));return ((uint16_t)buf << 8) + buf;}extern uint16_t CT_Color_table[];void GT911_Scan(void){uint8_t buf;uint8_t Clearbuf = 0;uint8_t i;if (Dev_Now.Touch == 1){Dev_Now.Touch = 0;GT911_RD_Reg(GT911_READ_XY_REG, buf, 1);if ((buf&0x80) == 0x00){GT911_WR_Reg(GT911_READ_XY_REG, (uint8_t *)&Clearbuf, 1);//printf("%x\r\n",buf);HAL_Delay(10);}else{//printf("bufstat:%x\r\n",buf);Dev_Now.TouchpointFlag = buf;Dev_Now.TouchCount = buf&0x0f;if (Dev_Now.TouchCount > 5){GT911_WR_Reg(GT911_READ_XY_REG, (uint8_t *)&Clearbuf, 1);return ;}GT911_RD_Reg(GT911_READ_XY_REG+1, &buf, Dev_Now.TouchCount*8);GT911_WR_Reg(GT911_READ_XY_REG, (uint8_t *)&Clearbuf, 1);Dev_Now.Touchkeytrackid = buf;Dev_Now.X = ((uint16_t)buf << 8) + buf;Dev_Now.Y = 480 - (((uint16_t)buf << 8) + buf);Dev_Now.S = ((uint16_t)buf << 8) + buf;Dev_Now.Touchkeytrackid = buf;Dev_Now.X = ((uint16_t)buf << 8) + buf;Dev_Now.Y = 480 - (((uint16_t)buf << 8) + buf);Dev_Now.S = ((uint16_t)buf << 8) + buf;Dev_Now.Touchkeytrackid = buf;Dev_Now.X = ((uint16_t)buf << 8) + buf;Dev_Now.Y = 480 - (((uint16_t)buf << 8) + buf);Dev_Now.S = ((uint16_t)buf << 8) + buf;Dev_Now.Touchkeytrackid = buf;Dev_Now.X = ((uint16_t)buf << 8) + buf;Dev_Now.Y = 480 - (((uint16_t)buf << 8) + buf);Dev_Now.S = ((uint16_t)buf << 8) + buf;Dev_Now.Touchkeytrackid = buf;Dev_Now.X = ((uint16_t)buf << 8) + buf;Dev_Now.Y = 480 - (((uint16_t)buf << 8) + buf);Dev_Now.S = ((uint16_t)buf << 8) + buf;for (i = 0; i< Dev_Backup.TouchCount;i++){if(Dev_Now.Y<20)Dev_Now.Y=20;if(Dev_Now.Y>460)Dev_Now.Y=460;if(Dev_Now.X<20)Dev_Now.X=20;if(Dev_Now.X>780)Dev_Now.X=780;//LCD_Draw_Coordinates(Dev_Backup.X,Dev_Backup.Y,White);}for (i=0;i<Dev_Now.TouchCount;i++){if(Dev_Now.Y<20)Dev_Now.Y=20;if(Dev_Now.Y>460)Dev_Now.Y=460;if(Dev_Now.X<20)Dev_Now.X=20;if(Dev_Now.X>780)Dev_Now.X=780;Dev_Backup.X = Dev_Now.X;Dev_Backup.Y = Dev_Now.Y;Dev_Backup.TouchCount = Dev_Now.TouchCount;//LCD_Draw_Coordinates(Dev_Now.X,Dev_Now.Y,Color_table);//printf("%d,%d ", Dev_Now.X,Dev_Now.Y);}//printf("\r\n");}}}void GT911_TEST(void){uint8_t config_Checksum = 0,i;CT_I2C_Init();//CLR_CLK();GT911_ReadStatue();//debug/*GT911_RD_Reg(GT911_CONFIG_REG, (uint8_t *)&s_GT911_CfgParams, 186);for(i=0;i<sizeof(s_GT911_CfgParams)-2;i++){config_Checksum += s_GT911_CfgParams;//printf("0x%02X ", s_GT911_CfgParams);//if ((i+1)%10==0)//printf("\r\n");}//printf("0x%02X 0x%02X\r\nconfig_Checksum=0x%2X\r\n", s_GT911_CfgParams,s_GT911_CfgParams, ((~config_Checksum)+1)&0xff);if (s_GT911_CfgParams == (((~config_Checksum)+1)&0xff)){//printf("READ CONFIG SUCCESS!\r\n");//printf("%d*%d\r\n",s_GT911_CfgParams<<8|s_GT911_CfgParams,s_GT911_CfgParams<<8|s_GT911_CfgParams);if ((X_OUTPUT_MAX != (s_GT911_CfgParams<<8|s_GT911_CfgParams)) || (Y_OUTPUT_MAX != (s_GT911_CfgParams<<8|s_GT911_CfgParams))){s_GT911_CfgParams = 82;s_GT911_CfgParams = X_OUTPUT_MAX&0xff;s_GT911_CfgParams = X_OUTPUT_MAX>>8;s_GT911_CfgParams = Y_OUTPUT_MAX&0xff;s_GT911_CfgParams = Y_OUTPUT_MAX>>8;s_GT911_CfgParams = 1;//printf("%d*%d\r\n",s_GT911_CfgParams<<8|s_GT911_CfgParams,s_GT911_CfgParams<<8|s_GT911_CfgParams);config_Checksum = 0;for(i=0;i<sizeof(s_GT911_CfgParams)-2;i++){config_Checksum += s_GT911_CfgParams;}s_GT911_CfgParams = (~config_Checksum)+1;//printf("config_Checksum=0x%2X\r\n",s_GT911_CfgParams);//printf("\r\n*************************\r\n");//for(i=0;i<sizeof(s_GT911_CfgParams);i++)//{//printf("0x%02X ", s_GT911_CfgParams);//if ((i+1)%10==0)//printf("\r\n");//}//printf("\r\n*************************\r\n");GT911_WR_Reg(GT911_CONFIG_REG, (uint8_t *)s_GT911_CfgParams, sizeof(s_GT911_CfgParams));GT911_RD_Reg(GT911_CONFIG_REG, (uint8_t *)&s_GT911_CfgParams, 186);config_Checksum = 0;for(i=0;i<sizeof(s_GT911_CfgParams)-2;i++){config_Checksum += s_GT911_CfgParams;//printf("0x%02X ", s_GT911_CfgParams);//if ((i+1)%10==0)//printf("\r\n");}//printf("0x%02X ", s_GT911_CfgParams);//printf("0x%02X ", s_GT911_CfgParams);//printf("\r\n");//printf("config_Checksum=0x%2X\r\n",((~config_Checksum)+1)&0xff);}}*///debug endHAL_Delay(300);GT911_ReadFirmwareVersion();while(1){GT911_Scan();}}