the program works but the delay is not 20us as calculated it is about 30us.
i'm using a pic16f876@20mhz
compiler version 3.208
where is the problem??
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
Posted: Fri Dec 03, 2004 11:00 am
The extra time is from the int handler over head. Try adding a value to timer1 to cancel the effect.
timer1 += (0xFFE7);
Be aware of overflow, your timer would have to roll through a complete cycle before you get an int.
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
Posted: Fri Dec 03, 2004 11:29 am
This is the classic problem that everyone starting to work with interrupt based timers stumbles across.
You are assuming that an interrupt is an 'instant' action, requiring no time at all to start. The thing is that before entering your interrupt handler function the compiler has added several instructions for saving all the possible registers that you might change in your function.
On exiting the interrupt the compiler has again added code for restoring the previously saved registers.
On a PIC16 it takes about 18 instructions to enter the interrupt routine the same to exit again. The PIC18 has more registers and so takes even longer with approximately two times 36 instructions.
The easiest solution is to change your interrupt routine by not setting timer1 with a fixed value, but reading timer1 first and then subtract your delay from this.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum