why trm0 preset don't work in this position?


Closed Thread
Results 1 to 7 of 7
  1. #1

    Default why trm0 preset don't work in this position?

    where i have to point the tmr0 preset to properly work?

    mainloop:

    if INTCON.2=1 THEN
    toggle portb.2
    endif
    INTCON.2=0
    TMR0 = 128 'here don't work

    goto mainloop

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: why trm0 preset don't work in this position?

    Which PIC?

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: why trm0 preset don't work in this position?

    it's the 16f628a

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: why trm0 preset don't work in this position?

    How do you know presetting TMR0 doesn't work?
    It looks to me like you're doing it (and clearing the interrupt flag) all the time (ie outside of the IF/ENDIF block).....
    I guess what you actually want to do is:
    Code:
    mainloop:
    
    if INTCON.2=1 THEN 
      INTCON.2=0
      toggle portb.2 
      TMR0 = 128 'yes work 
    endif
    
    goto mainloop
    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: why trm0 preset don't work in this position?

    Yes now it work, with the frequency counter, measuring the frequency now it's 483 hz, nearly correct for the 1:8 prescaler and 4mhz quartz. The toggle don't half the frequency? For example without tmr0 presetting i measure 244,14 hz that is just 488,28/2 cause the toggle, with the presetting at your point now is 483,05 hz.......

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: why trm0 preset don't work in this position?

    The toggle does effectively halve the frequency but neither time or the timer stops during the actual time it takes for the instructions to execute.

    Clearing the interrupt flag takes instruction(s), toggle the output takes multiple instructions, reloading the timer takes instruction(s). Each instruction takes time. By the time you're actually presetting the timer to 128 it's already continued counting and you're "pulling it back" ever so slightly every time, so the frequency drops. No secrets, nothing special, just the way it works. Think about it.

    Here are two different versions you can try to see which one is the most accurate for your needs, then you can always tweak the value.
    Code:
    mainloop:
    
    if INTCON.2=1 THEN 
      TMR0 = 128 'yes work 
      INTCON.2=0
      toggle portb.2 
    endif
    
    goto mainloop
    Code:
    mainloop:
    
    if INTCON.2=1 THEN 
      TMR0 = TMR0 + 128 'yes work 
      INTCON.2=0
      toggle portb.2 
    endif
    
    goto mainloop
    /Henrik.

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: why trm0 preset don't work in this position?

    thanks, now the frequency is 484 hz

Similar Threads

  1. bit position variable
    By dsicon in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th December 2012, 21:41
  2. TMR0 preset problem, PBP3.0
    By Norbert in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th December 2011, 19:36
  3. Calculating Time for clock's preset
    By menta in forum General
    Replies: 33
    Last Post: - 5th July 2008, 06:09
  4. how to know servo position on lcd?
    By macx75 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd March 2006, 22:52
  5. TMR1 Preset Calculation help
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd December 2005, 00:53

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