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.
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
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!