STM32开发板电容触摸屏驱动,单点有效
扫描二维码
随时随地手机看文章
STM32开发板电容触摸屏驱动,单点有效
头文件touch.h:
u8 ft5x0x_read_data(void);
void ft5x0x_i2c_init(void);
struct _ts_event
{
u16 x1;
u16 y1;
u16 x2;
u16 y2;
u16 x3;
u16 y3;
u16 x4;
u16 y4;
u16 x5;
u16 y5;
u8 touch_point;
};
extern struct _ts_event ts_event;
源文件cap_touch.c
#include
#include "lcd.h"
#include "delay.h"
#include "stdlib.h"
#include "gui.h"
#include "myiic.h"
#include "touch.h"
struct _ts_event ts_event;
u8 ft5x0x_i2c_txdata(u8 *txdata, u8 length)
{
u8 ret =0;
int i;
IIC_Start();
//发送写命令
IIC_Send_Byte(0x74);
IIC_Wait_Ack();
//开始发送数据
for(i=0;i { IIC_Send_Byte(txdata[i]);//发送数据 IIC_Wait_Ack(); } IIC_Stop();//产生一个停止条件 delay_ms(10); return ret; } u8 ft5x0x_i2c_rxdata(u8 *rxdata, u8 length) { u8 ret; u8 *rxdatatmp = rxdata; IIC_Start(); //发送读命令 IIC_Send_Byte(0x75); IIC_Wait_Ack(); // IIC_Start(); //进入接收模式 for(ret=0;ret { rxdatatmp[ret]=IIC_Read_Byte(1); } IIC_Stop();//产生一个停止条件 return ret; } u8 ft5x0x_read_data(void) { u8 buf[32] = {0}; u8 ret = 0; #ifdef CONFIG_FT5X0X_MULTITOUCH ret = ft5x0x_i2c_rxdata(buf, 26); #else ret = ft5x0x_i2c_rxdata(buf, 7); #endif /* for(i=0;i<7;i++) { printf("buf[%d]=%drn",i,buf[i]); } */ ts_event.touch_point = buf[2] & 0xf;// 000 0111 if (ts_event.touch_point == 0) { return 0; } #ifdef CONFIG_FT5X0X_MULTITOUCH switch (ts_event.touch_point) { case 5: ts_event.x5 = (s16)(buf[0x1b] & 0x0F)<<8 | (s16)buf[0x1c]; ts_event.y5 = (s16)(buf[0x1d] & 0x0F)<<8 | (s16)buf[0x1e]; case 4: ts_event.x4 = (s16)(buf[0x15] & 0x0F)<<8 | (s16)buf[0x16]; ts_event.y4 = (s16)(buf[0x17] & 0x0F)<<8 | (s16)buf[0x18]; case 3: ts_event.x3 = (s16)(buf[0x0f] & 0x0F)<<8 | (s16)buf[0x10]; ts_event.y3 = (s16)(buf[0x11] & 0x0F)<<8 | (s16)buf[0x12]; case 2: ts_event.x2 = (s16)(buf[9] & 0x0F)<<8 | (s16)buf[10]; ts_event.y2 = (s16)(buf[11] & 0x0F)<<8 | (s16)buf[12]; case 1: ts_event.x1 = (s16)(buf[3] & 0x0F)<<8 | (s16)buf[4]; ts_event.y1 = (s16)(buf[5] & 0x0F)<<8 | (s16)buf[6]; break; default: return 0; } #else if (ts_event.touch_point == 1) { //读出的数据位480*800 ts_event.x1 = (s16)(buf[3] & 0x0F)<<8 | (s16)buf[4]; ts_event.y1 = (s16)(buf[5] & 0x0F)<<8 | (s16)buf[6]; //转换成320*480 ts_event.x1 = ts_event.x1*320/480; ts_event.y1 = ts_event.y1*480/800; ret = 1; } else { ts_event.x1 = 0xFFFF; ts_event.y1 = 0xFFFF; ret = 0; } #endif if (ts_event.touch_point == 1) printf("%d (%d, %d)rn", ts_event.touch_point, ts_event.x1, ts_event.y1); return ret; } void ft5x0x_i2c_reset(void) { GPIOE->ODR&=~(1<<4); //PE.4输出高 delay_ms(6); //手册上说至少要5ms复位时间 GPIOE->ODR|=1<<4; //PE.4输出高 } void ft5x0x_i2c_init(void) { GPIOE->CRL&=0XFFF0FFFF; GPIOE->CRL|=0X00030000;//PE.4推挽输出 GPIOE->ODR|=1<<4; //PE.4输出高 IIC_Init(); delay_ms(1); ft5x0x_i2c_reset(); ts_event.x1 = 0xFFFF; ts_event.y1 = 0xFFFF; }