0

I'm trying to program a Timer Interrupt that will go off every 33 ms. I am doing this on the ATtiny85 which has a 20MHz clock. The maximum prescaler I can set is 32 so the longest possible time a single interrupt can be is 408 microseconds.

My plan to reach 33 milliseconds is to just run the interrupt 81 times and that will get me the desired 33 milliseconds. However, I have no clue as to how you can actually count how many time an interrupt has taken place. Anyone on here know how to do that or have any idea as to what would work?

2

Yes, just add a check inside your ISR statement.

ISR(TIMER0_OVF_vect)
{
  static uint8_t postscale = 0;
  if (++postscale == 81) 
  {
    postscale = 0;
    do_stuff();
  }
  else
  {
    go_to_sleep();
  }
}

You always have to enter the ISR method once you're done with the interrupt so just add that if statement and use a static variable (so that it keeps its value) to check if you have made the correct number of loops.

| improve this answer | |
  • 3
    else return; - I suggest. I wouldn't try sleeping inside an ISR. – Nick Gammon Jun 18 '16 at 3:18
  • What would it be returning? – Gabriela Marinho Jun 18 '16 at 15:01
  • Nothing. return on its own is used with a function returning void (like an ISR). So in fact you could omit the else and the return altogether. – Nick Gammon Jun 18 '16 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.