LCD触摸屏功能实现控制电机的设计

电子说

1.3w人已加入

描述

LCD屏显示实现触摸功能

驱动320* 240 ST7789的SPI屏幕进行内容显示,显示文字和触控屏的基本使用;参考了:驱动模型中定义了可供上层应用调用的关键接口,如 读/写framebuffer 、开/关触控动作的操作画面 、获取屏幕设备信息 、 设置亮度/对比度/像素格式/方向等。

下图为触控屏的四个触控开/关四个电机的“按键”

SPI

图一(上电开机后显示)

SPI

图二(1号电机工作)

SPI

图三(2号电机工作)

SPI

图四(3号电机工作)

SPI

图五(4号电机工作)

SPI

图六(等待操作的状态)

显示触控屏的常用API接口

void display_get_capabilities(const struct device *dev, struct display_capabilities *capabilities)

参数说明

SPI

图七

显示屏设备树配置

&csk6002_9s_nano_pinctrl{
            ...
            /* 显示屏SPI接口配置 */
            pinctrl_spi0_sclk_default: spi0_sclk_default {
                    pinctrls = < &pinmuxa 15 6 >;
            };
            pinctrl_spi0_mosi_default: spi0_mosi_default {
                    pinctrls = < &pinmuxa 10 6 >;
            };
            pinctrl_spi0_miso_default: spi0_miso_default {
                    pinctrls = < &pinmuxa 17 6 >;
            };
            pinctrl_spi0_cs_default: spi0_cs_default {
                    pinctrls = < &pinmuxa 12 6 >;
            }; 
};

触摸屏设备树配置

用到引脚:i2c0_scl(pb2)、i2c0_sda(pb3)。

在 csk6011a_nano.overlay中完成外设接口的配置,具体实现如下:

在app/boards/目录下的csk6002_9s_nano.overlay文件并添加如下设备树配置:

SPI

图八

&csk6002_9s_nano_pinctrl{
   // ...
    /* 触摸屏I2C接口配置 */
    pinctrl_i2c0_scl_default: i2c0_scl_default{
            pinctrls = <&pinmuxb 2 8>;
    };
    
    pinctrl_i2c0_sda_default: i2c0_sda_default{
            pinctrls = <&pinmuxb 3 8>;
    }; 
};

&i2c0 {
        status = "okay";
        pinctrl-0 = <&pinctrl_i2c0_scl_default &pinctrl_i2c0_sda_default>; 
        pinctrl-names = "default";
        ft5336@0 {
                compatible = "focaltech,ft5336";
                reg = <0x38>;
                label = "FT5336";
                status = "okay";
                int-gpios = <&gpioa 3 0>;
        };
};

日志串口设备树配置

本示例中将SDK默认的日志串口(GPIOA_2、GPIOA_3)中的GPIOA_3复用为触控屏的int使能脚,因此将日志输出串口配置为GPIOb_10 、GPIOb_11,具体如下:

&csk6002_9s_nano_pinctrl{  
            /* 日志串口配置 */
            pinctrl_uart0_rx_default: uart0_rx_default{
                    pinctrls = <&pinmuxb 10 2>;
            };
            
            pinctrl_uart0_tx_default: uart0_tx_default{
                    pinctrls = <&pinmuxb 11 2>;
            };
            ...
};

触摸屏组件配置

在prj.conf文件中打开触摸屏功能配置:

触摸配置

CONFIG_KSCAN=y

I2C功能配置

CONFIG_I2C=y

触摸屏屏驱动配置

CONFIG_KSCAN_BL6XXX=y

显示屏组件配置

在prj.conf文件中打开显示屏功能配置:

CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_LOG=y

GPIO功能配置

CONFIG_GPIO=y

显示功能配置

CONFIG_DISPLAY=y

spi功能配置

CONFIG_SPI=y

ST7789V显示屏驱动配置

CONFIG_ST7789V=y

LCD屏显示实现

void main(void)
{
    size_t x;
    size_t y;
    size_t rect_w;
    size_t rect_h;
    size_t h_step;
    size_t scale;
    size_t grey_count;
    uint8_t *buf;
    int32_t grey_scale_sleep;
    const struct device *display_dev;
    struct display_capabilities capabilities;
    struct display_buffer_descriptor buf_desc;
    size_t buf_size = 0;
    fill_buffer fill_buffer_fnc = NULL;

    LOG_INF("Display sample for %s", DISPLAY_DEV_NAME);
    
    /* kscan初始化 */
    kscan_init();
    
    /* 获取display设备实例 */
    display_dev = device_get_binding(DISPLAY_DEV_NAME);

    if (display_dev == NULL) {
        LOG_ERR("Device %s not found. Aborting sample.",
            DISPLAY_DEV_NAME);
        RETURN_FROM_MAIN(1);
    }
    
    /* 获取显示功能 */
    display_get_capabilities(display_dev, &capabilities);

    if (capabilities.screen_info & SCREEN_INFO_MONO_VTILED) {
        rect_w = 32;
        rect_h = 10;
    } else {
        rect_w = 2;
        rect_h = 1;
    }

    h_step = rect_h;
    scale = (capabilities.x_resolution / 8) / rect_h;

    rect_w *= scale;
    rect_h *= scale;

    if (capabilities.screen_info & SCREEN_INFO_EPD) {
        grey_scale_sleep = 10000;
    } else {
        grey_scale_sleep = 100;
    }

    buf_size = rect_w * rect_h;

    if (buf_size < (capabilities.x_resolution * h_step)) {
        buf_size = capabilities.x_resolution * h_step;
    }
    
    /* 色块配置 */
    switch (capabilities.current_pixel_format) {
    case PIXEL_FORMAT_ARGB_8888:
        fill_buffer_fnc = fill_buffer_argb8888;
        buf_size *= 4;
        break;
    case PIXEL_FORMAT_RGB_888:
        fill_buffer_fnc = fill_buffer_rgb888;
        buf_size *= 3;
        break;
    case PIXEL_FORMAT_RGB_565:
        fill_buffer_fnc = fill_buffer_rgb565;
        buf_size *= 2;
        break;
    case PIXEL_FORMAT_BGR_565:
        fill_buffer_fnc = fill_buffer_bgr565;
        buf_size *= 2;
        break;
    case PIXEL_FORMAT_MONO01:
    case PIXEL_FORMAT_MONO10:
        fill_buffer_fnc = fill_buffer_mono;
        buf_size /= 8;
        break;
    default:
        LOG_ERR("Unsupported pixel format. Aborting sample.");
        RETURN_FROM_MAIN(1);
    }

    buf = k_malloc(buf_size);

    if (buf == NULL) {
        LOG_ERR("Could not allocate memory. Aborting sample.");
        RETURN_FROM_MAIN(1);
    }
    
    (void)memset(buf, 0xFFu, buf_size);

    buf_desc.buf_size = buf_size;
    buf_desc.pitch = capabilities.x_resolution;
    buf_desc.width = capabilities.x_resolution;
    buf_desc.height = h_step;

    /*整屏填充白色背景*/
    for (int idx = 0; idx < capabilities.y_resolution; idx += h_step) {
        display_write(display_dev, 0, idx, &buf_desc, buf);
    }

    /*左上角填充红色块*/
    fill_buffer_fnc(TOP_LEFT, 0, buf, buf_size);
    x = 0;
    y = 0;
    display_write(display_dev, x, y, &buf_desc, buf);

    /*右上角填充绿色块*/
    fill_buffer_fnc(TOP_RIGHT, 0, buf, buf_size);
    x = capabilities.x_resolution - rect_w;
    y = 0;
    display_write(display_dev, x, y, &buf_desc, buf);

    /*右下角填充蓝色块*/
    fill_buffer_fnc(BOTTOM_RIGHT, 0, buf, buf_size);
    x = capabilities.x_resolution - rect_w;
    y = capabilities.y_resolution - rect_h;
    display_write(display_dev, x, y, &buf_desc, buf);
    
    /* 关闭显示消隐 */
    display_blanking_off(display_dev);

    grey_count = 0;
    x = 0;
    y = capabilities.y_resolution - rect_h;

    /*左下角灰色动态色块*/
    while (1) {
        fill_buffer_fnc(BOTTOM_LEFT, grey_count, buf, buf_size);
        display_write(display_dev, x, y, &buf_desc, buf);
        ++grey_count;
        k_msleep(grey_scale_sleep);
    }
}

触摸屏功能实现

Zephyr具备kscan(keyboard scan matrix)驱动模型,其驱动程序用于检测矩阵键盘或带有按钮的设备中的按键(用户触碰触摸屏本质上是生成一个行列坐标)。查阅zephyr官网Display Interface。

SPI

图九

注册callback函数

/* 触摸回调函数,打印坐标 */
static void k_callback(const struct device *dev, uint32_t row, uint32_t col,
               bool pressed)
{
    ARG_UNUSED(dev);
    printk("row = %u col = %u, pressed:%s\\n", row, col, pressed ? "TRUE" : "FLASE");
}

void kscan_init(void)
{   
    /* 获取kscan设备实例 */
    const struct device *kscan_dev = device_get_binding(DISPLAY_KSAN_DEV_NAME);

    if (!device_is_ready(kscan_dev)) {
        LOG_ERR("kscan device %s not ready", kscan_dev->name);
        return;
    }
    
    /* 注册回调,在k_callback中可看到对应的坐标与状态printk输出操作 */
    kscan_config(kscan_dev, k_callback);
    /* 使能回调 */
    kscan_enable_callback(kscan_dev);
}

void main(void)
{
    kscan_init();
    ...
}

编译的指令是:

lisa zep build

烧录的指令是:

lisa zep flash

结果

触控屏显示了四个触控开关符,分别触摸对应显示在(LED1符 LED2符 LED3符 LED4符的下面触控开关)。四个功能(GPIO) PWM 控制电机运行,后面补上(高发性的疫情过后,回公司上班补上,焊接上电机驱动板)。我们开发了一个PC上位机软件(视觉图分析用)。GC032A摄像头上传的图像以后可以在触控屏显(一个静态图)。

SPI

图十

审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分