inaccurate frequency using TMR1 PI18F452


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    6

    Default 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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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!
    <br>
    DT

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    There's some days like that dude! Hopefully there's no male PMS... as far as i'm aware of

    Quote Originally Posted by Darrel
    Forth, when everything is working correctly, a Const of 65430 will give an interrupt frequency of 22.8 khz instead of 19.8.
    So i guess a tool comes handy now?
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1123&stc=1&d=116063699 6">
    Attached Images Attached Images  
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Steve,

    Yup, that's exactly what I used to figure it out. Extremely handy!

    When you gonna release it?
    <br>
    P.S. Looks a little different again
    DT

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yup it's getting better and better with the feedback i got from the BetaTester team I'm still ear open!

    The final release is still unknown as i have few new 'lazy code generation tool' in my head
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Oct 2006
    Posts
    6


    Did you find this post helpful? Yes | No

    Default 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!
    <br>

  8. #8
    Join Date
    Oct 2006
    Posts
    6


    Did you find this post helpful? Yes | No

    Default 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.

  9. #9
    Join Date
    Oct 2006
    Posts
    6


    Did you find this post helpful? Yes | No

    Default 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.

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    >> 4. When I add the ticks until interrupt routine executes the output frequency seems to be more unstable!!!!

    That shouldn't be the case. Can you show the code for how you're adding it into the timer value?
    <br>
    DT

  11. #11
    Join Date
    Oct 2006
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Interrupt routine

    Code:
    Disable  
    miint:
    	IF PIR1.0 = 1 THEN
    		T1CON.0 = 0
    		OFFCNT_H =TMR1H
    		OFFCNT_L =TMR1L
    		IF ARRANCA_MOTOR = 1 THEN
    			PORTC.5 = 0				'PULSO DE STEP
    			PORTA.5 = 0					'SALIDA PARA SHAFT
    			PRESCALER = FRECUENCIA + OFFCNT
    			TMR1H = PRESCALERH
    			TMR1L = PRESCALERL
    			PORTA.5 = 1				'SALIDA PARA SHAFT
    			PORTC.5 = 1				'PULSO DE STEP
    		ELSE
    			IF EMULACION = 1 THEN
    				IF ACTIVA_PRESENCIA = 0 THEN
    					TRIGGER = 0
    				ELSE
    					TRIGGER = 1
    				ENDIF
    			ENDIF
    		ENDIF
    	'*****************************************************************************************************
    	ENDIF
    	T1CON.0 = 1
    	PIR1.0 = 0
    	'**************************** INTERRUPCION POR TIMER 0  ****************************
    	
    salida1:
    	Resume                 ' retorna al programa principal
     	Enable
    Last edited by Darrel Taylor; - 13th October 2006 at 22:18. Reason: added [code][/code] tags

Similar Threads

  1. Code Measure Frequency by TMR1
    By blinkstar88 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2011, 09:51
  2. TMR1 external LP xtal setup check
    By comwarrior in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th October 2009, 18:11
  3. HPWM command and oscillator frequency
    By RussMartin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2009, 22:41
  4. HPWM10 Frequency Updating
    By duncan303 in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 19th January 2008, 12:30
  5. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 01:47

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts