测试下显示屏的电容触摸屏。
一、硬件电路
电容触摸屏连接开发板上的I2C1接口。
二、程序部分
2.1、gt9xx驱动程序
I2C驱动程序在开发板的SDK包内已经写好了,这里我直接使用这个文件,文件的名称
2.2、测试程序
touch.c
#include "main.h"
#include "touch.h"
stc_touchpad_data_t m_stcTouchData;
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] Return true is the touchpad is pressed
* @param None
* @retval Press state
*/
bool TOUCHPAD_IsPressed(void)
{
uint8_t u8Tmp;
uint8_t u8Status;
bool bPressed = false;
BSP_GT9XX_REG_Read(GT9XX_TOUCH_STATUS, &u8Status, 1UL);
if ((u8Status & 0x80U) != 0U) {
u8Tmp = 0U;
BSP_GT9XX_REG_Write(GT9XX_TOUCH_STATUS, &u8Tmp, 1U);
if ((0U < (u8Status & 0x0FU)) && ((u8Status & 0x0FU) < 6U)) {
bPressed = true;
}
}
return bPressed;
}
/**
* @brief Get touch data.
* @param [out] pstcData Pointer to a [url=home.php?mod=space&uid=1064992]@ref[/url] stc_touchpad_data_t structure.
* @retval None
*/
void TOUCHPAD_Read(stc_touchpad_data_t *pstcData)
{
static uint16_t u16LastX = 0U;
static uint16_t u16LastY = 0U;
/*Save the pressed coordinates and the state*/
if (TOUCHPAD_IsPressed()) {
BSP_GT9XX_GetXY(GT9XX_POINT1, &u16LastX, &u16LastY);
pstcData->enPointPress = SET;
} else {
pstcData->enPointPress = RESET;
}
/*Set the last pressed coordinates*/
pstcData->stcPoint.u16X = u16LastX;
pstcData->stcPoint.u16Y = u16LastY;
}
/**
* @brief Check if a point is on an window
* @param [in] pstcWin Pointer to a @ref stc_touchpad_window_t structure.
* @param [in] pstcPoint Pointer to a @ref stc_touchpad_point_t structure.
* @retval bool:
* - true: The point is in the area.
* - false: The point is out the area.
*/
bool TOUCHPAD_IsPointOn(const stc_touchpad_window_t *pstcWin, const stc_touchpad_point_t *pstcPoint)
{
bool bIsPointOnWin = false;
if((pstcPoint->u16X >= pstcWin->u16X1 && pstcPoint->u16X <= pstcWin->u16X2) && \
(pstcPoint->u16Y >= pstcWin->u16Y1 && pstcPoint->u16Y <= pstcWin->u16Y2)) {
bIsPointOnWin = true;
}
return bIsPointOnWin;
}
void init_touch(void)
{
BSP_GT9XX_Init();
}
main.c
#include "main.h"
#include "lcd.h"
#include "sram.h"
#include "timer0.h"
#include "led.h"
#include "touch.h"
stc_touchpad_data_t touchdat;
int32_t main(void)
{
uint16_t u=0;
uint16_t cord[6]={WHITE, BLUE, BRED, GBLUE, RED, YELLOW};
/* Register write enable for some required peripherals. */
LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_EFM | LL_PERIPH_SRAM);
BSP_CLK_Init();
/* EXCLK 60MHz */
CLK_SetClockDiv(CLK_BUS_EXCLK, CLK_EXCLK_DIV4);
DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_Preinit);
BSP_IO_Init();
BSP_LED_Init();
BSP_LCD_IO_Init();
init_led();
init_touch();
init_lcd();
init_sram();
init_timer0();
LL_PERIPH_WP(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_EFM | LL_PERIPH_SRAM);
POINT_COLOR=RED;
LCD_Clear(WHITE);
//sram_test();
for (;;)
{
TOUCHPAD_Read(&touchdat);
if (touchdat.enPointPress == SET)
{
DDL_Printf("x: %d, y: %d \r\n",touchdat.stcPoint.u16X,touchdat.stcPoint.u16Y );
}
DDL_DelayMS(50UL);
}
}
三、测试结果
串口输出触摸时的坐标点
|