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

    skimask your last description is exactly what I am trying to do. My code essentially does just that over a 200 cycle period but not very gracefully as written, the start and stop of Timer1 I don't thinks is well done as I am using software to do it rather than hardware. In my attempt to measure the frequency fast, the physics of the problem limits how fast the measurement can be done for a given accuracy, but when comparing to a frequency counter approach I would need a 10 second period for 0.1Hz resolution, where by measuring the period as you point out can provide great resolution fast relative to a frequency counter, I guess I should have been more clear in my original posting, because my target is ~100 msecs for the measurement time. After doing more research it looks like the approach of using the capture/compare module with Timer1 as you point out Don is the way to go. I have not used the capture mode in the PIC's in an interrupt mode, are there any good code examples.
    Terry

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Terry View Post
    I guess I should have been more clear in my original posting, because my target is ~100 msecs for the measurement time. After doing more research it looks like the approach of using the capture/compare module with Timer1 as you point out Don is the way to go. I have not used the capture mode in the PIC's in an interrupt mode, are there any good code examples.
    As they say, you can have 2 of 3 things...fast,good,cheap...
    You can have it fast, you can have it good, it won't be cheap.
    You can have it good, and you can have it cheap, but it won't be fast.
    You can have it cheap, and you can have it fast, but it won't be good

    I think you're method will work, and I wouldn't doubt that what you are using right now could work well enough. Why not 'remember' the last bunch of readings, say the last 10 readings, then average them out, discard the oldest, plug in the newest...a moving average if you will. You won't necessarily gain resolution, well, yes you will since you've increased the effective sampling time. You can keep your 100ms measurement time, and still get fairly decent resolution.

  3. #3


    Did you find this post helpful? Yes | No

    Default ccp

    Terry, I don't know specifically of capture code examples, the microchip data books lay out general routines. You probably know;
    -set ccp for capture on 16 rise edge
    -tmr1 prescale to 1
    -make int routine in asm and add int pointer name for basic
    -set pie and pir's for interrupt enables
    ---on int -- check for tmr1 overflow(not usable count)
    ---stop ccp and tmr1
    ---move count high and low to basic count word
    ---reset flags capture and tmr1 overflow
    ---tmr1h and tmr1L = 0
    ---start ccp and tmr1

    basic prog can start and stop interrupts as desired.
    took me some good head scratching for int's but is very reliable when working.
    Once working, pic will just keep spitting out counts automatically.
    don

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

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

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

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

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