can I count frequency more than Fosc?

Thread Starter

micropad

Joined Dec 24, 2011
106
Dear All

my MUC is 16f877a, I used Timer0 module and set to counter mode
my Fosc is 4MHz
can I count frequency more than Fosc? without prescaler


please advice
 

MMcLaren

Joined Feb 14, 2010
861
I believe MrChips is correct. Use the prescaler to count frequencies up to about 50 MHz. Then use a "trick" to read the prescaler. That is, after you gate the T0CKI input "off" by setting its TRIS bit to 'output', you toggle the TMR0 edge select bit (T0SE) in OPTION_REG which has the effect of toggling the T0CKI input, and you count toggles until the prescaler overflows into TMR0. It sounds much more complicated than it really is...

Rich (BB code):
;
;  new count operation
;
NewCount
        clrf    TMR0            ; clear TMR0 and prescaler        |B0
        clrf    CountL          ; clear 24 bit counter var        |B0
        clrf    CountH          ;                                 |B0
        clrf    CountU          ;                                 |B0
        movlw   200             ;                                 |B0
        movwf   msctr           ; gate timer = 200 msecs          |B0
        movlw   TRISIO          ;                                 |B0
        movwf   FSR             ; setup TRISIO indirect access    |B0
        bsf     INDF,2          ; counter "on" (T0CKI = input)    |B0
;
;  count T0CKI input for precisely 200-msecs (isochronous loop)
;
GateOn
        setz                    ; set Z=1                         |B0
        btfsc   INTCON,T0IF     ; tmr0 overflow? no, skip, else   |B0
        incf    CountU,F        ; bump CountU (Z=0)               |B0
        skpz                    ; tmr0 overflow? no, skip, else   |B0
        bcf     INTCON,T0IF     ; clear T0IF interrupt flag       |B0
        DelayCy(1*msecs-8)      ; delay 1 msec minus 8 cycles     |B0
        decfsz  msctr,F         ; 200 msecs? yes, skip, else      |B0
        goto    GateOn          ; loop again                      |B0
GateOff
        bcf     INDF,2          ; counter "off" (T0CKI = output)  |B0
        btfsc   INTCON,T0IF     ; tmr0 overflow? no, skip, else   |B0
        incf    CountU,F        ; bump CountU                     |B0
        bcf     INTCON,T0IF     ; clear T0IF interrupt flag       |B0
;
;  add 8-bit TMR0 and 8-bit prescaler to the 24 bit result
;
        movf    TMR0,W          ;                                 |B0
        movwf   CountH          ; add TMR0 value to result        |B0
Flush
        bsf     STATUS,RP0      ; bank 1                          |B1
        bcf     OPTION_REG,T0SE ; pulse TMR0 edge select bit      |B1
        bsf     OPTION_REG,T0SE ;                                 |B1
        bcf     STATUS,RP0      ; bank 0                          |B0
        decf    CountL,F        ; decrement counter LSB           |B0
        movf    CountH,W        ;                                 |B0
        xorwf   TMR0,W          ; prescaler overflow into TMR0?   |B0
        bz      Flush           ; no, clock it again              |B0
;
;  CountU:CountH:CountL = 0..10000000 (frequency/5) result
;
 

Markd77

Joined Sep 7, 2009
2,806
If you can use timer1 in asynchronous mode, then the timing requirements are 30ns high, 30ns low. Also it's a 16 bit counter which makes things easier.
 

MMcLaren

Joined Feb 14, 2010
861
Mark is correct. You should be able to count frequencies up to around 16 MHz using Timer 1 and the code is perhaps a little simpler, but beware of an anomaly with Timer 1 where you miss the very first count when using it in asynchronous counter mode...
 

THE_RB

Joined Feb 11, 2008
5,438
Mark is correct. You should be able to count frequencies up to around 16 MHz using Timer 1 and the code is perhaps a little simpler, but beware of an anomaly with Timer 1 where you miss the very first count when using it in asynchronous counter mode...
There were plenty of PIC based freq meter designs in the "old days" that used the externally clocked TMR1 to read up to 50MHz, even with a 20MHz PIC xtal on old PICs like 16F84.

I'm not sure if it violates the official TMR1 ext clock specs but TMR1 is good for 50MHz. :)
 

russ_hensel

Joined Jan 11, 2009
825
You can also use an external circuit to divide the input frequency. Dividing by powers of 2 can be done by cheap easily available ics. Google should point the rest of the way.
 

MrChips

Joined Oct 2, 2009
30,712
Actually, if you look at Param No. 47, Asynchronous, Standard (F),
it states 60ns minimum for T1CLK Input Period. Which suggests that TIMER1 input is good to 16MHz when used in asynchronous mode (see page 59, section 6.4)

(So I stand corrected. I was not aware that TIMER1 can run using asynchronous mode.)
 

MMcLaren

Joined Feb 14, 2010
861
There were plenty of PIC based freq meter designs in the "old days" that used the externally clocked TMR1 to read up to 50MHz, even with a 20MHz PIC xtal on old PICs like 16F84.
Can you point me to one of those please Roman? I've seen a lot of examples over the years but I haven't seen a 50 MHz counter that used TMR1 and so I wonder if you may be mistaken?

Cheerful regards, Mike
 
Last edited:

Thread Starter

micropad

Joined Dec 24, 2011
106
Microchip PIC16F87XA Data Sheet (DS39582B), Page 185, parameters 45 and 46.
Thanks for reply

Dear sir,
that is related to timer1
so I am still stuck with timer0
as per data sheet in counter mode Q2 to Q4 renaming (TtoH and Tt0L) for the sync purpose . let me know , will it be happens every clock that insert to T0CKI
Please advice
 

THE_RB

Joined Feb 11, 2008
5,438
Can you point me to one of those please Roman? I've seen a lot of examples over the years but I haven't seen a 50 MHz counter that used TMR1 and so I wonder if you may be mistaken?

Cheerful regards, Mike
I googled for "50MHz freq meter with PIC 16F84" and there are a few there, like this;
http://www.qsl.net/yo5ofh/pic/frequency_meter_with_pic_16.htm

Then I remebered seeing it even before PIC 16F84, in the 16C54 OTP days, so googled for "50MHz freq meter with PIC 16C54" and got the official microchip appnote saying how to make a "50MHz freq counter with PIC 16C54";
AN592;
http://ww1.microchip.com/downloads/en/AppNotes/00592d.pdf

That uses TMR0 (I said TMR1 sorry!) so that might make a difference?

Didn't think I was going mad, but it has been a lot of years and memories do change over time... ;)
 

MrChips

Joined Oct 2, 2009
30,712
Thanks for reply

Dear sir,
that is related to timer1
so I am still stuck with timer0
as per data sheet in counter mode Q2 to Q4 renaming (TtoH and Tt0L) for the sync purpose . let me know , will it be happens every clock that insert to T0CKI
Please advice
If you are stuck with Timer0 then it is back to a limitation of 4MHz if the XTAL is 20MHz.
 
Top