rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed May 12, 2010 9:06 am |
|
|
You're trying to use the timer incorrectly. First, never place a printf() or puts() statement in an ISR. Printf()'s takes way too much time. You want to be in and out as quickly as possible, and delays are not good either.
set_timer() takes several instruction cycles to execute and your timer will be many clocks down the road before you're ready to do whatever it is you want to accomplish.
You're not calculating things properly either. Your main clock is 4MHZ. This clock is divided by 4 before it goes into the timer circuitry. You now have a 1MHZ clock coming in. That is being devided by 8 by the prescaler. Your clock is now 125KHZ. The timer is a 16-bit timer which will count up to 65535 before rolling over to zero, at which time the ISR is entered. So, your calculation should be 4MHZ/4/8/65536 = ~1.9. You're having a little over 1.9 interrupts per second.
Now, I would suggest you use timer2 instead. It is possible to configure this to give you just what you want. Configure it with setup_timer_2(T2_DIV_BY_16,249,10);. Timer2 is an 8-bit counter with a preset number that will fire the interrupt when that number is counted to. Your prescaler will be 16, postscaler will be 10 and your ISR will enter when the timer rolls over from 249. So, your calculation will be:
4MHZ/4/16/250/10 = 25. Your ISR will happen 25 times each second. Next, have your counter variable count to 50. When it reaches 50, set a flag (1 bit variable) that can be evaluated in your main() section.
Clear as mud?
Ronald |
|