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– user439142014年06月03日 00:06:06 +00:00Commented 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– Passerby2014年06月03日 00:23:59 +00:00Commented 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\$Connor Wolf– Connor Wolf2014年06月03日 05:16:12 +00:00Commented Jun 3, 2014 at 5:16