inaccurate frequency using TMR1 PI18F452


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    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 23:18. Reason: added [code][/code] tags

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


    Did you find this post helpful? Yes | No

    Default

    You always want the timer to reload the same on each interrupt. By placing it inside IF/THEN statements, it can change depending on the conditions.

    It also needs to be reloaded and re-started as soon as possible. Putting lot's of statements in-between turning the timer off and then back on again put's a variable amount of time in there as well.

    Try it this way...
    Code:
    @Timer1 = TMR1L
    Timer1  VAR  WORD EXT
    
    Disable  
    miint:
    	IF PIR1.0 = 1 THEN
    		T1CON.0 = 0
    		Timer1 = Timer1 + FRECUENCIA
    		T1CON.0 = 1
    
    		IF ARRANCA_MOTOR = 1 THEN
    			PORTC.5 = 0				'PULSO DE STEP
    			PORTA.5 = 0				'SALIDA PARA SHAFT
    			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
    	'*****************************************************************************************************
    	    PIR1.0 = 0
    	ENDIF
    	'**************************** INTERRUPCION POR TIMER 0  ****************************
    	
    salida1:
    	Resume                 ' retorna al programa principal
     	Enable
    --------------------------------------------------------------------------------
    DT

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Let me edit a small part.


    Code:
    @Timer1 = TMR1L
    Timer1  VAR  WORD EXT
    
    Disable  
    miint:
    	IF PIR1.0 = 1 THEN
    		T1CON.0 = 0
    		Timer1 = Timer1 + FRECUENCIA
    		T1CON.0 = 1
    
    		IF ARRANCA_MOTOR = 1 THEN
    			PORTC.5 = 0	'PULSO DE STEP
    			PORTA.5 = 0	'SALIDA PARA SHAFT
    			PORTA.5 = 1	'SALIDA PARA SHAFT
    			PORTC.5 = 1	'PULSO DE STEP
    		ELSE
    			IF EMULACION = 1 THEN TRIGGER = ACTIVA_PRESENCIA 
    
    		ENDIF
    	'*********************************************************
    	PIR1.0 = 0
    	ENDIF
    	'**************** INTERRUPCION POR TIMER 0  **************
    	
    salida1:
    	Resume                 ' retorna al programa principal
     	Enable
    --------------------------------------------------------------------------------
    Last edited by sayzer; - 14th October 2006 at 04:48.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Oct 2006
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Pic18f452

    I've introduced the changes you suggested.
    I'm VERY pleased to find that frequency is no longer a function of the interrupt routine duration!
    Frequency output is still unstable and is constantly fluctuating end to end about 150 Hz when I set TMR1 to 65200 (7.3 KHz range). How can I improve frequency stability?
    Thanks a lot.

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


    Did you find this post helpful? Yes | No

    Default

    Use an assembler ISR or use 'Instant interrupts' by Mr Darrel Taylor. REALLY NICE STUFF!
    http://www.pbpgroup.com/modules/wfse...p?articleid=19
    Steve

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

Similar Threads

  1. Code Measure Frequency by TMR1
    By blinkstar88 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2011, 10:51
  2. TMR1 external LP xtal setup check
    By comwarrior in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th October 2009, 19:11
  3. HPWM command and oscillator frequency
    By RussMartin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2009, 23:41
  4. HPWM10 Frequency Updating
    By duncan303 in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 19th January 2008, 13:30
  5. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 02: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