nicko-391
Member level 2
I want to measure pulse width of a signal.I wrote this.but It always gives constant pulse width whether frequency is changed.What is the wrong with this?
#include <18f2550.h>
#include <math.h>
#use delay(clock=8000000)
#fuses HSPLL,NOWDT,NOPROTECT,BROWNOUT, PUT, NOLVP,NODEBUG,USBDIV,CPUDIV1,VREGEN
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,errors)
static unsigned int8 x,y,ex;
static float duration;
static char vt[5];
static int1 got_RE,got_P,display;
#INT_CCP1
void detect(){
disable_interrupts(global);
if(got_RE){
x=ccp_1;
setup_CCP1(CCP_CAPTURE_FE);
got_RE=FALSE;
printf("\r%d\n",x);
}
else{
y=ccp_1;
duration=frexp(ccp_1,&ex);
duration=duration+y;
setup_CCP1(CCP_CAPTURE_RE);
got_RE=TRUE;
printf("\r%d\n",(y-x));
got_P=TRUE;
}
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
}
void main()
{
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_2);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
x=0;
y=0;
got_RE=TRUE;
got_P=FALSE;
ccp_1=0;
while(1){
if(got_P){
ccp_1=0;
}
}
}
#include <18f2550.h>
#include <math.h>
#use delay(clock=8000000)
#fuses HSPLL,NOWDT,NOPROTECT,BROWNOUT, PUT, NOLVP,NODEBUG,USBDIV,CPUDIV1,VREGEN
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,errors)
static unsigned int8 x,y,ex;
static float duration;
static char vt[5];
static int1 got_RE,got_P,display;
#INT_CCP1
void detect(){
disable_interrupts(global);
if(got_RE){
x=ccp_1;
setup_CCP1(CCP_CAPTURE_FE);
got_RE=FALSE;
printf("\r%d\n",x);
}
else{
y=ccp_1;
duration=frexp(ccp_1,&ex);
duration=duration+y;
setup_CCP1(CCP_CAPTURE_RE);
got_RE=TRUE;
printf("\r%d\n",(y-x));
got_P=TRUE;
}
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
}
void main()
{
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_2);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
x=0;
y=0;
got_RE=TRUE;
got_P=FALSE;
ccp_1=0;
while(1){
if(got_P){
ccp_1=0;
}
}
}
Last edited: