Accurate Freq Measurment


Closed Thread
Results 1 to 23 of 23

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Bruce,
    Thanks for the info on the 18F2431, I just order some today, I like the idea of having less code. My application is where I have a device that puts out two frequencies, and I need to know the frequency difference very accurately between the two of them, such that I am getting on the order the effectiveness of a 20 bit A/D process. I have your code up and running perfectly from the thread you listed earlier (the first code example), it works much better than mine. But I when tried changing the mode for the 2nd capture event from a falling edge to the 16th edge mode, I had odd values, acting like I had captured the 9th edge rather than the 16th, very strange. The only change to your code was going from:

    CCP1CON.0 = 0 ' Configure capture for falling edge now
    to
    CCP1CON = %00000111 ' Capture mode, capture on 16th rising edge

    Have you tried this capture mode, or is there something wrong with this simple change.
    Cheers,
    Terry

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Terry,

    Check the datasheet to see the effect of changing the prescaler with capture enabled.

    That may be what's causing the bad capture value. You should be OK if you set it up for
    every 16th edge before enabling capture. Switching prescaler with capture enabled can
    cause odd results.

    Edit: If you do need to do this, try disabling capture first, then changing prescaler.

    CCP1CON=0 ' disable CCP module
    Capture=0 ' clear capture flag
    CCP1CON=%00000111 ' re-enable for every 16th edge

    Never tried it myself, but it looks like it should work. Disabling the CCP module clears the
    prescaler count.
    Last edited by Bruce; - 5th June 2008 at 23:37.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Bruce,
    You nailed the problem with the prescaler, set it and don't change it half ways through. A simple code change and it all now works great! My next step is to try your 2nd code example using Darrel Taylors' instant interrupts, I haven't had a need to try them yet, but they look very useful and reasonably easy to use for this application.
    Thanks to ALL for your help and suggestions on my problem, I continue to learn many things from this great forum of users.
    Cheers,
    Terry

  4. #4


    Did you find this post helpful? Yes | No

    Default timer 5

    Bruce & Terry,
    I have been following posts on freq measure. That timer 5 with the motion feedback module is just super-neat. Hardly any code to get very accurate measure. Being able to measure small ,small errors is amazing.
    Terry, if you don't mind, how much are your readings varying around the 4khz and what clock did you end up using?
    Also, what are you doing with the output value?
    Hats off to you guys.
    don
    amgen

  5. #5
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Don,
    I have been experimenting with both the prescaler (divide by 16) and skipping it. When I use an Agilent frequency generator for testing, the count only jitters by 1 count with either approach, the measurement is very stable under this test situation with both approaches. Now that I have the PIC code working, I am now experimenting with my actual application to see which is the better method.
    When measuring the period of 200 cycles of a 4 kHz signal, the frequency resolution is great, 1 count corresponds to 0.016 Hz at 4 KHz and the measurement time is 50 msec. The PIC counters are great once you understand how to set them up.
    I output the count data via RS-232 to a PC.
    Terry

  6. #6
    Join Date
    May 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Accurate Freq Measurment

    Hi,

    I've read the great advice and example code here during a search of PIC period measurement. I'm in the process of using a PIC 18F2431 and using C.

    Not being an expert with PICBasic, could I ask what PIR3.1 refers too from the example given above?

    Code:
    Capture VAR PIR3.1
    Thanks in advance, David.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default Re: Accurate Freq Measurment

    Hi,

    "C" ... What is "C" ...

    is there anything called "C" somewhere ????

    has "C" ever existed ???

    Just open your pic datasheet, interrupts flags section ... you'll find it.

    BTW : VAR ... just means " other name for " ( something like # DEFINE ... Ooops ... )

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: Accurate Freq Measurment

    PIR3.1 is bit #1 in the PIR3 register. Capture VAR PIR3.1 just creates an alias for this interrupt flag bit.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Easy and Accurate Clocks without RTC IC
    By paul borgmeier in forum Code Examples
    Replies: 18
    Last Post: - 28th October 2013, 22:28
  2. reducing shiftout clock freq and PS2/PC interfacing ....
    By wireless magic in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th February 2008, 21:15
  3. accurate vref < 1vdc 16F876 comparator
    By mslaney in forum Schematics
    Replies: 1
    Last Post: - 23rd March 2005, 02:44
  4. HPWM on a 628 is not changing freq.
    By dtit in forum General
    Replies: 2
    Last Post: - 25th February 2005, 10:34
  5. More accurate resolution from the A/d converters
    By pjsmith in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th August 2004, 23:49

Members who have read this thread : 0

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