• AVR Freaks

Hot!Issue with Timer config

Author
mpittman
Starting Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2016/04/05 08:17:25
  • Location: 0
  • Status: offline
2020/06/24 12:20:08 (permalink)
0

Issue with Timer config

I am using MCC v 5.35 with a PIC24FJ128GC010. The Primary osc is set at 18.432MHz (Fosc) giving an Fosc/2 of 9.216MHz.
 
When configuring a Timer (TMR2), I am selecting the Fosc/2 as the Clock Source and the Input Freq correctly appears as 9.216MHz. I choose a Prescaler of 1:64. Configured as 16 bit timer.
 
In the Timer Period window is shows a Timer Period range of 13.8889us to 455.111ms??   With a clock source of 9.216MHz each timer count should be 0.1085us which at 1:64 should give a range of ~ 6.944us to 455.1ms should it not? Where is the 13.8889us coming from??
#1

6 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3980
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Issue with Timer config 2020/06/24 12:33:29 (permalink)
    0
    13.8889 = 6.9444 * 2
    You cannot have a (synchronous) timer that's counting 0 - 0 - 0 - ...  That's just impossible.
    The fastest you can have is counting 0 - 1 - 0 - 1 - 0 - ...
    post edited by du00000001 - 2020/06/24 12:37:50

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    mpittman
    Starting Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2016/04/05 08:17:25
    • Location: 0
    • Status: offline
    Re: Issue with Timer config 2020/06/24 12:39:40 (permalink)
    4 (1)
    I understand what you're saying.  However, when I try this same configuration with a different device (PIC24FJ256GA705) I get the Timer range of 6.944us to 455.1ms as I expected.  Same clock freq, both 16 bit devices.  Why are they different?
    #3
    du00000001
    Just Some Member
    • Total Posts : 3980
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Issue with Timer config 2020/06/24 12:46:46 (permalink)
    0
    I don't think they are different.
    Unfortunately, the templates in MCC seem to be individual for every single derivative. Seems there's not always the same programmer writing these. So the insight into the minimum timer interval may be not so wide-spread.
    But you're free to try using the other device with the shortest period MCC allows for: might create one or another fancy effect  :)
    (When scaling further down, or using a slower clock source, one might be able to try getting a timer interrupt from this "0-period" timer.)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #4
    pr.mchp
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2015/03/23 06:20:48
    • Location: 0
    • Status: offline
    Re: Issue with Timer config 2020/06/25 01:31:34 (permalink)
    0
    Hi
    1)The minimum possible count is 1.

    2)Tested it on the latest version of MCC available on web. Even with PIC24FJ256GA705 device and for the clock settings mentioned, the range for Timer is 13.88us and 455.11ms as expected.

    Attached Image(s)

    #5
    mpittman
    Starting Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2016/04/05 08:17:25
    • Location: 0
    • Status: offline
    Re: Issue with Timer config 2020/06/25 05:56:45 (permalink)
    0
    @pr.mchp yeah thanks.  When I did that test for the PIC24FJ256GA705 device it was with a slightly older version of MCC and it does give you the 6.94us to 455.1 ms range, but you are correct the later versions do not.
     
    I think I understand where my confusion came from now.  The period for the Timer with that clock is actually 6.94us and NOT 13.889us.  However, when setting the period of a timer PRy, the actual period is always PRy+1.  So if you set the period to PRy=1 the actual period is (1+1)*6.94us = 13.889us.  For a PRy of 2 it is (2+1)*6.94us = 20.833us, etc.  all the way up to max period:  PRy=65535(FFFF):  (65535+1)*6.94us = 455.111ms.
     
    Has to do with the roll over when a match is made.  when PRy = TMR, the next cycle goes to 0 so the period is actually PRy+1.  See this post: https://www.microchip.com/forums/m1070494.aspx
     
    This is also why in MCC when you input a specific time period in the Timer Period box, the value shown in the Period Count will always be 1 less than you expect because they are accounting for the PRy+1 by subtracting 1 from it.
     
    The strange thing is there is no mention of this in the Timer section of the datashseet, but there is in the Output Compare section of the datasheet in section 15.3.1.
     
    Thanks for the responses.
    #6
    SteveAtWork
    Starting Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2007/08/27 15:27:01
    • Location: 0
    • Status: offline
    Re: Issue with Timer config 2020/09/20 18:57:38 (permalink)
    0
    Similar issue, different processor. Context, Core 4.85, MPLABX IDE 5.30, Processor 16LF15356. Timer mode 16bit.
     
    When setting up a timer 0 to run on unsync LPINTOSC for a timeout of 60 seconds the config module gets is completely incorrect. For anything beyond Postscale 1:1 the output is not calculated. Note my representation of the TMR0 value is as a 16bit value, I am aware this is actually two 8bit registers (TMR0H/TMR0L).
     
    Example (from my 60 second timer:
    Prescale 1:32, Postscale 1:1 Actual shown as 60s, suggested (Registers tab) TMR0 value 0x1CF3, Correct
    Prescale 1:16, Postscale 1:2 Actual shown as 67.64903s, suggested (Registers tab) TMR0 value 0x0000, Incorrect. (Correct value is same 0x1CF3)
     
    Another example (as 2 second timer)
    Prescale 1:2, Postscale 1:1, Actual shown as 2s, suggested (Registers tab) TMR0 value 0x86E8, Correct
    Prescale 1:1, Postscale 1:2, Actual shown as 4.114s, suggested (Registers tab) TMR0 value 0x0000, Incorrect
     
    So basically because it can't handle the postscale value MCC makes the TMR0 value 0x0000 and then calculates the actual time as being full scale. It is also not reliant on the clock source selected. 
     
    I have resorted to using an excel sheet (for this project) to calculate the correct timer values. Also I am also not reliant on the value that MCC produces as I am swapping and changing timer modes etc throughout my code. MCC mostly provides a quick (usually) method of obtaining the values as I can swap and change the various settings and then just grab the values from the registers page when I am happy.  
     
    I have not tried other cores or other processors, I did try Timer 2 on this, it works correctly however this has a much different configuration and as such the result is probably calculated differently. 
     
    I did try Timer0 as 8bit mode and as this then uses TMR0H as the equivalent of PR0 (a period register rather than an overflow register) and this does work correctly (desired timeout is set as 10ms). Swapping the Pre and post scalers produces the correct values (for TMR0H).
     
    Conclusion is that for 16bit mode the calculation is broken. 
     
    Regards,
    Steve B
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5