Frequency detection

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Frequency detection

492 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by akshimmu on Tue Jun 16 22:05:33 MST 2015
I have to read 5 different frequencies(square wave) by polling 5 different pins.
Im using a single timer interrupt only,for every 1 millisecond.
Polling of the pins would be done in the ISR.

The algorithm i have thought of so far is:
1.Count number of HIGH
2.Count number of LOW
3.Check if sum of HIGH+LOW=Time period.
This algorithm seems slow and is not practical.

Is there any Filter functions that i could use to check the frequency at pin so that all i have to do would be to call that function?
Any other algorithms also for frequency detection would be good.

I am restricted to only 1 interrupt in my code(timer interrupt)

Labels (1)
0 Kudos
6 Replies

404 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mathseng on Thu Jun 18 17:41:36 MST 2015
Hi
I think the most effective answer can be found by looking in the NXP Application note AN11242, which gives details of two of the approaches I would think of using - count number of transitions in a gate time, and count number of cpu cycles in a transition. It also shows the errors to be expected with both of these approaches, and sample code is provided.

Querying this site with 'counter' shows a number of results which may be helpful.

Presuming that duty cycle is not required, then I would probably use one of the following:
With only one interrupt permitted, then I would probably go for the count of transitions in a gate time. Make the gate time 1S (1000 timer interrupts). With a 100/120MHz cpu, there should be enough cycles to do a simple if/then test for changes on each of the 5 pins, and update counters on change. Maybe even enough to have 2 counters, offset by 0.5S giving an update rate of 0.5S with a good resolution. This should also allow enough time to send data down a serial line without impacting on accuracy at these rates. Advantage is the don't care of edge timing, only looking at levels.

Another option could be to connect the 5 inputs to the 4 timer capture inputs. Unfortunately, this won't allow all 5 inputs concurrently - only 4.  Read the length of a pulse using the capture feature, then change the timer with 2 inputs to respond to the second pin. This approach allows higher frequencies to be measured - the only error being the jitter of the input edges to the cpu clock. The 1mS timer is still needed to allow timeout when the inputs have a too-low frequency.

Another option, but with a possibly greater error (due to interrupts and other code getting in way) is to have a loop reading the edges. On change, read the System Tick Timer, beware of the reload value. For long cycle periods, there will be many reloads (1 per timer tick). Lots of housekeeping required. I treat the timer value as an offset from the count of 1mS ticks - the error being the varying time to read after the desired edge.
0 Kudos

404 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanB on Wed Jun 17 23:39:54 MST 2015
Don't forget the words of Harry Nyquist. If you want to measure frequencies up to 20kHz, you have to sample at >40kHz, otherwise you'll get aliasing and it will give you the wrong answer.

Capture is an excellent way to measure period. Taking the reciprocal will give you the frequency, but it accuracy depends on how steady the input frequency is.

If you want to know frequency and not period (and don't have the time to do the maths) then set the counter to clock on an external input, and read the count at regular intervals.

If you want 1Hz accuracy, you'll have to count for a whole second.
0 Kudos

404 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MikeSimmonds on Wed Jun 17 11:15:44 MST 2015
However, that is exactly what the timer capture peripheral is designed to do. [Measure frequency.]

Cheers, Mike.
0 Kudos

404 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by 1234567890 on Wed Jun 17 09:08:30 MST 2015
20 kHz isn't much, so the method above should work.
0 Kudos

404 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by akshimmu on Wed Jun 17 04:24:45 MST 2015
Frequency range: anything below 20khz
0 Kudos

404 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by 1234567890 on Wed Jun 17 03:52:11 MST 2015
1 ms isn't much time for 5 pins and calculations. So what is the range of the frequencies and how fast do they change?

If the frequencies are not too high and change slowly you can simply measure the time between 2 identical edges at each related pin on time.
0 Kudos