能写是stm32平台一个5寸GT911驱动代码
时间: 2023-12-14 21:36:22 浏览: 41
以下是基于STM32平台的5寸GT911触摸屏驱动代码,需要先初始化I2C总线:
```c
#include "stm32f4xx.h"
#define GT911_ADDR 0xBA
#define GT911_CFG_NUM 0x8047
void GT911_Init(void)
{
uint8_t cfg_data[2];
cfg_data[0] = GT911_CFG_NUM >> 8;
cfg_data[1] = GT911_CFG_NUM & 0xFF;
// Send config command
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, cfg_data[0]);
I2C_WriteData(I2C1, cfg_data[1]);
I2C_Stop(I2C1);
// Delay for GT911 to process the config command
Delay_us(100);
// Read config data from GT911
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x80);
I2C_Stop(I2C1);
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Receiver);
uint8_t data[14];
for (int i = 0; i < 14; i++) {
data[i] = I2C_ReadData(I2C1, i == 13 ? I2C_NACK : I2C_ACK);
}
I2C_Stop(I2C1);
// Parse config data and configure GT911
uint16_t threshold = ((uint16_t)data[1] << 8) | data[0];
uint8_t touch_cfg = data[3];
uint16_t touch_panel_x = ((uint16_t)data[6] << 8) | data[5];
uint16_t touch_panel_y = ((uint16_t)data[8] << 8) | data[7];
uint16_t touch_screen_x = ((uint16_t)data[10] << 8) | data[9];
uint16_t touch_screen_y = ((uint16_t)data[12] << 8) | data[11];
uint8_t cfg_buf[5];
cfg_buf[0] = 0x41;
cfg_buf[1] = 0x00;
cfg_buf[2] = 0x01;
cfg_buf[3] = 0x5A;
cfg_buf[4] = 0x5A;
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x8040);
I2C_WriteData(I2C1, 0x02);
I2C_Stop(I2C1);
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x8046);
I2C_WriteData(I2C1, 0x01);
I2C_Stop(I2C1);
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x8047);
I2C_WriteData(I2C1, threshold & 0xFF);
I2C_Stop(I2C1);
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x8048);
I2C_WriteData(I2C1, threshold >> 8);
I2C_Stop(I2C1);
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x804D);
I2C_WriteData(I2C1, touch_cfg);
I2C_Stop(I2C1);
cfg_buf[1] = touch_panel_x >> 8;
cfg_buf[2] = touch_panel_x & 0xFF;
cfg_buf[3] = touch_panel_y >> 8;
cfg_buf[4] = touch_panel_y & 0xFF;
GT911_WriteConfig(0x8150, cfg_buf, 5);
cfg_buf[1] = touch_screen_x >> 8;
cfg_buf[2] = touch_screen_x & 0xFF;
cfg_buf[3] = touch_screen_y >> 8;
cfg_buf[4] = touch_screen_y & 0xFF;
GT911_WriteConfig(0x8140, cfg_buf, 5);
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, 0x8040);
I2C_WriteData(I2C1, 0x01);
I2C_Stop(I2C1);
}
void GT911_WriteConfig(uint16_t addr, uint8_t *buf, uint8_t len)
{
I2C_StartTransmission(I2C1, GT911_ADDR, I2C_Direction_Transmitter);
I2C_WriteData(I2C1, addr >> 8);
I2C_WriteData(I2C1, addr & 0xFF);
for (int i = 0; i < len; i++) {
I2C_WriteData(I2C1, buf[i]);
}
I2C_Stop(I2C1);
}
```
这段代码中使用了`GT911_WriteConfig`函数来写入配置数据,需要定义如下的I2C读写函数:
```c
void I2C_StartTransmission(I2C_TypeDef *I2Cx, uint8_t address, uint8_t direction)
{
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2Cx, ENABLE);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, address, direction);
if (direction == I2C_Direction_Transmitter) {
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
} else if (direction == I2C_Direction_Receiver) {
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
}
}
void I2C_WriteData(I2C_TypeDef *I2Cx, uint8_t data)
{
I2C_SendData(I2Cx, data);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
uint8_t I2C_ReadData(I2C_TypeDef *I2Cx, uint8_t ack)
{
if (ack == I2C_ACK) {
I2C_AcknowledgeConfig(I2Cx, ENABLE);
} else {
I2C_AcknowledgeConfig(I2Cx, DISABLE);
}
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
uint8_t data = I2C_ReceiveData(I2Cx);
return data;
}
void I2C_Stop(I2C_TypeDef *I2Cx)
{
I2C_GenerateSTOP(I2Cx, ENABLE);
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_STOPF));
}
```
其中,`I2C_StartTransmission`函数用于启动I2C总线传输,`I2C_WriteData`函数用于写入数据,`I2C_ReadData`函数用于读取数据,`I2C_Stop`函数用于停止I2C总线传输。需要根据实际情况更改函数名称和参数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)