Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardware Timers #214

Closed
williamhemmingsen opened this issue Feb 17, 2017 · 13 comments
Closed

Hardware Timers #214

williamhemmingsen opened this issue Feb 17, 2017 · 13 comments

Comments

@williamhemmingsen
Copy link

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!

@me-no-dev
Copy link
Member

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);
    }
}

@arcao
Copy link
Contributor

arcao commented Feb 17, 2017

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() {}

@me-no-dev
Copy link
Member

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

@williamhemmingsen
Copy link
Author

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

@me-no-dev
Copy link
Member

:)

@arcao
Copy link
Contributor

arcao commented Feb 17, 2017

@williamhemmingsen
Copy link
Author

williamhemmingsen commented Feb 23, 2017 via email

@tobozo
Copy link
Contributor

tobozo commented Feb 23, 2017

Your comment is exposing your email address

@me-no-dev
Copy link
Member

not anymore :)

@maxpromer
Copy link
Contributor

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.

@XingyumLee
Copy link

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

@XingyumLee
Copy link

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?

@XingyumLee
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants