inaccurate frequency using TMR1 PI18F452
I'm working with a PIC18F452 with a 20 MHz extrenal clock, writing a program with PBP 2.46
I'm generating pulses using PORTC.5 in the interrupt routine to drive a stepper motor.
I'm measuring the output pulse on pin 28 (portc.5) with an oscilloscope.
Variable PRESCALER is used to load TMR1
if PRESCALER = 65430 output frequency is 19.85 KHz
if PRESCALER = 65431 output frequency is 19.85 KHz
if PRESCALER = 65432 output frequency is 19.85 KHz
if PRESCALER = 65433 output frequency is 20.83 KHz !!!!!!!!!!!!!!!!!!!!!!!!!!
if PRESCALER = 65434 output frequency is 20.77 KHz
if PRESCALER = 65435 output frequency is 20.77 KHz
if PRESCALER = 65436 output frequency is 20.83 KHz
if PRESCALER = 65437 output frequency is 20.83 KHz
if PRESCALER = 65438 output frequency is 21.74 KHz !!!!!!!!!!!!!!!!
if PRESCALER = 65439 output frequency is 21.74 KHz
if PRESCALER = 65440 output frequency is 21.82 KHz
Can someone please explain me why I get 1 KHz frequency gaps ? Is this normal?
Below part of my code:
PRESCALER VAR WORD
PRESCALERL VAR PRESCALER.byte0
PRESCALERH VAR PRESCALER.byte1
T1CON = %00010100 'TIMER1 PRESCALER 1:2, TIMER , CLOCK INTERNO (F/4), STOP TIMER1 &&&
miint:
IF PIR1.0 = 1 THEN
T1CON.0 = 0
PORTC.5 = 0 'STEP pulse
TMR1H = PRESCALERH
TMR1L = PRESCALERL
PORTC.5 = 1 'PULSO DE STEP
ENDIF
T1CON.0 = 1
PIR1.0 = 0
Resume ' retorna al programa principal
Norberto Karpovich
inaccurate frequency using TMR1 PIC18F452
Thanks Darrel for your reply,
1. I'm only showing part of the code, not all of it.
2. I'm not concerned about obtaining a frequency lower than it should be, I'm rather concerned about the gaps in frequency when changing by 1 the value I load into TMR1...
3. While the program is waiting for the interrupt it is only executing a while ...wend loop checking for two digital inputs, that's all. I presume the interrupt doesn't take that much in this case (no PAUSE instructions nor LCDOUT...)
4. When I add the ticks until interrupt routine executes the output frequency seems to be more unstable!!!!
suggestions are welcome, thanks a lot.
Quote:
Originally Posted by Darrel Taylor
First off, You can't expect ON INTERRUPT to perform accurately in the range of 20khz. It's more of a, "Whenever I get around to it" interrupt system. If you want an accurate frequency in this range, you need to use ASM.
Second, you need to ADD the constant to the existing timer value, instead of just loading the number on each interrupt. Otherwise you lose however many "Ticks" the ON INTERRUPT waited before finally servicing it.
Third, you have to account for the time it takes to ADD the constant to the timer value. And for that, you need to know how long that takes. Hard to know with Basic Interrupts.
Forth, when everything is working correctly, a Const of 65430 will give an interrupt frequency of 22.8 khz instead of 19.8.
and Fifth, You're loading a value into the TIMER registers, not the prescaler.
Ok, let's go for sixth. Setting the T1SYNC (T1CON.2) bit has no effect when using the system clock (FOSC/4) for the timer input. Doesn't hurt, but doesn't do anything constructive either.
And, yes I've been having a bad day. Sorry! :o
<br>
inaccurate frequency using TMR1 PIC18F452
Quote:
Originally Posted by mister_e
is that your whole code?
If so, there's few things missing about the interrupt declaration and handling (Disable, Enable, On interupt goto,...,.. )
i can't explain why it should longer to load one WORD value or Another. so it's certainely something elsewhere in the whole program.
Thanks Mister_e for your reply,
1. I'm only showing part of the code, not all of it.
2. I'm not concerned about obtaining a frequency lower than it should be, I'm rather concerned about the gaps in frequency when changing by 1 the value I load into TMR1...
3. While the program is waiting for the interrupt it is only executing a while ...wend loop checking for two digital inputs, that's all. I presume the interrupt doesn't take that much in this case (no PAUSE instructions nor LCDOUT...)
4. When I add the ticks until interrupt routine executes the output frequency seems to be more unstable!!!!
suggestions are welcome, thanks a lot.
inaccurate frequency using TMR1 PIC18F452
1. I'm only showing part of the code, not all of it.
2. I'm not concerned about obtaining a frequency lower than it should be, I'm rather concerned about the gaps in frequency when changing by 1 the value I load into TMR1...
3. While the program is waiting for the interrupt it is only executing a while ...wend loop checking for two digital inputs, that's all. I presume the interrupt doesn't take that much in this case (no PAUSE instructions nor LCDOUT...)
4. When I add the ticks until interrupt routine executes the output frequency seems to be more unstable!!!!
suggestions are welcome, thanks a lot.