xpt 2046的触摸屏 rt thread设备驱动框架

1 基于rtt 开发触摸屏驱动

准备使用rtt 框架 , 驱动xpt 2046的触摸屏, 翻阅大量资料发现, 大部分文章强调的是时序图, 而且很多代码要么直接操作寄存器, 要么是io 口模拟, 只能用于特定的单片机, 对于移植非常不便. 在这里就不对其时序图进行描述, 直接放程序逻辑以及相关代码, 参考别人的时序图即可无障碍阅读.

2 编写驱动

首先, 在目标平台上, 配置好总线驱动, 可以用 list_device 查看到目标总线
然后, 驱动触摸屏只需要两个主要函数, 1 初始化函数, 2 读取电阻值函数

3 初始化代码

初始化, 目前主要是注册到spi 子设备到总线上, 搭配一个片选 io 口即可实现挂载spi 设备

#include "drv_touch.h"
#include "rtdevice.h"
#include "drv_common.h"
#include "stm32f4xx.h"
#include "drv_spi.h"
#include "rtdbg.h"
#define DBG_TAG "touch"

#define X_LEN 320 // 屏幕分辨率
#define Y_LEN 240 // 屏幕分辨率

#define X_MAX 7508 // 实际x 方向最大采样值
#define X_MIN 384  // 实际y 方向最大采样值
#define Y_MAX 7632 // 实际x 方向最小采样值
#define Y_MIN 448  // 实际x 方向最小采样值


#define T_SPI_BUS "spi1" // 使用spi1 作为屏幕接口, 速度更快 触摸也挂上去

#define T_SPI_DEVICE "spi11"


#define XPT_PIN GET_PIN(B, 0) // 笔中断引脚

static struct rt_spi_device *spi_dev_t;
void touch_init(void)
{

    rt_pin_mode(XPT_PIN, PIN_MODE_INPUT_PULLUP); // 设置感应笔引脚
    // 注册spi 设备
        __HAL_RCC_GPIOB_CLK_ENABLE();
          rt_hw_spi_device_attach(T_SPI_BUS, T_SPI_DEVICE, GPIOB, GPIO_PIN_1);
          spi_dev_t = (struct rt_spi_device *)rt_device_find(T_SPI_DEVICE);

          /* config spi */
          {
              struct rt_spi_configuration cfg;
              cfg.data_width = 8;
              cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_3| RT_SPI_MSB;
              cfg.max_hz = 10 * 1000 * 1000; /* 42M,SPI max 42MHz,lcd 4-wire spi */

              rt_spi_configure(spi_dev_t, &cfg);
          }

        //  return RT_EOK;
}
INIT_APP_EXPORT(touch_init);

4 读取数值

1 读取状态 被按下标志, 以及坐标转换
在这里为了方便, 写了两个驱动函数, get_x_y_raw 是未转换之前的最大值最小值, 用来校准坐标用, 比如触摸笔按下0点的位置, 记录下最大最小的 x_min y_min 的值, 以及右下角的位置, 记录 y_max x_max的数值, 即可完成校准, 目前没有做自动校准流程
2 在gui 的驱动函数中, 调用 get_x_y 函数, 即可获取坐标点, 同时适配 rtt
3 本次测试中, 触摸屏设备, 和液晶屏设备挂载在同一个spi 总线上, 测试没有什么问题, 帧率可达25帧, 可用 , 测试平台 rt thread studio + lvgl 7.11 + stm32f411ccu6 + 320 * 240 ips 屏

int8_t sendbuf[3],recv_buf[3];



uint8_t is_preass(void)
{
uint8_t res =   rt_pin_read(XPT_PIN);
}


void get_x_y_raw(uint16_t *x,uint16_t *y) // 获取 坐标的采样值 , 用来校准用
{
uint16_t res;

    sendbuf[0] =  0x90; //  x
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);
    rt_hw_us_delay(50);// rt_thread_delay(1);
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);

    res = (recv_buf[0]<<8) + recv_buf[1];

    *x = res>>2;




    sendbuf[0] =  0xd0; //  y
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);
   // rt_thread_delay(1);
    rt_hw_us_delay(50);
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);

    res = (recv_buf[0]<<8) + recv_buf[1];

    *y = res>>2;


}
static uint16_t x_buf[3],y_buf[3];
static uint8_t x_cnt = 0,y_cnt = 0;  // 求平均滤波

void get_x_y(uint16_t *x,uint16_t *y)
{
uint16_t res;
x_cnt = 0,y_cnt = 0;

    sendbuf[0] =  0x90; //  x
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);
    rt_hw_us_delay(50);// rt_thread_delay(1);
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);    res = (recv_buf[0]<<8) + recv_buf[1];x_buf[x_cnt%3] = res >> 2; x_cnt ++;
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);    res = (recv_buf[0]<<8) + recv_buf[1];x_buf[x_cnt%3] = res >> 2; x_cnt ++;
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);    res = (recv_buf[0]<<8) + recv_buf[1];x_buf[x_cnt%3] = res >> 2; x_cnt ++;
    res = (recv_buf[0]<<8) + recv_buf[1];


    *x = (x_buf[0] + x_buf[1] + x_buf[2])/3;

  //  *x =( X_MAX - *x ) * X_LEN / (X_MAX - X_MIN); //
    *x =( *x - X_MIN ) * X_LEN / (X_MAX - X_MIN); // 取反操作
    if(*x > X_LEN)
        *x = 320;
    if(*x < 0 )
        *x = 0;



    sendbuf[0] =  0xd0; //  y
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2);
   // rt_thread_delay(1);
    rt_hw_us_delay(50);
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2); res = (recv_buf[0]<<8) + recv_buf[1];y_buf[y_cnt%3] = res >> 2;y_cnt ++;
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2); res = (recv_buf[0]<<8) + recv_buf[1];y_buf[y_cnt%3] = res >> 2;y_cnt ++;
    rt_spi_send_then_recv(spi_dev_t,sendbuf,1,recv_buf,2); res = (recv_buf[0]<<8) + recv_buf[1];y_buf[y_cnt%3] = res >> 2;y_cnt ++;




    *y = (y_buf[0] + y_buf[1] + y_buf[2])/3;

    *y =( Y_MAX - *y ) * Y_LEN / (Y_MAX - Y_MIN);

    if(*y > Y_LEN)
        *y = 240;
    if(*y < 0 )
        *y = 0;
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值