第 29 章 电阻触摸屏—触摸画板
时间:2022-10-01 06:00:00
29.1 触摸屏简介
1.1 电阻触摸屏检测原理
1.2 电阻触摸屏控制芯片
1.3 电容式触摸屏检测原理
29.2 电阻触摸屏-触摸屏实验
2.1 硬件设计
2.2 软件设计
2.2.1 编程要点
(1) 编写软件模拟 SPI 协议的驱动;
(2) 编写触摸芯片的控制驱动器,如发送命令字、获取触摸坐标等;
(3) 编制触摸校正程序;
(4) 编制测试程序检查驱动程序。
2.2.2 代码分析
1.触摸屏硬件的宏定义
2.初始触摸屏控制引脚
3.模拟 SPI 协议的读写顺序
4.收集触摸原始数据
5.多次采样平均值
6.根据原始数据计算坐标值
7.触摸校正
8.触摸状态检测机
9.获取和处理触摸坐标
2.2.3 main 函数
main.c
/** ****************************************************************************** * @file main.c * @author fire * @version V1.0 * @date 2013-xx-xx * @brief 触摸画板实验 ****************************************************************************** * @attention * * 实验平台:野火 F103-指南者 STM32
开发板 * 论坛 :http://www.firebbs.cn * 淘宝 :https://fire-stm32.taobao.com * ****************************************************************************** */ #include "stm32f10x.h" #include "./usart/bsp_usart.h" #include "./lcd/bsp_ili9341_lcd.h" #include "./lcd/bsp_xpt2046_lcd.h" #include "./flash/bsp_spi_flash.h" #include "./led/bsp_led.h" #include "palette.h" #include
int main(void) { //LCD 初始化 ILI9341_Init(); ///触摸屏初始化 XPT2046_Init(); //从FLASH若FLASH如果没有参数,则使用模式3进行校正 Calibrate_or_Get_TouchParaWithFlash(3,0); /* USART config */ USART_Config(); LED_GPIO_Config(); printf("\r\n ********** 触摸画板程序 *********** \r\n"); printf(); //其中0、3、5、6 模式适合从左至右显示文字, //不推荐使用其它模式显示文字 其它模式显示文字会有镜像效果 //其中 6 模式为大部分液晶例程的默认显示方向 ILI9341_GramScan ( 3 ); //绘制触摸画板界面 Palette_Init(LCD_SCAN_MODE); while ( 1 ) { //触摸检测函数,本函数至少10ms调用一次 XPT2046_TouchEvenHandler(); } } /* ------------------------------------------end of file---------------------------------------- */
palette.h
#ifndef _PALETTE_H
#define _PALETTE_H
#include "stm32f10x.h"
#include "./lcd/bsp_ili9341_lcd.h"
#define COLOR_BLOCK_WIDTH 40
#define COLOR_BLOCK_HEIGHT 28
#define BUTTON_NUM 16
#define PALETTE_START_Y 0
#define PALETTE_END_Y LCD_Y_LENGTH
#if 1 //按钮栏在左边
#define BUTTON_START_X 0
#define PALETTE_START_X (COLOR_BLOCK_WIDTH*2+1)
#define PALETTE_END_X LCD_X_LENGTH
#else //按钮栏在右边,(存在触摸按键时也会的bug仅用于测试触摸屏左边界)
#define BUTTON_START_X (LCD_X_LENGTH-2*COLOR_BLOCK_WIDTH)
#define PALETTE_START_X 0
#define PALETTE_END_X (LCD_X_LENGTH-2*COLOR_BLOCK_WIDTH)
#endif
/* LCD 颜色代码,CL_是Color的简写 16Bit由高位至低位, RRRR RGGG GGGB BBBB 下面的RGB 宏将24位的RGB值转换为16位格式。 启动windows的画笔程序,点击编辑颜色,选择自定义颜色,可以获得的RGB值。 推荐使用迷你取色器软件获得你看到的界面颜色。 */
#if LCD_RGB_888
/*RGB888颜色转换*/
#define RGB(R,G,B) ( (R<< 16) | (G << 8) | (B)) /* 将8位R,G,B转化为 24位RGB888格式 */
#else
/*RGB565 颜色转换*/
#define RGB(R,G,B) (((R >> 3) << 11) | ((G >> 2) << 5) | (B >> 3)) /* 将8位R,G,B转化为 16位RGB565格式 */
#define RGB565_R(x) ((x >> 8) & 0xF8)
#define RGB565_G(x) ((x >> 3) & 0xFC)
#define RGB565_B(x) ((x << 3) & 0xF8)
#endif
enum
{
CL_WHITE = RGB(255,255,255), /* 白色 */
CL_BLACK = RGB( 0, 0, 0), /* 黑色 */
CL_RED = RGB(255, 0, 0), /* 红色 */
CL_GREEN = RGB( 0,255, 0), /* 绿色 */
CL_BLUE = RGB( 0, 0,255), /* 蓝色 */
CL_YELLOW = RGB(255,255, 0), /* 黄色 */
CL_GREY = RGB( 98, 98, 98), /* 深灰色 */
CL_GREY1 = RGB( 150, 150, 150), /* 浅灰色 */
CL_GREY2 = RGB( 180, 180, 180), /* 浅灰色 */
CL_GREY3 = RGB( 200, 200, 200), /* 最浅灰色 */
CL_GREY4 = RGB( 230, 230, 230), /* 最浅灰色 */
CL_BUTTON_GREY = RGB( 195, 195, 195), /* WINDOWS 按钮表面灰色 */
CL_MAGENTA = RGB(255, 0, 255), /* 红紫色,洋红色 */
CL_CYAN = RGB( 0, 255, 255), /* 蓝绿色,青色 */
CL_BLUE1 = RGB( 0, 0, 240), /* 深蓝色 */
CL_BLUE2 = RGB( 0, 0, 128), /* 深蓝色 */
CL_BLUE3 = RGB( 68, 68, 255), /* 浅蓝色1 */
CL_BLUE4 = RGB( 0, 64, 128), /* 浅蓝色1 */
/* UI 界面 Windows控件常用色 */
CL_BTN_FACE = RGB(236, 233, 216), /* 按钮表面颜色(灰) */
CL_BOX_BORDER1 = RGB(172, 168,153), /* 分组框主线颜色 */
CL_BOX_BORDER2 = RGB(255, 255,255), /* 分组框阴影线颜色 */
CL_MASK = 0x7FFF /* RGB565颜色掩码,用于文字背景透明 */
};
typedef struct
{
uint16_t start_x; //按键的x起始坐标
uint16_t start_y; //按键的y起始坐标
uint16_t end_x; //按键的x结束坐标
uint16_t end_y; //按键的y结束坐标
uint32_t para; //颜色按钮中表示选择的颜色,笔迹形状按钮中表示选择的画刷
uint8_t touch_flag; //按键按下的标志
void (*draw_btn)(void * btn); //按键描绘函数
void (*btn_command)(void * btn); //按键功能执行函数,例如切换颜色、画刷
}Touch_Button;
/*画刷形状列表*/
typedef enum
{
LINE_SINGLE_PIXCEL = 0, //单像素线
LINE_2_PIXCEL, //2像素线
LINE_4_PIXCEL, //4像素线
LINE_6_PIXCEL, //6像素线
LINE_8_PIXCEL, //8像素线
LINE_16_PIXCEL, //16像素线
LINE_20_PIXCEL, //20像素线
LINE_WITH_CIRCLE, //珠子连线
RUBBER, //橡皮
}SHAPE;
/*画刷参数*/
typedef struct
{
uint32_t color;
SHAPE shape;
}Brush_Style;
/*brush全局变量在其它文件有使用到*/
extern Brush_Style brush;
void Palette_Init(uint8_t LCD_Mode);
void Touch_Button_Init(void);
void Touch_Button_Down(uint16_t x,uint16_t y);
void Touch_Button_Up(uint16_t x,uint16_t y);
void Draw_Trail(int16_t pre_x,int16_t pre_y,int16_t x,int16_t y,Brush_Style* brush);
#endif //_PALETTE_H
palette.c
/** ****************************************************************************** * @file palette.c * @author fire * @version V1.0 * @date 2015-xx-xx * @brief 触摸画板应用函数 ****************************************************************************** * @attention * * 实验平台:野火 STM32 F429 开发板 * 论坛 :http://www.firebbs.cn * 淘宝 :https://fire-stm32.taobao.com * ****************************************************************************** */
#include "palette.h"
#include "./lcd/bsp_xpt2046_lcd.h"
#include "./lcd/bsp_ili9341_lcd.h"
/*按钮结构体数组*/
Touch_Button button[BUTTON_NUM];
/*画笔参数*/
Brush_Style brush;
static void Draw_Color_Button(void *btn);
static void Draw_Clear_Button(void *btn);
static void Draw_Shape_Button(void *btn);
static void Command_Select_Color(void *btn);
static void Command_Select_Brush(void *btn);
static void Command_Clear_Palette(void *btn);
static void LCD_DrawUniLineCircle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint8_t thick );
/** * @brief Palette_Init 画板初始化 * @param 无 * @retval 无 */
void Palette_Init(uint8_t LCD_Mode)
{
uint8_t i;
ILI9341_GramScan ( LCD_Mode );
/* 整屏清为白色 */
LCD_SetBackColor(CL_WHITE);
ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
/* 初始化按钮 */
Touch_Button_Init();
/* 描绘按钮 */
for(i=0;i<BUTTON_NUM;i++)
{
button[i].draw_btn(&button[i]);
}
/* 初始化画笔 */
brush.color = CL_BLACK;
brush.shape = LINE_SINGLE_PIXCEL;
LCD_SetTextColor(brush.color);
}
/** * @brief Touch_Button_Init 初始化按钮参数 * @param 无 * @retval 无 */
void Touch_Button_Init(void)
{
/*第一列,主要为颜色按钮*/
button[0].start_x = BUTTON_START_X;
button[0].start_y = 0;
button[0].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[0].end_y = COLOR_BLOCK_HEIGHT;
button[0].para = CL_BLACK;
button[0].touch_flag = 0;
button[0].draw_btn = Draw_Color_Button ;
button[0].btn_command = Command_Select_Color ;
button[1].start_x = BUTTON_START_X;
button[1].start_y = COLOR_BLOCK_HEIGHT;
button[1].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[1].end_y = COLOR_BLOCK_HEIGHT*2;
button[1].para = CL_GREY;
button[1].touch_flag = 0;
button[1].draw_btn = Draw_Color_Button ;
button[1].btn_command = Command_Select_Color ;
button[2].start_x = BUTTON_START_X;
button[2].start_y = COLOR_BLOCK_HEIGHT*2;
button[2].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[2].end_y = COLOR_BLOCK_HEIGHT*3;
button[2].para = CL_BLUE;
button[2].touch_flag = 0;
button[2].draw_btn = Draw_Color_Button ;
button[2].btn_command = Command_Select_Color ;
button[3].start_x = BUTTON_START_X;
button[3].start_y = COLOR_BLOCK_HEIGHT*3;
button[3].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[3].end_y = COLOR_BLOCK_HEIGHT*4;
button[3].para = CL_CYAN;
button[3].touch_flag = 0;
button[3].draw_btn = Draw_Color_Button ;
button[3].btn_command = Command_Select_Color ;
button[4].start_x = BUTTON_START_X;
button[4].start_y = COLOR_BLOCK_HEIGHT*4;
button[4].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[4].end_y = COLOR_BLOCK_HEIGHT*5;
button[4].para = CL_RED;
button[4].touch_flag = 0;
button[4].draw_btn = Draw_Color_Button ;
button[4].btn_command = Command_Select_Color ;
button[5].start_x = BUTTON_START_X;
button[5].start_y = COLOR_BLOCK_HEIGHT*5;
button[5].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[5].end_y = COLOR_BLOCK_HEIGHT*6;
button[5].para = CL_MAGENTA;
button[5].touch_flag = 0;
button[5].draw_btn = Draw_Color_Button ;
button[5].btn_command = Command_Select_Color ;
button[6].start_x = BUTTON_START_X;
button[6].start_y = COLOR_BLOCK_HEIGHT*6;
button[6].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[6].end_y = COLOR_BLOCK_HEIGHT*7;
button[6].para = CL_GREEN;
button[6].touch_flag = 0;
button[6].draw_btn = Draw_Color_Button ;
button[6].btn_command = Command_Select_Color ;
button[7].start_x = BUTTON_START_X;
button[7].start_y = COLOR_BLOCK_HEIGHT*7;
button[7].end_x = BUTTON_START_X+COLOR_BLOCK_WIDTH ;
button[7].end_y = LCD_Y_LENGTH;
button[7].para = CL_BUTTON_GREY;
button[7].touch_flag = 0;
button[7].draw_btn = Draw_Clear_Button ;
button[7].btn_command = Command_Clear_Palette ;
/*第二列,主要为画刷按钮*/
button[8].start_x = BUTTON_START_X + COLOR_BLOCK_WIDTH;
button[8].start_y = 0;
button[8].end_x = BUTTON_START_X + COLOR_BLOCK_WIDTH*2 ;
button[8].end_y = COLOR_BLOCK_HEIGHT;
button[8].para = LINE_SINGLE_PIXCEL;
button[8].touch_flag = 0;
button[8].draw_btn = Draw_Shape_Button ;
button[8].btn_command = Command_Select_Brush ;
button[9].start_x = BUTTON_START_X + COLOR_BLOCK_WIDTH;
button[9].start_y = COLOR_BLOCK_HEIGHT;
button[9].end_x = BUTTON_START_X + COLOR_BLOCK_WIDTH*2 ;
button[9].end_y = COLOR_BLOCK_HEIGHT*2;
button[9].para = LINE_2_PIXCEL;
button[9].touch_flag = 0;
button[9].draw_btn = Draw_Shape_Button ;
button[9].btn_command = Command_Select_Brush ;