15.1實驗內(nèi)容
通過本實驗主要學(xué)習(xí)以下內(nèi)容:
- LCD觸控原理
- SPI外設(shè)功能
15.2實驗原理
本實驗介紹通過SPI接口控制8080接口屏的touch功能。
15.3硬件設(shè)計
8080接口屏touch部分實現(xiàn)如下圖所示,通過SPI接口進行實現(xiàn)。
15.4代碼解析
15.4.1Touch 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.2SPI初始化配置
Touch SPI初始化配置如下所示:
JSON void bsp_spi_init(typdef_spi_struct *spix) { driver_spi_init(spix); } |
15.4.3屏幕touch狀態(tài)獲取函數(shù)
屏幕touch狀態(tài)獲取函數(shù)如下所示,通過該函數(shù)可掃描獲取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主函數(shù)
本例程主函數(shù)如下所示,在本例程中,8080液晶屏上顯示4個LED圖標,通過觸控點到哪個LED,MCU檢測到后打開對應(yīng)的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(); /* 延時和公共驅(qū)動部分初始化 */ 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實驗結(jié)果
將本例程燒錄到海棠派開發(fā)板中,通過手指點擊屏幕上的LED圖標,可以點亮對應(yīng)的LED.
本教程由GD32 MCU方案商聚沃科技原創(chuàng)發(fā)布,了解更多GD32 MCU教程,關(guān)注聚沃科技官網(wǎng)
-
單片機
+關(guān)注
關(guān)注
6039文章
44588瀏覽量
636758 -
嵌入式
+關(guān)注
關(guān)注
5087文章
19150瀏覽量
306356 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5087瀏覽量
97785 -
GD32
+關(guān)注
關(guān)注
7文章
404瀏覽量
24400
發(fā)布評論請先 登錄
相關(guān)推薦
評論