ISR Timer 1 second

Guys, does anyone have an example of how I configure the timer to generate an interrupt every 1 second.

  • Please be more explicit.

FYI

Which processor? Which timer?

1 Like

I have a program that reads the pulses received on a pin through an interrupt and counts them. This part works well.

I made a routine using millis() to display the count value every 1 second and reset it to zero. It works well too.

But unfortunately I had to add some delay() at some point in the program, which causes the routine using millis() to sometimes be executed well after 1s.

So I would like to learn how to use the timer, I don't know which one to generate an interrupt every 1s.

Arduino UNO 16MHz

I would like to do this if I use a library.

I don't know which timer the Arduino UNO uses for the delay() and millis() function and also which timer the PWM uses. So I'm afraid of changing some timer that I shouldn't

Timer0. PWM uses Timers 1 and 2.

  • This is 99.9% never necessary. :sunglasses:

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

Code?

How close to a second is close enough?

Re:

Getting rid of the delay will likely pay off, and then you might not need an interrupt.

If you still want an interrupt, this trick takes advantage of the already-configured timer0 to give free 976.5625Hz interrupts:

Precision is not very important, as the number of pulses I will receive in one second will not be greater than 50, so if in one second I have 49, 50, 51 pulses counted in one second it will not be a problem.

Well, here's the Adafruit timer0 multitasking example modified to do the guts of BlinkWithoutDelay inside an ISR:

// using an interrupt to do a 1 second process
// https://wokwi.com/projects/396818172976688129
//
// Built from
// https://learn.adafruit.com/multi-tasking-the-arduino-part-2/timers
// modified to do the code work of Blink without delay inside an ISR
// https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/

const int ledPin = LED_BUILTIN;  // the number of the LED pin
const long interval = 1000;  // interval at which to blink (milliseconds)
byte ledState = LOW;  // ledState used to set the LED
volatile unsigned long previousMillis = 0;  // will store last time LED was updated

void setup() {
  pinMode(ledPin, OUTPUT);
  // Timer0 is already used for millis() - we'll just interrupt somewhere
  // in the middle and call the "Compare A" function below
  OCR0A = 0xAF; // TIMER0_COMPA_ value
  TIMSK0 |= _BV(OCIE0A); // enable TIMER0_COMPA_vect ISR
}

void loop() {
}

// Interrupt is called about once a millisecond,
ISR(TIMER0_COMPA_vect)
{
  // This is the main part of BWOD,
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis += interval; // update timestamp and account for slippage
    //   previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Would this work? Doesn't use interrupts. But then, why? If you don't need 'em.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.