#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// --- Pines ---
#define LED_PIN 12
#define TFT_CS 15
#define TFT_RST 4
#define TFT_DC 2
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// --- Punteros para los 4 Timers ---
hw_timer_t *timer0_tft = NULL; // Tarea 1: TFT (1s)
hw_timer_t *timer1_ej3 = NULL; // Tarea 2: Ejemplo 3 (0.2s)
hw_timer_t *timer2_hola = NULL; // Tarea 3: "HOLA" (2s)
hw_timer_t *timer3_led = NULL; // Tarea 4: LED (3.5s)
// --- Variables Volátiles (para ISRs) ---
volatile int32_t tftSecondCounter = 0; // Tarea 1
volatile bool printEj3Flag = false; // Tarea 2
volatile bool printHolaFlag = false; // Tarea 3
volatile bool ledState = false; // Tarea 4
// --- Variables de seguimiento ---
int32_t lastTftSecond = -1;
// ISR para Timer 0 (1 segundo)
void IRAM_ATTR onTimer0_TFT() {
tftSecondCounter++;
}
// ISR para Timer 1 (0.2 segundos)
void IRAM_ATTR onTimer1_Ej3() {
printEj3Flag = true;
}
// ISR para Timer 2 (2 segundos)
void IRAM_ATTR onTimer2_Hola() {
printHolaFlag = true;
}
// ISR para Timer 3 (3.5 segundos)
void IRAM_ATTR onTimer3_LED() {
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Demo 4 Timers ESP32");
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
// --- Configurar los 4 Timers en orden ---
// 1. Timer 0 (TFT @ 1s)
// Frecuencia: 1MHz (1µs por tick)
timer0_tft = timerBegin(1000000);
timerAttachInterrupt(timer0_tft, &onTimer0_TFT);
// Alarma: 1,000,000 ticks = 1s
timerAlarm(timer0_tft, 1000000, true, 0);
// 2. Timer 1 (Ejemplo 3 @ 0.2s)
// Frecuencia: 5MHz (equiv. Prescaler 16)
timer1_ej3 = timerBegin(5000000);
timerAttachInterrupt(timer1_ej3, &onTimer1_Ej3);
// Alarma: 1,000,000 ticks = 0.2s * 5,000,000 Hz
timerAlarm(timer1_ej3, 1000000, true, 0);
// 3. Timer 2 ("HOLA" @ 2s)
// Frecuencia: 1MHz
timer2_hola = timerBegin(1000000);
timerAttachInterrupt(timer2_hola, &onTimer2_Hola);
// Alarma: 2,000,000 ticks = 2s
timerAlarm(timer2_hola, 2000000, true, 0);
// 4. Timer 3 (LED @ 3.5s)
// Frecuencia: 1MHz
timer3_led = timerBegin(1000000);
timerAttachInterrupt(timer3_led, &onTimer3_LED);
// Alarma: 3,500,000 ticks = 3.5s
timerAlarm(timer3_led, 3500000, true, 0);
}
void loop() {
// Tarea 1: Actualizar TFT (basado en Timer 0)
if (tftSecondCounter != lastTftSecond) {
lastTftSecond = tftSecondCounter;
tft.fillRect(50, 80, 150, 40, ILI9341_BLACK);
tft.setCursor(50, 80);
tft.print("Seg: ");
tft.print(tftSecondCounter);
}
// Tarea 2: Imprimir "Ejemplo 3" (basado en Timer 1)
if (printEj3Flag) {
printEj3Flag = false;
Serial.println("Interrupción TMR1 cada 0.2 s");
}
// Tarea 3: Imprimir "HOLA" (basado en Timer 2)
if (printHolaFlag) {
printHolaFlag = false;
Serial.println("HOLA");
}
// Tarea 4 (LED) se maneja 100% en la interrupción (Timer 3)
}