rbfowler9
Newbie level 3
Hi,
I need to create a precision timer for my PIC18F13K50 application. I'm running it at 8MHz with the 4x PLL enabled, resulting in a 0.125us instruction clock.
Are all PIC instructions single-clock? I'm building my delay routines around a Timer 0 set to 1:1 (no prescaler), so the timer clock period is also 0.125us. I tried counting to 8 achieve a 1us delay, but it seems that the needed instructions plus some controller latency are longer than 8 clock cycles, and the microcontroller hangs because of repeated interrupt recursion (I guess).
Trying to achieve a 5us delay, I tried counting to 40, but it results in a delay quite longer than 5us...
Here's my current code, I'm using MPLAB:
My delay routines are simple compares with the current T0TC timer counter:
Am I missing something?
I need to create a precision timer for my PIC18F13K50 application. I'm running it at 8MHz with the 4x PLL enabled, resulting in a 0.125us instruction clock.
Are all PIC instructions single-clock? I'm building my delay routines around a Timer 0 set to 1:1 (no prescaler), so the timer clock period is also 0.125us. I tried counting to 8 achieve a 1us delay, but it seems that the needed instructions plus some controller latency are longer than 8 clock cycles, and the microcontroller hangs because of repeated interrupt recursion (I guess).
Trying to achieve a 5us delay, I tried counting to 40, but it results in a delay quite longer than 5us...
Here's my current code, I'm using MPLAB:
Code:
#pragma code high_vector=0x08;
void int_high_vector(void)
{
_asm GOTO timer_isr _endasm
}
#pragma code
#pragma interrupt timer_isr(void)
{
// clear flag
INTCONbits.TMR0IF=0;
// count to 40
TMR0L=-40;
// my internal counter, declared globally elsewhere
T0TC++;
return
}
My delay routines are simple compares with the current T0TC timer counter:
Code:
timer_now=T0TC;
// if T0TC period were 5us, then 1ms would be 200 T0TC "ticks"
while (timer_now + 200 >= T0TC);
Am I missing something?