View previous topic :: View next topic |
Author |
Message |
nuno12345
Joined: 16 Jul 2008 Posts: 50
|
frequency measure update rate |
Posted: Tue Sep 25, 2012 2:27 pm |
|
|
Hello, I've build a rpm/freq meter, but since frequency is measured over a second my update rate is very low, stucking the program until 1s has passed.
I'm using the following code:
Code: |
set_timer1(0);
delay_ms(1000);
rpm=get_timer1();
|
I tried changing the delay to 500 and currently I'm with 250ms but as I decrease the delay the frequency accuracy is affected and I have to multiply the freq by 4, is there any way to fix this?
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Sep 25, 2012 2:38 pm |
|
|
The 'fast' way is to measure the reciprocal. Use the fastest clock you have available, and measure the time between two of the edges from the RPM sensor.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Sep 25, 2012 2:58 pm |
|
|
What is the range of frequencies you are trying to measure?
How sharp are the edges from your RPM sensor?
A forum search will produce several discussions on this topic.
Mike
Last edited by Mike Walne on Sat Sep 29, 2012 2:08 am; edited 1 time in total |
|
|
nuno12345
Joined: 16 Jul 2008 Posts: 50
|
|
Posted: Tue Sep 25, 2012 7:47 pm |
|
|
Thank you for your answer.
My frequencies are from 20hz to 300hz.
I think the edges are pretty sharp, its from a distributor hall sensor (12v). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Sep 26, 2012 12:02 am |
|
|
Assuming your CPU clocks something like 4MHz or better, then the reciprocal will be at least as accurate as counting the pulses (typically much better), and 'worst case' will only take 1/20th second to measure. More than an order of magnitude better.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Sep 26, 2012 1:54 am |
|
|
With your original 1 second gate time, +/-1 count at 20Hz is 5%.
Assume a 4Mhz CPU clock that's 1MHz instuction cycle and gating for one period of your input waveform.
With the reciprocal method your gating error is:-
1 part in ~3000 for 300Hz, that's ~0.3%.
1 part in 50,000 for 20Hz.
Worst case is now more than an order of magnitude better than before.
You can improve by timing over several periods, and still have a reasonable update rate.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Sep 26, 2012 7:45 am |
|
|
and (of course), as one further comment, if you can move the connection from the RPM sensor, to the CCP input, then the actual timing can all be done in hardware, and you can be calculating/displaying one reading, while the next is being taken!.
Best Wishes |
|
|
nuno12345
Joined: 16 Jul 2008 Posts: 50
|
|
Posted: Wed Sep 26, 2012 12:35 pm |
|
|
First of all thank you all for your help.
So, as I understand what you guys say is to do a loop, cycling for the most possible frequency thatIi want to read?
I saw some code here in the forums and it used something like that but it uses 2 loops using delay_cycle and despite he explained the math I can't understand it since we have to take in care the PIC clock and the time that instructions take to operate.
I'm not english and I cant quite understand what you guys say with "reciprocal method", applied to PIC coding..(just a loop counting high inputs?)
I'm using 12F675, which I think it doesn't have ccp, but I'll move to 16f88 since I need more ports.
Another question, since my hall sensor is using 12v I'll need to add a voltage divisor, will this affect the frequency?
Again, thanks |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Sep 26, 2012 4:56 pm |
|
|
Quote: | Another question, since my hall sensor is using 12v I'll need to add a voltage divisor, will this affect the frequency? | Dividing the signal from 12V to 5V will not alter the frequency.
Quote: | I'm not english and I cant quite understand what you guys say with "reciprocal method", applied to PIC coding..(just a loop counting high inputs?) |
There are two simple ways to measure frequency:-
1) The method you used initially (the obvious one). You count the number of either positive (or negative) edges of your input signal in a period of 1 second. Using your PIC clock to generate the one second period. A variation on this is to count for a greater (or lesser) period and adjust the count to convert to actual frequency.
2) The reciprocal method. Here you measure the period between adjacent rising (or falling) edges, by counting PIC instruction clock cycles. With a 4MHz CPU clock that's 1us per instruction cycle. Suppose the period is 5,000us, the frequency is the reciprocal of 5000us, that's 1/5,000us = 200Hz. To get better resolution you use either (or both) a higher CPU clock frequency or time more than one input period, and adjust the final calculation.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Sep 27, 2012 12:55 am |
|
|
and again the PIC has hardware to record a time when an event occurs.
The CCP, in 'capture' mode, records the count from a timer (normally timer1), into a storage register, when either the rising edge, or falling edge is seen (you can specify which). Once setup, this is done automatically for you, without you having to sit polling the pin. The value in the register gives the time the last edge happened, more accurately, than any software polling method.
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Sep 27, 2012 4:43 am |
|
|
Lets try to explain:
'Reciprocal', is just a mathematical term, for 1/something.
Imagine you had an existing fence, filling a gap 100m long, and were told you needed to replace it. You need to buy the fence panels. Two different solutions exist:
1) You walk the entire 100m, and count the panels. This is the 'time pulses over one second' approach.
2) You just measure the length of one panel. You already know the gap is 100m (this is the 1 second in the frequency calculation), so you can say (if the panel is 1.5m long say)
Panels needed = 100/1.5 = 66.666
Now if we imagine that the processor clock is 4MHz, then the timers can run at up to 1MHz. So if we can measure the number of timer counts between two successive 'edges' of the incoming signal, we get the equivalent of the length of the panel. We already know the counts/second of the timer clock, so if we get (say) 4200 counts between edges, then the frequency is:
1000000/4200 = 238.09Hz.
We can measure this in just _one_ cycle of the incoming signal. You can also elect to do the maths using either float or integers. The former is slower, while the later will give the same integer result as you'd get from counting edges, with you sometimes getting one extra.
Now, you don't have to look at the edges, and sit in a loop to do this, because the PIC hardware has a built in 'time between edges' function in the CCP. Equivalent to perhaps you having a distance measuring 'wheel' when working on the fence. It can give you a number recorded each time it sees an edge, allowing you then to just take the last two numbers recorded, and immediately 'know' the number of clock cycles between the edges.
Quicker.
More accurate (assuming the timer used is at least 300* the maximum frequency).
and potentially able to resolve 'part cycles'.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Sep 28, 2012 10:47 am |
|
|
I can only add one comment to this excellent discussion:
The choice of measurement by gating vs multi cycle interval counting has one other consideration. If FreqX has a lot of jitter I think you will find that in spite of the longer delay between readings that counting can be a better choice than gating due to the averaging that counting provides.
It may all depend on what "RMS" accuracy you need. |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Fri Sep 28, 2012 12:05 pm |
|
|
One thing to be careful of with the "reciprocal" version - depending on the engine you might get strange results for RPM - my old Buick V-6 was basically a V-8 engine with 2 cylinders removed (no kidding) and as such, the firing angle between cylinders was NOT the same for each one (if you looked at the cam in the distributor, you could see the lobes were not equally spaced - resulted in a funny idle. Also results in the angle between any pair of cylinders being different. I don't know how many of the newer V-6 engines are the same way, but it could give some interesting results if they were (to make the angle symmetrical between cylinders would require changing the angle of the "V" in the engine).
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
nuno12345
Joined: 16 Jul 2008 Posts: 50
|
|
Posted: Fri Sep 28, 2012 12:15 pm |
|
|
You guys must be teachers.
So back to code (sorry for the bad coding :P)
Slow counting method:
Code: | set_timer1(0);
delay_ms(1000);
freq=get_timer1(); |
Reciprocal method:
Code: | if(input(PIN_B0))
{
for(edges=0;edges=1;edges++)
{
while(input(PIN_B0))
{
time++;
delay_cycles(25);
}
while(!input(PIN_B0))
{
time++;
delay_cycles(25);
}
}
}
freq=1/time; |
CCP method (by PCM programmer, thank you)
Code: | #int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
current_ccp = CCP_1;
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
}
freq = (int16)(1000000L / current_ccp_delta); |
Is my reciprocal method that wrong?
I would like to use it since it will work with many pics and its more clear to my head |
|
|
nuno12345
Joined: 16 Jul 2008 Posts: 50
|
|
Posted: Fri Sep 28, 2012 12:26 pm |
|
|
gpsmikey wrote: | One thing to be careful of with the "reciprocal" version - depending on the engine you might get strange results for RPM - my old Buick V-6 was basically a V-8 engine with 2 cylinders removed (no kidding) and as such, the firing angle between cylinders was NOT the same for each one (if you looked at the cam in the distributor, you could see the lobes were not equally spaced - resulted in a funny idle. Also results in the angle between any pair of cylinders being different. I don't know how many of the newer V-6 engines are the same way, but it could give some interesting results if they were (to make the angle symmetrical between cylinders would require changing the angle of the "V" in the engine).
mikey |
2 cylinders removed? but you could se the 2 extra holes in the cylinder block? weird.
I think we had to use two delays, counting a separate rising and falling edge, since the signal wont be simetric and increasing the number of edges to count to make a readable/simetric frequency |
|
|
|