Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
触摸屏驱动求助
发布于 2014-03-08 14:20:15 浏览:3256
订阅该版
在红龙103的板子上移植了RTT和RTGUI,都已经成功了,现在想加上触摸屏驱动跑一下DEMO,移植完以后,发现每次触摸无论什么地方都是打印 touch up: (0, 0) 屏上没有反应,还是DEMO的首页。 代码如下,希望能得到帮助,谢谢! ```#include
#include "stm32f10x.h" #include "board.h" #include "touch.h" #include
#include
#include
#include
#include
/* MISO PB14 MOSI PB15 CLK PB13 CS PB12 INT PG7 */ #define CS_0() GPIO_ResetBits(GPIOB,GPIO_Pin_12) #define CS_1() GPIO_SetBits(GPIOB,GPIO_Pin_12) /* 7 6 - 4 3 2 1-0 s A2-A0 MODE SER/DFR PD1-PD0 */ //#define TOUCH_MSR_Y 0x90 //读X轴坐标指令 addr:1 //#define TOUCH_MSR_X 0xD0 //读Y轴坐标指令 addr:3 #define TOUCH_MSR_Y 0xD0 //读X轴坐标指令 addr:1 #define TOUCH_MSR_X 0x90 //读Y轴坐标指令 addr:3 struct rtgui_touch_device { struct rt_device parent; rt_timer_t poll_timer; rt_uint16_t x, y; rt_bool_t calibrating; rt_touch_calibration_func_t calibration_func; rt_uint16_t min_x, max_x; rt_uint16_t min_y, max_y; }; static struct rtgui_touch_device *touch = RT_NULL; extern unsigned char SPI_WriteByte(unsigned char data); rt_inline void EXTI_Enable(rt_uint32_t enable); struct rt_semaphore spi2_lock; void rt_hw_spi2_baud_rate(uint16_t SPI_BaudRatePrescaler) { SPI2->CR1 &= ~SPI_BaudRatePrescaler_256; SPI2->CR1 |= SPI_BaudRatePrescaler; } uint8_t SPI_WriteByte(unsigned char data) { //Wait until the transmit buffer is empty while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); // Send the byte SPI_I2S_SendData(SPI2, data); //Wait until a data is received while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); // Get the received data data = SPI_I2S_ReceiveData(SPI2); // Return the shifted data return data; } //SPI写数据 static void WriteDataTo7843(unsigned char num) { SPI_WriteByte(num); } #define X_WIDTH 800 #define Y_WIDTH 480 static void rtgui_touch_calculate() { rt_kprintf("cal "); if (touch != RT_NULL) { rt_sem_take(&spi2_lock, RT_WAITING_FOREVER); /* SPI1 configure */ rt_hw_spi2_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */ //读取触摸值 { rt_uint16_t tmpx[10]; rt_uint16_t tmpy[10]; unsigned int i; /* From the datasheet: * When the very first CLK after the control byte comes in, the * DOUT of ADS7843 is not valid. So we could only get 7bits from * the first SPI_WriteByte. And the got the following 5 bits from * another SPI_WriteByte.(aligned MSB) */ for(i=0; i<10; i++) { CS_0(); WriteDataTo7843(TOUCH_MSR_X); tmpx* = (SPI_WriteByte(0x00) & 0x7F) << 5; tmpx* |= (SPI_WriteByte(TOUCH_MSR_Y) >> 3) & 0x1F; tmpy* = (SPI_WriteByte(0x00) & 0x7F) << 5; tmpy* |= (SPI_WriteByte(0x00) >> 3) & 0x1F; WriteDataTo7843( 1<<7 ); /* 打开中断 */ CS_1(); } //去最高值与最低值,再取平均值 { 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; }//去最高值与最低值,再取平均值 }//读取触摸值 rt_sem_release(&spi2_lock); /* if it's not in calibration status */ 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; } } } void touch_timeout(void* parameter) { static unsigned int touched_down = 0; struct rtgui_event_mouse emouse; static struct _touch_previous { rt_uint32_t x; rt_uint32_t y; } touch_previous; //rt_kprintf("ccc "); /* touch time is too short and we lost the position already. */ if ((!touched_down) && GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_7) != 0) return; if (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_7) != 0) { //rt_kprintf("ddd "); 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); touched_down = 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(touched_down == 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); touched_down = 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) { /* PB1 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) { NVIC_Configuration(); EXTI_Configuration(); /* PC4 touch CS */ { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOB,&GPIO_InitStructure); CS_1(); } /*预先设定好校准值,如果需要再次矫正的话,需要认为调用校正函数*/ touch->min_x=171; touch->max_x=3635; touch->min_y=213; touch->max_y=3861; touch->calibrating = RT_FALSE;//跳过校准 CS_0(); WriteDataTo7843( 1<<7 ); /* 打开中断 */ CS_1(); 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; } break; } return RT_EOK; } void EXTI9_5_IRQHandler(void) { rt_kprintf("bbb "); /* disable interrupt */ EXTI_Enable(0); /* start timer */ rt_timer_start(touch->poll_timer); EXTI_ClearITPendingBit(EXTI_Line7); } void rtgui_touch_hw_init(void) { /* SPI1 config */ { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; rt_kprintf("aaa "); /* Enable SPI1 Periph clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO , ENABLE ); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_Init(GPIOB,&GPIO_InitStructure); /* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); //Configure PG7 pin: TP_INT pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_Init(GPIOG,&GPIO_InitStructure); */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式 SPI_I2S_DeInit(SPI2); SPI_Init(SPI2, &SPI_InitStructure); /* Enable SPI_MASTER */ SPI_Cmd(SPI2, ENABLE); SPI_CalculateCRC(SPI2, DISABLE); if (rt_sem_init(&spi2_lock, "spi2lock", 1, RT_IPC_FLAG_FIFO) != RT_EOK) { rt_kprintf("init spi2 lock semaphore failed "); } } /* SPI1 config */ 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; /* 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 ```
查看更多
7
个回答
默认排序
按发布时间排序
ruixiang
2014-03-08
这家伙很懒,什么也没写!
现在触摸屏可以工作了,但是触摸定位非常不准,同一个位置点几个居然坐标完全不同,如下所示: touch up: (73, 106) touch up: (1, 107) touch up: (60, 121) touch up: (799, 479) touch up: (192, 152) touch up: (0, 95) touch up: (35, 113) touch up: (0, 92) 请教各位,这是怎么回事呢?
grissiom
2014-03-08
这家伙很懒,什么也没写!
没有校准?
jiezhi320
2014-03-08
这家伙很懒,什么也没写!
rtgui默认会将calibration.c加入工程,finsh下运行calibration()进入校准。或者直接修改touch.c中的默认触摸参数
ruixiang
2014-03-08
这家伙很懒,什么也没写!
谢谢楼上两位。。 我是校准了,可是即使校准了以后,依然还是会有上面的情况发生。
jiezhi320
2014-03-08
这家伙很懒,什么也没写!
那就怀疑你驱动了
ruixiang
2014-03-08
这家伙很懒,什么也没写!
我修改了RTT源码下的stm32f103的那个touch.c,除了几个引脚重新定义为我板子上引脚之外,一点都没有改啊。
撰写答案
登录
注册新账号
关注者
0
被浏览
3.3k
关于作者
ruixiang
这家伙很懒,什么也没写!
提问
20
回答
40
被采纳
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
RTduino物联网应用零基础入门
2
TinyUSB Demo运行教程
3
RT-Thread学习大礼包一键带走!
4
freemodbus从机调试说明
5
【1024】瑞萨 RA 系列 BSP 制作与适配最新版本的 Keil 、 RSC、固件,较新的 FSP
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
cubemx
PWM
flash
packages_软件包
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
rt_mq_消息队列_msg_queue
keil_MDK
ulog
C++_cpp
at_device
本月问答贡献
张世争
7
个答案
2
次被采纳
用户名由3_15位
10
个答案
1
次被采纳
KunYi
4
个答案
1
次被采纳
踩姑娘的小蘑菇
2
个答案
1
次被采纳
bernard
1
个答案
1
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
4
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部