In the example "Repeat timer example", in the setup() -> Configure a timer interrupt EVERY 1 [sec] ->
Code: Select all
void setup() {
Serial.begin(115200);
// Set BTN_STOP_ALARM to input mode
pinMode(BTN_STOP_ALARM, INPUT);
// Create semaphore to inform us when the timer has fired
timerSemaphore = xSemaphoreCreateBinary();
// Set timer frequency to 1Mhz
timer = timerBegin(1000000);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 1000000, true, 0);
}
_____________________________________________________
The questions is, if I need a frequency to 1000 [Hz] = 1 [KHz] ?, the changes are ->
Code: Select all
// Set timer frequency to 1KHz
timer = timerBegin(1000);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 1000, true, 0);
Code: Select all
hw_timer_t *timerZERO = NULL;
volatile SemaphoreHandle_t timerSemaphoreZERO;
portMUX_TYPE timerMuxZERO = portMUX_INITIALIZER_UNLOCKED;
void setUpTimerZERO(){
// Create semaphore to inform us when the timer has fired
//timerSemaphoreZERO = xSemaphoreCreateBinary();
timerZERO = timerBegin(0,80,true);
Serial.print(F("timerZERO -> "));
Serial.println(timerZERO != NULL ? "is OK." : "have a PROBLEM.");
// Attach onTimer function to our timer.
timerAttachInterrupt(timerZERO, &onTimerZERO,true);
// Set timer frequency to 1 000 [Hz]
timerAlarmWrite(timerZERO, 1000, true);
/* Start an alarm */
timerAlarmEnable(timerZERO);
}
This part compile and Run without problem, in the NEW version 3.0.1 with the changes mentioned, I got an error when the timer set Up.
Here is the complete code with changes in version 3.0.1 ->
Code: Select all
/** SKETCH: AR-ESP32-V.3.0.1-Timer-01.2.ino
FECHA: 10/06/2024 15:00
VERSION: 01.2
OBJECT:
____________________________________________________
Obtener un Timer Interrupt cada 1[ms] SIN ERROR
__________________________________________________________
Repeat timer example
This example shows how to use hardware timer in ESP32. The timer calls onTimer
function every second. The timer can be stopped with button attached to PIN 0
(IO0).
This example code is in the public domain.
*/
// Stop button is attached to PIN 0 (IO0)
#define BTN_STOP_ALARM 0
/** NOTA: No la utilizo */
///////////////////////////////////////
/** SET: timerUNO >>> */
hw_timer_t *timerUNO = NULL;
volatile SemaphoreHandle_t timerSemaphoreUNO;
portMUX_TYPE timerMuxUNO = portMUX_INITIALIZER_UNLOCKED;
/** SET: timerZERO >>> */
hw_timer_t *timerZERO = NULL;
volatile SemaphoreHandle_t timerSemaphoreZERO;
portMUX_TYPE timerMuxZERO = portMUX_INITIALIZER_UNLOCKED;
///////////////////////////////////////
volatile uint32_t isrCounter = 0;
volatile uint32_t isrZeroCOUNTER = 0;
volatile uint32_t lastIsrAt = 0;
volatile bool bTimerUNO=false;
volatile bool bFlag1MS=false;
/** DESHABILITADO:*/
void ARDUINO_ISR_ATTR onTimerZERO() {
// Increment the counter
portENTER_CRITICAL_ISR(&timerMuxZERO);
if(++isrZeroCOUNTER==1000){
isrZeroCOUNTER=0;
lastIsrAt = millis();
bFlag1MS=true;
}
portEXIT_CRITICAL_ISR(&timerMuxZERO);
// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphoreZERO, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output
}
void ARDUINO_ISR_ATTR onTimerUNO() {
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMuxUNO);
//isrCounter++;
++isrCounter;
lastIsrAt = millis();
bTimerUNO=true;
portEXIT_CRITICAL_ISR(&timerMuxUNO);
// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphoreUNO, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output
}
/** DESHABILITADO:*/
void setUpTimerZERO(){
// Create semaphore to inform us when the timer has fired
timerSemaphoreZERO = xSemaphoreCreateBinary();
// Set timer frequency to 1 [KHz]
timerZERO = timerBegin(1000);
Serial.print(F("timerZERO -> "));
Serial.println(timerZERO != NULL ? "is OK." : "have a PROBLEM.");
// Attach onTimer function to our timer.
timerAttachInterrupt(timerZERO, &onTimerZERO);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timerZERO, 1000, true, 0);
}
void setUpTimerUNO(){
// Create semaphore to inform us when the timer has fired
timerSemaphoreUNO = xSemaphoreCreateBinary();
// Set timer frequency to 1 [MHz]
timerUNO = timerBegin(1000000);
Serial.print(F("timerUNO -> "));
Serial.println(timerUNO != NULL ? "is OK." : "have a PROBLEM.");
// Attach onTimer function to our timer.
timerAttachInterrupt(timerUNO, &onTimerUNO);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timerUNO, 1000000, true, 0);
}
void setup() {
Serial.begin(115200);
Serial.println(F("Probando timerZERO & timerUNO."));
// Set BTN_STOP_ALARM to input mode
pinMode(BTN_STOP_ALARM, INPUT);
setUpTimerZERO();
setUpTimerUNO();
}
void loop() {
// If Timer has fired
// if ((xSemaphoreTake(timerSemaphoreZERO, 0) == pdTRUE && bFlag1MS==true) {
if (xSemaphoreTake(timerSemaphoreUNO, 0) == pdTRUE) {
//if( bTimerUNO==true){
// bTimerUNO=false;
uint32_t isrCount = 0, isrTime = 0;
// Read the interrupt count and time
portENTER_CRITICAL(&timerMuxUNO);
isrCount = isrCounter;
isrTime = lastIsrAt;
portEXIT_CRITICAL(&timerMuxUNO);
// Print it
Serial.print("onTimerUNO N° ");
Serial.print(isrCount);
Serial.print(" at ");
Serial.print(isrTime);
Serial.println(" ms");
}
/** DESHABILITADA: Para HABILITAR: hay que sacar el COMENTARIO: >>>
// If button is pressed
if (digitalRead(BTN_STOP_ALARM) == LOW) {
// If timer is still running
if (timerUNO) {
// Stop and free timer
timerEnd(timerUNO);
timerUNO = NULL;
}
}
*/
}
For now, I back to the old version, until I get the solution.
Best regards,
Adrian