Synchronising Timer0 and Timer1


Closed Thread
Results 1 to 9 of 9
  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,615


    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,615


    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

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Duncan

    This is the same question as " I want to measure the 100th of degree with a LM35 and VDD ref " ...

    99 times upon 100 ... no real need ... exept "for the show" ... but 1% remain useful ...

    but, good exercise to have a reflexion about what offers the chip as features ...

    Alain
    Last edited by Acetronics2; - 9th April 2008 at 12:31.
    ************************************************** ***********************
    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 " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

    Default

    but, good exercise to have a reflexion about what offers the chip as features ...
    your are quite right Alain.

    I have taken a very big step away from the bench for a while now, (just can’t seem to get the level of concentration back).

    There are so many ways of achieving a result that sometimes I have had difficulty seeing through the “liminal”. And by that I mean the mental area where I can unnecessarily execute a task in PBP that can be completed very simply using the inbuilt features of a PIC, and Visa-versa
    Approaching a problem from a PIC perspective or PBP Perspective. And I haven’t started on peripheral components.

    So it is really valuable to have a reflection

    I am in a mental state where I am looking at code and circuits and simply cannot believe they are mine, what they are for and why I build them in the first place. I am finding it quite depressing.

    Anyway, I am hijacking a thread again, so back to it,

    On a 252 I might set an interrupt on the two comparators (single interrupt only inbuilt I think) for each channel that I wish to monitor and use that interrupt event to start one timer with a preload and interrupt on overflow…..Whilst……. using the other two CCP and associated timers to capture all the events on the two channels.

    So I might have THREE consecutive lines starting each of the three timers, just as Alain suggested

    If the events on the two CCP channels are very close together and are triggered before the reset/write is completed then consider a PIC with more Timers that can be associated with the CCP’s so that each subsequent trigger on a particular channel uses a different timer.

    Is every edge being triggered (random pulses) or is it looking at a frequency. So much of this is application specific.

    Duncan

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


    Did you find this post helpful? Yes | No

    Cool Synchronising Timer0 and Timer1

    Thanks for the response folks! I need to determine the EXACT frequency difference between the 2 channels, with frequencies in the 2MHz range. Both frequencies are changing continuously and are close (or equal) all the time. The PIC clock is 20MHz. In my application a single count difference between channels is significant, so the lag caused by coding the Start/Stop on successive program lines will give rise to an error.

    From reviewing your replies, it seems to me that the only totally satisfactory solution is to:

    1 Gate both channels externally using a pair of AND or NAND gates, enabling these via a spare PIC pin
    2 Start both timers in the program
    3 Then feed both timers by enabling the gates
    4 Disable the gates to stop the feed
    5 Then read Timer0 and Timer1.

    It's just a shame that we need to add a clunky 14 pin IC to do this trivial job!

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 : 2

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