Electronic – Using Timer0 overflow to gate Timer1 on PIC12F1822

microchippictimer

I am trying to use TMR1 to count TMR0 overflows. To do this I am using TMR1 with gate enabled, and using TMR0 overflow as the gate source.

Using MPLAB SIM, I can see that TMR0 is counting and overflowing. The TMROIF bit in INTCON is being set correctly.

TMR1 counts correctly without the gate enabled, and stops when I enable the gate control – so far so good.

However, TMR0 overflowing does not seem to be triggering the gate. The T1GVAL bit of T1GCON is never set.

I am running TMR1 at Fosc and I have the gate running not in toggle mode or single pulse mode…
TMR0 is running at instruction clock / 256, although I presume that this doesn't matter…

Here are my config bits:

__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF</code>
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

and here are the relevant lines where I configure TMR1

banksel T1CON
movlw   b'01000001' ; source = Fosc, prescale 1:1, LP osc - no, Timer on
movwf   T1CON

banksel T1GCON
movlw   b'11000001' ; gate enabled, active high, toggle no, pulse mode no, source TMR0
movwf   T1GCON

Best Answer

I think your problem is based on confusion over exactly what the TMR1 Gate signal actually does. It does not, by itself, cause the timer to count, it merely enables it to count if there are suitable events occurring on its clock input.

It's subtle, but if you look at "FIGURE 21-1: TIMER1 BLOCK DIAGRAM" in the datasheet (p. 179), you'll see the TMR1H/TMR1L register in the middle. All of the logic above that is related to gating, and all of the logic below that is related to clocking. It all comes together at the flip-flop just to the right of the TMR1 register, where the gate signal drives the EN (enable) input of that flip-flop, and the clock signal drives the D (data) input.

What this means is that the output of the flip-flop will toggle (and cause TMR1 to increment) only if the gate signal is high and the clock signal is toggling.

There simply is no way to route overflow events from TMR0 to the clock logic of TMR1, either inside or outside the chip.

EDIT: OK, digging a little deeper (prompted by the comments below), I notice that you have the T1SYNC bit in the T1CON register set to zero. This causes the clock source to be synchronized to Fosc. If that clock source is in fact Fosc itself, the output of the synchronizer will be a constant level — always high or always low, but not toggling.

I believe this is the root cause of your problem. Try setting T1CON to b'01000101'. This should accomplish what you want, assuming that the overflow pulse from TMR0 is exactly one Fosc period long.

Related Topic