• AVR Freaks

Hot!16bit Timer Calculation

Page: 12 > Showing page 1 of 2
Author
ft.ovah
Starting Member
  • Total Posts : 61
  • Reward points : 0
  • Joined: 2020/01/10 06:17:03
  • Location: 0
  • Status: offline
2020/02/17 10:23:04 (permalink)
0

16bit Timer Calculation

Hi, can anyone point out what im doing wrong with this formula on a dspic30f2010 having 1 instruction completed in 4 clock cycles below:
Xtal= 7.73Mhz ,Fp = 7.73Mhz/4 =1842500 this value is divided further in postscaler with 64 giving me 28789Hz ,period 1/28789=3.4735E-05. I want a delay of 0.5 seconds so 500 ms/3.4735E-05=14394 clocks and if i subract it from 65563 i get 51141.Now when i insert this into the PR1 register Im getting a time of 444.10ms.I was doing the same calculation on a pic18f mcu and it worked pretty much fine.Am i doing something wrong with the settings ??
Thankyou ,
 
  

Attached Image(s)

#1

23 Replies Related Threads

    mbrowning
    USNA79
    • Total Posts : 1746
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 10:55:59 (permalink)
    0
    You have some misconceptions. You've entered 7.37MHz as the INSTRUCTION CLOCK not the oscillator clock. There are two four clocks per instruction, not 4 as in 8bit PICs but this doesn't enter into the calculation, just that the simulated oscillator is 2 4*7.37M. PR register is what the timer counts up to before it resets, not where it starts counting.
     
    Thus, you have set PR = 51141 so (51141+1) * 64 / 7.37MHz = 444.1ms
     
    What you want is 0.5sec * 7.37MHz / 64 = 57578 -> PR = 57577
    post edited by mbrowning - 2020/02/17 13:30:52
    #2
    ft.ovah
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 11:07:28 (permalink)
    0
    If i were to simulate this on an oscilloscope would i get the same delay of 0.5 seconds ?
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11824
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 11:14:42 (permalink)
    5 (1)
    There are two clocks per instruction, not 4

     
    There are 4.
    #4
    ft.ovah
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 11:17:37 (permalink)
    0
    Exactly, but more or less the time did come out to be 0.499 seconds on the stopwatch.
    #5
    ric
    Super Member
    • Total Posts : 27071
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 12:26:48 (permalink)
    0
    mbrowning
    ...
    PR register is what the timer counts up to before it resets, not where it starts counting.

    So do NOT subtract your calculated value from 65536, use it directly.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    ric
    Super Member
    • Total Posts : 27071
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 12:26:48 (permalink)
    0
    --- deleted double post.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    mbrowning
    USNA79
    • Total Posts : 1746
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 13:27:54 (permalink)
    0
    jtemples
    There are two clocks per instruction, not 4

    There are 4.

    Apologize for the incorrect information. I had not studied dsPIC30 datasheets and thought they were clocked the same as pic24/dsPIC33. I don't believe it matters in terms of the timer calculations above.
    #8
    ft.ovah
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 21:17:32 (permalink)
    0
    If i were to simulate this on an oscilloscope would i get the same delay of 0.5 seconds ?
    #9
    ric
    Super Member
    • Total Posts : 27071
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 22:05:23 (permalink)
    0
    What oscilloscope?
     
    The figures in your simulation don't add up. If it thinks that 3273087 cycles is 444.109 ms, that implies the cycle rate is 3273087 / 0.444109 = 7,370,008 MHz, not 7,730,000
    So, I think you entered your crystal frequency where the simulator wants the instruction clock, and you transposed the 7 and 3.
     
    That means your delay is (51141 +1) / (7,370,000 /64) = 0.441 seconds
     
    Try changing the instruction clock setting to 7,730,000 / 4 = 1,842,500
    and change PR2 to (0.5 * 1,842,500 / 64) -1 = 15,096
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    ft.ovah
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 22:29:48 (permalink)
    0
    0.5sec * 7.37MHz / 64 = 57578 -> PR = 57577
    ric only this seems to work for me ,first of all i cant transpose the number 7.37 because this is going to be my external crystal  Xtal as im not using the internal one . SO i thought why not simulate the same frequency.
    post edited by Ow19m - 2020/02/17 23:27:08

    Attached Image(s)

    #11
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3845
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 23:29:35 (permalink)
    0
    My summary of 16bit cycles and instructions
    There is 1 cycle per instruction on the MC 16bit cpus.
    70% to 80% (maybe more, I will count them later)
     
    Some are 2 cycles per instruction such as move double but then that performs two operations in one instruction.
    (only 1 cycle with acc)
     
    Branches are more depending if taken or not.
     
    DSC instructions can perform 3 instructions in 1 cycle on 40bit accumulators.
    (including 2 x pre-fetch)
     
    Also I would use an external xtal such 10 or 20MHz.
    I never did like the 7.37MHz freq where as newer chips use 8MHz.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #12
    ft.ovah
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 23:35:13 (permalink)
    0
    actually its my mistake , i didnt change my frequency option to hertz
    post edited by Ow19m - 2020/02/17 23:42:56
    #13
    ft.ovah
    Starting Member
    • Total Posts : 61
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 23:41:25 (permalink)
    0
    well the max you could clock a dspic30f (why am i using this old dspic sad: sad) is 120 Mhz with a X16PLL, if i use a 10 Mhz wouldnt that give me 160Mhz which is not even possible,atleast i haven't tried it
     
    #14
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3845
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 23:44:58 (permalink)
    0
    Back to the 1 cycle.
     
    delay of 0.5 seconds.
    CPU runs at: i.e. 20MHz
     
    1/20,000,000 = ‭0.00000005‬ seconds
     
     
    cycles = 0.5 / 0.00000005‬ = ‭10,000,000‬ cycles therefore a 32bit timer is needed.
    Depending on cpu, a prescaler of 256 would make this fit into a 16bit timer with less accuracy.
     
    PERIOD then:
    PR3L = 10,000,000‬ & 0xFFFF
    PR3H = 10,000,000‬ >> 16
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #15
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3845
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 23:55:05 (permalink)
    0
    Says in the specs. that this chip runs at 30MIPS so there no way you are getting 120MHz (fcy or even fosc)
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #16
    ric
    Super Member
    • Total Posts : 27071
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/17 23:57:18 (permalink)
    0
    Ow19m
    ric only this seems to work for me ,first of all i cant transpose the number 7.37 because this is going to be my external crystal  Xtal as im not using the internal one.

    Make up your mind.
    Is the crystal 7.37 MHz or 7.73 MHz?
    You stated 7.73MHz twice in your first post
     

    SO i thought why not simulate the same frequency.

    Have you checked WHAT frequency the simulator wants?
    Crystal, or instruction?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11824
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/18 00:05:00 (permalink)
    0
    Gort2015
    Says in the specs. that this chip runs at 30MIPS so there no way you are getting 120MHz (fcy or even fosc)

     
    Wrong.  30 MIPS = 120 MHz Fosc.
    #18
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3845
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/18 00:14:19 (permalink)
    0
     
    You are using a timer for this delay?
     
    If so:
     
    The internal xtal freq (7.37MHz)., is irelivent when it comes to time period calculations.
     
    You are at 30MHz MIPS therefore the timer runs at 30MIPS.
     
    I thought I'd spend ten minutes on the net, 2 hours went by.
     
    post edited by Gort2015 - 2020/02/18 00:15:24

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #19
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3845
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: 16bit Timer Calculation 2020/02/18 00:40:35 (permalink)
    0
    This is a slow chip, I'd get one of the newer ones, didn't realise how slow the clock cycles were.
     
    A good reason to get something up to date.
     
    My above simple equations will work 100% with FJ and CH chips.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5