Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
触摸驱动求助。
发布于 2013-05-08 15:25:40 浏览:2767
订阅该版
```/** ****************************************************************************** * @file XPT2046.c * @author wbright * @version V1.0 * @date 2013年4月29日 * @brief This file provides touch HardWare control. * @desc I rebuild This file is only for xpt2046 DRIVER IC. ****************************************************************************** */ #include
#include "stm32f10x.h" #include "board.h" #include "xpt2046.h" #include
#include
#include
#include
#include
#define X_WIDTH 320 #define Y_WIDTH 240 //默认横屏 u8 CMD_RDY=0XD0; u8 CMD_RDX=0X90; rt_inline void EXTI_Enable(rt_uint32_t enable); /* MISO PB14 MOSI PB15 CLK PB13 CS PB12 */ /* 定义了触摸芯片的SPI片选控制 */ #define TP_CS() GPIO_ResetBits(GPIOB,GPIO_Pin_12) #define TP_DCS() GPIO_SetBits(GPIOB,GPIO_Pin_12) /**************************************************************************** * 名 称:unsigned char SPI_WriteByte(unsigned char data) * 功 能:SPI1 写函数 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法: ****************************************************************************/ __inline unsigned char SPI_WriteByte(unsigned char data) { unsigned char Data = 0; //等待发送缓冲区空 while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET); // 发送一个字节 SPI_I2S_SendData(SPI2,data); //等待是否接收到一个字节 while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET); // 获得该字节 Data = SPI_I2S_ReceiveData(SPI2); // 返回收到的字节 return Data; } /**************************************************************************** * 名 称:void SpiDelay(unsigned int DelayCnt) * 功 能:SPI1 写延时函数 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法: ****************************************************************************/ __inline void SpiDelay(unsigned int DelayCnt) { unsigned int i; for(i=0;i
>3; //移位换算成12位的有效数据0-4095 return (x); } /**************************************************************************** * 名 称:u16 TPReadY(void) * 功 能:触摸屏Y轴数据读出 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法: ****************************************************************************/ __inline u16 TPReadY(void) { u16 y=0; TP_CS(); //选择XPT2046 SpiDelay(10); //延时 SPI_WriteByte(CMD_RDY); //设置Y轴读取标志 SpiDelay(10); //延时 y=SPI_WriteByte(0x00); //连续读取16位的数据 y<<=8; y+=SPI_WriteByte(0x00); SpiDelay(10); //禁止XPT2046 TP_DCS(); y = y>>3; //移位换算成12位的有效数据0-4095 return (y); } static void rtgui_touch_calculate() { if (touch != RT_NULL) { //读取触摸值 { rt_uint16_t tmpx[10]; rt_uint16_t tmpy[10]; unsigned int i; for(i=0; i<10; i++) { /* read X */ tmpy* = TPReadX(); tmpx* = TPReadY(); } //去最高值与最低值,再取平均值 { rt_uint32_t min_x = 0xFFFF,min_y = 0xFFFF; rt_uint32_t max_x = 0,max_y = 0; rt_uint32_t total_x = 0; rt_uint32_t total_y = 0; unsigned int i; for(i=0;i<10;i++) { if( tmpx* < min_x ) { min_x = tmpx*; } if( tmpx* > max_x ) { max_x = tmpx*; } total_x += tmpx*; if( tmpy* < min_y ) { min_y = tmpy*; } if( tmpy* > max_y ) { max_y = tmpy*; } total_y += tmpy*; } total_x = total_x - min_x - max_x; total_y = total_y - min_y - max_y; touch->x = total_x / 8; touch->y = total_y / 8; }//去最高值与最低值,再取平均值 }//读取触摸值 /* if it's not in calibration status */ // touch->x=0.089*touch->x+65506;//将结果转换为屏幕坐标 // touch->y=0.064*touch->y+65522; if (touch->calibrating != RT_TRUE) { if (touch->max_x > touch->min_x) { touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x); } else if (touch->max_x < touch->min_x) { touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x); } if (touch->max_y > touch->min_y) { touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y); } else if (touch->max_y < touch->min_y) { touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y); } // normalize the data if (touch->x & 0x8000) touch->x = 0; else if (touch->x > X_WIDTH) touch->x = X_WIDTH - 1; if (touch->y & 0x8000) touch->y = 0; else if (touch->y > Y_WIDTH) touch->y = Y_WIDTH - 1; } } } static unsigned int flag = 0; void touch_timeout(void* parameter) { struct rtgui_event_mouse emouse; static struct _touch_previous { rt_uint32_t x; rt_uint32_t y; } touch_previous; if (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_7) != 0) { int tmer = RT_TICK_PER_SECOND/8 ; EXTI_Enable(1); emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP); /* use old value */ emouse.x = touch->x; emouse.y = touch->y; /* stop timer */ rt_timer_stop(touch->poll_timer); rt_kprintf("touch up: (%d, %d) ", emouse.x, emouse.y); flag = 0; if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL)) { /* callback function */ touch->calibration_func(emouse.x, emouse.y); } rt_timer_control(touch->poll_timer , RT_TIMER_CTRL_SET_TIME , &tmer); } else { if(flag == 0) { int tmer = RT_TICK_PER_SECOND/20 ; /* calculation */ rtgui_touch_calculate(); /* send mouse event */ emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; emouse.parent.sender = RT_NULL; emouse.x = touch->x; emouse.y = touch->y; touch_previous.x = touch->x; touch_previous.y = touch->y; /* init mouse button */ emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN); // rt_kprintf("touch down: (%d, %d) ", emouse.x, emouse.y); flag = 1; rt_timer_control(touch->poll_timer , RT_TIMER_CTRL_SET_TIME , &tmer); } else { /* calculation */ rtgui_touch_calculate(); #define previous_keep 8 //判断移动距离是否小于previous_keep,减少误动作. if( (touch_previous.x
x+previous_keep) && (touch_previous.x>touch->x-previous_keep) && (touch_previous.y
y+previous_keep) && (touch_previous.y>touch->y-previous_keep) ) { return; } touch_previous.x = touch->x; touch_previous.y = touch->y; /* send mouse event */ emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON ; emouse.parent.sender = RT_NULL; emouse.x = touch->x; emouse.y = touch->y; /* init mouse button */ emouse.button = (RTGUI_MOUSE_BUTTON_RIGHT |RTGUI_MOUSE_BUTTON_DOWN); // rt_kprintf("touch motion: (%d, %d) ", emouse.x, emouse.y); } } /* send event to server */ if (touch->calibrating != RT_TRUE) rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); } static void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the EXTI0 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } rt_inline void EXTI_Enable(rt_uint32_t enable) { EXTI_InitTypeDef EXTI_InitStructure; /* Configure EXTI */ EXTI_InitStructure.EXTI_Line = EXTI_Line7; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//Falling下降沿 Rising上升 if (enable) { /* enable */ EXTI_InitStructure.EXTI_LineCmd = ENABLE; } else { /* disable */ EXTI_InitStructure.EXTI_LineCmd = DISABLE; } EXTI_Init(&EXTI_InitStructure); EXTI_ClearITPendingBit(EXTI_Line7); } static void EXTI_Configuration(void) { /* PC0 touch INT */ { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOG,&GPIO_InitStructure); } GPIO_EXTILineConfig(GPIO_PortSourceGPIOG, GPIO_PinSource7); /* Configure EXTI */ EXTI_Enable(1); } /* RT-Thread Device Interface */ static rt_err_t rtgui_touch_init (rt_device_t dev) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* SPI2 时钟使能 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); /* SPI2 SCK(PB13)、MISO(PB14)、MOSI(PB15) 设置 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线速度50MHZ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用模式 GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI1 触摸芯片的片选控制设置 PB12 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线速度50MHZ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出模式 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //输入上拉模式 GPIO_Init(GPIOG, &GPIO_InitStructure); /* SPI1总线 配置 */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟极性 空闲状态时,SCK保持低电平 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟相位 数据采样从第一个时钟边沿开始 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件产生NSS SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //波特率控制 SYSCLK/64 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据高位在前 SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC多项式寄存器初始值为7 SPI_Init(SPI2, &SPI_InitStructure); /* SPI1 使能 */ SPI_Cmd(SPI2,ENABLE); NVIC_Configuration(); EXTI_Configuration(); return RT_EOK; } static rt_err_t rtgui_touch_control (rt_device_t dev, rt_uint8_t cmd, void *args) { switch (cmd) { case RT_TOUCH_CALIBRATION: touch->calibrating = RT_TRUE; touch->calibration_func = (rt_touch_calibration_func_t)args; break; case RT_TOUCH_NORMAL: touch->calibrating = RT_FALSE; break; case RT_TOUCH_CALIBRATION_DATA: { struct calibration_data* data; data = (struct calibration_data*) args; //update touch->min_x = data->min_x; touch->max_x = data->max_x; touch->min_y = data->min_y; touch->max_y = data->max_y; //save setup // radio_setup.touch_min_x = touch->min_x; // radio_setup.touch_max_x = touch->max_x; // radio_setup.touch_min_y = touch->min_y; // radio_setup.touch_max_y = touch->max_y; // save_setup(); } break; } return RT_EOK; } void EXTI7_IRQHandler(void) { /* disable interrupt */ EXTI_Enable(0); /* start timer */ rt_timer_start(touch->poll_timer); EXTI_ClearITPendingBit(EXTI_Line7); } void rtgui_touch_hw_init(void) { touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device)); if (touch == RT_NULL) return; /* no memory yet */ /* clear device structure */ rt_memset(&(touch->parent), 0, sizeof(struct rt_device)); touch->calibrating = false; // touch->min_x = radio_setup.touch_min_x; // touch->max_x = radio_setup.touch_max_x; // touch->min_y = radio_setup.touch_min_y; // touch->max_y = radio_setup.touch_max_y; /* init device structure */ touch->parent.type = RT_Device_Class_Unknown; touch->parent.init = rtgui_touch_init; touch->parent.control = rtgui_touch_control; touch->parent.user_data = RT_NULL; /* create 1/8 second timer */ touch->poll_timer = rt_timer_create("touch", touch_timeout, RT_NULL, RT_TICK_PER_SECOND/8, RT_TIMER_FLAG_PERIODIC); /* register touch device to RT-Thread */ rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR); } #ifdef RT_USING_FINSH #include
void touch_t( rt_uint16_t x , rt_uint16_t y ) { struct rtgui_event_mouse emouse ; emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; emouse.parent.sender = RT_NULL; emouse.x = x ; emouse.y = y ; /* init mouse button */ emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN ); rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); rt_thread_delay(2) ; emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP ); rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); } FINSH_FUNCTION_EXPORT(touch_t, x & y ) ; #endif``` 原理图:![QQ截图20130508152454.png](/uploads/5692_725efb86dbe528bbbd2d107dd7a88807.png)
查看更多
6
个回答
默认排序
按发布时间排序
aozima
2013-05-08
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
``` void EXTI0_IRQHandler(void) { /* disable interrupt */ EXTI_Enable(0); rt_kprintf("touch me! "); /* start timer */ rt_timer_start(touch->poll_timer); EXTI_ClearITPendingBit(EXTI_Line7); } ```
wbright
2013-05-08
这家伙很懒,什么也没写!
硬件平台:stm32f103zet6 屏幕尺寸:3.2 操作系统:rtt1.1.0 GUI:RTGUI0.6.2 问题: 上面的触摸驱动在RTGUI中没有作用,表现为使用RTGUI的test_cases 中的button无反应。
wbright
2013-05-08
这家伙很懒,什么也没写!
EXTI0_IRQHandler(void)改为 EXTI7_IRQHandler(void)后 calibration_init(); 还是没反应,PG7 点击时电压有变化。求教。
aozima
2013-05-08
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
STM32只有 EXTI9_5_IRQHandler ``` EXTI_InitStructure.EXTI_Line = EXTI_Line7; NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; ```
wbright
2013-05-08
这家伙很懒,什么也没写!
谢谢,已经解决。是中断没有在init中执行初始化。
撰写答案
登录
注册新账号
关注者
0
被浏览
2.8k
关于作者
wbright
这家伙很懒,什么也没写!
提问
2
回答
6
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
env中添加lvgl软件包后,keil编译包--c99错误
2
【NXP-MCXA153】 定时器驱动移植
3
GD32F450 看门狗驱动适配
4
【NXP-MCXA153】看门狗驱动移植
5
RT-Thread Studio V2.2.9 Release Note
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
PWM
freemodbus
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
keil_MDK
msh
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
7
个答案
2
次被采纳
a1012112796
18
个答案
1
次被采纳
红枫
5
个答案
1
次被采纳
Ryan_CW
5
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
本月文章贡献
YZRD
3
篇文章
6
次点赞
catcatbing
3
篇文章
6
次点赞
lizimu
2
篇文章
10
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部