2026-08-26 07:26:58 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* @Author: CTW
|
|
|
|
|
|
* @Date: 2025-11-14 15:36:53
|
|
|
|
|
|
* @FilePath: \PhoenixSMGT_Testproject\Drivers\LCD_ILI9341\Drv_XPT2046_Touch.h
|
|
|
|
|
|
* @Description: XXX
|
|
|
|
|
|
* @INPUT: XXX
|
|
|
|
|
|
* @OUTPUT: XXX
|
|
|
|
|
|
*/
|
|
|
|
|
|
#ifndef __DRV_XPT2046_TOUCH_H__
|
|
|
|
|
|
#define __DRV_XPT2046_TOUCH_H__
|
|
|
|
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
|
|
|
|
// 触摸引脚定义(MCU视角:输出=MCU→XPT2046,输入=XPT2046→MCU)
|
|
|
|
|
|
#define TP_IRQ_PIN GPIO_PINS_12 // 输入:XPT2046触摸中断输出(低电平触发)
|
|
|
|
|
|
#define TP_IRQ_PORT GPIOC
|
|
|
|
|
|
#define TP_OUT_PIN GPIO_PINS_15 // 输入:XPT2046 SPI数据输出(ADC数据)
|
|
|
|
|
|
#define TP_OUT_PORT GPIOA
|
|
|
|
|
|
#define TP_CLK_PIN GPIO_PINS_8 // 输出:MCU SPI时钟输出
|
|
|
|
|
|
#define TP_CLK_PORT GPIOC
|
|
|
|
|
|
#define TP_CS_PIN GPIO_PINS_7 // 输出:MCU片选输出(选中/释放XPT2046)
|
|
|
|
|
|
#define TP_CS_PORT GPIOC
|
|
|
|
|
|
#define TP_DIN_PIN GPIO_PINS_6 // 输出:MCU SPI数据输出(命令/数据→XPT2046)
|
|
|
|
|
|
#define TP_DIN_PORT GPIOC
|
|
|
|
|
|
|
|
|
|
|
|
// 引脚操作宏(明确方向,AT32 库)
|
|
|
|
|
|
#define TP_IRQ_Read() gpio_input_data_bit_read(TP_IRQ_PORT, TP_IRQ_PIN) // 读中断(输入)
|
|
|
|
|
|
#define TP_OUT_Read() gpio_input_data_bit_read(TP_OUT_PORT, TP_OUT_PIN) // 读XPT2046输出数据(输入)
|
|
|
|
|
|
#define TP_CLK_Clr() gpio_bits_reset(TP_CLK_PORT, TP_CLK_PIN) // 时钟输出低
|
|
|
|
|
|
#define TP_CLK_Set() gpio_bits_set(TP_CLK_PORT, TP_CLK_PIN) // 时钟输出高
|
|
|
|
|
|
#define TP_CS_Clr() gpio_bits_reset(TP_CS_PORT, TP_CS_PIN) // 片选输出低(选中)
|
|
|
|
|
|
#define TP_CS_Set() gpio_bits_set(TP_CS_PORT, TP_CS_PIN) // 片选输出高(释放)
|
|
|
|
|
|
#define TP_DIN_Clr() gpio_bits_reset(TP_DIN_PORT, TP_DIN_PIN) // DIN输出低(MCU→XPT2046)
|
|
|
|
|
|
#define TP_DIN_Set() gpio_bits_set(TP_DIN_PORT, TP_DIN_PIN) // DIN输出高(MCU→XPT2046)
|
|
|
|
|
|
|
2026-08-31 11:13:01 +08:00
|
|
|
|
// XPT2046命令(与 BOOT 工程一致)
|
|
|
|
|
|
#define XPT2046_READ_X 0xD0
|
|
|
|
|
|
#define XPT2046_READ_Y 0x90
|
|
|
|
|
|
#define XPT2046_READ_Z1 0xB0
|
|
|
|
|
|
#define XPT2046_READ_Z2 0xC0
|
2026-08-26 07:26:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 触摸状态结构体
|
|
|
|
|
|
typedef struct
|
|
|
|
|
|
{
|
|
|
|
|
|
uint16_t x; // 校准后X坐标
|
|
|
|
|
|
uint16_t y; // 校准后Y坐标
|
|
|
|
|
|
uint8_t pressed; // 1=按下,0=未按下
|
|
|
|
|
|
} TP_Point;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//// 琴弦结构体
|
|
|
|
|
|
//typedef struct
|
|
|
|
|
|
//{
|
|
|
|
|
|
// uint16_t x; // 校准后X坐标
|
|
|
|
|
|
// uint16_t y; // 校准后Y坐标
|
|
|
|
|
|
// uint8_t pressed; // 1=按下,0=未按下
|
|
|
|
|
|
//} TP_Point;
|
|
|
|
|
|
|
|
|
|
|
|
//电阻屏函数
|
|
|
|
|
|
extern void XPT2046_Init(void); // 触摸初始化(含引脚配置)
|
|
|
|
|
|
extern void XPT2046_IRQ_Init(void); // 配置IRQ中断(可选,用于触摸触发)
|
|
|
|
|
|
extern void TP_Write_Byte(uint8_t cmd); // 向XPT2046写入1字节命令(MCU→DIN输出,XPT2046→DIN输入)
|
|
|
|
|
|
extern uint16_t TP_Read_AD(uint8_t CMD); // 从触摸屏IC读取adc值
|
|
|
|
|
|
extern uint8_t TP_CheckPressed(uint8_t n); // 简单去抖动读取(连续n次相同状态视为有效)
|
|
|
|
|
|
extern TP_Point TP_ReadRaw(void); // 读取原始坐标(未校准)
|
|
|
|
|
|
extern TP_Point TP_Read(void); // 校准后获取LCD坐标
|
|
|
|
|
|
|
|
|
|
|
|
extern void EXTI15_10_IRQHandler(void); // EXTI15_10中断服务函数(处理TP_IRQ触发)
|
|
|
|
|
|
//extern void HAL_EXTI_Callback(EXTI_HandleTypeDef *hexti); // 中断回调函数(触摸触发时执行)
|
|
|
|
|
|
|
|
|
|
|
|
extern void TP_Drow_Touch_Point(uint16_t x, uint16_t y, uint16_t color); //画一个坐标校准点
|
|
|
|
|
|
extern void TP_Draw_Big_Point(uint16_t x, uint16_t y, uint16_t color); //画一个大点
|
|
|
|
|
|
extern void LCD_DrawRoughLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); //画粗一点的线
|
|
|
|
|
|
extern void TP_FormatCoord(char *buf, TP_Point p); // 辅助函数:格式化坐标字符串(x,y转字符串)
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|