Timer issue


Closed Thread
Results 1 to 13 of 13

Thread: Timer issue

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,116

    Default Timer issue

    I am not getting the time I expect from the following Interrupt Driven timer.

    Controller is 16F887,internal OSC at 8MHz, expected interrupt every 25usec and I get every 101usec.

    If I change the reload value to 63, the expected interrupt is 100usec and I get 146usec.

    A check on the IntOSC with Clock out on RA6 prooves that the Clock is correct @ 2MHz (8MHz/4).

    I tested also with the Timer2 w/o pre-post scalers with exactly the same results.

    Here is the test code that is making me nervous:

    Code:
    DEFINE OSC 8
    
    OSCCON = %01110001 'SET SYSTEM CLOCK SWITCH BIT
    
    @Line1 = _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_ON
    @Line2 = _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    
    @ __CONFIG _CONFIG1, Line1 & Line2
    
    @ __CONFIG _CONFIG2, _WRT_HALF & _BOR40V
    
    PORTA=0:PORTB=0:PORTC=0:PORTD=0:PORTE=0
    
    TRISA=%00101101
    TRISB=%00000000
    TRISC=%10000000
    TRISD=%00000011
    TRISE=%00000000
    
    OPTION_REG=00001111
    
    WPUB = %00111111
    
    ADCON0 = %11000001
    ADCON1 = %00000000	'Set PORTA/E analog/digital, right justify result
    ANSEL  = %00101101  'lower port A as Analog input
    ANSELH = %00000000  'all others Digital inputs
    
    DEFINE ADC_BITS 10  'Number of bits in Adcin result
    DEFINE ADC_CLOCK 3  'ADC clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50
                         
    pwm1            var portc.2
    
    INCLUDE "DT_INTS-14.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    
    '**************************************************************************
    '****
    '****                            Initialization
    '****
    '**************************************************************************
    
    't2con = %0000100    ;Fosc/4, no prescaler, no postscaler
    
    't1con = %00110001   'Enable Timer1 (16bit) /8 prescaller, int. Fosc/4 clock
                        '@ 20MHz times out every 104,86ms
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ASM
    INT_LIST  macro    ; IntSource,        Label,       Type, ResetFlag?
    			INT_Handler   TMR0_INT,  _UpdateTimer,   PBP,  yes
    ;			INT_Handler   AD_INT,    _ADC_Sub,       PBP,  yes
    	endm
    	INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::       
    
    @ INT_ENABLE  TMR0_INT      ; enable Timer 1 interrupts
    ;@ INT_ENABLE  AD_INT        ; enable ADC Interrupt
    
    CLEAR
    
    loop1:
    pause 100
    goto loop1
    
    UpdateTimer:                        'This interrupt should happen every 25usec :(
        tmr0=213
        toggle portc.2
    @ INT_RETURN
    I miss something here but ... what?

    Ioannis
    Last edited by Ioannis; - 2nd January 2011 at 18:07.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    What happens if you make it an ASM interrupt instead of a PBP int.?
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Ioannis,

    Try changing the PAUSE 100 to PAUSE 1. I read somewhere that using Pause 1 instead of a bigger number is better for the interrupts. Give it a try and let me know.

    Robert
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,116


    Did you find this post helpful? Yes | No

    Default

    Thanks Dave.

    Hmm, almost half.

    PBP int., 207 reload, 101 usec
    ASM int., 207 reload, 41 usec

    But according to the calculations, 8MHz clock/4=2MHz or 0,5usec clock to TMR0. So a 207 reload should give about 24usec.

    Instead we get 41 or 101 usec, according to selected Interrupt.

    And this leads me to ask, why/when to use ASM instead of PBP as long as I use PBP in the ISR?

    @Robert. This is true for ON INTERRUPTS of the Compiler. Here I use DT_INTS14, so this is of no importance. Thanks anyway.

    Ioannis

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,116


    Did you find this post helpful? Yes | No

    Default

    OK, after some tweaking, and using ASM as TYPE (Don't know why yet), is getting correct results up to a reload value of around 180.

    The changes to get close are:

    Code:
    @ INT_DISABLE TMR0_INT
        TMR0=TMR0+180
        toggle pwm1
    @ INT_ENABLE TMR0_INT
    By adding the value,we have more precise control of the reload value.

    But not close to my target of 25usec

    Ioannis

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Only guessing here, but I think Darrel ment the ASM type when there is ASM code in the ISR. I have noticed better accuracy with timers just by changing the type though.

    Now that I read this page again about the underscore, I may be doing something wrong. http://darreltaylor.com/DT_INTS-14/asm_ints.html

    Hmmm. Always something interesting..
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,116


    Did you find this post helpful? Yes | No

    Default

    There are some shadows on the DT-INTs routines as there is no Manual. I am willing to help writing a manual document whenever Darrel can provide more insides.

    But for the moment the ASM idea seem to get closer to the target.

    Ioannis

Members who have read this thread : 1

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