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

Help...Measure Pulse Width

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







Help...Measure Pulse Width
PostPosted: Thu Jul 17, 2008 4:38 pm     Reply with quote

Hello

I am trying to measure a width from continuous pulse that is 200ms wide when high and 100ms when low. I just can't figure out how to do this. I'd ultimately like to get both positive & negative pulse widths. When I run the the CCS ex_ccpmp example I get some value that is not even close to what the measurement should be for the positive pulse width. Can someone please help maybe post an example?? thanks


Code:


#include <16F873.h>
#FUSES NOWDT, XT, NOPUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

int1 ready=FALSE;
unsigned long rise,fall,pulse_width;

void initialize()
{         
   setup_ccp1(CCP_CAPTURE_RE);   
   setup_ccp2(CCP_CAPTURE_FE);   
   setup_timer_1(T1_INTERNAL);   
   enable_interrupts(INT_CCP2);
   enable_interrupts(GLOBAL);
}

#int_ccp2
void isr()
{
   rise = CCP_1;
   fall = CCP_2;
   pulse_width = (fall-rise);
   ready=TRUE;
}


void main()
{
   initialize();
   
   while(TRUE)
   {
     if(ready)
     {
         ready=FALSE;
         
         printf("\r raw=%lu ms   cal= %lu ms ", pulse_width,pulse_width/5);
     }
   }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 17, 2008 5:08 pm     Reply with quote

You could change the prescaler on Timer1 to 8, and this would reduce
the timer clock to 625000 Hz. However, this is still too fast of a clock
for the long pulse widths that you want to measure. I suggest that you
reduce your PIC crystal to 4 MHz. Then you can do it (with the prescaler
set to 8 for Timer1).
Brad
Guest







Help...Measure Pulse Width
PostPosted: Mon Jul 21, 2008 8:44 am     Reply with quote

OK I upped the prescaler to 8 and switched the xtal to 4 MHz. I am still getting the wrong values.

For an incoming pulse of 100ms on both rising & falling. I get the following values:rise=18832 fall=31367 pulse= 13250

For an incoming pulse of 200ms on both rising & falling. I get the following values:rise=31891 fall=56942 pulse= 10121

any ideas??
Ttelmah
Guest







PostPosted: Mon Jul 21, 2008 10:12 am     Reply with quote

Er. Calculate fall-rise from your own figures.
31367-18832 = 12535
56942-31891 = 25051

Exactly the right sort of result.
What is almost certainly going wrong, is that the output takes too long. The values get updated, while you are still printing.

Disable the interrupt, copy the 'live' values, into a dummy set. Re-enable the interrupt, and print the dummy values, not the ones being updated in the interrupt.
Also, in your arithmetic, what happens if the timer 'wraps' between the two readings?. If the 'fall', is less than the 'rise'?. You should to trap and handle this.

Best Wishes
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