How to Count Rising and Falling Edges?

Status
Not open for further replies.
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.
 
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.
 
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?
 
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?
 
Status
Not open for further replies.
Back
Top