hobby_85
Junior Member level 2
Hi, i need to accurately measure the time difference it takes for two pins on my PIC 16F690 to go high. Basically what i intend to do is use the time difference to measure the distance between two points. A source sends out an RF and a ultrasonic pulse at the same time, and because US signals lag RF signals by a bit, i should get a time difference.
anyway, i have written code but the problem i have is i keep getting two sets of values for each distance. so lets say im 1.2m away, i get 11.6m or 19.05m. when im 2.4m, i get 12.8m or 20.2m. Basically for any distance away, i keep getting two answers. if i got just one, i could possibly add a 'hack' to my code to fix it, but since im getting two answers, i dont know what to do. I know its not accurate, not anyway near accurate even, but im only trying to solve the 2 answers problem for now.
ive attached my code. please please please, any help/suggestions/advice would be great. thanks all
anyway, i have written code but the problem i have is i keep getting two sets of values for each distance. so lets say im 1.2m away, i get 11.6m or 19.05m. when im 2.4m, i get 12.8m or 20.2m. Basically for any distance away, i keep getting two answers. if i got just one, i could possibly add a 'hack' to my code to fix it, but since im getting two answers, i dont know what to do. I know its not accurate, not anyway near accurate even, but im only trying to solve the 2 answers problem for now.
ive attached my code. please please please, any help/suggestions/advice would be great. thanks all
Code:
#include <16F690.h>
#fuses INTRC, NOWDT, NOPROTECT, BROWNOUT, PUT, HS
#use delay(clock = 4000000)
#use rs232(baud=19200, xmit=PIN_B7, rcv=PIN_B5)
int16 ctr;
int tripped;
int tripped1;
long value;
int counter;
float distance;
int16 ctr1;
#int_CCP1
void CCP1_isr(void)
{
value = CCP_1;
value = value + (counter * 65536);
tripped=1;
disable_interrupts(INT_CCP1);
}
#int_TIMER1
void TIMER1_isr(void)
{
counter = counter + 1;
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(GLOBAL);
setup_ccp1(CCP_CAPTURE_RE);
for(;;){
contact_slave3(); //After this, PINC1 should go high, followed closely by PINC5
for (ctr=0;ctr<65535;ctr++) { //Wait for PINC1 to go high until timeout
if(input(PIN_C1)) {
set_timer1(0); //set timer value to 0, start counting
enable_interrupts(INT_TIMER1);
counter=0;
ctr=1; //PINC1 went high, set ctr=0, break
break;
}
}
if (ctr==1){ //at this point, C1 went high, start timer and wait for C5 to go high
enable_interrupts(INT_CCP1); //wait for rising edge of pinC5 until timeout
for (ctr1=0;ctr1<10000;ctr1++) {
if(tripped) {
delay_ms(200);
printf("TDOA is: %Lu us\r\n",value);
distance = (value) * (0.000008) * (348.4);
printf("Distance is:%f meters\r\n", distance);
tripped=0;
distance=0;
value=0;
tripped1 = 1;
break;
}
}
if (tripped1 == 0){
printf("\r\nNO US DETECTED\r\n");
tripped1=0;
}
}
else{ //PINC1 did not go high, error
printf("\r\nSlave Node 3 RF signature not found\r\n");
}
delay_ms(1000);
tripped = 0;
tripped1 = 0;
counter=0;
}
}