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.

Would you rather build a clock or a frequency counter?

Status
Not open for further replies.

blueroomelectronics

Well-Known Member
I'm putting together documentaion for the Dragonfly kit (6 digit LED display kit with an accurate timebase, pushbuttons etc...)
I try to include a sample program in the documentation to get people started so my question is would you rather have a HHMMSS clock or a 10hz to 10MHz frequency counter program as the sample?
 
Considering that you can buy a clock for next to nothing and that every tech could use a frequency counter;
frequency counter! :D
 
Really no reason why you couldn't do both. For me though, a six digit 10-MHz counter wouldn't be much use for my HF ham radio gear...
 
Well it's TTL input can be prescaled. To simplify the design and I don't have use of TMR1's gated clock (it's used by the 32KHz timebase) I'll try it with RA1 comparator to RA4/TMR0 as a TMR0 gate control.
 
blueroomelectronics said:
Well it's TTL input can be prescaled. To simplify the design and I don't have use of TMR1's gated clock (it's used by the 32KHz timebase) I'll try it with RA1 comparator to RA4/TMR0 as a TMR0 gate control.
hi Bill,
My two pennyworth would be to make the counter, with the option to measure
the 'period' of the input waveform [ squarewave].
Quite often it very useful for checking out the mark/space ratio of a wave, by selecting the edge '+ or -' trigger.

Eric
 
blueroomelectronics said:
Well it's TTL input can be prescaled. To simplify the design and I don't have use of TMR1's gated clock (it's used by the 32KHz timebase) I'll try it with RA1 comparator to RA4/TMR0 as a TMR0 gate control.

You might check the VERY old MicroChip application note for a frequency counter, or one of the many more modern variations - they work up to 50MHz.
 
blueroomelectronics said:
Well it's TTL input can be prescaled. To simplify the design and I don't have use of TMR1's gated clock (it's used by the 32KHz timebase) I'll try it with RA1 comparator to RA4/TMR0 as a TMR0 gate control.
The 50-MHz timer 0 input is fine. I wouldn't need an external prescaler. I was referring to the six digit display and your 10-MHz top end as limiting factors (for me).

As for "gate control" you don't really need an additional pin to gate the T0CKI counter input on and off. Just toggle the T0CKI pin from an input to an output. And, you can still 'tickle' the timer 0 edge select bit to empty the 256:1 timer 0 prescaler while the pin is an output. The example below uses indirect addressing to access TRISIO while in bank 0.

Code:
;
;  make the T0CKI pin an output to stop counting after 200-msecs
;  (indirect but same as bcf TRISIO,T0CKI)
;
        bcf     INDF,GP2        ; counter input 'off' (output)    |B0
;
;  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          ; put it in our 32 bit count      |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
;
 
Last edited:
Actually, now that I think about it, using a "gate" pin may not be a bad idea.

Since you'll most like be using an interrupt driven display, you won't be able to use a DelayMS() routine effectively. And using timer interrupts and toggling a "gate" pin or the T0CKI TRIS pin from inside the ISR could be affected by the plus or minus 1 cycle interrupt latency.

I think I would use the CCP1 pin connected to the T0CKI pin as a "gate" and use either "capture with toggle" or "pwm" mode to provide a rock solid gate with no jitter. This way you could process display interrupts and Timer 0 overflow interrupts without affecting the counter "gate" period.
 
I've been pouring through the 16F886 datasheet. This is a much more refined 16F876 with puddles of enhancements. The comparator has gotten quite an overhaul and supports gating timer1, set/reset mode and programmable input pin mux.

On the Dragonfly...
Timer1 has a gate but it's connected to a 32KHz crystal.
CCPx pins are not availabe to the user on Dragonfly
Comparator pin are available
RA1,RA2, RA4(TMR0), RC3(scl) and RC4(sck) are available to the user.

The 16F84 version short TMR0 to ground as a gate, I was hoping to avoid that. I was thinking of a single interrupt when 1 second passes (TMR1)
Code:
;** fast timer0 overflow count loop
fast    btfss   INTCON,T0IF
        goto    fast
        incfsz  bin+1
        goto    fast
        incfsz  bin+2
        goto    fast
        incf    bin+3

Using a 18F2525 would work as it has a gated Timer0 and priority interrupts but that's a different story.
 
It's got an RS485 port in hardware. (easy to talk with a single RS232 port)
It would be very possible to do, it's the TIMER0 gating that's a bit tricky with no external HW for me.
 
I know, no CCPx on Dragonfly, but they're put to good use on Mongoose.

The frequency counter is more complex than a clock (I've posted the clock source in the far from complete Dragonfly Assembly Instructions)

I'll post the frequeny counter as a project on the site though. I'm using this design as a basis.
It goes to 50MHz using only the TIMER0 pin (RA4 is available on Dragonfly)
https://www.qsl.net/dl4yhf/freq_counter/freq_counter.html
**broken link removed**
 
I think they're easier than a clock/calendar but perhaps it's the examples you're studying.

I'm attaching a very simple Serial 50-MHz Counter example that uses a 20-MHz 16F628A and 1-msec Timer 2 interrupts. Basically it alternates between 200-msec "count" and 200-msec "done" cycles. The main program waits for the "done" flag from the ISR then processes the raw count data and sends it through the rs-232 port.

Mike

<corrected listing>
 

Attachments

  • 16F628A Freq Counter 1.txt
    11.2 KB · Views: 178
Last edited:
Thanks Mike, I've always wondered why many frequency counters use the prescaler? Doesn't this lower the count accuracy?

I've used Mike Keitz BCD routine in my version too :)
 
Last edited:
blueroomelectronics said:
Thanks Mike, I've always wondered why many frequency counters use the prescaler? Doesn't this lower the count accuracy?

I've used Mike Keitz BCD routine in my version too :)
If you check out the electrical characteristics section of the Data Sheets you'll find that the only way to clock Timer 0 at 50-MHz (a 20-nsec period) is to use the prescaler. Otherwise the upper limit is just like Timer 1 at around 16-MHz.

Why would you think you would lose accuracy when you use the prescaler? Study the example code and you'll see we use the prescaler to turn Timer 0 into a 16 bit counter instead of just an 8 bit counter.

Download that example again after I update it. I introduced an error while prettying it up. Sorry.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top