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

Low frequency measurement
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
OSUschap



Joined: 26 Sep 2005
Posts: 4

View user's profile Send private message AIM Address

Low frequency measurement
PostPosted: Mon Sep 26, 2005 11:50 pm     Reply with quote

Hello,
I am attempting to measure frequency with 16F876 or 877 using a 20 MHz crystal. The frequencies range from very low say 100Hz to a max around 3.5kHz. Since the frequencies are so low, a sampling time method would cause too much delay. My current plan is to use external edge triggered interupts and the standard timers (16 bit). I plan to use the timers to keep track of the constantly changing period of the incoming signal. Every time the trigger occurs, the frequency value would be updated and then the timer could be reset or left running I suppose. However, I do not plan on using any pre-scalers, and so my question is this. At the higher frequencies, will the interrupts be coming in too fast for the PIC to be able to perform other functions that may be needed, such as a pulse modulated output. Is this a really bad way of doing this, or does it sound solid at all? As I'm sure it shows, I do not have a lot of PIC experience so any replys will have to be at somewhat of a beginner level. Any suggestions or at least confirmation that this would work would be appreciated. Thanks
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Sep 27, 2005 12:17 am     Reply with quote

How much delay is too much? Say, your frequency of interest changes from 200Hz to 100Hz. How fast do you want to rect?
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: Low frequency measurement
PostPosted: Tue Sep 27, 2005 6:04 am     Reply with quote

OSUschap wrote:
I am attempting to measure frequency....
I plan to use the timers to keep track of the constantly changing period of the incoming signal.


Not intending to be picky but this is not measuring frequency. Frequency is measured by counting the number of cycles (such as low to high transitions) that occur over a given period.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

Re: Low frequency measurement
PostPosted: Tue Sep 27, 2005 6:52 am     Reply with quote

asmallri wrote:
OSUschap wrote:
I am attempting to measure frequency....
I plan to use the timers to keep track of the constantly changing period of the incoming signal.


Not intending to be picky but this is not measuring frequency. Frequency is measured by counting the number of cycles (such as low to high transitions) that occur over a given period.


Measuring the period and taking it's reciprocal is most assuredly a valid way to measure frequency (by definition), as is counting cycles during a given period of time, as is taking the fft and looking for the peak, as is using a frequency-to-voltage converter and reading the voltage, and a slew of other methods that don't spring immediately to mind. Each method will have advantages and disadvantages, and be susceptible to different types of errors.

Scott
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: Low frequency measurement
PostPosted: Tue Sep 27, 2005 7:54 am     Reply with quote

Quote:

Measuring the period and taking it's reciprocal is most assuredly a valid way to measure frequency (by definition). Scott


No. Neither is this the definition of frequency. Frequency is defined as the number of cycles per second. However the period method is a means of determining the frequency of a constant waveform. Consider for example a waveform with a mean period of 1ms but with a jitter of 100us. This wavform has a freqency of 1KHz. The result of measuring the cycle by cycle period would give you a varying frequency but with the same average frequency.

I did say I did not mean to be picky...
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
cbrock
Guest







PostPosted: Tue Sep 27, 2005 9:45 am     Reply with quote

Check out this months Circuit Cellar. There is an article using the CCS compiler and a simple DSP algorithm.
OSUschap



Joined: 26 Sep 2005
Posts: 4

View user's profile Send private message AIM Address

PostPosted: Tue Sep 27, 2005 11:02 am     Reply with quote

kender wrote:
How much delay is too much? Say, your frequency of interest changes from 200Hz to 100Hz. How fast do you want to rect?


Some delay is ok, but it needs to react in ms if not us. I cannot sample for so many seconds and count pulses there. However, if my output will simply continue to output the old data until the new calculations are complete, that would be desired.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Sep 27, 2005 12:01 pm     Reply with quote

OSUschap wrote:
Quote:

The frequencies range from very low say 100Hz to a max around 3.5kHz. Since the frequencies are so low, a sampling time method would cause too much delay.


You didnīt tell us the time the measuring signal takes to shift from 100Hz to 3500Hz.

Quote:

Some delay is ok, but it needs to react in ms if not us.


For an incoming (assuming constant freq as asmallri well stated) signal of 100Hz
you need at least 10ms to get a measurable event.
If the signal is changing in frequency, you need another 10ms just to know that is changing.
The key is not to measure high frequency, (you can measure 50Mhz with a 20Mhz crystal)
the key is if the input frequency is stable enough at sampled times and the extra time
needed to do some task.
You would define this tradeoff before start coding.

Humberto
OSUschap



Joined: 26 Sep 2005
Posts: 4

View user's profile Send private message AIM Address

a look at what I'm trying
PostPosted: Tue Sep 27, 2005 1:46 pm     Reply with quote

The signal is one with constantly but slowly changing frequency. I understand that when waiting for an edge, I will have to wait 10 ms to get a frquency measurement for the worst case scenario (100Hz). However, this is probably well below what I will bet getting anyhow and if the frequency changed to such a value, the 10 ms would be an acceptable delay on the output as long as it didn't just go to 0 or something for that 10 ms. Here is what I started with, maybe it will give an idea of what I'm trying to do, although now that I've typed it, the code doesn't look quite right.
[/code]
//global variables:
long freq = 0.0;

#int_CCP2
CCP2_isr()
{
int period = CCP_2;
freq = 1/period;
set_timer1(0)
}

void main()
{
// Setup:
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // 200ns resolution
setup_ccp2(CCP_CAPTURE_RE); // interrupt on rising edge

enable_interrupts(INT_CCP2); // interrupt on CCP2 rising edge
enable_interrupts(global);
enable_interrupts(global);

and then of course main will be doing other things
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Sep 27, 2005 8:21 pm     Reply with quote

Initially I thought that you could sample the signal at a high rate for the amount of time less then one perion, do an FFT, and then look for the peak. If the signal is slow, there is plenty of time to do the FFT. Bu then I came to realize that the bin size will be big, and it can make the method not work.[/code]
OSUschap



Joined: 26 Sep 2005
Posts: 4

View user's profile Send private message AIM Address

code didn't work
PostPosted: Wed Sep 28, 2005 11:15 am     Reply with quote

Well, I wrote some code and you guys are probably not surprised that it didn't work. Can anybody tell me if I just made a rookie mistake or if this method is simply invalid? Thanks a lot for the tips guys, but I still have no idea if i'm approaching this in a reasonable way.
Code:

//global variables:
long freq = 0.0;
long period = 1;

#int_CCP2
CCP2_isr(){

   period = (CCP_2*0.0000002);
   freq = (1/period);
   set_timer1(0);
}


void main()
{
   // Setup:
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);    // 200ns resolution
   setup_ccp2(CCP_CAPTURE_RE);                // interrupt on rising edge

   enable_interrupts(INT_CCP2);               // interrupt on CCP2 rising edge pin_c1
   enable_interrupts(global);                 
 
   while (true){
      if (freq <400)
         output_high(pin_b0);
      else if (400<freq<1500)
         output_high(pin_b1);
      else if (1500<freq<3000)
         output_high(pin_b2);
   }
}
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Wed Sep 28, 2005 11:30 am     Reply with quote

Have you considered using a Frequency to Voltage converter like the AD650?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 28, 2005 1:44 pm     Reply with quote

Code:
   period = (CCP_2*0.0000002);

Dividing CCP_2 by 5 million? CCP_2 is a 16 bit number that can't exceed 65535.

It would have to be a float and you wouldn't want to do that. Just figure out what the CCP values would be and use those in your if statements. Also, you probably want to turn off the other LEDs when you turn on one. Otherwise, they might all be lite at once.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 28, 2005 1:54 pm     Reply with quote

Quote:
#int_CCP2
CCP2_isr(){

period = (CCP_2*0.0000002);
freq = (1/period);
set_timer1(0);
}

http://www.ccsinfo.com/forum/viewtopic.php?t=17806
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 28, 2005 2:13 pm     Reply with quote

PCM programmer wrote:
Quote:
#int_CCP2
CCP2_isr(){

period = (CCP_2*0.0000002);
freq = (1/period);
set_timer1(0);
}

http://www.ccsinfo.com/forum/viewtopic.php?t=17806


Actually, 65535 at 20ns resolution would allow a 76Hz signal to be read so he doesn't really need to extend the resolution unless he wants to measure less than say 80Hz. Also, there is a mode for the CCP that will automatically reset timer1 to prevent latency problems.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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