View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
Settings timer1 for have 20µs delay |
Posted: Fri Jan 27, 2012 3:47 pm |
|
|
Hi,
I have a question about the timer utilisation....
Timer1 16 bits, 2^16=65536.
20Mhz-> 1 /20Mhz=50ns....x4=200ns.
>>So 65536-64536=100.....100*200ns=20µs.... in theory...
In fact, with a scope, i have 30.40µs...
>> (With timer0, i have the same problem)
If someone could say me why ???
Code: |
#include <18F4523.h>
#fuses NOWDT, HS
#use delay(clock=20000000)
//-------------------------------------------------------------
#int_timer1
void interruption_20us()
{
set_timer1(65436);
output_high(pin_a0);
output_low(pin_a0);
}
//-------------------------------------------------------------
void main(void)
{
set_timer1(65436);
setup_timer_1(T1_internal|T1_DIV_BY_1);
enable_interrupts(int_timer1);
enable_interrupts(global);
for(;;)
{
}
}
//-------------------------------------------------------------
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Timer error |
Posted: Sat Jan 28, 2012 4:13 am |
|
|
As PCM programmer points out, most of your timing error is due to the time taken to get into the interrupt routine.
I tested your program with MPLAB SIM and got a repeat rate of 150 instruction cycles, which is near enough your value for me.
I then replaced your interrupt line
with the lines
Code: |
signed int16 x;
x = get_timer1();
x-=100;
set_timer1(x);
|
The repeat rate is now 113 instruction cycles. The error is now essentially the time taken to process the timer1 register. I haven't had time to test it with a 'scope, but I usually find that the MPLAB SIM is out by a few cycles so have to adjust empirically on the real hardware.
Mike |
|
|
|