ST意法半导体
直播中

陈勇

7年用户 1239经验值
私信 关注
[问答]

触摸LCD时线条显示在远离触摸位置的地方是什么问题?

我成功地使用了一个 HAL 库来驱动一个 TFT ili9341 LCD,并尝试通过触摸 LCD 在 LCD 上画一条线,代码非常准确,然后我用寄存器重写了代码。现在触摸不准确,当我触摸 LCD 时,线条显示在远离触摸位置的位置,我附上了代码。可能是什么问题?引脚定义为最大频率。
  • #include "ILI9341_Touchscreen.h"
  • #include "STM32f10x.h"
  • #include "main.h"
  • //Internal Touchpad command, do not call directly
  • uint16_t TP_Read(void)
  • {
  •     uint8_t i = 16;
  •     uint16_t value = 0;
  •     while(i > 0x00)
  •     {
  •         value <<= 1;
  •                            TP_CLK_PORT->BSRR=TP_CLK_PIN;
  •                            TP_CLK_PORT->BRR=TP_CLK_PIN;
  •                                         if ((TP_MISO_PORT->IDR&TP_MISO_PIN)!=0)
  •         {
  •             value++;
  •         }
  •         i--;
  •     };
  •     return value;
  • }
  • //Internal Touchpad command, do not call directly
  • void TP_Write(uint8_t value)
  • {
  •     uint8_t i = 0x08;
  •                 TP_CLK_PORT->BRR=TP_CLK_PIN;
  •     while(i > 0)
  •     {
  •         if((value & 0x80) != 0x00)
  •         {
  •                                         TP_MOSI_PORT->BSRR=TP_MOSI_PIN;
  •         }
  •         else
  •         {
  •                                                 TP_MOSI_PORT->BRR=TP_MOSI_PIN;
  •         }
  •         value <<= 1;
  •                                 TP_CLK_PORT->BSRR=TP_CLK_PIN;
  •                                 TP_CLK_PORT->BRR=TP_CLK_PIN;
  •         i--;
  •     };
  • }
  • //Read coordinates of touchscreen press. Position[0] = X, Position[1] = Y
  • uint8_t TP_Read_Coordinates(uint16_t Coordinates[2])
  • {
  •                 TP_CLK_PORT->BSRR=TP_CLK_PIN;
  •                 TP_MOSI_PORT->BSRR=TP_MOSI_PIN;
  •           TP_CS_PORT->BSRR=TP_CS_PIN;
  •     uint32_t avg_x, avg_y = 0;
  •                 uint16_t rawx, rawy = 0;
  •                 uint32_t calculating_x, calculating_y = 0;
  •     uint32_t samples = NO_OF_POSITION_SAMPLES;
  •     uint32_t counted_samples = 0;
  •         TP_CS_PORT->BRR=TP_CS_PIN;
  •     while((samples > 0)&&((TP_IRQ_PORT->IDR)& TP_IRQ_PIN) == 0)
  •     {
  •         TP_Write(CMD_RDY);
  •                                 rawy = TP_Read();
  •                                 avg_y += rawy;
  •                                 calculating_y += rawy;
  •         TP_Write(CMD_RDX);
  •         rawx = TP_Read();
  •                                 avg_x += rawx;
  •                                 calculating_x += rawx;
  •         samples--;
  •                                 counted_samples++;
  •     };
  •                 TP_CS_PORT->BSRR=TP_CS_PIN;
  •                 if((counted_samples == NO_OF_POSITION_SAMPLES)&&((TP_IRQ_PORT->IDR)&TP_IRQ_PIN) == 0)
  •                 {
  •                 calculating_x /= counted_samples;
  •                 calculating_y /= counted_samples;
  •                 rawx = calculating_x;
  •                 rawy = calculating_y;
  •                 rawx *= -1;
  •                 rawy *= -1;
  •                 //CONVERTING 16bit Value to Screen coordinates
  •     // 65535/273 = 240!
  •                 // 65535/204 = 320!
  •     Coordinates[0] = ((240 - (rawx/X_TRANSLATION)) - X_OFFSET)*X_MAGNITUDE;
  •                 Coordinates[1] = ((rawy/Y_TRANSLATION)- Y_OFFSET)*Y_MAGNITUDE;
  •                 return TOUCHPAD_DATA_OK;
  •                 }
  •                 else
  •                 {
  •                         Coordinates[0] = 0;
  •                         Coordinates[1] = 0;
  •                         return TOUCHPAD_DATA_NOISY;
  •                 }
  • }
  • //Check if Touchpad was pressed. Returns TOUCHPAD_PRESSED (1) or TOUCHPAD_NOT_PRESSED (0)
  • uint8_t TP_Touchpad_Pressed(void)
  • {
  •         if((TP_IRQ_PORT->IDR&TP_IRQ_PIN) == 0)
  •         {
  •                 return TOUCHPAD_PRESSED;
  •         }
  •         else
  •         {
  •                 return TOUCHPAD_NOT_PRESSED;
  •         }
  • }



回帖(1)

李妍

2023-1-5 09:51:05
我定义了一个虚拟变量 a 并在两者之间写了 a++


  • TP_CLK_PORT->BRR=TP_CLK_PIN;




  • if ((TP_MISO_PORT->IDR&TP_MISO_PIN)!=0)

现在工作正常!我的问题解决了。我认为它运行不佳的原因是我违反了基于数据表的 200ns tCH 规范。HAL 代码工作的原因可能是因为 HAL 代码指令需要更长的时间来运行。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分