<div dir="ltr"><div>Zdravim,</div><div><br></div><div>hraju si trochu s ESP32 a prozkoumavam ruzne examply - ted jsem si zkousel jak funguje timer. Nemuzu prijit na to proc mi neblika vestavena LEDka v procedure onTimer. Kdyz ten kus kodu na rozsviceni presunu do hlavni smycky kde se testuje odpaleni timeru a provadi vypis na seriovej port tak tam to blika.</div><div><br></div><div>RV</div><div><br></div><div><br></div><div>/*</div><div> Repeat timer example</div><div><br></div><div> This example shows how to use hardware timer in ESP32. The timer calls onTimer</div><div> function every second. The timer can be stopped with button attached to PIN 0</div><div> (IO0).</div><div><br></div><div> This example code is in the public domain.</div><div> */</div><div><br></div><div>// Stop button is attached to PIN 0 (IO0)</div><div>#define BTN_STOP_ALARM    0</div><div>#define LED_BIN           2</div><div><br></div><div><br></div><div>hw_timer_t * timer = NULL;</div><div>volatile SemaphoreHandle_t timerSemaphore;</div><div>portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;</div><div><br></div><div>volatile uint32_t isrCounter = 0;</div><div>volatile uint32_t lastIsrAt = 0;</div><div><br></div><div>void IRAM_ATTR onTimer(){</div><div>  // Increment the counter and set the time of ISR</div><div>  portENTER_CRITICAL_ISR(&timerMux);</div><div>  isrCounter++;</div><div>  lastIsrAt = millis();</div><div>  portEXIT_CRITICAL_ISR(&timerMux);</div><div>  // Give a semaphore that we can check in the loop</div><div>  xSemaphoreGiveFromISR(timerSemaphore, NULL);</div><div>  // It is safe to use digitalRead/Write here if you want to toggle an output</div><div>  digitalWrite(LED_BIN, 1);</div><div>  delay(100);</div><div>  digitalWrite(LED_BIN, 0);</div><div>}</div><div><br></div><div>void setup() {</div><div>  Serial.begin(115200);</div><div><br></div><div>  // Set BTN_STOP_ALARM to input mode</div><div>  pinMode(BTN_STOP_ALARM, INPUT);</div><div><br></div><div>  // Set LED_BIN to input mode</div><div>  pinMode(LED_BIN, OUTPUT);</div><div><br></div><div>  // Create semaphore to inform us when the timer has fired</div><div>  timerSemaphore = xSemaphoreCreateBinary();</div><div><br></div><div>  // Use 1st timer of 4 (counted from zero).</div><div>  // Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more</div><div>  // info).</div><div>  timer = timerBegin(0, 80, true);</div><div><br></div><div>  // Attach onTimer function to our timer.</div><div>  timerAttachInterrupt(timer, &onTimer, true);</div><div><br></div><div>  // Set alarm to call onTimer function every second (value in microseconds).</div><div>  // Repeat the alarm (third parameter)</div><div>  timerAlarmWrite(timer, 1000000, true);</div><div><br></div><div>  // Start an alarm</div><div>  timerAlarmEnable(timer);</div><div>}</div><div><br></div><div>void loop() {</div><div>  // If Timer has fired</div><div>  if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE){</div><div>    uint32_t isrCount = 0, isrTime = 0;</div><div>    // Read the interrupt count and time</div><div>    portENTER_CRITICAL(&timerMux);</div><div>    isrCount = isrCounter;</div><div>    isrTime = lastIsrAt;</div><div>    portEXIT_CRITICAL(&timerMux);</div><div>    // Print it</div><div>    Serial.print("onTimer no. ");</div><div>    Serial.print(isrCount);</div><div>    Serial.print(" at ");</div><div>    Serial.print(isrTime);</div><div>    Serial.println(" ms");</div><div>  }</div><div>  // If button is pressed</div><div>  if (digitalRead(BTN_STOP_ALARM) == LOW) {</div><div>    // If timer is still running</div><div>    if (timer) {</div><div>      // Stop and free timer</div><div>      timerEnd(timer);</div><div>      timer = NULL;</div><div>    }</div><div>  }</div><div>}</div></div>