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 support@ccsinfo.com

FREQUENCY COUNTER PROBLEM

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



Joined: 18 Dec 2009
Posts: 27

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

FREQUENCY COUNTER PROBLEM
PostPosted: Sat Jun 21, 2014 9:09 am     Reply with quote

Hello All
i want to make a frequency counter. The frequency range is from 0Hz to 1000Hz... I am using 11.0592MHz crystal... Also i want to generate a 100Hz pulse using timer 2...

my code is as under

#include <main.h>

int count = 0;
int16 isr_ccp_delta;
int16 current_ccp_delta;
int16 frequency;

#int_ccp2
void ccp2_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;

// Read the 16-bit hardware CCP2 register
current_ccp = CCP_2;

// Calculate the time interval between the
// previous rising edge of the input waveform
// and the current rising edge. Put the result
// in a global variable, which can be read by
// code in main().
isr_ccp_delta = current_ccp - old_ccp;

// Save the current ccp value for the next pass.
old_ccp = current_ccp;
}

#INT_TIMER2
int_tmr2()
{
count = 1;
}

void main()
{
setup_adc_ports(ALL_ANALOG);
setup_timer_2(T2_DIV_BY_16,173,10);
set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
setup_ccp2(CCP_CAPTURE_RE);
enable_interrupts(INT_TIMER2);
clear_interrupt(INT_CCP2);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);

while(1)
{
if(count == 1)
{
output_high(PIN_C3);
disable_interrupts(GLOBAL);
current_ccp_delta = isr_ccp_delta;
enable_interrupts(GLOBAL);

// To calculate the frequency of the input signal,
// we take the number of clocks that occurred
// between two consecutive edges of the input signal,
// and divide that value into the number of Timer1
// clocks per second. Since we're using a 11.0592 MHz
// clock, the Timer1 clock is 2764800 MHz (Timer1 runs
// at the instruction cycle rate, which is 1/4 of the
// internal clock frequency). For example, suppose the
// the input waveform has a frequency of 244 Hz.
// 244 Hz has a period of about 4098 usec.
// Timer1 is clocked at 2764800 MHz, so between two
// consecutive rising edges of the input signal,
// it will count up by 4098 clocks. To find the
// frequency, we divide 4098 into the number of
// clocks that occur in 1 second, which is 2764800.



frequency = (2764800L / current_ccp_delta);
printf("%4X\n\r",frequency);
count = 0;
}
if(count == 0)
output_low(PIN_C3);
}

}


1. I am unable to read the correct frequency... Please help
the frequency range i want to read is from 0Hz to 1000Hz
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 21, 2014 10:42 am     Reply with quote

OK....

WHAT is the signal source you're trying to read ?

What is the frequency you are trying to read AND what is the displayed value the PIC sends to the PC terminal program?

Also WHICH PIC are you using...

What version of the compiler ...

We need to know this before we can help further...


Jay
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Jun 21, 2014 12:06 pm     Reply with quote

You also need to explain how you determine the "edges" of the waveform (and what that waveform is). If you do not have good edge detection and some form of filtering, you will probably get numbers all over the place since the edge detection is not accurate.

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
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Jun 21, 2014 3:39 pm     Reply with quote

I did code for a frequency meter section in this link.
http://www.ccsinfo.com/forum/viewtopic.php?t=47375
It SHOULD provide you with all you need for that section.

The 100Hz square wave is a separate issue. What is the accuracy required for the square wave?

Mike

EDIT Scrub your other post on this same topic, it wastes everyone's time.
And learn to use the code button.
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