Hic!anh F ơi!em mua con SRF05 ở cty anh rồi,nhưng sao em phát xung 10uS vào chân trigger mà chờ mãi ko thấy Echo phản hồi,không biết nó có hư rùi không.Đây là đoạn code của em,anh coi thử nhé:
#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP,NOCPD,NOWRT, NODEBUG
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use fast_io(B)
#use fast_io(D)
#define TRIGGER PIN_B1
#define ECHO PIN_B0
#bit TMR3ON=53.0
#bit TMR3IF=54.1
#byte TMR3H=53
#byte TMR3L=53
#byte T3CON=53
void main()
{
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
set_tris_b(0x01);
set_tris_d(0x00);
output_high(PIN_D1);//led de test
delay_ms(1000);
output_low(PIN_D1);
delay_ms(1000);
while(1)
{
output_high(PIN_B1);//start trigger
delay_us(10);
output_low(PIN_B1);//stop trigger
TMR3H=0;
TMR3L=0;
T3CON=0x30;//prescale 8:1
TMR3IF=0;
while(!input(PIN_B0)&&!TMR3IF); //wait Echo pulse to start
TMR3ON=1; //start timer3
//output_high(PIN_D1);
while(!ECHO&&!TMR3IF); //wait Echo pulse to stop=> nó bị ở chỗ này(vòng lặp vô tận)
TMR3ON=0;
output_high(PIN_D1);//=>led ko sáng
if(!TMR3IF)
{
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
}
delay_ms(1000);
}
}
|