US-100 Ultrasonic Sensor Module

Thread Starter

rogercores

Joined Jul 14, 2012
8
I bought a Ultrasonic sensor lately and read its datasheet carefully. I tried using it but it doesn't reply......

code:
Rich (BB code):
#include<AT89x51.h>
void LCD_init();
void LCD_busy();
void LCD_command(unsigned char);
void LCD_data(unsigned char);
void LCD_string(unsigned char[]);
char itoc(int a);
#define Tx P2_6
#define Rx P2_7
unsigned char b,mode;
int c;
void delayer() interrupt 1{ //timer 0 interrupt service routine
    TH0=0XFC;
      TL0=0X18; // for 1ms delay
    c++;
    if(mode==0){
        if(c == 1){
            Tx = 0;
            
        }
        else if(c == 2){
            Tx = 1;
        
        }
        else if(c == 12){// 10 ms signal
            Tx = 0;
            TR0 = 0;
            c = 0;
            b = 0;//stop waiting
            mode = 1;
            
            
        }

    }
}
void main(){
    Rx = 1;//config as input
    EA = 1;//enable interrupts
    ET0 = 1;//for timer 0
    LCD_init();//initialize lcd
    LCD_command(0x01);//clear display
    LCD_string("Whats up!");//send string to lcd
    while(1){
    
    Tx = 0;//transmition line is low
    mode = 0;//for timing purpose
    c = 0;//milli second counter
    b = 1;//for waiting
    TMOD = 0x01;// timer 0 16 bit autoreload
    TH0 = 0xFC;
    TL0 = 0x18;    // for 1ms delay
    TR0 = 1;//start timer
    while(b);    //wait
    TH0 = 0xFC;
    TL0 = 0x18;// for 1ms delay
    while(!Rx);//wait for reply
    c = 0; //reload counter
    TR0 = 1;//start counting time periiod i.e pulse width
    while(Rx);//pulse end
    TR0 = 0;//stop counting
    LCD_command(0x01);
    itoc(c);//convert counted millliseconds (integer) to string and send to lcd
    }
}

I am new to 8051 and sensor, hence maybe its a silly mistake, please help me.
I am attaching my circuit and us100 datasheet below,

thanx in advance.....
 

Attachments

Last edited by a moderator:

BMorse

Joined Sep 26, 2009
2,675
looks like you may have your timing all off, you need to set the trigger pin high for 5ms then set it low, then wait until the echo line goes high and start your timer, once the echo line goes low, stop the timer and see how much "time" has elapsed (or pulse width), take this "time" and multiply it by 0.0001657 to get the distance in meters.... I do not see anywhere in your code where the trigger pin is only held high for 5ms..... this pin needs to be low before you start to "listen" for an echo.


So the code should go:

set TX pin high;
wait(5ms);
set TX pin low;
wait(for RX pin to go high);
If RX pin = 1 then start timer;
wait(while RX pin is high);
If RX pin=0 then stop timer;
Distance = Timer_Value*0.0001657;

You can figure out how to write the code, this is just a guide...
 
Last edited:
Top