CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How Do i Increase Resolution with CCP Capture?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
santana



Joined: 16 Oct 2003
Posts: 3

View user's profile Send private message

How Do i Increase Resolution with CCP Capture?
PostPosted: Fri Dec 12, 2003 3:33 pm     Reply with quote

Hello Guys
I need some help with me project as it has being driving me mad all
week.
I am using Pic16F877 capture mode to measure a frequency with varies from 1Khz to 5Khz.
This works fine up to +/- 5Hz accuracy using a 20mhz Xtal.
For example my pic display say 2410 Hz and if the frequency increase to
say 2413Hz the pic displays 2415Hz the same thing happens if the frequency decreaces. ( it displays 2405Hz )
i need to get at least +/- 1Hz accuracy
i am thinking of upgrading to 18F452 at 40Mhz but this would only give me (if i am right -/+ 2.5Hz resolution)

Is there a way i can do this ?

Santana
Ttelmah
Guest







Re: How Do i Increase Resolution with CCP Capture?
PostPosted: Fri Dec 12, 2003 3:46 pm     Reply with quote

santana wrote:
Hello Guys
I need some help with me project as it has being driving me mad all
week.
I am using Pic16F877 capture mode to measure a frequency with varies from 1Khz to 5Khz.
This works fine up to +/- 5Hz accuracy using a 20mhz Xtal.
For example my pic display say 2410 Hz and if the frequency increase to
say 2413Hz the pic displays 2415Hz the same thing happens if the frequency decreaces. ( it displays 2405Hz )
i need to get at least +/- 1Hz accuracy
i am thinking of upgrading to 18F452 at 40Mhz but this would only give me (if i am right -/+ 2.5Hz resolution)

Is there a way i can do this ?

Santana

What interval are you counting over?.
If you are counting the clock itself, and use one second, the resolution will be 1Hz. If you are only using 0.2 of a second, and multiplying the value by five, you will get 5Hz resolution.
The alternative, is to count the master clock counts between two successive falling (or rising) edges. The frequency will be 5000000/delta count. The resolution in this case, will be higher at the lower frequencies. If you use an external clock, you can feed this to the timer, rather than using the internal 'divided' version, which increases the resolution by a factor of 4.

Best Wishes
santana



Joined: 16 Oct 2003
Posts: 3

View user's profile Send private message

Re: How Do i Increase Resolution with CCP Capture?
PostPosted: Fri Dec 12, 2003 4:02 pm     Reply with quote

i am using 0.2 of a second, and multiplying the value by five, to get 5Hz resolution.
i thought of doing it the way you discribed , by applying a 1Hz pulse from
my RTC (DS1307) to RB0 and then use RC0 for the measurement but this
don't work with my application as the frequency i an measure would have disappeared by the time 1 second is up.
i have to make the reading within 0.2 of a second.
this is because i am measuring the frequency of a vibrating wire which is sweept and it stops vibrating after 0.3 of a second

Santana
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 12, 2003 5:12 pm     Reply with quote

You could use a CCP pre-scaler of 16, and a Timer1 pre-scaler of 1.
This gives you a timer clock of 5 MHz.

Let's do an example for 5000 Hz and 5001 Hz input frequencies to
the CCP pin.

5000 Hz / 16 = 312.5 Hz
5 MHz / 312.5 Hz = 16000 clocks counted by the CCP between edges.

Now do it for 5001 Hz input:

5001 Hz / 16 = 312.5625 Hz
5 MHz / 312.5625 Hz = 15996.8 clocks (actual CCP count = 15996)

OK, now let's do the frequency calculations, based on those counts.
This is the math that you have to do in your C source code.
For the 5000 Hz example:

(5,000,000 x 16) / 16000 clocks = 5000 Hz as the calculated frequency.

For 5001 Hz:

(5,000,000 x 16) / 15996 clocks = 5001 Hz

So this works OK. But what about the 1000 Hz example ?
In that case, the number of counts will be 5 times higher, or about
80,000 counts. That exceeds the size of the Timer1, which is only
16 bits. So you have 3 choices. You can reduce the CCP pre-scaler
to 4 (that's the next available choice). Or, you could change the
Timer1 pre-scaler to 2. Or, you could extend the Timer1 resolution
to be 24 bits. I have some sample code on how to do that, here:
http://www.ccsinfo.com/forum/viewtopic.php?t=906

The problem with changing pre-scalers, is that you have to detect
when to do it, and design code for that. After looking at the problem,
I decided the most simple way was to keep the Timer1 prescaler of 1,
and just extend it to 24 bits.
santana



Joined: 16 Oct 2003
Posts: 3

View user's profile Send private message

PostPosted: Sun Dec 14, 2003 1:14 pm     Reply with quote

Thanks for that example i would be experimenting
with this tomorrow and does this mean that i wouldn't need to upgrade to
18F452 ?

Santana
Ttelmah
Guest







PostPosted: Mon Dec 15, 2003 3:23 am     Reply with quote

santana wrote:
Thanks for that example i would be experimenting
with this tomorrow and does this mean that i wouldn't need to upgrade to
18F452 ?

Santana

Changing to the 452, wouldn't improve the resolution. The problem with the way you are working, is that since you only count over 1/5th of a second, a change of 5Hz, is needed, before there is a change of '1' in the count.
The alternative (which is what I mentioned at the end of my post, and then PCM programmer has given more detailed information about), is to measure the 'period' of the signal, rather than the frequency (frequency=1/period). You do this, by counting the number of edges on a much faster clock, that occur between the edges on the incoming signal. The more edges you can count, the more accurate the resolution. Since the CCP module can be set to count the incoming clock/4, this gives 5MHz, and the analysis allready posted.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Dec 16, 2003 4:54 pm     Reply with quote

If you are measuring frequency by counting pulses, by counting an INTEGER number of pulses, you can not measure a difference less than one pulse. At 0.2 sec you can not measure a change of less than 5Hz because 5Hz is one pulse.

The only solution is to measure the PERIOD of your signal and calculate frequency. At the start of the measurement you look for the first rising edge. Then using a very fast clock you measure the time till the next rising edge. If you know how many cycles of tone there will be you may be able to count several consecutive cycles for more accuracy.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group