#include <Arduino.h>
#include <esp32-hal-timer.h>
// Define the LED pin
const int ledPin = 18;
// Define the button pin
const int buttonPin = 34;
volatile bool button = false;
// Define the timer frequency (10 MHz)
//const int timerFrequency = 10000000;
// Define the timer period (50% duty cycle, 1 Hz)
//const int timerPeriod = timerFrequency / 2;
// Create a timer object
hw_timer_t * timer = NULL;
// Flag to indicate whether the LED is currently blinking
//bool ledBlinking = true;
void IRAM_ATTR onTimer() {
// Toggle the LED on each timer interrupt
digitalWrite(ledPin, !digitalRead(ledPin));
}
void buttonInterrupt()
{
button =!button;
}
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize the button pin as an input with pull-up
pinMode(buttonPin, INPUT_PULLUP);
// Create a timer with a frequency of 10 MHz
timer = timerBegin(0, 8, true);
timerAttachInterrupt(timer, &onTimer, true);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, RISING);
}
void loop()
{
// Check for button presses
if (button) // Debounce the button press)
{
// Start blinking the LED using the timer
timerAlarmWrite(timer, 500000, true);
timerAlarmEnable(timer);
}
else
{
// Turn off the LED and disable the timer
digitalWrite(ledPin, LOW);
timerAlarmDisable(timer);
}
}
**
I am not able to generate clock pulse when push the switch.
**