• AVR Freaks

Hot!timer0 faster than expected

Author
Catriona
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2019/03/27 08:22:26
  • Location: 0
  • Status: offline
2020/02/15 11:02:11 (permalink)
0

timer0 faster than expected

Hello everyone, I am a but confused with what i'm seeing on my oscilloscope when running my simple timer program I have done.  I have attached the code but as a quick summary:
 
PIC18F47K72
clock - internal clock set at 1Mhz from the configuration bits option list. 
timer0 clock source - Fosc/4 (250Khz) (This is correct as I have checked by enabling the clock output pin and measuring on my scope)
prescaler 1:4
postscaler 1:1
8 bit mode
 
I have written the code to turn on a digital output then start the timer and wait for the interrupt flag to set on overflow before turning off the digital output and resetting the timer.  At the moment it is triggered by a pushbutton input.
 
I would have expected that when using 250KHz clock (Fosc/4), and 4:1 prescaler and the timer incrementing every instruction cycle (the datasheet says that timer0 acts as timer only when using internal clock source and timer mode increments every instruction cycle) that the count rate should be 64uS and the counter will fill up after 256 counts which is 16.38ms.
 
250,000Hz/4 = 62,500Hz (after prescaler); 62500Hz/4 = 15625Hz (per instruction cycle); 
1/15625Hz = 0.000064seconds (64us)
8 bit counter gives 256 increments = 64us * 256 = 0.01638s (16.38ms)
 
therefore I would expect my digital ouput to be on for approximately the same length of time however on my scope it is only on for 40us.  Nothing I do on paper with my calculations makes this number make sense so I must be doing something every obvious wrong.  Can someone tell me what is going on?
 
#1

8 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 10550
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: timer0 faster than expected 2020/02/15 11:44:35 (permalink)
    0
    Missing a semicolon at the end of this statement:
            while(INTERRUPT_FLAG == 0)

    #2
    Catriona
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2019/03/27 08:22:26
    • Location: 0
    • Status: offline
    Re: timer0 faster than expected 2020/02/15 11:48:03 (permalink)
    0
    Oh God!! How did it manage to compile without errors or warnings?
    #3
    1and0
    Access is Denied
    • Total Posts : 10550
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: timer0 faster than expected 2020/02/15 11:55:31 (permalink)
    +2 (2)
    Catriona
    Oh God!! How did it manage to compile without errors or warnings?

    Because there is nothing wrong with it. It just executes what you coded it to do. ;)
    while(INTERRUPT_FLAG == 0)
        PULSE = 0;

    ... which clear PULSE as long as INTERRUPT_FLAG is zero.
    #4
    1and0
    Access is Denied
    • Total Posts : 10550
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: timer0 faster than expected 2020/02/15 12:41:41 (permalink)
    +1 (1)
    In case you still don't understand the difference, this with the semicolon:
    while(INTERRUPT_FLAG == 0);

    is the same as this
    while(INTERRUPT_FLAG == 0)
        ;

    or this
    while(INTERRUPT_FLAG == 0)
    {
    }

    which is a loop with an empty body. That is, it loops doing "nothing" while INTERRUPT_FLAG is zero.
    #5
    Catriona
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2019/03/27 08:22:26
    • Location: 0
    • Status: offline
    Re: timer0 faster than expected 2020/02/15 12:50:55 (permalink)
    0
    Yeah, I realised that after looking at my code. Guy at work told me I didn't need it but I think he meant I didn't need to have an empty statement in curly brackets and I misunderstood what he meant.
    #6
    gr8legs
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2015/06/30 11:32:52
    • Location: 0
    • Status: offline
    Re: timer0 faster than expected 2020/02/19 21:02:37 (permalink)
    0
    I wish I had a nickel for every ; I forgot!
    post edited by gr8legs - 2020/02/19 21:05:06
    #7
    ric
    Super Member
    • Total Posts : 26091
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: timer0 faster than expected 2020/02/19 21:05:52 (permalink)
    0
    You seem to have missed that this was pointed out and corrected four days ago...
     

    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!
    #8
    1and0
    Access is Denied
    • Total Posts : 10550
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: timer0 faster than expected 2020/02/20 08:20:35 (permalink)
    0
    gr8legs
    I wish I had a nickel for every ; I forgot!

    I'd be nickleless .... ;)
     
    #9
    Jump to:
    © 2020 APG vNext Commercial Version 4.5