1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\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\$ Commented 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\$ Commented 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\$ Commented Jun 3, 2014 at 5:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.