Calculating a known and deadly accurate interrupt frequency rate (pic multicalc etc)


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Calculating a known and deadly accurate interrupt frequency rate (pic multicalc etc)

    Ok, so I'm back to PICs after an enforced time away...I'm wanting to get stuck into DDS, but straight out the blocks, I'm confused (it doesn't take much!)

    Right, I want a steady and accurate 20khz interrupt rate - being of the lazy ilk, my first port of call was mister_e's mutlicalc ('Timer helper' tab V1.3.1 dated 2006 - is this the latest version?)

    I'm using timer2 (8 bit timer)
    Oscillator is running @ 16Mhz
    The calculator said I should use a preloader of 63

    So I knocked up this little program...

    Code:
    @ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF & _LVP_OFF
    @ __CONFIG _CONFIG2, _LVP_OFF
    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    Osccon = %01111010   'sets the internal oscillator to 16Mhz
    DEFINE  OSC 16
    TrisA.5 = 0     'Pin2 an output.     
    CM1CON0 = 0   ' COMPARATORS OFF
    CM2CON0 = 0   ' COMPARATORS OFF
    ASM
    INT_LIST  macro ; IntSource,    Label,         Type, ResetFlag?
        INT_Handler  TMR2_INT,  _DDS,  asm,  YES
        endm
        INT_CREATE       ; Creates the interrupt processor
    ENDASM
    T2CON.2 = 1   ' Timer2 on
    PR2 =  63     ' this should yield an interrupt rate of 20khz at 16Mhz.
     
    @ INT_ENABLE TMR2_INT
    Main:
        pause 100
        goto main   
    END
    '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    DDS:
    toggle PORTA.5
    @ INT_RETURN

    But when I actually scope pin 2 - I see a frequency of 31.2Khz - now bearing in mind because I'm toggling, the scoped frequency will be half of the actual interrupt frequency, therefore there must be an interrupt frequency of 62.4khz going on with the PR2 value?!!.

    So what am not doing right?!! (it's sure to be embarrasingly simple!)

    (oh, btw whats's the purpose of the 'reload (instr cycle)' slider in mister_e's calculator??)

    So after dicking about for a while, I decided that better get my backside in gear & knock up a spreadsheet to work this all out...

    Code:
    Oscillator Freq         16000000Hz
    Instruction clock        4000000Hz
    Time per instru       0.00000025 Seconds
    PR2 set to                   200 counts
    Interrupt takes          0.00005 Seconds
    Interrupt Frequency        20000Hz
    Scoped  Toggle rate        10000Hz
    Now when I scope pin2, I see something more like the expected result - 9.93khz ....but where did the other .07khz go?!!

    So what am I missing to get an accurate interrupt toggle frequency reading of 10khz? (which would be a 20khz interrupt rate)
    Last edited by HankMcSpank; - 26th August 2011 at 01:08.

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Calculating a known and deadly accurate interrupt frequency rate (pic multicalc

    Hank, with regular timers, you need to account for the time it takes to reload the timer after it times out. that what the slider is for. But when using PR, it is my understanding the timer just restarts, so no re-load time, no accounting for interrupt time or ISR entry and exit. It should be right on as I understand it. Are you quite sure your scope is true? can you check the OSC with it? maybe the 16M isn't quite. (Maybe you can put a clock ot on one of the OSC pins if using internal clock)
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

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


    Did you find this post helpful? Yes | No

    Default Re: Calculating a known and deadly accurate interrupt frequency rate (pic multicalc

    PicMultiCalc doesn't do TMR2,4,6 unless PR2=255 and you reload on every interrupt.

    TMR2 counts up to the PR2 value + 1, then resets and interrupts.
    So you were only getting 64 counts. (62.5Khz)

    For 200 counts, set PR2 to 199.
    Last edited by Darrel Taylor; - 26th August 2011 at 04:13.
    DT

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Calculating a known and deadly accurate interrupt frequency rate (pic multicalc

    Thanks guys.... PR2 of 199 does indeed get me a toggle frequency of 10khz (actually it's showing bouncing between 9.99khz & 10.0khz ....but that's likely to be a rounding/beating error to do with my scope).

    Darrel thanks for solving the multicalc puzzle to ...thought I was going insane (I probably am anyway "Nurse, more medication quick").

  5. #5
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Calculating a known and deadly accurate interrupt frequency rate (pic multicalc

    You know I knew that, and as soon as I opened this this morning I thought "PR needs to be reload value -1", I bet thats what DT has posted. It still doesn't explain 9.93K, cuz the math if using 201 for the value (pr=200) works out to 9.95K. Thats where I was stumped.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

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