Frequency counter


Yesterday 22:13
Forum: Proton Plus Compiler v3
Starter: gdesantis
Views: 0
Replies: 0
Yesterday 18:35
Forum: The Lounge
Starter: Les
Views: 0
Replies: 11
+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Frequency counter130 days old

  1. #1
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    264
    Thumbs Up
    Received: 8
    Given: 1
    Total Downloaded
    3.76 GB

    0 Not allowed!

    Default Frequency counter

    Hello everybody
    Can someone help me with a piece of code to measure a frequency (1Hz to 160 Hz) precision two decimals.
    The result is transmitted to the serial port.
    I use PIC18F14K22
    Thanks in advance,
    Teo

  2. #2
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    657
    Thumbs Up
    Received: 122
    Given: 2
    Total Downloaded
    3.41 GB

    0 Not allowed!

    Default Re: Frequency counter

    Hi Teo,
    You could use my project HERE.
    I am going to review the code for the PIC18Fxx20 family and with 8 displays, I could help you in this matter.
    Regards
    Alberto
    [U]73's de Alberto ea3agv[/U]

  3. #3
    Administrator John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,550
    Thumbs Up
    Received: 108
    Given: 29
    Total Downloaded
    2.69 GB

    0 Not allowed!

    Default Re: Frequency counter

    Hi Teo,
    If you use a counter you'll need to keep the gate open for 100 seconds for two decimal places. Is that practical?
    Another approach would be to measure pulse width. There's an application note on the Microchip site on doing just that but it assumes the pulses are symmetrical.
    John

  4. #4
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    1,852
    Thumbs Up
    Received: 163
    Given: 162
    Total Downloaded
    3.24 GB

    0 Not allowed!

    Default Re: Frequency counter

    Hey alberto, any chance you could do that with the option to omit the displays?
    George

  5. #5
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    484
    Thumbs Up
    Received: 51
    Given: 20
    Total Downloaded
    2.00 GB

    0 Not allowed!

    Default Re: Frequency counter

    You could use the signal you're trying to measure to gate a signal of higher frequency, say for example, 32kHz. 1Hz would give you a count of 32k, 160Hz proportionally less. With a little bit of maths you could optain the frequency of the gating signal.

  6. #6
    Administrator John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,550
    Thumbs Up
    Received: 108
    Given: 29
    Total Downloaded
    2.69 GB

    0 Not allowed!

    Default Re: Frequency counter

    The Microchip approach gives period but makes use of Bob's approach using the internal clock. Frequency is then the inverse of period. I use the method to convert PWM from an accelerometer to period. Works a treat.
    John

  7. #7
    Administrator John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,550
    Thumbs Up
    Received: 108
    Given: 29
    Total Downloaded
    2.69 GB

    1 Not allowed!

    Default Re: Frequency counter

    Hi Teo,
    Here's my code adapted from the Microchip paper.
    It uses Timer 1 running in a 16F628A
    John

    Code:
    GetData:    
            'set capture to every rising waveform
            TMR1L = 0                               'reset the timer to 0
            TMR1H = 0               
            CCP1CON = 000101                     'set ccp interrupt
            PIE1.2 = 0                              'clear the ccp1 interrupt bit
            CaptureFlag = 0                         'reset the flag after setting mode
            'configure time prescaler so that timer will run Tmax without overflow - done in general setup    
            'enable the CCP interrupt
            
            PIE1.2=1                                'set the interrupt on port B3 (CCP)         
    
            'save the captured timer value thus providing the zero count 
            While CaptureFlag=0:Wend                'wait for the flag to go high
            FirstCapture.LowByte=CCPR1L
            FirstCapture.HighByte=CCPR1H            'firstcapture now has value of timer         
            'set capture to every falling waveform
            CCP1CON = 000100                     'interrupt on falling waveform
            CaptureFlag=0                           'prepare for next interrupt must be reset after changing mode   
    
            'when CCP interrupt occurs again subtract saved value (T2) from previous captured value - pulse width
            While CaptureFlag=0:Wend                'wait for the flag to go high
            SecondCapture.LowByte=CCPR1L
            SecondCapture.HighByte=CCPR1H           'firstcapture now has value of timer 
            PulseWidth=SecondCapture - FirstCapture 'now we have the pulse width
            'set capture to every rising waveform
            CCP1CON = 000101 
            CaptureFlag=0                           'capture flag must be reset after change in mode to avoid false trigger
    
            'when CCP interrupt occurs again subtract saved value (T1) from this new value to give T, period of waveform
            While CaptureFlag=0:Wend                'wait for the flag to go high
            ThirdCapture.LowByte=CCPR1L
            ThirdCapture.HighByte=CCPR1H             'firstcapture now has value of timer 
            CaptureFlag=0                           'prepare for next interrupt 
            Period = ThirdCapture - FirstCapture    'now we have the period
            PIE1.2=0                                'disable the interrupt on port B3 (CCP)
            CCP1CON = 000000                     'turn off module

  8. #8
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    264
    Thumbs Up
    Received: 8
    Given: 1
    Total Downloaded
    3.76 GB

    0 Not allowed!

    Default Re: Frequency counter

    Hello everybody,
    I was delayed due to virus fears.
    The idea is to measure two frequencies simultaneously and compare them.
    The frequencies are small and therefore I have to measure the periods of the two signals, calculate the frequencies and compare them.This must be done during a period.
    The signals are not symmetrical, 50% "1" logically 50% "0" logical, so you have to measure the duration of "1" and the duration of "0", sum them up and get the period.
    Thank you all for your help and ... maybe you can help me further.
    Teo

  9. #9
    Administrator John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,550
    Thumbs Up
    Received: 108
    Given: 29
    Total Downloaded
    2.69 GB

    0 Not allowed!

    Default Re: Frequency counter

    That's what my routine does.
    John

  10. #10
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    264
    Thumbs Up
    Received: 8
    Given: 1
    Total Downloaded
    3.76 GB

    0 Not allowed!

    Default Re: Frequency counter

  11. Thank you very much,
    Teo

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. A Frequency Counter with the PIC16F628A
    By towlerg in forum WIKI Discussion
    Replies: 2
    Last Post: 12th May 2016, 19:27
  2. A Frequency Counter
    By AlbertoFS in forum WIKI Discussion
    Replies: 3
    Last Post: 14th July 2015, 18:26
  3. Frequency Counter - 40Mhz
    By CharlieBrown in forum The Lounge
    Replies: 15
    Last Post: 30th December 2014, 12:32
  4. Frequency Counter - complex
    By Jessica in forum Projects discussion
    Replies: 4
    Last Post: 30th January 2014, 09:32
  5. Parsing a frequency counter output.
    By david in forum The Lounge
    Replies: 31
    Last Post: 20th September 2013, 07:26

Members who have read this thread since 25th November 2018, 09:13 : 3

Actions :  (Set Date)  (Clear Date)

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts