View previous topic :: View next topic |
Author |
Message |
deepakomanna
Joined: 06 Mar 2007 Posts: 92 Location: Pune,India
|
how to measure frequency... |
Posted: Tue Feb 12, 2008 5:52 am |
|
|
Dear sir,
Any body can tell me how to measure frequency by using ccp1 interrupt.In this timer1 having external clock of 32.768 KHz & for internal processing clock 8 Mhz.
I searched in this forum but everybody is using internal clock for ccp1.
I want to measure freq. from 10 Hz to 600 Hz.
Plz help me... _________________ Thank You,
With Best Regards,
Deepak. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Tue Feb 12, 2008 2:15 pm |
|
|
Use input capture mode. You get get a sample of Timer 1 on each rising edge of the frequency to be measured on the CCP1 pin. Subtract two consecutive Timer 1 readings and you will have the period of one cycle. By dividing that period into one second you will have the frequency.
Depending on the accuracy that you need, you might need to average the period over several cycles of the frequency to be measured.
Robert Scott
Real-Time Specialties |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
fequency |
Posted: Tue Feb 12, 2008 9:53 pm |
|
|
use ccp interrupt as RLScott says
set timer1 to (0) after ccp interrupt.
ccp will catch the time passed from the previous interrupt
8 Mhz clock
if timer1 prescaller 1:8 you will have timer1 inrement 4us
to calculate Hz:
Hz=captures/second
1,000,000/time in us=Hz
if the ccp content=25,000 you have 10 Hz
1,000,000/(25,000*4)
if the ccp content=416 you have 601 Hz
1,000,000/(416*4)
joseph |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 13, 2008 12:44 am |
|
|
His question is really, can he use the Timer1 oscillator with an external
crystal in the CCP capture mode ? I tried experimenting with this a
few days or weeks ago, while reading one of his previous threads, and
I couldn't make it work. It would not capture. Switching to an internal
clock for Timer1 caused it to capture.
The 16F917 data sheet says this:
Quote: | Timer1 must be running in Timer mode or Synchronized
Counter mode for the CCP module to use the capture
feature. In Asynchronous Counter mode, the capture
operation may not work. |
Well, an external 32.768 KHz crystal oscillator (driving Timer1)
is certainly an asynchronous source. It's not synchronous to
the internal 8 MHz oscillator in the PIC. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Wed Feb 13, 2008 7:33 am |
|
|
Then here is what he can do. Forget about input capture mode. Use the frequency to be measured to generate an interrupt, either on RB0 or on some other interrupt-on-change input. Then in the ISR, read Timer 1. This will be just as good as input capture since 32kHz gives 30 microseconds between ticks. Therefore the interrupt latency is less than one tick.
Robert Scott
Real-Time Specialties |
|
|
deepakomanna
Joined: 06 Mar 2007 Posts: 92 Location: Pune,India
|
how to measure frequency |
Posted: Thu Feb 14, 2008 8:10 am |
|
|
RLScott wrote: | Then here is what he can do. Forget about input capture mode. Use the frequency to be measured to generate an interrupt, either on RB0 or on some other interrupt-on-change input. Then in the ISR, read Timer 1. This will be just as good as input capture since 32kHz gives 30 microseconds between ticks. Therefore the interrupt latency is less than one tick.
Robert Scott
Real-Time Specialties |
In my application all seg0 to seg14 are used for LCD segment only RC5 & RE3 are remainng & Only for RB0 to RB7 are used for IOC,then how could i calculate frquency by using external source for timer1 at RA7/RA6. _________________ Thank You,
With Best Regards,
Deepak. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: how to measure frequency |
Posted: Thu Feb 14, 2008 9:19 am |
|
|
Since you have not told us which PIC you are using, I have no idea what functions are on RC5, RE3, RA7 and RA6. But if one of these is an input capture pin, perhaps the input capture interrupt will work even if the loading of the PR register does not. In that case you can use the interrupt to read Timer 1 yourself. If this interrupt does not work when Timer 1 is asynchronous, then you are out of luck.
Robert Scott
Real-Time Specialties |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Fri Feb 15, 2008 5:43 pm |
|
|
Options are limited. I too experimented with the capture (on a different pic) and got some odd results.
Could just use timer 1 as a counter for the input signal on the T1CKI pin (RC5 if it's the 16F917), and count for one second.
The count is the frequency in Hz if the counter was cleared first.
Use
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
Another timer, timer 0 or timer 2, could be used for timing the one second. Timer 2 is convenient for this purpose.
eg with
setup_timer_2(T2_DIV_BY_16,249,10);
the timer 2 interrupt will occur every 20mS. In the timer 2 interrupt service routine count the interrupts,
and when 50 have occurred get the count from timer 1. Set a flag for main() to display the count.
Accuracy would be 1Hz at the 10 Hz end of the input range, and within the accuracy limits of the internal 8MHz RC oscillator higher up the range. |
|
|
|