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 width of a pulse

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



Joined: 05 Dec 2004
Posts: 31

View user's profile Send private message

measure the width of a pulse
PostPosted: Tue Jul 17, 2007 11:07 am     Reply with quote

I use 16f877 with 32768Hz crystal to measure a pulse width.

the pulse is as follows :

high to low transition at A, after 9 ms there is a low to high transition B.,
so there is a length of time 9ms btn AB. the code is as follows but no success !!!

#include <16F877.h>
#fuses lp,NOWDT,NOPROTECT,NOLVP
#use delay( clock=32768)
#include "c:\lcd.c"
int count;
long time,time1,time2;
#int_ccp1
void time_isr()
{
time1=get_timer1();
count++;
setup_ccp1(ccp_capture_re);
clear_interrupt(int_ccp1);
}
void main()
{ count=0;
time=time1;
lcd_init();
setup_ccp1(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1)
{
if (count==2)
{
time2=time1-time;
printf(lcd_putc,"%LX",time2); // time2/32768/4 is the time
}
}
}

external pulse is connect to ccp1. Can anyone give me a hand ????
thanks very much !!!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 17, 2007 12:46 pm     Reply with quote

CCS has an example file, though it uses both CCP1 and CCP2.
Here's the file location:
Quote:
c:\Program Files\PICC\Examples\Ex_ccpmp.c
Bill Boucher



Joined: 04 Feb 2005
Posts: 34
Location: Chatham,ON,CA

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

PostPosted: Tue Jul 17, 2007 5:33 pm     Reply with quote

I can see a few problems...

Aside from the obvious that this program will only capture 1 pulse properly because it never sets itself back to look for falling edges... but that it will respond to each subsequent rising edge and print a value for every time count hits 2 again... assuming that pulses were to continue arriving on infinitum...

1/ var time1 is not initialized with a value on startup but var time is assigned the value of time1 and so var time remains unknown.

2/ var time never gets updated with timer1 value so there is no reference for the final calculation. The result will be random because timer1 is free running and var time contains a fixed value and so provides no reference to calc the value stored in var time2.

3/ the var time1 gets the value of timer1 twice, once at the leading edge and again at the trailing edge so time1's first value gets overwritten by the second.

In your main(), add this:

Code:

if (count == 1)
{
time = time1;
}
else if (count == 2)
{
time2 = time1 - time;
printf(lcd_putc,"%LX",time2);
}


Once you get this to work, you should try to reorganise it to be able to measure incoming pulses repeatedly. However, if pulses repeat too quickly, your LCD will have trouble keeping up so you might have to add code to wait for a minimum amount of time between LCD updates and have it always show the last captured value. Think of it like a button debounce routine. In your simple program, just add a delay_ms(250) after the print statement. Another suggestion, move the "if (count..." code into the ISR. The timer1 values, both reference and final, should be done within the ISR so that they are not missed if the main() loop is busy doing other things. If the ISR captures and stores both timer1 values, then the calculation can be done by main() at its convenience without possibility of main() missing a comparison of count at any specific value. Also, you should clear count after the pulse time calc is completed and also set the edge to scan for back to the initial direction. That way everything will work again when the next pulse arrives.
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