【兆易GD32H759I-EVAL】LCD触摸屏测试
[复制链接]
本帖最后由 TL-LED 于 2024-5-15 13:55 编辑
这篇测试下开发板LCD触摸屏。代码参考原点电子。
一、硬件部分
1.1、触摸部分电路图
开发板板上的触摸屏是电阻屏,使用SPI4接口。
1.2、数据手册中,SPI4引脚映射表
1.3、显示屏板上的触摸芯片型号XPT2046
1.4、xpt2046手册中连接示意图
二、程序部分
2.1、spi配置程序
刚开始测试,使用硬件总线SPI的方式,结果读出触摸屏的数据偏差较大,移植有点问题,软件配置如下
void touch_gpio_config(void)
{
//SPI总线方式
rcu_periph_clock_enable(RCU_GPIOF);
rcu_periph_clock_enable(RCU_GPIOH);
rcu_periph_clock_enable(RCU_SPI4);
rcu_spi_clock_config(IDX_SPI4, RCU_SPISRC_APB2);
/* connect port to SPI4_NSS -> PF6
SPI4_SCK -> PH6
SPI4_MISO -> PH7
SPI4_MOSI -> PF9 */
gpio_af_set(GPIOF, GPIO_AF_5, GPIO_PIN_9);
gpio_af_set(GPIOH, GPIO_AF_5, GPIO_PIN_6 | GPIO_PIN_7);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOH, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7);
gpio_output_options_set(GPIOH, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6 | GPIO_PIN_7 );
gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6);
gpio_bit_set(GPIOF, GPIO_PIN_6);
gpio_mode_set(GPIOG, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
}
void touch_spi_config(void)
{
spi_parameter_struct spi_init_struct;
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI4);
spi_struct_para_init(&spi_init_struct);
/* SPI4 parameter config */
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.data_size = SPI_DATASIZE_8BIT;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.prescale = SPI_PSC_64;
spi_init(SPI4, &spi_init_struct);
spi_byte_access_enable(SPI4);
/* enable SPI NSS output */
spi_nss_output_enable(SPI4);
spi_enable(SPI4);
}
//SPI读写数据
uint16_t spi_send_receive(uint16_t txdata)
{
uint16_t rxdata;
spi_master_transfer_start(SPI4, SPI_TRANS_START);
while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TP));
spi_i2s_data_transmit(SPI4, txdata);
while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TC));
rxdata = spi_i2s_data_receive(SPI4);
return rxdata;
}
//读AD值
uint16_t tp_read_ad(uint8_t cmd)
{
uint16_t adv=0;
TOUCH_SPI4_CS_LOW();
spi_send_receive(cmd);
// delay_us(0xfff);
adv=spi_send_receive(0x00);
adv=adv<<8;
adv|=spi_send_receive(0x00);
adv = adv>>4;
TOUCH_SPI4_CS_HIGH();
return adv;
}
通过应用文档了解到,STM32H系列和之前的F4系列的SPI软件上有较大区别,不兼容之前的程序。
后面测试使用模拟方式配置SPI,代码如下:
void delay_us(uint32_t ds)
{
uint32_t dr=0;
for(dr=0;dr<ds;dr++)
{
}
}
void tp_write_byte(uint8_t dat)
{
uint8_t ct = 0;
for (ct = 0; ct < 8; ct++)
{
if (dat & 0x80)
{
TP_MOSI(1);
}
else
{
TP_MOSI(0);
}
dat <<= 1;
TP_CLK(0);
delay_us(0xff);
TP_CLK(1);
delay_us(0xff);
}
}
uint16_t tp_read_ad(uint8_t cmd)
{
uint8_t ct = 0;
uint16_t num = 0;
TP_CLK(0);
TP_MOSI(0);
TP_CS(0);
tp_write_byte(cmd);
delay_us(0x7ff);
TP_CLK(0);
delay_us(0xff);
TP_CLK(1);
delay_us(0xff);
TP_CLK(0);
for (ct = 0; ct < 16; ct++)
{
num <<= 1;
TP_CLK(0);
delay_us(0xff);
TP_CLK(1);
delay_us(0xff);
if (TP_MISO==1)
{
num++;
}
}
num >>= 4;
TP_CS(1);
return num;
}
2.2、touch.c
#include "main.h"
char *const TP_REMIND_MSG_TBL = "Please use the stylus click the cross on the screen.The cross will always move until the screen adjustment is completed.";
#define TP_READ_TIMES 5 /* 读取次数 */
#define TP_LOST_VAL 1 /* 丢弃值 */
#define TP_ERR_RANGE 50 /* 误差范围 */
_m_tp_dev tp_dev =
{
init_touch,
touch_scan,
touch_adjust,
0,0,0,0,0,0,0,0,
};
//static void lcd_data_write(uint32_t data)
//{
// while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TP));
// spi_i2s_data_transmit(SPI4, data);
// while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TC));
//}
//void spi_send_receive(uint8_t *tx_buffer, uint8_t *rx_buffer, uint16_t length)
//{
// /* 等待SPI0通讯完成 */
// while(RESET != spi_i2s_flag_get(SPI4, SPI_FLAG_TP));
//
// /* 发送数据 */
// for(uint16_t i = 0; i < length; i++){
// spi_i2s_data_transmit(SPI4, tx_buffer[i]);
// /* 等待数据被接收 */
// while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TC));
// rx_buffer[i] = spi_i2s_data_receive(SPI4);
// }
//}
void delay_us(uint32_t ds)
{
uint32_t dr=0;
for(dr=0;dr<ds;dr++)
{
}
}
////SPI读写数据
//uint16_t spi_send_receive(uint16_t txdata)
//{
// uint16_t rxdata;
//
// spi_master_transfer_start(SPI4, SPI_TRANS_START);
// while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TP));
// spi_i2s_data_transmit(SPI4, txdata);
//
// while(RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_TC));
//
// rxdata = spi_i2s_data_receive(SPI4);
// return rxdata;
//
//}
void tp_write_byte(uint8_t dat)
{
uint8_t ct = 0;
for (ct = 0; ct < 8; ct++)
{
if (dat & 0x80)
{
TP_MOSI(1);
}
else
{
TP_MOSI(0);
}
dat <<= 1;
TP_CLK(0);
delay_us(0xff);
TP_CLK(1);
delay_us(0xff);
}
}
uint16_t tp_read_ad(uint8_t cmd)
{
uint8_t ct = 0;
uint16_t num = 0;
TP_CLK(0);
TP_MOSI(0);
TP_CS(0);
tp_write_byte(cmd);
delay_us(0x7ff);
TP_CLK(0);
delay_us(0xff);
TP_CLK(1);
delay_us(0xff);
TP_CLK(0);
for (ct = 0; ct < 16; ct++)
{
num <<= 1;
TP_CLK(0);
delay_us(0xff);
TP_CLK(1);
delay_us(0xff);
if (TP_MISO==1)
{
num++;
}
}
num >>= 4;
TP_CS(1);
return num;
}
////读AD值
//uint16_t tp_read_ad(uint8_t cmd)
//{
// uint16_t adv=0;
// TOUCH_SPI4_CS_LOW();
// spi_send_receive(cmd);
//
//// delay_us(0xfff);
//
// adv=spi_send_receive(0x00);
// adv=adv<<8;
// adv|=spi_send_receive(0x00);
//
// adv = adv>>4;
// TOUCH_SPI4_CS_HIGH();
// return adv;
//}
//读x或y一个坐标值
static uint16_t tp_read_xoy(uint8_t cmd)
{
uint16_t i, j;
uint16_t buf[TP_READ_TIMES];
uint16_t sum = 0;
uint16_t temp;
for (i = 0; i < TP_READ_TIMES; i++)
{
buf[i] = tp_read_ad(cmd);
}
for (i = 0; i < TP_READ_TIMES - 1; i++)
{
for (j = i + 1; j < TP_READ_TIMES; j++)
{
if (buf[i] > buf[j])
{
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
sum = 0;
for (i = TP_LOST_VAL; i < TP_READ_TIMES - TP_LOST_VAL; i++)
{
sum += buf[i];
}
temp = sum / (TP_READ_TIMES - 2 * TP_LOST_VAL);
return temp;
}
//读xy坐标值
static void tp_read_xy(uint16_t *x, uint16_t *y)
{
uint16_t xval, yval;
// if (tp_dev.touchtype & 0X01)
// {
xval = tp_read_xoy(0X90);
yval = tp_read_xoy(0XD0);
// }
// else
// {
// xval = tp_read_xoy(0xD0);
// yval = tp_read_xoy(0x90);
// }
*x = xval;
*y = yval;
}
static uint8_t tp_read_xy2(uint16_t *x, uint16_t *y)
{
uint16_t x1, y1;
uint16_t x2, y2;
tp_read_xy(&x1, &y1); /* 读取第一次数据 */
tp_read_xy(&x2, &y2); /* 读取第二次数据 */
/* 前后两次采样在+-TP_ERR_RANGE内 */
if (((x2 <= x1 && x1 < x2 + TP_ERR_RANGE) || (x1 <= x2 && x2 < x1 + TP_ERR_RANGE)) &&
((y2 <= y1 && y1 < y2 + TP_ERR_RANGE) || (y1 <= y2 && y2 < y1 + TP_ERR_RANGE)))
{
*x = (x1 + x2) / 2;
*y = (y1 + y2) / 2;
return 1;
}
return 0;
}
static void tp_draw_touch_point(uint16_t x, uint16_t y, uint16_t color)
{
LCDDrawLine(x - 12, y, x + 13, y, color);
LCDDrawLine(x, y - 12, x, y + 13, color);
LCDDrawPoint(x + 1, y + 1, color);
LCDDrawPoint(x - 1, y + 1, color);
LCDDrawPoint(x + 1, y - 1, color);
LCDDrawPoint(x - 1, y - 1, color);
LCDDrawCircle(x, y, 6, color);
}
void tp_draw_big_point(uint16_t x, uint16_t y, uint16_t color)
{
LCDDrawPoint(x, y, color);
LCDDrawPoint(x + 1, y, color);
LCDDrawPoint(x, y + 1, color);
LCDDrawPoint(x + 1, y + 1, color);
}
uint8_t touch_scan(uint8_t mode)
{
if (TP_PEN == 0)
{
if (mode)
{
tp_read_xy2(&tp_dev.x[0], &tp_dev.y[0]);
}
else if (tp_read_xy2(&tp_dev.x[0], &tp_dev.y[0]))
{
tp_dev.x[0] = (signed short)(tp_dev.x[0] - tp_dev.xc) / tp_dev.xfac + LCD_PIXEL_WIDTH/2;
tp_dev.y[0] = (signed short)(tp_dev.y[0] - tp_dev.yc) / tp_dev.yfac + LCD_PIXEL_HEIGHT/2;
}
if ((tp_dev.sta & TP_PRES_DOWN) == 0)
{
tp_dev.sta = TP_PRES_DOWN | TP_CATH_PRES;
tp_dev.x[CT_MAX_TOUCH - 1] = tp_dev.x[0];
tp_dev.y[CT_MAX_TOUCH - 1] = tp_dev.y[0];
}
}
else
{
if (tp_dev.sta & TP_PRES_DOWN)
{
tp_dev.sta &= ~TP_PRES_DOWN;
}
else
{
tp_dev.x[CT_MAX_TOUCH - 1] = 0;
tp_dev.y[CT_MAX_TOUCH - 1] = 0;
tp_dev.x[0] = 0xffff;
tp_dev.y[0] = 0xffff;
}
}
return tp_dev.sta & TP_PRES_DOWN;
}
void tp_save_adjust_data(void)
{
uint8_t temp = 0;
uint8_t *p = (uint8_t *)&tp_dev.xfac;
/* p指向tp_dev.xfac的地址, p+4则是tp_dev.yfac的地址
* p+8则是tp_dev.xoff的地址,p+10,则是tp_dev.yoff的地址
* 总共占用12个字节(4个参数)
* p+12用于存放标记电阻触摸屏是否校准的数据(0X0A)
* 往p[12]写入0X0A. 标记已经校准过.
*/
eeprom_buffer_write(p, 0x00, 12);
temp=0x0a;
eeprom_buffer_write(&temp, 0x0c, 1);
}
uint8_t tp_get_adjust_data(void)
{
uint8_t *p = (uint8_t *)&tp_dev.xfac;
uint8_t temp = 0;
eeprom_buffer_write(&temp, 0x0c, 1);
eeprom_buffer_read(p, 0x00, 12);
eeprom_buffer_read(&temp, 0x0c, 1);
if (temp == 0x0A)
{
return 1;
}
return 0;
}
void tp_adjust_info_show(uint16_t xy[5][2], double px, double py)
{
uint8_t i;
char sbuf[20];
for (i = 0; i < 5; i++)
{
sprintf(sbuf, "x%d:%d", i + 1, xy[i][0]);
LCDShowString(40, 160 + (i * 20), LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_RED, NULL, sbuf);
sprintf(sbuf, "y%d:%d", i + 1, xy[i][1]);
LCDShowString(40 + 80, 160 + (i * 20), LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_RED, NULL, sbuf);
}
LCDFillPixel(40, 160 + (i * 20), LCD_PIXEL_WIDTH - 1, 16, LCD_COLOR_WHITE);
sprintf(sbuf, "px:%0.2f", px);
sbuf[7] = 0;
LCDShowString(40, 160 + (i * 20), LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_RED, NULL, sbuf);
sprintf(sbuf, "py:%0.2f", py);
sbuf[7] = 0;
LCDShowString(40 + 80, 160 + (i * 20), LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_RED, NULL, sbuf);
}
void touch_adjust(void)
{
uint16_t pxy[5][2];
uint8_t cnt = 0;
short s1, s2, s3, s4;
double px, py;
uint16_t outtime = 0;
cnt = 0;
LCDClear(LCD_COLOR_WHITE);
LCDShowString(40, 40, LCD_PIXEL_WIDTH-80, 16, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_RED, NULL,TP_REMIND_MSG_TBL);
tp_draw_touch_point(20, 20, LCD_COLOR_RED);
tp_dev.sta = 0;
while (1)
{
tp_dev.scan(1);
if ((tp_dev.sta & 0xc000) == TP_CATH_PRES)
{
outtime = 0;
tp_dev.sta &= ~TP_CATH_PRES;
pxy[cnt][0] = tp_dev.x[0];
pxy[cnt][1] = tp_dev.y[0];
cnt++;
switch (cnt)
{
case 1:
tp_draw_touch_point(20, 20, LCD_COLOR_WHITE);
tp_draw_touch_point(LCD_PIXEL_WIDTH - 20, 20, LCD_COLOR_RED);
break;
case 2:
tp_draw_touch_point(LCD_PIXEL_WIDTH - 20, 20, LCD_COLOR_WHITE);
tp_draw_touch_point(20, LCD_PIXEL_HEIGHT - 20, LCD_COLOR_RED);
break;
case 3:
tp_draw_touch_point(20, LCD_PIXEL_HEIGHT - 20, LCD_COLOR_WHITE);
tp_draw_touch_point(LCD_PIXEL_WIDTH - 20, LCD_PIXEL_HEIGHT - 20, LCD_COLOR_RED);
break;
case 4:
LCDClear(LCD_COLOR_WHITE);
tp_draw_touch_point(LCD_PIXEL_WIDTH / 2, LCD_PIXEL_HEIGHT / 2, LCD_COLOR_RED);
break;
case 5:
s1 = pxy[1][0] - pxy[0][0];
s3 = pxy[3][0] - pxy[2][0];
s2 = pxy[3][1] - pxy[1][1];
s4 = pxy[2][1] - pxy[0][1];
px = (double)s1 / s3;
py = (double)s2 / s4;
if (px < 0)px = -px;
if (py < 0)py = -py;
if (px < 0.95 || px > 1.05 || py < 0.95 || py > 1.05 ||
abs(s1) > 4095 || abs(s2) > 4095 || abs(s3) > 4095 || abs(s4) > 4095 ||
abs(s1) == 0 || abs(s2) == 0 || abs(s3) == 0 || abs(s4) == 0
)
{
cnt = 0;
tp_draw_touch_point(LCD_PIXEL_WIDTH / 2, LCD_PIXEL_HEIGHT / 2, LCD_COLOR_WHITE);
tp_draw_touch_point(20, 20, LCD_COLOR_RED);
tp_adjust_info_show(pxy, px, py);
continue;
}
tp_dev.xfac = (float)(s1 + s3) / (2 * (LCD_PIXEL_WIDTH - 40));
tp_dev.yfac = (float)(s2 + s4) / (2 * (LCD_PIXEL_HEIGHT - 40));
tp_dev.xc = pxy[4][0];
tp_dev.yc = pxy[4][1];
LCDClear(LCD_COLOR_WHITE);
LCDShowString(35, 110, LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_BLUE, NULL, "Touch Screen Adjust OK!");
delay_1ms(1000);
tp_save_adjust_data();
LCDClear(LCD_COLOR_WHITE);
return;
}
}
delay_1ms(10);
outtime++;
if (outtime > 1000)
{
tp_get_adjust_data();
break;
}
}
}
//模拟SPI接口定义
void spi_gpio_config(void)
{
//模拟接口方式
rcu_periph_clock_enable(RCU_GPIOF);
rcu_periph_clock_enable(RCU_GPIOH);
//SPI4_MISO
gpio_mode_set(GPIOH, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_7);
//gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6);
//gpio_bit_set(GPIOF, GPIO_PIN_6);
//SPI4_SCK
gpio_mode_set(GPIOH, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6 );
gpio_output_options_set(GPIOH, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6 );
gpio_bit_set(GPIOH, GPIO_PIN_6);
//CS
gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_9);
gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6 | GPIO_PIN_9);
gpio_bit_set(GPIOF, GPIO_PIN_6);
gpio_bit_set(GPIOF, GPIO_PIN_9);
//T_PEN
gpio_mode_set(GPIOG, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
}
void touch_gpio_config(void)
{
//SPI总线方式
rcu_periph_clock_enable(RCU_GPIOF);
rcu_periph_clock_enable(RCU_GPIOH);
rcu_periph_clock_enable(RCU_SPI4);
rcu_spi_clock_config(IDX_SPI4, RCU_SPISRC_APB2);
/* connect port to SPI4_NSS -> PF6
SPI4_SCK -> PH6
SPI4_MISO -> PH7
SPI4_MOSI -> PF9 */
gpio_af_set(GPIOF, GPIO_AF_5, GPIO_PIN_9);
gpio_af_set(GPIOH, GPIO_AF_5, GPIO_PIN_6 | GPIO_PIN_7);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOH, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7);
gpio_output_options_set(GPIOH, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6 | GPIO_PIN_7 );
gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6);
gpio_bit_set(GPIOF, GPIO_PIN_6);
gpio_mode_set(GPIOG, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
}
void touch_spi_config(void)
{
spi_parameter_struct spi_init_struct;
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI4);
spi_struct_para_init(&spi_init_struct);
/* SPI4 parameter config */
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.data_size = SPI_DATASIZE_8BIT;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.prescale = SPI_PSC_64;
spi_init(SPI4, &spi_init_struct);
spi_byte_access_enable(SPI4);
/* enable SPI NSS output */
spi_nss_output_enable(SPI4);
spi_enable(SPI4);
}
uint8_t init_touch(void)
{
spi_gpio_config();
//touch_gpio_config();
//touch_spi_config();
tp_read_xy(&tp_dev.x[0], &tp_dev.y[0]);
if (tp_get_adjust_data())
{
return 0;
}
else
{
LCDClear(LCD_COLOR_WHITE);
touch_adjust();
tp_save_adjust_data();
}
tp_get_adjust_data();
return 1;
}
2.3、touch.h
#ifndef _TOUCH_H
#define _TOUCH_H
#include "stdint.h"
#define T_PEN_GPIO_PORT GPIOG
#define T_PEN_GPIO_PIN GPIO_PIN_3
#define T_CS_GPIO_PORT GPIOF
#define T_CS_GPIO_PIN GPIO_PIN_6
#define T_MISO_GPIO_PORT GPIOH
#define T_MISO_GPIO_PIN GPIO_PIN_7
#define T_MOSI_GPIO_PORT GPIOF
#define T_MOSI_GPIO_PIN GPIO_PIN_9
#define T_CLK_GPIO_PORT GPIOH
#define T_CLK_GPIO_PIN GPIO_PIN_6
#define TP_PEN gpio_input_bit_get(T_PEN_GPIO_PORT, T_PEN_GPIO_PIN)
#define TP_MISO gpio_input_bit_get(T_MISO_GPIO_PORT, T_MISO_GPIO_PIN)
#define TP_MOSI(x) do{ x ? \
gpio_bit_set(T_MOSI_GPIO_PORT, T_MOSI_GPIO_PIN): \
gpio_bit_reset(T_MOSI_GPIO_PORT, T_MOSI_GPIO_PIN); \
}while(0) /* T_MOSI */
#define TP_CLK(x) do{ x ? \
gpio_bit_set(T_CLK_GPIO_PORT, T_CLK_GPIO_PIN) : \
gpio_bit_reset(T_CLK_GPIO_PORT, T_CLK_GPIO_PIN); \
}while(0) /* T_CLK */
#define TP_CS(x) do{ x ? \
gpio_bit_set(T_CS_GPIO_PORT, T_CS_GPIO_PIN) : \
gpio_bit_reset(T_CS_GPIO_PORT, T_CS_GPIO_PIN); \
}while(0) /* T_CS */
#define TP_PRES_DOWN 0x8000
#define TP_CATH_PRES 0x4000
#define CT_MAX_TOUCH 10
typedef struct
{
uint8_t (*init)(void);
uint8_t (*scan)(uint8_t);
void (*adjust)(void);
uint16_t x[CT_MAX_TOUCH];
uint16_t y[CT_MAX_TOUCH];
uint16_t sta;
float xfac;
float yfac;
short xc;
short yc;
uint8_t touchtype;
} _m_tp_dev;
extern _m_tp_dev tp_dev;
uint8_t init_touch(void);
//uint16_t spi_send_receive(uint16_t txdata);
void touch_adjust(void);
uint8_t touch_scan(uint8_t mode);
void tp_draw_big_point(uint16_t x, uint16_t y, uint16_t color);
#endif
2.4、main.c
#include "main.h"
void cache_enable(void);
void load_draw_dialog(void)
{
LCDClear(LCD_COLOR_WHITE);
LCDShowString(LCD_PIXEL_WIDTH - 40, 0, 200, 16, LCD_FONT_16, LCD_TEXT_TRANS, LCD_COLOR_BLUE, NULL,"CLEAR");
}
void touch_test(void)
{
while (1)
{
tp_dev.scan(0);
if (tp_dev.sta & TP_PRES_DOWN)
{
if (tp_dev.x[0] < LCD_PIXEL_WIDTH && tp_dev.y[0] < LCD_PIXEL_HEIGHT)
{
if (tp_dev.x[0] > (LCD_PIXEL_WIDTH - 24) && tp_dev.y[0] < 16)
{
load_draw_dialog();
}
else
{
tp_draw_big_point(tp_dev.x[0], tp_dev.y[0], LCD_COLOR_RED);
}
}
}
else
{
delay_1ms(10);
}
}
}
int main(void)
{
uint8_t x=0;
uint32_t sd=0;
uint32_t yd=0;
cache_enable();
systick_config();
init_usart(115200);
init_led();
gpio_config();
i2c_config();
i2c_eeprom_init();
exmc_synchronous_dynamic_ram_init(EXMC_SDRAM_DEVICE0);
init_lcd();
LCDDisplayDir(LCD_SCREEN_HORIZONTAL);
LCDClear(LCD_COLOR_WHITE);
tp_dev.init();
load_draw_dialog();
touch_test();
while(1)
{
delay_1ms(100);
}
}
void cache_enable(void)
{
/* enable i-cache */
SCB_EnableICache();
/* enable d-cache */
SCB_EnableDCache();
}
三、运行结果
下载程序后,开发板运行视频
touch
|