can I count frequency more than Fosc?

Discussion in 'Embedded Systems and Microcontrollers' started by micropad, Dec 24, 2011.

  1. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    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
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    The simple answer is no.
     
    micropad likes this.
  3. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    But you could use an external counter and read its contents in parallel.
     
  4. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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...

    Code ( (Unknown Language)):
    1. ;
    2. ;  new count operation
    3. ;
    4. NewCount
    5.         clrf    TMR0            ; clear TMR0 and prescaler        |B0
    6.         clrf    CountL          ; clear 24 bit counter var        |B0
    7.         clrf    CountH          ;                                 |B0
    8.         clrf    CountU          ;                                 |B0
    9.         movlw   200             ;                                 |B0
    10.         movwf   msctr           ; gate timer = 200 msecs          |B0
    11.         movlw   TRISIO          ;                                 |B0
    12.         movwf   FSR             ; setup TRISIO indirect access    |B0
    13.         bsf     INDF,2          ; counter "on" (T0CKI = input)    |B0
    14. ;
    15. ;  count T0CKI input for precisely 200-msecs (isochronous loop)
    16. ;
    17. GateOn
    18.         setz                    ; set Z=1                         |B0
    19.         btfsc   INTCON,T0IF     ; tmr0 overflow? no, skip, else   |B0
    20.         incf    CountU,F        ; bump CountU (Z=0)               |B0
    21.         skpz                    ; tmr0 overflow? no, skip, else   |B0
    22.         bcf     INTCON,T0IF     ; clear T0IF interrupt flag       |B0
    23.         DelayCy(1*msecs-8)      ; delay 1 msec minus 8 cycles     |B0
    24.         decfsz  msctr,F         ; 200 msecs? yes, skip, else      |B0
    25.         goto    GateOn          ; loop again                      |B0
    26. GateOff
    27.         bcf     INDF,2          ; counter "off" (T0CKI = output)  |B0
    28.         btfsc   INTCON,T0IF     ; tmr0 overflow? no, skip, else   |B0
    29.         incf    CountU,F        ; bump CountU                     |B0
    30.         bcf     INTCON,T0IF     ; clear T0IF interrupt flag       |B0
    31. ;
    32. ;  add 8-bit TMR0 and 8-bit prescaler to the 24 bit result
    33. ;
    34.         movf    TMR0,W          ;                                 |B0
    35.         movwf   CountH          ; add TMR0 value to result        |B0
    36. Flush
    37.         bsf     STATUS,RP0      ; bank 1                          |B1
    38.         bcf     OPTION_REG,T0SE ; pulse TMR0 edge select bit      |B1
    39.         bsf     OPTION_REG,T0SE ;                                 |B1
    40.         bcf     STATUS,RP0      ; bank 0                          |B0
    41.         decf    CountL,F        ; decrement counter LSB           |B0
    42.         movf    CountH,W        ;                                 |B0
    43.         xorwf   TMR0,W          ; prescaler overflow into TMR0?   |B0
    44.         bz      Flush           ; no, clock it again              |B0
    45. ;
    46. ;  CountU:CountH:CountL = 0..10000000 (frequency/5) result
    47. ;
    48.  
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.
     
    micropad likes this.
  6. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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...
     
  7. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    Dear Sir,

    can you explain what is the mathematical meaning of above
    please advice
     
  8. evilclem

    Member

    Dec 20, 2011
    118
    16
    What is the period of a 16MHz sine wave?
     
    micropad likes this.
  9. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    yes I got it
    so where did you find this specification ( 30ns high, 30ns low..)

    Thanks in advance
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    Microchip PIC16F87XA Data Sheet (DS39582B), Page 185, parameters 45 and 46.
     
  11. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
  12. russ_hensel

    Well-Known Member

    Jan 11, 2009
    818
    47
    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.
     
  13. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    Dear All
    Please help me to read this spec: I am in bit doubtful
     
  14. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    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.)
     
  15. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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: Dec 25, 2011
  16. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    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
     
  17. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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... ;)
     
  18. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    If you are stuck with Timer0 then it is back to a limitation of 4MHz if the XTAL is 20MHz.
     
  19. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    I think that is about timer1
     
  20. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    Yes, that's what I was referring to... I had never seen a 50 MHz counter that used TMR1...
     
Loading...