Arduino-esp32: Hardware Timers

Created on 17 Feb 2017  Â·  13Comments  Â·  Source: espressif/arduino-esp32

Are there any examples of hardware timers for Arduino IDE? I have used ESP32 hw timers a lot, but can't make since of them in the ESP32.

I need to sample the adc a a specified rate. I would like to attachinterrupt to and ISR. Here is what I'm doing in the ESP8266. How do I do this in the ESP32?

void timer_init(void) {
timer1_isr_init();
timer1_attachInterrupt(sample_isr);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write(8333); //9600 samples/s
}

void ICACHE_RAM_ATTR sample_isr() {
...do some stuff
}

Thanks!

Most helpful comment

Edit: @me-no-dev was faster :)


There is example which call onTimer every second:

hw_timer_t * timer = NULL;

void onTimer(){
  static unsigned int counter = 1;

  Serial.print("onTimer ");
  Serial.print(counter);
  Serial.print(" at ");
  Serial.print(millis());
  Serial.println(" ms");

  if (counter == 10)
    endTimer();

  counter++;
}

void startTimer() {
  timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
  timerAttachInterrupt(timer, &onTimer, true); // edge = true
  timerAlarmWrite(timer, 1000000, true);  //1000 ms
  timerAlarmEnable(timer);
}

void endTimer() {
  timerEnd(timer);
  timer = NULL; 
}

void setup() {
  Serial.begin(115200);
  startTimer();
}

void loop() {}

All 13 comments

hw_timer_t * timer = NULL;

void onTimer(){
    Serial.print("timer ");
    Serial.println(millis());
}

// start/stop the timer
void toggleTimer(){
    if(timer){
        timerEnd(timer);
        timer = NULL;
    } else {
        timer = timerBegin(3, 80, 1);//div 80
        timerAttachInterrupt(timer, &onTimer, 1);
        timerAlarmWrite(timer, 1000000, true);//1000ms
        timerAlarmEnable(timer);
    }
}

Edit: @me-no-dev was faster :)


There is example which call onTimer every second:

hw_timer_t * timer = NULL;

void onTimer(){
  static unsigned int counter = 1;

  Serial.print("onTimer ");
  Serial.print(counter);
  Serial.print(" at ");
  Serial.print(millis());
  Serial.println(" ms");

  if (counter == 10)
    endTimer();

  counter++;
}

void startTimer() {
  timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
  timerAttachInterrupt(timer, &onTimer, true); // edge = true
  timerAlarmWrite(timer, 1000000, true);  //1000 ms
  timerAlarmEnable(timer);
}

void endTimer() {
  timerEnd(timer);
  timer = NULL; 
}

void setup() {
  Serial.begin(115200);
  startTimer();
}

void loop() {}

@arcao you asked me as I was looking at the issue :D

Thanks Guys, That was perfect. Thanks again for the quick response

:)

Thanks!  I used your timer example successfully to implement my project.
Thanks again

GitHub - williamhemmingsen/WSPR-ESP32: WSPR implementation using the ESP32 and Si5351

|
|
|
| | |

|

|
|
| |
GitHub - williamhemmingsen/WSPR-ESP32: WSPR implementation using the ESP32 ...
WSPR-ESP32 - WSPR implementation using the ESP32 and Si5351 | |

|

|

  From: Martin Sloup <[email protected]>

To: espressif/arduino-esp32 arduino-esp32@noreply.github.com
Cc: William Hemmingsen ; Author author@noreply.github.com
Sent: Friday, February 17, 2017 7:23 AM
Subject: Re: [espressif/arduino-esp32] Hardware Timers (#214)

There is example which call onTimer every second:hw_timer_t * timer = NULL;

void onTimer(){
static unsigned int counter = 1;

Serial.print("onTimer ");
Serial.print(counter);
Serial.print(" at ");
Serial.print(millis());
Serial.println(" ms");

if (counter == 10)
endTimer();

counter++;
}

void startTimer() {
timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
timerAttachInterrupt(timer, &onTimer, true); // edge = true
timerAlarmWrite(timer, 1000000, true); //1000 ms
timerAlarmEnable(timer);
}

void endTimer() {
timerEnd(timer);
timer = NULL;
}

void setup() {
Serial.begin(115200);
startTimer();
}

void loop() {}—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Your comment is exposing your email address

not anymore :)

How to restart timer ? I test by

hw_timer_t * timer = NULL;

void onTimer(){
    Serial.print("timer ");
    Serial.println(millis());
}

// start/stop the timer
void toggleTimer(){
    if(timer){
        timerEnd(timer);
        timer = NULL;
    } else {
        timer = timerBegin(3, 80, 1);//div 80
        timerAttachInterrupt(timer, &onTimer, 1);
        timerAlarmWrite(timer, 100000, true);//1000ms
        timerAlarmEnable(timer);
    }
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  toggleTimer();
  delay(1000);
}

timer not new start.

hw_timer_t * timer = NULL;

void onTimer(){
    Serial.print("timer ");
    Serial.println(millis());
}

// start/stop the timer
void toggleTimer(){
    if(timer){
        timerEnd(timer);
        timer = NULL;
    } else {
        timer = timerBegin(3, 80, 1);//div 80
        timerAttachInterrupt(timer, &onTimer, 1);
        timerAlarmWrite(timer, 1000000, true);//1000ms
        timerAlarmEnable(timer);
    }
}

where to download the lib

Edit: @me-no-dev was faster :)

There is example which call onTimer every second:

hw_timer_t * timer = NULL;

void onTimer(){
  static unsigned int counter = 1;

  Serial.print("onTimer ");
  Serial.print(counter);
  Serial.print(" at ");
  Serial.print(millis());
  Serial.println(" ms");

  if (counter == 10)
    endTimer();

  counter++;
}

void startTimer() {
  timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
  timerAttachInterrupt(timer, &onTimer, true); // edge = true
  timerAlarmWrite(timer, 1000000, true);  //1000 ms
  timerAlarmEnable(timer);
}

void endTimer() {
  timerEnd(timer);
  timer = NULL; 
}

void setup() {
  Serial.begin(115200);
  startTimer();
}

void loop() {}

so if i end the timer,how can i restart it?

How to restart timer ? I test by

hw_timer_t * timer = NULL;

void onTimer(){
    Serial.print("timer ");
    Serial.println(millis());
}

// start/stop the timer
void toggleTimer(){
    if(timer){
        timerEnd(timer);
        timer = NULL;
    } else {
        timer = timerBegin(3, 80, 1);//div 80
        timerAttachInterrupt(timer, &onTimer, 1);
        timerAlarmWrite(timer, 100000, true);//1000ms
        timerAlarmEnable(timer);
    }
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  toggleTimer();
  delay(1000);
}

timer not new start.

is seems that it just relay for 1 seconds but dont know whether the timer is stopped or not

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0x1abin picture 0x1abin  Â·  3Comments

zuqualla picture zuqualla  Â·  4Comments

DrewHoyt picture DrewHoyt  Â·  4Comments

mpatafio picture mpatafio  Â·  4Comments

Darkhub picture Darkhub  Â·  3Comments