|
|
View previous topic :: View next topic |
Author |
Message |
Brad Guest
|
Help...Measure Pulse Width |
Posted: Thu Jul 17, 2008 4:38 pm |
|
|
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
|
|
Posted: Thu Jul 17, 2008 5:08 pm |
|
|
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 |
Posted: Mon Jul 21, 2008 8:44 am |
|
|
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
|
|
Posted: Mon Jul 21, 2008 10:12 am |
|
|
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 |
|
|
|
|
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
|