Synchronising Timer0 and Timer1


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Isle of Man
    Posts
    32

    Cool Synchronising Timer0 and Timer1

    I am using an 18F252 to count pulses into Timer0 and Timer1. The following is that part of the program for pulse counting into Timer0 for 100ms. Similar bits of code are used to control Timer1, as indicated.

    'Initialise Timer0
    T0CON.3=1 'Do not use prescaler
    T0CON.4=0 'Increment on low-high edges
    T0CON.5=1 'Count on external pulses
    T0CON.6=0 'Configure as 16 bit counter

    '.... similarly for Timer1

    TMR0H=0: TMR0L=0 'Clears Timer0 Note: MUST write in this order
    T0CON.7=1 'Start Timer0
    pause 100 'Count into Timer0 for 0.1s
    T0CON.7=0 'Stop Timer0

    '.... similarly for Timer1

    'Form Timer0 Pulsecount word
    Lobyte0=TMR0L: Hibyte0=TMR0H 'Note: MUST read in this order
    Pulsecount=Hibyte0
    Pulsecount=Pulsecount1<<8
    Pulsecount=Pulsecount+Lobyte0

    '.... similarly for Timer1

    My question is: how can I modify my code to ensure that both timers start and stop EXACTLY at the same time. Obviously, because of code delays in my program, this can not be the case and there will be a phase lag in the counts. At the moment, the only way I can see to solve this is to switch both pulse streams externally using another pin on the PIC to control a CMOS gate.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink Which is Pic architecture ???

    Hi,

    Good Question !!!

    NO the two timers can't start ( or stop ) at exact same moment ... due to Pic working way !

    BUT you can preload the second ( or third...or ...) timer to get a correct value ...

    Alain
    Last edited by Acetronics2; - 9th April 2008 at 10:33.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Oct 2004
    Location
    North Norfolk UK
    Posts
    146


    Did you find this post helpful? Yes | No

    Default

    hi
    how can I modify my code to ensure that both timers start and stop EXACTLY at the same time
    I might think about placing T0CON.7=1 and T1CON.7=1 on consecutive lines, depending on your clock you can calculate the instruction cycle delay and calibrate that into your calculations

    Depends how "Exactly" fits into your frame of reference, you are measuring pulses on both timers, therefore the difference will always be a constant and you could correct it as a standard systematic error.

    Others will probably know of a way to reduce the delay further than I have suggested but ultimately I suggest it depends on the PIC itself.

    Does the datasheet give a method of starting two timers with the same instruction?

    Just a thought

    Duncan

  4. #4
    Join Date
    Oct 2004
    Location
    North Norfolk UK
    Posts
    146


    Did you find this post helpful? Yes | No

    Default

    Oh hell

    I am always getting logged out when trying to write a contribution, I know I am slow..... but this is tooooo much.

    Anybody know of a setting for "old gits"

    or should I just take the hint and simply give up

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    at second sight ...

    You have two "capture" modules in a '252 ...

    WHY not use both ... triggered at the SAME time ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    Join Date
    Oct 2004
    Location
    North Norfolk UK
    Posts
    146


    Did you find this post helpful? Yes | No

    Default

    Hi Alain

    You have two "capture" modules in a '252 ...

    WHY not use both ... triggered at the SAME time ...
    Mmmm

    Although the "event" trigger would take place at a specific time, would there not be a problem stopping the timers?. Surely they cannot be stopped at the "same "time. One has to take priority over the other, by at least one instruction cycle if not two, one for the capture and a second for the stop.
    Maybe then three cycles for the second timer (extra cyle whilst waiting for the first to be stopped.)


    As mentioned.... how exactly is Exactly?



    Just random thoughts

    Duncan

Similar Threads

  1. ASM code
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 1st March 2010, 23:55
  2. Need help with INTerrupts
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 3rd August 2007, 00:53
  3. 16F690 TIMER0 and TIMER1
    By nickdaprick in forum mel PIC BASIC
    Replies: 2
    Last Post: - 18th April 2007, 14:49
  4. Low-Frequency Counter
    By Zeke in forum General
    Replies: 7
    Last Post: - 19th January 2006, 18:06

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