Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: How to Count Rising and Falling Edges?

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    3

    How to Count Rising and Falling Edges?

    I'm using a Teensy 3.5 to count pulses using the CountFreq (https://www.pjrc.com/teensy/td_libs_FreqCount.html) library. This is working well.

    I now would like to count both rising and falling edges. This will double the resolution on the parameter being measured. Is this possible? Any advice would be very much welcome.

    Regards, Ben

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,063
    Have a look at the FreqMeasureMulti library. The core functionality is capturing rising and falling edges.

  3. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    3
    Thank you. The FreqMeasureMulti library looks like it is exactly what I was looking for. I'll give it a try.

  4. #4
    Junior Member
    Join Date
    Nov 2018
    Posts
    3
    I've had a look at the FreqMeasureMulti library in more detail and it's not quite what I need. I need to count the total number of rising and falling edges over time. The period between each edge is also useful but not essential. What exactly does the read() function return? I was expecting a buffer of all period counts between calls. Instead it returns just one number.

  5. #5
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,063
    The read function returns the raw bus clock pulse counts between two events either rise-rise or rise-fall or fall-rise or fall-fall, depending on the selected channel configuration.

    When I pointed you towards that library, I didn't mean that you would use it directly for your pulse counting, but rather as a tutorial on how to configure the FTM channels to capture either rise or fall or both events, so that you could execute your event counting within your own interrupt handler, even for 8 channels in parallel if needed.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,671
    FreqMeasureMulti is best for relative low frequencies, because it needs an interrupt for each edge, and because it measures elapsed time rather than counting the total number of edges. Best to keep this speed limitation in mind, since we have no sense of what sort of waveforms you're measuring.

    To answer your question, read() returns the elapsed time from the prior rising edge.

    So, assuming your code is able to keep up, you could implement counting by just ignoring the measurement and count the total number of reads. Or you could edit the FreqMeasure code to do this.

    Or you could do something as simple as attachInterrupt() and write a function which just increments a volatile variable. attachInterrupt supports a CHANGE option which interrupts on both edges. So again, if using fairly low frequency (kHz range) maybe this could be a viable option?

Tags for this Thread

Posting Permissions

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