一板网电子技术论坛

风筝
发表于: 2019-8-20 19:05:07 | 显示全部楼层

在本篇文章中,您将学习如何使用和设置用于Arduino的2.4寸触摸LCD扩展板。首先,您将看到有关此扩展板的一些基础信息。在学习如何设置扩展板之后,您将看到3个实际项目。


Arduino 2.4寸触摸屏扩展板的功能

屏幕在电子项目中的作用非常重要。屏幕可以是非常简单的类型,例如7段或字符LCD或更高级的模型,如OLED和TFT LCD。

tft_2.jpg


2.4寸TFT液晶屏是最广泛使用的图形屏幕之一。以下是其最重要的功能:

●    分辨率为240 * 320像素

●    能够显示262000种不同的颜色

●    包括触摸板

●    5v供电电压


Arduino引脚2、3、A5和A4是空闲的,您可以使用它们连接此扩展板。

arduino-LCD-touchscreen.jpg


2.4寸触摸屏所需的库

我们需要用到以下几个库文件:Adafruit GFX库Adafruit TouchScreen库Adafruit TFT LCD库。

TFTLCD库支持932x、7575、9341和HX8357D驱动器。

如果您的液晶显示屏无法使用此库,请尝试使用Mcufriend_kbv

您可以使用引脚A0至A5将命令应用于此LCD。如果使用SD卡,所有Arduino引脚都将处于忙状态。


如何校准触摸屏?

该LCD最重要的功能之一是包括触摸屏。如果您要使用液晶显示屏,则需要知道触摸点的坐标。为此,您应该在Arduino板上上传以下代码并打开串行监视器。然后触摸所需位置并写入串行监视器上显示的坐标。您可以在任何其他项目中使用此校准。

  1. /*
  2.   TFT LCD - TFT Touch Coordinate
  3. */

  4. #include <stdint.h>
  5. #include "TouchScreen.h"

  6. #define YP A2  
  7. #define XM A3
  8. #define YM 8   
  9. #define XP 9   

  10. // For better pressure precision, we need to know the resistance
  11. // between X+ and X- Use any multimeter to read it
  12. // For the one we're using, its 300 ohms across the X plate
  13. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

  14. void setup(void) {
  15.   Serial.begin(9600);
  16. }

  17. void loop(void) {
  18.   
  19.   TSPoint p = ts.getPoint();
  20.   if (p.z > ts.pressureThreshhold) {
  21.      Serial.print("X = "); Serial.print(p.x);
  22.      Serial.print("\tY = "); Serial.print(p.y);
  23.      Serial.print("\tPressure = "); Serial.println(p.z);
  24.   }
  25.   delay(100);
  26. }
复制代码

TSPoint p = ts.getPoint(); 将长度(x)、宽度(y)和压力(z)存储到p对象。


在Arduino 2.4寸LCD上显示文本和形状

  1. /*
  2.   TFT LCD - TFT Simple driving
  3.   modified on 21 Feb 2019
  4.   by Saeed Hosseini
  5.   https://electropeak.com/learn/
  6. */

  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_TFTLCD.h>

  9. #define LCD_CS A3
  10. #define LCD_CD A2
  11. #define LCD_WR A1
  12. #define LCD_RD A0
  13. #define LCD_RESET A4

  14. #define BLACK       0x0000
  15. #define BLUE        0x001F
  16. #define RED         0xF800
  17. #define GREEN       0x07E0
  18. #define CYAN        0x07FF
  19. #define MAGENTA     0xF81F
  20. #define YELLOW      0xFFE0
  21. #define WHITE       0xFFFF
  22. #define ORANGE      0xFD20
  23. #define GREENYELLOW 0xAFE5
  24. #define NAVY        0x000F
  25. #define DARKGREEN   0x03E0
  26. #define DARKCYAN    0x03EF
  27. #define MAROON      0x7800
  28. #define PURPLE      0x780F
  29. #define OLIVE       0x7BE0
  30. #define LIGHTGREY   0xC618
  31. #define DARKGREY    0x7BEF

  32. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

  33. void setup() {

  34.   Serial.begin(9600);
  35.   Serial.println(F("TFT LCD test"));

  36. #ifdef USE_ADAFRUIT_SHIELD_PINOUT
  37.   Serial.println(F("Using Adafruit 2.4" TFT Arduino Shield Pinout"));
  38. #else
  39.   Serial.println(F("Using Adafruit 2.4" TFT Breakout Board Pinout"));
  40. #endif
  41.   Serial.print("TFT size is ");
  42.   Serial.print(tft.width());
  43.   Serial.print("x");
  44.   Serial.println(tft.height());

  45.   tft.reset();

  46.   uint16_t identifier = tft.readID();

  47.   if (identifier == 0x9325) {
  48.     Serial.println(F("Found ILI9325 LCD driver"));
  49.   } else if (identifier == 0x9328) {
  50.     Serial.println(F("Found ILI9328 LCD driver"));
  51.   } else if (identifier == 0x7575) {
  52.     Serial.println(F("Found HX8347G LCD driver"));
  53.   } else if (identifier == 0x9341) {
  54.     Serial.println(F("Found ILI9341 LCD driver"));
  55.   } else if (identifier == 0x8357) {
  56.     Serial.println(F("Found HX8357D LCD driver"));
  57.   } else {
  58.     Serial.print(F("Unknown LCD driver chip: "));
  59.     Serial.println(identifier, HEX);
  60.     Serial.println(F("If using the Adafruit 2.4" TFT Arduino shield, the line:"));
  61.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  62.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  63.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  64.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  65.     Serial.println(F("matches the tutorial."));
  66.     return;
  67.   }

  68.   tft.begin(identifier);

  69.   Serial.println(F("Benchmark                Time (microseconds)"));

  70.   Serial.print(F("Screen fill              "));
  71.   Serial.println(FillScreen());
  72.   delay(500);

  73.   tft.setTextColor(YELLOW);
  74.   tft.setCursor(70, 180);
  75.   tft.setTextSize(1);
  76.   tft.println("Electropeak");
  77.   delay(200);
  78.   tft.fillScreen(PURPLE);
  79.   tft.setCursor(50, 170);
  80.   tft.setTextSize(2);
  81.   tft.println("Electropeak");
  82.   delay(200);
  83.   tft.fillScreen(PURPLE);
  84.   tft.setCursor(20, 160);
  85.   tft.setTextSize(3);
  86.   tft.println("Electropeak");
  87.   delay(500);
  88.   tft.fillScreen(PURPLE);
  89. ....
  90.     start = micros();
  91.     tft.fillRect(cx - i2, cy - i2, i, i, color1);
  92.     t    += micros() - start;
  93.     // Outlines are not included in timing results
  94.     tft.drawRect(cx - i2, cy - i2, i, i, color2);
  95.   }

  96.   return t;
  97. }
复制代码

有关代码和函数的更多信息,请查看基本TFT LCD命令


显示BMP图片

  1. /*
  2. This code is TFTLCD Library Example
  3. */

  4. #include <Adafruit_GFX.h>      
  5. #include <Adafruit_TFTLCD.h>  
  6. #include <SD.h>
  7. #include <SPI.h>

  8. #define LCD_CS A3
  9. #define LCD_CD A2
  10. #define LCD_WR A1
  11. #define LCD_RD A0

  12. #define SD_CS 10     

  13. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, A4);

  14. void setup()
  15. {
  16.   Serial.begin(9600);

  17.   tft.reset();

  18.   uint16_t identifier = tft.readID();

  19.   if (identifier == 0x9325) {
  20.     Serial.println(F("Found ILI9325 LCD driver"));
  21.   } else if (identifier == 0x9328) {
  22.     Serial.println(F("Found ILI9328 LCD driver"));
  23.   } else if (identifier == 0x7575) {
  24.     Serial.println(F("Found HX8347G LCD driver"));
  25.   } else if (identifier == 0x9341) {
  26.     Serial.println(F("Found ILI9341 LCD driver"));
  27.   } else if (identifier == 0x8357) {
  28.     Serial.println(F("Found HX8357D LCD driver"));
  29.   } else {
  30.     Serial.print(F("Unknown LCD driver chip: "));
  31.     Serial.println(identifier, HEX);
  32.     Serial.println(F("If using the Adafruit 2.4" TFT Arduino shield, the line:"));
  33.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  34.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  35.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  36.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  37.     Serial.println(F("matches the tutorial."));
  38.     return;
  39.   }

  40.   tft.begin(identifier);

  41.   Serial.print(F("Initializing SD card..."));
  42.   if (!SD.begin(SD_CS)) {
  43.     Serial.println(F("failed!"));
  44.     return;
  45.   }
  46.   Serial.println(F("OK!"));

  47.   bmpDraw("pic1.bmp", 0, 0);
  48.   delay(1000);
  49.   bmpDraw("pic2.bmp", 0, 0);
  50.   delay(1000);
  51.   bmpDraw("pic3.bmp", 0, 0);
  52.   delay(1000);
  53. }

  54. void loop()
  55. {

  56. }

  57. #define BUFFPIXEL 20

  58. void bmpDraw(char *filename, int x, int y) {

  59.   File     bmpFile;
  60.   int      bmpWidth, bmpHeight;   // W+H in pixels
  61.   uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  62.   uint32_t bmpImageoffset;        // Start of image data in file
  63.   uint32_t rowSize;               // Not always = bmpWidth; may have padding
  64.   uint8_t  sdbuffer[3 * BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
  65.   uint16_t lcdbuffer[BUFFPIXEL];  // pixel out buffer (16-bit per pixel)
  66.   uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  67.   boolean  goodBmp = false;       // Set to true on valid header parse
  68.   boolean  flip    = true;        // BMP is stored bottom-to-top
  69.   int      w, h, row, col;
  70.   uint8_t  r, g, b;
  71.   uint32_t pos = 0, startTime = millis();
  72.   uint8_t  lcdidx = 0;
  73.   boolean  first = true;

  74.   if ((x >= tft.width()) || (y >= tft.height())) return;

  75.   Serial.println();
  76.   Serial.print(F("Loading image '"));
  77.   Serial.print(filename);
  78.   Serial.println('\'');
  79.   // Open requested file on SD card
  80.   if ((bmpFile = SD.open(filename)) == NULL) {
  81.     Serial.println(F("File not found"));
  82.     return;
  83.   }

  84.   // Parse BMP header
  85.   if (read16(bmpFile) == 0x4D42) { // BMP signature
  86.     Serial.println(F("File size: ")); Serial.println(read32(bmpFile));
  87.     (void)read32(bmpFile); // Read & ignore creator bytes
  88.     bmpImageoffset = read32(bmpFile); // Start of image data
  89.     Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  90.     // Read DIB header
  91.     Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  92.     bmpWidth  = read32(bmpFile);
  93.     bmpHeight = read32(bmpFile);
  94.     if (read16(bmpFile) == 1) { // # planes -- must be '1'
  95.       bmpDepth = read16(bmpFile); // bits per pixel
  96.       Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  97.       if ((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed

  98.         goodBmp = true; // Supported BMP format -- proceed!
  99.         Serial.print(F("Image size: "));
  100.         Serial.print(bmpWidth);
  101.         Serial.print('x');
  102.         Serial.println(bmpHeight);

  103.         // BMP rows are padded (if needed) to 4-byte boundary
  104.         rowSize = (bmpWidth * 3 + 3) & ~3;

  105.         // If bmpHeight is negative, image is in top-down order.
  106.         // This is not canon but has been observed in the wild.
  107.         if (bmpHeight < 0) { bmpHeight = -bmpHeight; flip = false; } // Crop area to be loaded w = bmpWidth; h = bmpHeight; if ((x + w - 1) >= tft.width())  w = tft.width()  - x;
  108.         if ((y + h - 1) >= tft.height()) h = tft.height() - y;

  109.         // Set TFT address window to clipped image bounds
  110.         tft.setAddrWindow(x, y, x + w - 1, y + h - 1);

  111.         for (row = 0; row < h; row++) { // For each scanline...
  112.           // Seek to start of scan line.  It might seem labor-
  113.           // intensive to be doing this on every line, but this
  114.           // method covers a lot of gritty details like cropping
  115.           // and scanline padding.  Also, the seek only takes
  116.           // place if the file position actually needs to change
  117.           // (avoids a lot of cluster math in SD library).
  118.           if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
  119.             pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  120.           else     // Bitmap is stored top-to-bottom
  121.             pos = bmpImageoffset + row * rowSize;
  122.           if (bmpFile.position() != pos) { // Need seek?
  123.             bmpFile.seek(pos);
  124.             buffidx = sizeof(sdbuffer); // Force buffer reload
  125.           }

  126.           for (col = 0; col < w; col++) { // For each column... // Time to read more pixel data? if (buffidx >= sizeof(sdbuffer)) { // Indeed
  127.               // Push LCD buffer to the display first
  128.               if (lcdidx > 0) {
  129.                 tft.pushColors(lcdbuffer, lcdidx, first);
  130.                 lcdidx = 0;
  131.                 first  = false;
  132.               }
  133.               bmpFile.read(sdbuffer, sizeof(sdbuffer));
  134.               buffidx = 0; // Set index to beginning
  135.             }

  136.             // Convert pixel from BMP to TFT format
  137.             b = sdbuffer[buffidx++];
  138.             g = sdbuffer[buffidx++];
  139.             r = sdbuffer[buffidx++];
  140.             lcdbuffer[lcdidx++] = tft.color565(r, g, b);
  141.           } // end pixel
  142.         } // end scanline
  143.         // Write any remaining data to LCD
  144.         if (lcdidx > 0) {
  145.           tft.pushColors(lcdbuffer, lcdidx, first);
  146.         }
  147.         Serial.print(F("Loaded in "));
  148.         Serial.print(millis() - startTime);
  149.         Serial.println(" ms");
  150.       } // end goodBmp
  151.     }
  152.   }

  153.   bmpFile.close();
  154.   if (!goodBmp) Serial.println(F("BMP format not recognized."));
  155. }

  156. // These read 16- and 32-bit types from the SD card file.
  157. // BMP data is stored little-endian, Arduino is little-endian too.
  158. // May need to reverse subscript order if porting elsewhere.

  159. uint16_t read16(File f) {
  160.   uint16_t result;
  161.   ((uint8_t *)&result)[0] = f.read(); // LSB
  162.   ((uint8_t *)&result)[1] = f.read(); // MSB
  163.   return result;
  164. }

  165. uint32_t read32(File f) {
  166.   uint32_t result;
  167.   ((uint8_t *)&result)[0] = f.read(); // LSB
  168.   ((uint8_t *)&result)[1] = f.read();
  169.   ((uint8_t *)&result)[2] = f.read();
  170.   ((uint8_t *)&result)[3] = f.read(); // MSB
  171.   return result;
  172. }
复制代码

要在液晶显示屏上显示图像,您应将图像保存为24位BMP彩色格式,尺寸为240 * 320。 然后将它们移动到SD卡并将SD卡放入LCD扩展板。 我们使用以下函数来显示图片。 这个函数有3个参数; 第一个参数代表图片名称,第二个和第三个参数代表图片左上角的长度和宽度坐标。

  1. bmpdraw(“filename.bmp”,x,y);
复制代码

tft_3.jpg


使用Arduino 2.4寸触摸屏创建一个绘图应用

  1. /*
  2. This code is TFTLCD Library Example
  3. */

  4. #include <Adafruit_GFX.h>        
  5. #include <Adafruit_TFTLCD.h>  
  6. #include <TouchScreen.h>

  7. #if defined(__SAM3X8E__)
  8.     #undef __FlashStringHelper::F(string_literal)
  9.     #define F(string_literal) string_literal
  10. #endif

  11. #define YP A3  
  12. #define XM A2  
  13. #define YM 9   
  14. #define XP 8   

  15. #define TS_MINX 150
  16. #define TS_MINY 120
  17. #define TS_MAXX 920
  18. #define TS_MAXY 940

  19. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

  20. #define LCD_CS A3
  21. #define LCD_CD A2
  22. #define LCD_WR A1
  23. #define LCD_RD A0
  24. #define LCD_RESET A4

  25. #define  BLACK   0x0000
  26. #define BLUE    0x001F
  27. #define RED     0xF800
  28. #define GREEN   0x07E0
  29. #define CYAN    0x07FF
  30. #define MAGENTA 0xF81F
  31. #define YELLOW  0xFFE0
  32. #define WHITE   0xFFFF

  33. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

  34. #define BOXSIZE 40
  35. #define PENRADIUS 3
  36. int oldcolor, currentcolor;

  37. void setup(void) {
  38.   Serial.begin(9600);
  39.   Serial.println(F("Paint!"));
  40.   
  41.   tft.reset();
  42.   
  43.   uint16_t identifier = tft.readID();

  44.   if(identifier == 0x9325) {
  45.     Serial.println(F("Found ILI9325 LCD driver"));
  46.   } else if(identifier == 0x9328) {
  47.     Serial.println(F("Found ILI9328 LCD driver"));
  48.   } else if(identifier == 0x7575) {
  49.     Serial.println(F("Found HX8347G LCD driver"));
  50.   } else if(identifier == 0x9341) {
  51.     Serial.println(F("Found ILI9341 LCD driver"));
  52.   } else if(identifier == 0x8357) {
  53.     Serial.println(F("Found HX8357D LCD driver"));
  54.   } else {
  55.     Serial.print(F("Unknown LCD driver chip: "));
  56.     Serial.println(identifier, HEX);
  57.     Serial.println(F("If using the Adafruit 2.4" TFT Arduino shield, the line:"));
  58.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  59.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  60.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  61.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  62.     Serial.println(F("matches the tutorial."));
  63.     return;
  64.   }

  65.   tft.begin(identifier);

  66.   tft.fillScreen(BLACK);

  67.   tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  68.   tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  69.   tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);
  70.   tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);
  71.   tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);
  72.   tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);

  73.   tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  74.   currentcolor = RED;

  75.   pinMode(13, OUTPUT);
  76. }

  77. #define MINPRESSURE 10
  78. #define MAXPRESSURE 1000

  79. void loop()
  80. {
  81.   digitalWrite(13, HIGH);
  82.   TSPoint p = ts.getPoint();
  83.   digitalWrite(13, LOW);


  84.   pinMode(XM, OUTPUT);
  85.   pinMode(YP, OUTPUT);

  86.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

  87.    
  88.     if (p.y < (TS_MINY-5)) {
  89.       Serial.println("erase");

  90.       tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
  91.     }

  92.     p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
  93.     p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);

  94.     if (p.y < BOXSIZE) {
  95.        oldcolor = currentcolor;

  96.        if (p.x < BOXSIZE) {
  97.          currentcolor = RED;
  98.          tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  99.        } else if (p.x < BOXSIZE*2) {
  100.          currentcolor = YELLOW;
  101.          tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
  102.        } else if (p.x < BOXSIZE*3) {
  103.          currentcolor = GREEN;
  104.          tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);
  105.        } else if (p.x < BOXSIZE*4) {
  106.          currentcolor = CYAN;
  107.          tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);
  108.        } else if (p.x < BOXSIZE*5) {
  109.          currentcolor = BLUE;
  110.          tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);
  111.        } else if (
复制代码

小提示:如果要在不使用SD卡的情况下显示图片,可以将其转换为代码然后再显示。 您可以无延迟地连续显示几张照片以制作动画。 但请注意,在这种情况下,Arduino UNO可能不适合(因为处理器速度低)。 我们建议使用Arduino Mega或Arduino DUE开发板。


以上就是本篇文章的全部内容,如遇到问题,请随时在本帖下面进行回复。

跳转到指定楼层
杨博士
发表于: 2019-10-5 13:34:11 | 显示全部楼层

/*______Import Libraries_______*/
#include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>
/*______End of Libraries_______*/
/*______Define LCD pins (I have asigned the default values)_______*/
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
/*_______End of defanitions______*/
/*______Assign names to colors and pressure_______*/
#define WHITE   0x0000 //Black->White
#define YELLOW    0x001F //Blue->Yellow
#define CYAN     0xF800 //Red->Cyan
#define PINK   0x07E0 //Green-> Pink
#define RED    0x07FF //Cyan -> Red
#define GREEN 0xF81F //Pink -> Green
#define BLUE  0xFFE0 //Yellow->Blue
#define BLACK   0xFFFF //White-> Black
#define MINPRESSURE 10
#define MAXPRESSURE 1000
/*_______Assigned______*/
/*____Calibrate TFT LCD_____*/
#define TS_MINX 125
#define TS_MINY 85
#define TS_MAXX 965
#define TS_MAXY 905
/*______End of Calibration______*/
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //Start communication with LCD
String symbol[4][4] = {
  { "7", "8", "9", "/" },
  { "4", "5", "6", "*" },
  { "1", "2", "3", "-" },
  { "C", "0", "=", "+" }
};
int X,Y;
long Num1,Num2,Number;
char action;
boolean result = false;

void setup() {
  Serial.begin(9600); //Use serial monitor for debugging
  tft.reset(); //Always reset at start
  tft.begin(0x9225); // My LCD uses LIL9341 Interface driver IC
  tft.setRotation(2); // I just roated so that the power jack faces \\
  tft.fillScreen(GREEN);
  IntroScreen();
  draw_BoxNButtons();
}
void loop() {
TSPoint p = waitTouch();
X = p.y; Y = p.x;
Serial.print(X); Serial.print(','); Serial.println(Y);// + " " + Y);
DetectButtons();
if (result==true)
CalculateResult();
DisplayResult();   
  delay(300);
}
TSPoint waitTouch() {
  TSPoint p;
  do {
    p = ts.getPoint();
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
  } while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);;
  return p;
}
void DetectButtons()
{
  
  if (X<50 && X>0) //Detecting Buttons on Column 1
  {
    if (Y>0 && Y<85) //If cancel Button is pressed
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
   
     if (Y>85 && Y<140) //If Button 1 is pressed
    {Serial.println ("Button 1");
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; //Pressed twice
    }
   
     if (Y>140 && Y<192) //If Button 4 is pressed
    {Serial.println ("Button 4");
    if (Number==0)
    Number=4;
    else
    Number = (Number*10) + 4; //Pressed twice
    }
   
     if (Y>192 && Y<245) //If Button 7 is pressed
    {Serial.println ("Button 7");
    if (Number==0)
    Number=7;
    else
    Number = (Number*10) + 7; //Pressed twice
    }
  }
    if (X<105 && X>50) //Detecting Buttons on Column 2
  {
    if (Y>0 && Y<85)
    {Serial.println ("Button 0"); //Button 0 is Pressed
    if (Number==0)
    Number=0;
    else
    Number = (Number*10) + 0; //Pressed twice
    }
   
     if (Y>85 && Y<140)
    {Serial.println ("Button 2");
     if (Number==0)
    Number=2;
    else
    Number = (Number*10) + 2; //Pressed twice
    }
   
     if (Y>140 && Y<192)
    {Serial.println ("Button 5");
     if (Number==0)
    Number=5;
    else
    Number = (Number*10) + 5; //Pressed twic
    }
   
     if (Y>192 && Y<245)
    {Serial.println ("Button 8");
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; //Pressed twic
    }   
  }
    if (X<165 && X>105) //Detecting Buttons on Column 3
  {
    if (Y>0 && Y<85)
    {Serial.println ("Button Equal");
    Num2=Number;
    result = true;
    }
   
     if (Y>85 && Y<140)
    {Serial.println ("Button 3");
     if (Number==0)
    Number=3;
    else
    Number = (Number*10) + 3; //Pressed twice
    }
   
     if (Y>140 && Y<192)
    {Serial.println ("Button 6");
    if (Number==0)
    Number=6;
    else
    Number = (Number*10) + 6; //Pressed twice
    }
   
     if (Y>192 && Y<245)
    {Serial.println ("Button 9");
    if (Number==0)
    Number=9;
    else
    Number = (Number*10) + 9; //Pressed twice
    }   
  }
      if (X<213 && X>165) //Detecting Buttons on Column 3
  {
    Num1 = Number;   
    Number =0;
    tft.setCursor(200, 20);
    tft.setTextColor(RED);
    if (Y>0 && Y<85)
    {Serial.println ("Addition"); action = 1; tft.println('+');}
     if (Y>85 && Y<140)
    {Serial.println ("Subtraction"); action = 2; tft.println('-');}
     if (Y>140 && Y<192)
    {Serial.println ("Multiplication"); action = 3; tft.println('*');}
     if (Y>192 && Y<245)
    {Serial.println ("Devesion"); action = 4; tft.println('/');}  
    delay(300);
  }  
}
void CalculateResult()
{
  if (action==1)
    Number = Num1+Num2;
  if (action==2)
    Number = Num1-Num2;
  if (action==3)
    Number = Num1*Num2;
  if (action==4)
    Number = Num1/Num2;
}
void DisplayResult()
{
    tft.fillRect(0, 0, 240, 80, CYAN);  //clear result box
    tft.setCursor(10, 20);
    tft.setTextSize(4);
    tft.setTextColor(BLACK);
    tft.println(Number); //update new value
}
void IntroScreen()
{
  tft.setCursor (55, 120);
  tft.setTextSize (3);
  tft.setTextColor(RED);
  tft.println("ARDUINO");
  tft.setCursor (30, 160);
  tft.println("CALCULATOR");
  tft.setCursor (30, 220);
  tft.setTextSize (2);
  tft.setTextColor(BLUE);
  tft.println("-Circut Digest");
  delay(1800);
}
void draw_BoxNButtons()
{
  //Draw the Result Box
  tft.fillRect(0, 0, 240, 80, CYAN);
//Draw First Column
  tft.fillRect  (0,260,60,60,RED);
  tft.fillRect  (0,200,60,60,BLACK);
  tft.fillRect  (0,140,60,60,BLACK);
  tft.fillRect  (0,80,60,60,BLACK);
//Draw Third Column  
  tft.fillRect  (120,260,60,60,GREEN);
  tft.fillRect  (120,200,60,60,BLACK);
  tft.fillRect  (120,140,60,60,BLACK);
  tft.fillRect  (120,80,60,60,BLACK);
  //Draw Secound & Fourth Column  
  for (int b=260; b>=80; b-=60)
{ tft.fillRect  (180,b,60,60,BLUE);
   tft.fillRect  (60,b,60,60,BLACK);}
  //Draw Horizontal Lines
  for (int h=80; h<=320; h+=60)
  tft.drawFastHLine(0, h, 240, WHITE);
  //Draw Vertical Lines
  for (int v=0; v<=240; v+=60)
  tft.drawFastVLine(v, 80, 240, WHITE);
  //Display keypad lables
  for (int j=0;j<4;j++) {
    for (int i=0;i<4;i++) {
      tft.setCursor(22 + (60*i), 100 + (60*j));
      tft.setTextSize(3);
      tft.setTextColor(WHITE);
      tft.println(symbol[j][i]);
    }
  }delay(2000);
}
回复

使用道具 举报

杨博士
发表于: 2019-10-5 13:37:11 | 显示全部楼层

这是一个触摸板的计算机程序,触摸屏幕串行监视口有反应,但是触摸屏一直白屏,并没有绘制图形,大神指教一下
回复

使用道具 举报

杨博士
发表于: 2019-10-5 13:53:44 | 显示全部楼层

这是串行监视口
捕获.PNG
回复

使用道具 举报

杨博士
发表于: 2019-10-5 13:56:33 | 显示全部楼层

这个2.4寸的触摸板屏幕是白屏,芯片型号是9341
回复

使用道具 举报

杨博士
发表于: 2019-10-5 13:59:35 | 显示全部楼层

这个是那个屏幕显示
867B4DE65FA90DA69144EFDB48AB5B05.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 377 | 回复: 691



手机版|

GMT+8, 2019-11-16 09:46 , Processed in 0.046874 second(s), 29 queries . Powered by Discuz! X3.4

YiBoard一板网 © 2015-2018 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表