PING Ultrasonic sensor
Scada
Posts: 1
I'm using a PIC16F877 microcontroller to send a pulse to a PING ultrasonic sensor to measure distances. If the pulse is sent to the sensor. the activity LED blinks and nothing happens again. This is the code to send and receive signal from the sensor.
void Get_distance()
{
·//unsigned short start_time,end_time;
·//unsigned short f_time;
·start_time=0;
·end_time=0;
·f_time=0;
·· ·//Clear TMR1 and enable TMR1 disable TMR1 interrupt
·TMR1ON=0;
·TMR1H=0x00;
·TMR1L=0x00;
·TMR1ON=1;
·TMR1IE=0;
·//Get fuel level in seconds
··TRISB3=0;
··· RB3=0;
··· _delay(2);
····RB3=1;···//Output trigger pulse
· ··_delay(5);···· //Trigger pulse is 5us long
· ··RB3=0;···//End trigger pulse
· ··_delay(200);·//Delay to take care of Thold-off
· ··TRISB3=1;··//Make RA0 an input
··do{
····if(RB3==1)
·· ··{
····· start_time=TMR1H;
·· ··· start_time<<=8;
·· ··· ·· start_time+=TMR1L;
······}
···}while(RB3==0);
···do{
····· if (RB3==0)
····· {
······· end_time=TMR1H;
·· ···· end_time<<=8;
·· ···· end_time+=TMR1L;
······ }
···}while(RB3==1);
·}···
void Get_distance()
{
·//unsigned short start_time,end_time;
·//unsigned short f_time;
·start_time=0;
·end_time=0;
·f_time=0;
·· ·//Clear TMR1 and enable TMR1 disable TMR1 interrupt
·TMR1ON=0;
·TMR1H=0x00;
·TMR1L=0x00;
·TMR1ON=1;
·TMR1IE=0;
·//Get fuel level in seconds
··TRISB3=0;
··· RB3=0;
··· _delay(2);
····RB3=1;···//Output trigger pulse
· ··_delay(5);···· //Trigger pulse is 5us long
· ··RB3=0;···//End trigger pulse
· ··_delay(200);·//Delay to take care of Thold-off
· ··TRISB3=1;··//Make RA0 an input
··do{
····if(RB3==1)
·· ··{
····· start_time=TMR1H;
·· ··· start_time<<=8;
·· ··· ·· start_time+=TMR1L;
······}
···}while(RB3==0);
···do{
····· if (RB3==0)
····· {
······· end_time=TMR1H;
·· ···· end_time<<=8;
·· ···· end_time+=TMR1L;
······ }
···}while(RB3==1);
·}···
Comments
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dave Andreae
Parallax Tech Support·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
·