#define LED1 21
#define LED2 22
#define Button 27
hw_timer_t *Timer0_Cfg = NULL;
hw_timer_t *Timer1_Cfg = NULL;
uint8_t count=0;
void IRAM_ATTR Timer0_ISR()
{
digitalWrite(LED1, !digitalRead(LED1));
}
void IRAM_ATTR Timer1_ISR()
{
digitalWrite(LED2, !digitalRead(LED2));
}
void setup()
{
Serial.begin(115200); // Allows serial monitor output (check baud rate)
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
//digitalWrite(LED1,HIGH);
// Initilise the timer.
// Parameter 1 is the timer we want to use. Valid: 0, 1, 2, 3 (total 4 timers)
// Parameter 2 is the prescaler. The ESP32 default clock is at 80MhZ. The value "80" will
// divide the clock by 80, giving us 1,000,000 ticks per second.
// Parameter 3 is true means this counter will count up, instead of down (false).
Timer0_Cfg = timerBegin(0, 80, true); //timer0
Timer1_Cfg = timerBegin(1, 80, true); // timer 1
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
timerAttachInterrupt(Timer1_Cfg, &Timer1_ISR, true);
// This is where we indicate the frequency of the interrupts.
// The value "1000000" (because of the prescaler we set in timerBegin) will produce
// one interrupt every second.
// The 3rd parameter is true so that the counter reloads when it fires an interrupt, and so we
// can get periodic interrupts (instead of a single interrupt).
//timerAlarmWrite(Timer0_Cfg, 1000000, true);
// timerAlarmEnable(Timer0_Cfg);
// timerAlarmWrite(Timer1_Cfg, 1000000, true);
// timerAlarmEnable(Timer1_Cfg);
}
void loop()
{
byte buttonStat= digitalRead(Button); // Do Nothing! P
Serial.print("button's status= "); Serial.print(buttonStat);
if(buttonStat)
{timer0start(10);
count++;
}
// if(count>0)
// {while(1);}
}
void timer0start(uint8_t sec)
{
uint16_t divVal= 1000000; // we will need to change 50000 to a value to setup the resut in msec correctly
timerAlarmWrite(Timer0_Cfg, 1000000, true);
timerAlarmEnable(Timer0_Cfg);
}