【海棠派开发板】第十五讲 TFT-8080电阻屏屏触摸实验

发布于 2024-09-18 17:49:55

15.1 实验内容

通过本实验主要学习以下内容:

• LCD触控原理

• SPI外设功能

15.2 实验原理

本实验介绍通过SPI接口控制8080接口屏的touch功能。

15.3 硬件设计

8080接口屏touch部分实现如下图所示,通过SPI接口进行实现。

15.4 代码解析

15.4.1 Touch SPI定义

Touch 的SPI定义如下所示:

JSON
//定义TOUCH_SPI
SPI_MASTER_SCK_GPIO_DEF(TOUCH_SPI,B,3,GPIO_AF_5);
SPI_MASTER_MOSI_GPIO_DEF(TOUCH_SPI,B,5,GPIO_AF_5);
SPI_MASTER_MISO_GPIO_DEF(TOUCH_SPI,B,4,GPIO_AF_5);
SPI_MASTER_CS_GPIO_DEF(TOUCH_SPI,A,8);

SPI_TX_DMA_DEF(TOUCH_SPI,SPI0,DMA1,DMA_CH1);
SPI_RX_DMA_DEF(TOUCH_SPI,SPI0,DMA1,DMA_CH0);
SPI_DEF(TOUCH_SPI,SPI0,SPI_MASTER,SPI_DATASIZE_8BIT,SPI_CK_PL_LOW_PH_1EDGE,SPI_PSC_64,SPI_ENDIAN_MSB,MODE_POLL);

15.4.2 SPI初始化配置

Touch SPI初始化配置如下所示:

JSON
void bsp_spi_init(typdef_spi_struct *spix)
{
driver_spi_init(spix);

}15.4.3 屏幕touch状态获取函数

屏幕touch状态获取函数如下所示,通过该函数可扫描获取touch位置。

JSON
Drv_Err bsp_touch_scan(uint16_t *x, uint16_t *y)
{
static uint8_t count=0;
uint16_t touch_ad_x,touch_ad_y = 0;
/* touch pen is active */
if (SET == bsp_touch_pen_irq())
{
if(++count>=20 || lcd_touch_panel_struct.TOUCH_State==1)
{
lcd_touch_panel_struct.TOUCH_State=1;
if((SUCCESS != bsp_touch_ad_xy_get(&touch_ad_x, &touch_ad_y))){
return DRV_ERROR;
}
//            if(invalid_count >= 20){
//                return DRV_ERROR;
//            }
}else
{
return DRV_ERROR;
}
}else{
count=0;
if(lcd_touch_panel_struct.TOUCH_State==1){
lcd_touch_panel_struct.TOUCH_State=2;
lcd_touch_panel_struct.Touch_x_Last=*x;
lcd_touch_panel_struct.Touch_y_Last=*y;
}
return DRV_ERROR;
}

*x=bsp_touch_coordinate_x_get(touch_ad_x);
*y=bsp_touch_coordinate_y_get(touch_ad_y);
if(count==20)
{
lcd_touch_panel_struct.TOUCH_State=1;
lcd_touch_panel_struct.Touch_x_First=*x;
lcd_touch_panel_struct.Touch_y_First=*y;
}
lcd_touch_panel_struct.Touch_x_Now=*x;
lcd_touch_panel_struct.Touch_y_Now=*y;

return DRV_SUCCESS;
}

15.4.4 主函数

本例程主函数如下所示,在本例程中,8080液晶屏上显示4个LED图标,通过触控点到哪个LED,MCU检测到后打开对应的LED灯。

JSON
int main(void)
{
uint16_t num[4]={0,0,0,0}; /* store the touch point number on four buttons */
uint8_t count=0;
char* led_string[4]={"LED1","LED2","LED3","LED4"} ;
uint8_t button_id=0;

driver_init();       /* 延时和公共驱动部分初始化 */
bsp_8080_lcd_init();                 /* 初始化LCD */
bsp_led_group_init();

//初始化SPI
bsp_spi_init(&TOUCH_SPI);

/* configure the GPIO of SPI touch panel */
bsp_touch_panel_init(bsp_8080_lcd_parameter.width,bsp_8080_lcd_parameter.height);

delay_ms(50);

/* clear the LCD screen */
bsp_8080_lcd_clear(WHITE);

bsp_8080_lcd_show_log();

bsp_8080_lcd_show_string(a1+30, b1+80,bsp_8080_lcd_parameter.width,FONT_ASCII_24_12,FONT_ASCII_24_12,led_string[0],BLUE);
/* draw picture of button on LCD screen */
bsp_8080_lcd_color_fill(a1+30,b1+40,a1+30+40-1,b1+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));

bsp_8080_lcd_show_string(a2+30, b1+80,bsp_8080_lcd_parameter.width,FONT_ASCII_24_12,FONT_ASCII_24_12,led_string[1],BLUE);
bsp_8080_lcd_color_fill(a2+30,b1+40,a2+30+40-1,b1+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));

bsp_8080_lcd_show_string(a1+30, b2+80,bsp_8080_lcd_parameter.width,FONT_ASCII_24_12,FONT_ASCII_24_12,led_string[2],BLUE);
bsp_8080_lcd_color_fill(a1+30,b2+40,a1+30+40-1,b2+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));

bsp_8080_lcd_show_string(a2+30, b2+80,bsp_8080_lcd_parameter.width,FONT_ASCII_24_12,FONT_ASCII_24_12,led_string[3],BLUE);
bsp_8080_lcd_color_fill(a2+30,b2+40,a2+30+40-1,b2+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));

while(1){
/* get the position of touch on LCD screen */
if(DRV_SUCCESS == bsp_touch_scan(&touch_x,&touch_y)){
count++;
get_touch_area(touch_x,touch_y,num);
}
else
{
count=0;
}
/*  generate response to the touch(turn on LED and change picture )*/
if(count==10){
button_id = find_max(num);
turn_on_led(button_id);
change_picture(button_id);
num[0]=num[1]=num[2]=num[3]=0;
count=0;
}
}
}

15.5 实验结果 将本例程烧录到海棠派开发板中,通过手指点击屏幕上的LED图标,可以点亮对应的LED.

0 条评论