I'm new to the timer 0 function of microcontrollers, and I'm having a bit of difficulty figuring something out.
The basic jist of what I want to do is:
Program is running in a loop - A button is pressed
Button press causes the MCU to execute a different loop of code, at the same time, TMR0 is started. The loop continues to execute while the TMR0 basically counts 6 seconds. At the end of the 6 seconds, a value is written to the EEPROM and then the loop continues and waits for another button press, which would send the MCU to yet another loop to start the process over.
I can try to explain a bit better if I need to.
My thoughts are:
The time it takes TMR0 to overflow is quite short, even with a prescaler set to 128. I imagine that I would have to have a variable defined and set a value to it. Each time TMR0 overflows, increment this variable and clear the TMR0 interrupt flag until the variable reaches the set value. I just don't have any idea how to figure out what value this should be, not sure about the math. With an oscillator of 4MHz and a 128 prescaler: 1/[oscillator/4] x prescaler x 256 = overflow time? Using numbers: 1/[4MHz/4] x 128 x 256 = ~32.8mS?
If the above is correct, set a variable to increment each time the timer overflows. For 6 seconds divided by 32.8mS would be roughly 183 counts, so TMR0 interrupt flag set, increment count, if count ==183 write EEPROM and clear IF, if not, Clear IF and keep counting.
Let me know if I need to clarify anything, and thanks for the help. Sorry about the length of the post.
Regards
The basic jist of what I want to do is:
Program is running in a loop - A button is pressed
Button press causes the MCU to execute a different loop of code, at the same time, TMR0 is started. The loop continues to execute while the TMR0 basically counts 6 seconds. At the end of the 6 seconds, a value is written to the EEPROM and then the loop continues and waits for another button press, which would send the MCU to yet another loop to start the process over.
I can try to explain a bit better if I need to.
My thoughts are:
The time it takes TMR0 to overflow is quite short, even with a prescaler set to 128. I imagine that I would have to have a variable defined and set a value to it. Each time TMR0 overflows, increment this variable and clear the TMR0 interrupt flag until the variable reaches the set value. I just don't have any idea how to figure out what value this should be, not sure about the math. With an oscillator of 4MHz and a 128 prescaler: 1/[oscillator/4] x prescaler x 256 = overflow time? Using numbers: 1/[4MHz/4] x 128 x 256 = ~32.8mS?
If the above is correct, set a variable to increment each time the timer overflows. For 6 seconds divided by 32.8mS would be roughly 183 counts, so TMR0 interrupt flag set, increment count, if count ==183 write EEPROM and clear IF, if not, Clear IF and keep counting.
Let me know if I need to clarify anything, and thanks for the help. Sorry about the length of the post.
Regards