Advertisement
mikroavr

lcd_tft

Aug 2nd, 2023 (edited)
1,313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_GFX.h>    // Core graphics library
  2. #include <Adafruit_I2CDevice.h>
  3. #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
  4. #include <SPI.h>             // Arduino SPI library
  5. #define TFT_MOSI 23  // SDA Pin on ESP32
  6. #define TFT_SCLK 18  // SCL Pin on ESP32
  7. #define TFT_CS   5  // Chip select control pin
  8. #define TFT_DC    17  // Data Command control pin
  9. #define TFT_RST   4  // Reset pin (could connect to RST pin)
  10.  
  11.  
  12. // Initialize Adafruit ST7789 TFT library
  13. Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
  14. unsigned long cur_time, old_time;
  15.  
  16. int counter = 0;
  17. char _buffer[7];
  18.  
  19. void setup() {
  20.   // put your setup code here, to run once:
  21.   Serial.begin(115200);
  22.   tft.init(240, 320, SPI_MODE2);
  23.   tft.setRotation(3);
  24.   tft.fillScreen(ST77XX_WHITE);
  25. }
  26.  
  27. void loop() {
  28.   // put your main code here, to run repeatedly:
  29.   cur_time = millis();
  30.   if (cur_time - old_time > 1000) {
  31.     counter++;
  32.     int Humi = random(50, 80);
  33.     // read temperature in degrees Celsius
  34.     int Temp = random(34, 40);
  35.  
  36.     // print temperature (in °C)
  37.     tft.setTextColor(ST77XX_RED, ST77XX_WHITE);  // set text color to red with black background
  38.     if (Temp < 0)   // if temperature < 0
  39.       sprintf(_buffer, "-%02u.%1u", (abs(Temp) / 10) % 100, abs(Temp) % 10);
  40.     else            // temperature >= 0
  41.       sprintf(_buffer, "Vol: %02u.%1u", (Temp / 10) % 100, Temp % 10);
  42.  
  43.     tft.setTextSize(3);
  44.     tft.setCursor(26, 71);
  45.     tft.print(_buffer);
  46.  
  47.     // print humidity (in %)
  48.     tft.setTextColor(ST77XX_CYAN, ST77XX_WHITE);  // set text color to cyan and black background
  49.     sprintf(_buffer, "Cur: %02u.%1u", (Humi / 10) % 100, Humi % 10);
  50.     tft.setCursor(26, 171);
  51.     tft.print(_buffer);
  52.     old_time = millis();
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement