Low-Frequency Counter


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Zeke's Avatar
    Zeke Guest

    Default Low-Frequency Counter

    Hopefully someone has some suggestions for me on this one.
    I need to write a program to be able to count pulses that run from around 0.25Hz to 4Hz. The PULSEIN command would likely time-out long before it reads any pulses. I think I will need to use tmr1 or similar and advance a counter at each roll-over of the timer, and then convert this value to a time between my external pulses. Then, with some simple math, I can figure out the frequency.
    My biggest problem is being able to setup the timer to run properly. I'm not sure how to do this. And, should I set-up my external pulse as an interrupt? I will likely be using a PIC16F627 as I have a couple of these handy.
    Any help would be much appreciated.
    Nick

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Something to start
    On this forum
    http://www.picbasic.co.uk/forum/showthread.php?t=1044

    Melabs Website... choose counter.zip
    http://www.melabs.com/resources/samples.htm#submitted

    Both are the same, the second include the schematic

    Sorry, based on a old and dirty F84 but should gives you some pointer.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Beurk... no forget that..i just see your frequency range...

    Maybe the use of PAUSE could be just enough.

    0.25HZ => period =4Sec
    4HZ => period = 0.25 Sec

    Something like
    Code:
    While FreqInput=1 : Wend
    While FreqInput=0 : wend
    While FreqInput = 1
        Pause 10
        HalfPeriod=HalfPeriod+1
    wend
    
    Frequency = 10000/(HalfPeriod * 2)
    In the above 4HZ will return => 400, 0.25HZ will return 25 as value....
    then just to play with the DEC, DIG to display your frequency.

    Should be enough to start.
    Last edited by mister_e; - 5th January 2006 at 01:22.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Zeke's Avatar
    Zeke Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    Thanks for the info. I'm going to start testing this in the next few days. I'll make sure I post what finally works.

    Nick

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Steve,

    Playing Devil's advocate here.

    What happens if the Duty Cycle of the pulses isn't 50% ?
    <br>
    DT

  6. #6
    Zeke's Avatar
    Zeke Guest


    Did you find this post helpful? Yes | No

    Default

    Darrel, in fact, the pulses will be coming from a sensor measuring Heart Rate, like an ECG signal, except that it will be run through a comparator so that I only pick-up the largest spike. Therefore, the signal will likely never have 50% duty cycle.
    My plan is this...
    1. Use Timer1 to count pulses (5,000 at 20MHz gives 1ms) starting at a pre-defined value, I think it works out to 60,535. In a tight loop I will check for the timer overflow bit, everytime this happens, I increment a variable as 1 ms and re-start the timer. This way I am counting milliseconds.
    2. I will use RB0 as my interrupt signal (which is my pulse signal) and perform an interrupt routine that will calculate the heart rate based on the time (milliseconds from my variable above) between pulses. This routine will then reset my millisecond variable to check for the next time between pulses.
    3. Another variable will check for 3 or 4 seconds to pass and then send the data via the built-in USART to a PC.

    This is roughly how it should work. There are other issues that I need to look at, but that's easy. The hard part is keeping it accurate since at a heart rate of about 240 beats per minute (BPM), that's about 4 Hz, or 250ms between pulses. Therefore, in this range, a difference of 1 ms, is approximately 1 BPM. At lower heart rates, the resolution increases.
    What do think??

    BTW: I really appreciate the input so far.

    Thanks,
    Nick

Similar Threads

  1. Sony LanC Program
    By l_gaminde in forum Code Examples
    Replies: 2
    Last Post: - 25th September 2009, 18:51
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  4. PIC16F84(A) counter
    By nmmr in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th May 2005, 04:27
  5. 4-line LCD Help - using PortA instead of B
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 31st March 2005, 03:14

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