Accurate Freq Measurment


Closed Thread
Results 1 to 23 of 23

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    See this thread for a few examples http://www.picbasic.co.uk/forum/showthread.php?p=23401

    You might want to download the .PDF Steve linked to in that thread for more info on using capture. It's very handy once you get the hang of using it.

    If you really want to have some fun, take a peek at the 18F2431 series. This one has a TON of options for measuring frequency, pulse width, PWM motor control, high-speed simultaneous 2-channel A/D sampling, and lots of other goodies.
    Regards,

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

  2. #2
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Bruce,
    Thanks for the link, I just found it a shortly before you posted your message, I also found a few more of your posts with code for using the capture mode with Timer1, once I realized I needed to search on Capture I think you are the expert on the hardware Timers! I hope I can now greatly improve my code.
    Terry

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


    Did you find this post helpful? Yes | No

    Default

    Hi Terry,

    Thanks, but I wouldn't call myself an expert on much beyond getting in trouble. At least, that's what my wife says..;o}

    However, I have spent an hour or two messing with timers & hardware. Grab yourself an 18F2431, and give this a shot. It's really simple, but very precise.

    I'm not really sure what the benefits of (sub hertz) measurements are going to give you. Oversampling A/D measurements is normally a good idea, so you can average measurements over time, but I really don't think this approach is going to give you any advantage when measuring a frequency. I mean, it's either X hZ or it's not.

    Oversampling is just going to give you an average over time / the number of samples. If your frequency is changing, the result is garbage anyhow.

    This is real simple. It sets up an 18F2431 to measure the time period between rising edges, which is the frequency.

    Code:
    DEFINE OSC 4
    DEFINE DEBUG_REG PORTC
    DEFINE DEBUG_BIT 6 
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true
    
    T1 VAR WORD
    
    Capture VAR PIR3.1
    False CON 0
    True  CON 1
    
    ' auto time base reset, frequency mode, capture on every rising edge
        Frequency  CON %01000101
      
        ANSEL0=0             ' All digital
        TRISA.2 = 1          ' this is your (CAP1) input pin measuring the frequency
        INTCON = 0           ' Interrupts off
        TMR5H = 0            ' Clear high byte of TMR5 counter
        TMR5L = 0            ' Clear low byte
        T5CON = %00000001    ' prescale=1:1, int clock, TMR5=on
        CAP1CON = Frequency  ' we're measuring a frequency
        
        DEBUG "START",13,10
    
    Main: 
        Capture = False       ' Reset capture flag
        GOSUB Freq            ' get frequency
        PAUSE 500
        GOTO Main
    
    Freq:    
        ' Frequency measurement mode, capture Timer5 count every rising edge
        ' with auto reset of Timer5 on each event
        WHILE Capture = False
        WEND
        T1.HighByte = CAP1BUFH
        T1.LowByte = CAP1BUFL
        DEBUG "Freq = 1/",DEC T1,"uS",13,10
        RETURN
    
        END
    With another PIC programmed to output a 4kHz signal into the 18F2431 CAP1 pin, this returns;

    Freq = 1/250uS
    Freq = 1/250uS
    Freq = 1/250uS

    1/250uS = 4kHz.

    The 18F2431 has a lot of nifty features that can save you a boat-load of code & time.

    It's definitely worth looking into. This captures the time between rising edges + resets the timer after each capture event. Very handy stuff...;o}
    Regards,

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

  4. #4
    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

  5. #5
    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 22:37.
    Regards,

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

  6. #6
    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

  7. #7


    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

Similar Threads

  1. Easy and Accurate Clocks without RTC IC
    By paul borgmeier in forum Code Examples
    Replies: 18
    Last Post: - 28th October 2013, 21: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, 20:15
  3. accurate vref < 1vdc 16F876 comparator
    By mslaney in forum Schematics
    Replies: 1
    Last Post: - 23rd March 2005, 01:44
  4. HPWM on a 628 is not changing freq.
    By dtit in forum General
    Replies: 2
    Last Post: - 25th February 2005, 09: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, 22: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