I have to implement a counter for upto 30 minutes on my microcontroller. However the hardware interrupts maximum time is nowhere close to this. So I think I would be using the interrupt and increment a variable everytime the interrupt is called by the hardware. However I would like to know if there are already any such open source timers available which I can port and use more easily rather than writing evertything from scratch. The second question is :

interrupt timer()
/increment a count


Other than setting a flag when my required counter value is reached is there any more efficient way of doing this?


1 Answer 1


Initialize a variable in the global scope, and then have your timer interrupt increment it and check the value. Its literally four lines of code.

  • \$\begingroup\$ I agree. It was just that I wasnt sure if this was the way it is generally done since polling it from the main loop may not be very efficient. \$\endgroup\$
    – user43914
    Jun 3, 2014 at 0:06
  • \$\begingroup\$ @user43914 done all the time. The addition and comparison is very minimal codewise, only takes a few clock cycles. \$\endgroup\$
    – Passerby
    Jun 3, 2014 at 0:23
  • 1
    \$\begingroup\$ @user43914 - Don't forget to declare the variable volatile, so checks don't get compiled out. \$\endgroup\$ Jun 3, 2014 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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