US-100 Ultrasonic Sensor Module

Discussion in 'General Electronics Chat' started by rogercores, Jul 30, 2012.

  1. rogercores

    Thread Starter New Member

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

    code:
    Code ( (Unknown Language)):
    1.  
    2. #include<AT89x51.h>
    3. void LCD_init();
    4. void LCD_busy();
    5. void LCD_command(unsigned char);
    6. void LCD_data(unsigned char);
    7. void LCD_string(unsigned char[]);
    8. char itoc(int a);
    9. #define Tx P2_6
    10. #define Rx P2_7
    11. unsigned char b,mode;
    12. int c;
    13. void delayer() interrupt 1{ //timer 0 interrupt service routine
    14.     TH0=0XFC;
    15.       TL0=0X18; // for 1ms delay
    16.     c++;
    17.     if(mode==0){
    18.         if(c == 1){
    19.             Tx = 0;
    20.            
    21.         }
    22.         else if(c == 2){
    23.             Tx = 1;
    24.        
    25.         }
    26.         else if(c == 12){// 10 ms signal
    27.             Tx = 0;
    28.             TR0 = 0;
    29.             c = 0;
    30.             b = 0;//stop waiting
    31.             mode = 1;
    32.            
    33.            
    34.         }
    35.  
    36.     }
    37. }
    38. void main(){
    39.     Rx = 1;//config as input
    40.     EA = 1;//enable interrupts
    41.     ET0 = 1;//for timer 0
    42.     LCD_init();//initialize lcd
    43.     LCD_command(0x01);//clear display
    44.     LCD_string("Whats up!");//send string to lcd
    45.     while(1){
    46.    
    47.     Tx = 0;//transmition line is low
    48.     mode = 0;//for timing purpose
    49.     c = 0;//milli second counter
    50.     b = 1;//for waiting
    51.     TMOD = 0x01;// timer 0 16 bit autoreload
    52.     TH0 = 0xFC;
    53.     TL0 = 0x18;    // for 1ms delay
    54.     TR0 = 1;//start timer
    55.     while(b);    //wait
    56.     TH0 = 0xFC;
    57.     TL0 = 0x18;// for 1ms delay
    58.     while(!Rx);//wait for reply
    59.     c = 0; //reload counter
    60.     TR0 = 1;//start counting time periiod i.e pulse width
    61.     while(Rx);//pulse end
    62.     TR0 = 0;//stop counting
    63.     LCD_command(0x01);
    64.     itoc(c);//convert counted millliseconds (integer) to string and send to lcd
    65.     }
    66. }
    67.  

    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.....
     
    Last edited by a moderator: Jul 30, 2012
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    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: Jul 30, 2012
Loading...