SRF05 sensor C18 code

Status
Not open for further replies.

daisordan

New Member
I am currently using PIC18F4455 chip to write a C18 code with SRF05 sensor. After i finish my code, it doesn't work. If i try to run it one by one like just run the "trig_pulse", RD2 will output a pulse to the sensor. But now if i try to run them together, there is no respond on the sensor. RD2 no output signal and RD3 no feedback. What is the program with my code?Do i have to use interrupt? I have look through many example of interrupt but still dun understand how to do it . It will be very helpful if someone can give me a hand on it Sad..Many Thanks!!

unsigned char distance;
void main (void){
TRISD=0b00001000; //set RD3(Echo)=input,RD2(Trigger)=output,RD6(servo)=output
while(1){
set_timer1();
while(1){
trig_pulse(); //send pulse to trigger
while(!PORTDbits.RD3); //wait Echo goes high
measure_pulse();
distance = TMR1H + TMR1L;
distance /= 58;
}
}
void trig_pulse (void){
LATDbits.LATD2 = 1;
Delay1TCY();
Delay1TCY();
Delay1TCY();
LATDbits.LATD2 = 0;
Delay1KTCYx(10);
}
void measure_pulse (void){
PIR1bits.TMR1IF = 0;
T1CONbits.TMR1ON = 1;
while(PORTDbits.RD3); //wait Echo goes low
T1CONbits.TMR1ON = 0;
}
void set_timer1(void){
OpenTimer1(TIMER_INT_ON & //interrupts enable
T1_8BIT_RW &
T1_SOURCE_INT &
T1_PS_1_1 );
INTCONbits.GIEH = 1;
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…