#define ILI9341_DRIVER
#define TFT_CS 5 // Chân Chip Select
#define TFT_DC 2 // Chân Data/Command
#define TFT_RST 4 // Chân Reset
#define TFT_MISO 19 // Chân MISO
#define TFT_MOSI 23 // Chân MOSI
#define TFT_SCLK 18 // Chân SCK
#include <TFT_eSPI.h> // Thư viện TFT_eSPI
// Tạo một đối tượng TFT
TFT_eSPI tft = TFT_eSPI();
void setup() {
Serial.begin(9600);
Serial.println("Setup...");
// Khởi tạo giao tiếp với màn hình
tft.init();
Serial.println("TFT init...");
// Xoay màn hình (0, 1, 2, 3) tương ứng với các góc 0, 90, 180, 270 độ
tft.setRotation(1);
Serial.println("TFT rotation...");
// Xóa màn hình với màu đen
tft.fillScreen(TFT_BLACK);
Serial.println("TFT fill screen...");
// Thiết lập màu và font cho văn bản
tft.setTextColor(TFT_WHITE, TFT_BLACK); // Màu chữ trắng, nền đen
tft.setTextSize(2); // Kích thước chữ
// Hiển thị một số văn bản
tft.setCursor(10, 10); // Đặt con trỏ
tft.println("Hello, Wokwi!"); // Hiển thị văn bản
Serial.println("TFT print...");
// Hiển thị hình vuông và đường thẳng
tft.drawRect(50, 50, 100, 100, TFT_RED); // Hình vuông
tft.drawLine(50, 50, 150, 150, TFT_GREEN); // Đường chéo màu xanh
Serial.println("TFT draw...");
}
void loop() {
Serial.println("Loop...");
// Vẽ hình tròn di chuyển
static int x = 50;
static int direction = 1;
// Xóa hình tròn cũ
tft.fillCircle(x, 100, 10, TFT_BLACK);
// Cập nhật vị trí
x += direction;
// Đảo chiều khi chạm cạnh
if (x > 120 || x < 40) {
direction = -direction;
}
// Vẽ hình tròn mới
tft.fillCircle(x, 100, 10, TFT_BLUE);
delay(50);
}