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

measure the period of a signal

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



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

measure the period of a signal
PostPosted: Thu Nov 11, 2004 10:58 am     Reply with quote

hi,

I am working with a PIC16c745 and a oscilator of 24 MHz.

I need to measure the period of a square wave every 0.5 seconds

i captured it from rc2 pic


i know that i have to use ccp1 interrupt

but my code doesn't work...


can someone help me writing the correct code that i must use?

moreover explain me what i have to do...

i tried to use this code
http://www.ccsinfo.com/forum/viewtopic.php?t=20829

but i don't succed to make it work


thank you in advance
Mark



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

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

PostPosted: Thu Nov 11, 2004 12:42 pm     Reply with quote

Post your code.
Guest








PostPosted: Thu Nov 11, 2004 8:18 pm     Reply with quote

Depending on the speed and resolution you need your code could be really simple:

Set up a continuously running counter
Wait for 1/2 sec to elapse
Wait for starting edge of signal
Read the counter
Wait for ending edge of signal
Read counter again
Do math
Report result
Wait for next 1/2 sec.

This is just straight line code with no interrupts or subtleties. If you need to get more complex than this tell us more about your application so we can help you.
Juan Pablo
Guest







measure the period of a signal
PostPosted: Fri Nov 12, 2004 2:59 am     Reply with quote

I finally used the next code in a PIC16F877 and it seems to work:

...
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
...
#int_ccp1 void velocidad() {
if(!second){
rise1 = CCP_1;
segundo=1;
}
else{
rise2 = CCP_1;
pulse_width = rise2 - rise1;
second=0;
show=1;
}
}
...

Using the RTCC I enable the capture every 500 ms and check the flag "show" to disable the capture in main when the period is obtained.
Anyway I began trying example code "ex_ccpmp.c", which uses CCP1 and CCP2.

Best Wishes.
zio_pecos



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

PostPosted: Fri Nov 12, 2004 9:47 am     Reply with quote

here is my code:

i have 3 questions:
1)
i have to transmit the value of freq or the period through usb
2)
how can i improve the resolution of the measurement
3)
what changes if i set
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4) instead of
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);???

thank you

//--------------------------------------------------------------------------
// CCP1 Data
//
int16 CCP1Value;
int16 CCP1OldValue;
BOOLEAN CCP1Captured;

//--------------------------------------------------------------------------
#int_CCP1
CCP1_isr()
{
CCP1Value = CCP_1 - CCP1OldValue;
CCP1OldValue = CCP_1;
CCP1Captured = TRUE;
}
//------------


setup_counters(RTCC_INTERNAL,RTCC_DIV_256);
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);

enable_interrupts(INT_CCP1);
enable_interrupts(global);

CCP1Value = 0;
CCP1OldValue = 0;
CCP1Captured = TRUE;


if (CCP1Captured) {

//---
// F = 1/T
// Timer1 prescaler DIV_BY_8
// Pic16C745 at 24Mz -> 1.66666e-7* 8=1.33333e-6
//

periodo=((float)CCP1Value*1.33333e-6)*550000;

//per=(long)periodo;
per=periodo;



CCP1Captured = FALSE;
}
zio_pecos



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

PostPosted: Fri Nov 12, 2004 9:53 am     Reply with quote

and if i set

setup_counters(RTCC_INTERNAL,RTCC_DIV_128);??

or

setup_counters(RTCC_INTERNAL,RTCC_DIV_64);?


etc???


what do i change in my project doing this??

thank you
zio_pecos



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

PostPosted: Fri Nov 12, 2004 11:40 am     Reply with quote

i can change the frequency of the signal connected to the ccp1 pin and i use this to control the resolution of my measurement because i also use an oscilloscope...

i only want to improve the resolution of the frequency measurement:

if the signal changes from 11k Hz to 10800Hz (this is controlled by me) i don't reach to read this changement. if the frequency changes over 250Hz i read a correct measure


why?


how can i do?

thanks
zio_pecos



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

PostPosted: Sat Nov 13, 2004 11:08 am     Reply with quote

anybody can help me???
Mark



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

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

PostPosted: Sat Nov 13, 2004 11:38 am     Reply with quote

With a 24MHz clock and a Timer1 divide by 8 the CCP count increments every 1.33333us

1/((24000000/4)/8) = 1.3333333333e-6.

So, say the CCP value is 68. 68*1.33us = 90.66us or 11.028KHz
So, say the CCP value is 69. 69*1.33us = 91.99us or 10.869KHz
So, say the CCP value is 70. 70*1.33us = 93.33us or 10.714KHz

Clear as mud?


Last edited by Mark on Sat Nov 13, 2004 5:49 pm; edited 1 time in total
zio_pecos



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

PostPosted: Sat Nov 13, 2004 11:59 am     Reply with quote

as i've understood this settings allow me to have a good resolution....


so why i can read a frequency changement only if is bigger than 250Hz??

maybe due to the break in 2 pieces i do to transmit through usb?
Mark



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

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

PostPosted: Sat Nov 13, 2004 5:51 pm     Reply with quote

Did you not understand what I posted Question

The resolution is not high enough for timer1's increment rate. Do the math and you will see.
zio_pecos



Joined: 24 Oct 2004
Posts: 21
Location: italy

View user's profile Send private message

PostPosted: Sun Nov 14, 2004 2:00 am     Reply with quote

Mark wrote:
Did you not understand what I posted Question

The resolution is not high enough for timer1's increment rate. Do the math and you will see.



yes yes!!

i'm wrong!! excuse me

so how can i do to increase resolution with CCP capture?

can this post help me?

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



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

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

PostPosted: Sun Nov 14, 2004 8:19 am     Reply with quote

Well you have Timer1 as a divide by 8. That will lower the resolution 8 times. Make it a divide by 1 and you will get 8 times the resolution.
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