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

Thread: How to Count Rising and Falling Edges?

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

    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,385
    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
    12
    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
    12
    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,385
    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
    19,927
    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?

  7. #7
    Junior Member
    Join Date
    Nov 2018
    Posts
    12
    Thank you Theremingenieur and PaulStoffregen, I've made some good progress with my project since my last post. I'm using the FreqMeasureMulti library to measure the width of both high and low pulses at about 50 Hz and this is working well. I am also using the FreqMeasure library to measure the pulse width of a higher frequency square wave. I'm currently working with a frequency of about 8 kHz but I will need to measure up to 33 kHz in the future. If possible I would like to capture the width of both the high and low pulses (the duty cycle is always very close to 50%).

    I've been looking at the code for FreqMeasure to see if I can modify if to include the functionality of FREQMEASUREMULTI_ALTERNATE that is used in FreqMeasureMulti but it's not clear to me how to do this.

    What would you suggest is the best way to capture the pulse width of both high and low pulses at up to 33 kHz?

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
  •