Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC is Timer1 good for a frequency counter?

Status
Not open for further replies.
Timer 1 is good for asynchronously counting a signal up to approximately 16-MHz where Timer 0 with prescaler is good for about 50-MHz.

Regards, Mike
 

Attachments

  • Freq Counter - Serial 12F683.JPG
    Freq Counter - Serial 12F683.JPG
    49.7 KB · Views: 1,463
Last edited:
And if you've been lookin' at the various example frequency counters out there, you might think you need to use a "gate" pin to freeze the counter input but that's not the case. It's easily accomplished simply with the single T0CKI or T1CKI pin.

Have fun. Regards, Mike
 
Mike said:
And if you've been lookin' at the various example frequency counters out there, you might think you need to use a "gate" pin to freeze the counter input but that's not the case. It's easily accomplished simply with the single T0CKI or T1CKI pin.

Assuming you're referring to the interconnected I/O pins?, it's NOT for gating, it's for manually clocking the pre-scaler so you can read it's value (as it can't be read by the PIC). This allows you to get a 16 bit value from the 8 bit timer!.
 
Nigel Goodwin said:
Assuming you're referring to the interconnected I/O pins?, it's NOT for gating, it's for manually clocking the pre-scaler so you can read it's value (as it can't be read by the PIC). This allows you to get a 16 bit value from the 8 bit timer!.
Hi Nigel,

I believe you may be mistaken.

(1) The 1:256 prescaler can be read/flushed easily by toggling the TMR0 edge select bit in the OPTION register until detecting the TMR0 change (preselector overflow). The following code snippet (16F628A) is used to extract the 8-bit prescaler value once the counter has been stopped;

Code:
;
;  the counter input is gated off after 200-msecs, now finalize
;  our 32-bit count accumulator
;
;  ACCA+0, Most significant byte (TMR0 overflows)
;  ACCA+1, Next significant byte (TMR0 overflows)
;  ACCA+2, copy of TMR0 register
;  ACCA+3, prescaler, Least significant byte
;
        movf    TMR0,W          ; get TMR0 value                  |B0
        movwf   ACCA+2          ;                                 |B0
;
;  empty the 1:256 prescaler by toggling the TMR0 edge select
;  bit and decrementing ACCA+3 (initially 00) until detecting
;  the prescaler overflow into TMR0.
;
FLUSH   bsf     STATUS,RP0      ; select Bank 1                   |B1
        bcf     OPTION_REG,T0SE ; clock on rising edge            |B1
        bsf     OPTION_REG,T0SE ; clock on falling edge           |B1
        bcf     STATUS,RP0      ; select Bank 0                   |B0
        decf    ACCA+3,f        ; decrement counter LSB           |B0
        movf    TMR0,W          ; compare TMR0 to saved value     |B0
        xorwf   ACCA+2,W        ; prescaler overflow?             |B0
        bz      FLUSH           ; no, clock it again              |B0
;
(2) If you try to use the "gate" pin (the "interconnected pin") to clock the prescaler along with the 'live' signal you risk high frequency signals clocking faster than your prescaler increment "pulse".

(3) I have seen designs that used a 74LS00 or 74LS132 and another pin in addition to a "gate" pin that was used for clocking the prescaler, but why bother with the extra hardware and pins when software can provide the same capability?

Kind regards, Mike
 
Last edited:
Mike said:
Hi Nigel,

I believe you may be mistaken.

Perhaps so, on 'modern' PIC's.

(3) I have seen designs that used another pin in addition to a "gate" pin that was used for clocking the prescaler, but why bother when software can provide the same capability?

The original design was based on an old OTP PIC, which presumably didn't have that capability?, most modern conversions just used the old code.
 
Hi Mike,

Clever use of the T0SE bit to toggle the prescalar and save one PIC pin.

One OT question: How do the bank "|B0|" comment in your code comes about? Do you manually place them after the comment using a key macro?
 
Nigel Goodwin said:
Perhaps so, on 'modern' PIC's.
What do you suppose most of us newcomers are using Nigel? Is a 16F84 or 16F84A really that 'modern'?

The original design was based on an old OTP PIC, which presumably didn't have that capability?, most modern conversions just used the old code.
I guess I haven't come across the 'original' design, or a design that uses an "interconnected pin" to clock the prescaler. Is it a Microchip Application Note? Anyway, I'd love to study the design and software if you happen to come across it.

I realize PIC development accounts for only a fraction of your many interests. I only wish you would consider qualifying some of your responses if you feel you're not keeping up-to-date.

Kind regards, Mike
 
eblc1388 said:
Hi Mike,

Clever use of the T0SE bit to toggle the prescalar and save one PIC pin.

One OT question: How do the bank "|B0|" comment in your code comes about? Do you manually place them after the comment using a key macro?
I put those bank reminders into the comment field manually. I suspect it seems silly but it's the method I found that works best for me to help keep track of banking at-a-glance while debugging.

Regards, Mike
 
Mike said:
What do you suppose most of us newcomers are using Nigel? Is a 16F84 or 16F84A really that 'modern'?

That's why I puut it in quotes :D

I guess I haven't come across the 'original' design, or a design that uses an "interconnected pin" to clock the prescaler. Is it a Microchip Application Note? Anyway, I'd love to study the design and software if you happen to come across it.

Yes, it was a MicroChip application note AN592, try:
**broken link removed**
 
Nigel,

Thanks for the link. I see how they're using the RA0 pin to clock the T0CKI input to advance the prescaler (the cover page and schematic says RA2 but the code is using RA0).

The RA0 output will easily overpower the signal generator output (through that 470 ohm series resistor) to prevent additional unwanted counts while RA0 pumps the T0CKI input to advance the prescaler.

I wonder if they could have used the 16C54 T0SE bit on this older device the same way we're using it on more recent devices to read the prescaler value?

Anyway, I hope it's clear to Forum members that "recent" (qualifier) PICs can certainly read the value of the TMR0 prescaler.

Kind regards, Mike
 
Mike said:
Nigel,

Thanks for the link. I see how they're using the RA0 pin to clock the T0CKI input to advance the prescaler (the cover page and schematic says RA2 but the code is using RA0).

The RA0 output will easily overpower the signal generator output (through that 470 ohm series resistor) to prevent additional unwanted counts while RA0 pumps the T0CKI input to advance the prescaler.

I wonder if they could have used the 16C54 T0SE bit on this older device the same way we're using it on more recent devices to read the prescaler value?

I don't know?, presumably it wasn't available back then? - I notice the application note is dated 1997 - but that really sounds too recent, so it may have been an updated application note?.

I'm sure I remember it pre-Windows 95, if not pre-Windows 3.1?.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top