标题: STM32F103c使用xpt2046触摸屏 Arduino下载的库不能自定义MOSI、MISO、SCLK的引脚 [打印本页]

作者: feng4253    时间: 2020-1-9 18:05
标题: STM32F103c使用xpt2046触摸屏 Arduino下载的库不能自定义MOSI、MISO、SCLK的引脚
按照网上的帖子将两块STM32F103C刷成了arduino的bootland,终于可以直接USB口上传IDE编程了,可是没有支持STM32的xpt2046触摸屏的库,主要是因为XPT2046是SPI接口的,Arduino下载的库只能自定义CS和IRQ引脚,不能自定义MOSI、MISO、SCLK的引脚,请问各位大神有没有解决办法呢?谢谢各位了。这是库文件。

单片机源程序如下:

  1. #include "XPT2046_Touchscreen.h"

  2. #define Z_THRESHOLD     400
  3. #define Z_THRESHOLD_INT        75
  4. #define MSEC_THRESHOLD  3
  5. #define SPI_SETTING     SPISettings(2000000, MSBFIRST, SPI_MODE0)

  6. static XPT2046_Touchscreen         *isrPinptr;
  7. void isrPin(void);

  8. bool XPT2046_Touchscreen::begin()
  9. {
  10.         SPI.begin();
  11.         pinMode(csPin, OUTPUT);
  12.         digitalWrite(csPin, HIGH);
  13.         if (255 != tirqPin) {
  14.                 pinMode( tirqPin, INPUT );
  15.                 attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
  16.                 isrPinptr = this;
  17.         }
  18.         return true;
  19. }

  20. #ifdef ESP32
  21. void IRAM_ATTR isrPin( void )
  22. #else
  23. void isrPin( void )
  24. #endif
  25. {
  26.         XPT2046_Touchscreen *o = isrPinptr;
  27.         o->isrWake = true;
  28. }

  29. TS_Point XPT2046_Touchscreen::getPoint()
  30. {
  31.         update();
  32.         return TS_Point(xraw, yraw, zraw);
  33. }

  34. bool XPT2046_Touchscreen::tirqTouched()
  35. {
  36.         return (isrWake);
  37. }

  38. bool XPT2046_Touchscreen::touched()
  39. {
  40.         update();
  41.         return (zraw >= Z_THRESHOLD);
  42. }

  43. void XPT2046_Touchscreen::readData(uint16_t *x, uint16_t *y, uint8_t *z)
  44. {
  45.         update();
  46.         *x = xraw;
  47.         *y = yraw;
  48.         *z = zraw;
  49. }

  50. bool XPT2046_Touchscreen::bufferEmpty()
  51. {
  52.         return ((millis() - msraw) < MSEC_THRESHOLD);
  53. }

  54. static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) {
  55.   int16_t da, db, dc;
  56.   int16_t reta = 0;
  57.   if ( x > y ) da = x - y; else da = y - x;
  58.   if ( x > z ) db = x - z; else db = z - x;
  59.   if ( z > y ) dc = z - y; else dc = y - z;

  60.   if ( da <= db && da <= dc ) reta = (x + y) >> 1;
  61.   else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
  62.   else reta = (y + z) >> 1;   //    else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;

  63.   return (reta);
  64. }

  65. // TODO: perhaps a future version should offer an option for more oversampling,
  66. //       with the RANSAC algorithm https://en.wikipedia.org/wiki/RANSAC

  67. void XPT2046_Touchscreen::update()
  68. {
  69.         int16_t data[6];

  70.         if (!isrWake) return;
  71.         uint32_t now = millis();
  72.         if (now - msraw < MSEC_THRESHOLD) return;
  73.        
  74.         SPI.beginTransaction(SPI_SETTING);
  75.         digitalWrite(csPin, LOW);
  76.         SPI.transfer(0xB1 /* Z1 */);
  77.         int16_t z1 = SPI.transfer16(0xC1 /* Z2 */) >> 3;
  78.         int z = z1 + 4095;
  79.         int16_t z2 = SPI.transfer16(0x91 /* X */) >> 3;
  80.         z -= z2;
  81.         if (z >= Z_THRESHOLD) {
  82.                 SPI.transfer16(0x91 /* X */);  // dummy X measure, 1st is always noisy
  83.                 data[0] = SPI.transfer16(0xD1 /* Y */) >> 3;
  84.                 data[1] = SPI.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
  85.                 data[2] = SPI.transfer16(0xD1 /* Y */) >> 3;
  86.                 data[3] = SPI.transfer16(0x91 /* X */) >> 3;
  87.         }
  88.         else data[0] = data[1] = data[2] = data[3] = 0;        // Compiler warns these values may be used unset on early exit.
  89.         data[4] = SPI.transfer16(0xD0 /* Y */) >> 3;        // Last Y touch power down
  90.         data[5] = SPI.transfer16(0) >> 3;
  91.         digitalWrite(csPin, HIGH);
  92.         SPI.endTransaction();
  93.         //Serial.printf("z=%d  ::  z1=%d,  z2=%d  ", z, z1, z2);
  94.         if (z < 0) z = 0;
  95.         if (z < Z_THRESHOLD) { //        if ( !touched ) {
  96.                 // Serial.println();
  97.                 zraw = 0;
  98.                 if (z < Z_THRESHOLD_INT) { //        if ( !touched ) {
  99.                         if (255 != tirqPin) isrWake = false;
  100.                 }
  101.                 return;
  102.         }
  103.         zraw = z;
  104.        
  105.         // Average pair with least distance between each measured x then y
  106.         //Serial.printf("    z1=%d,z2=%d  ", z1, z2);
  107.         //Serial.printf("p=%d,  %d,%d  %d,%d  %d,%d", zraw,
  108.                 //data[0], data[1], data[2], data[3], data[4], data[5]);
  109.         int16_t x = besttwoavg( data[0], data[2], data[4] );
  110.         int16_t y = besttwoavg( data[1], data[3], data[5] );
  111.        
  112.         //Serial.printf("    %d,%d", x, y);
  113.         //Serial.println();
  114.         if (z >= Z_THRESHOLD) {
  115.                 msraw = now;        // good read completed, set wait
  116.                 switch (rotation) {
  117.                   case 0:
  118.                         xraw = 4095 - y;
  119.                         yraw = x;
  120.                         break;
  121.                   case 1:
  122.                         xraw = x;
  123.                         yraw = y;
  124.                         break;
  125.                   case 2:
  126.                         xraw = y;
  127.                         yraw = 4095 - x;
  128.                         break;
  129.                   default: // 3
  130.                         xraw = 4095 - x;
  131.                         yraw = 4095 - y;
  132.                 }
  133.         }
  134. }
复制代码

所有资料51hei提供下载:
XPT2046_Touchscreen.zip (3.56 KB, 下载次数: 7)


作者: feng4253    时间: 2020-1-11 23:37
这个是专门写个STM32用arduino的驱动亲测XPT2046可以正常驱动,MOSI、CLK、MISO用硬SPI就行了。
作者: feng4253    时间: 2020-1-11 23:38
Serasidis_XPT2046_touch.zip (7.2 KB, 下载次数: 12)




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1