0
\$\begingroup\$

I am trying to interface an ultrasonic sensor HC-SR04 to a 51 family microcontroller like 89s51. After learning about how ultrasonic sensor works I wrote a program as per my knowledge about ultrasonic sensor but now this program doesnt seem to work. What the program basically does is that it detects any obstruction like wall in front of ultrasonic sensor and if the distance gets below 30cm it stops running both motors and runs only right motor for some time and again starts running both motors and when it detects wall within 30cm 2nd time it runs left motor for some time and again starts runing both motors 3rd time again only right motor and in this way cycle continues forever while ultrasonic constantly keeps detecting distance between it and wall. The program is as follows -

#include <reg51.h>
#include <stdio.h>
sfr16 DPTR =0x82                ;
sbit RM1_X = P2^0               ;    //Positive of Right Motors
sbit LM1_X = P2^1               ;    //Positive of Left Motors
sbit RM2_X = P2^2               ;    //Positive of Right Motorss
sbit LM2_X = P2^3               ;    //Positive of Left Motors
sbit US_E = P3^2                ;    //Echo pin of Ultrasonic sensor
sbit US_T= P3^5                 ;    //Trigger pin of Ultrasonic sensor
//function prototypes
void RM_F(void)                 ;
void RM_S(void)                 ;
void LM_F(void)                 ;
void LM_S(void)                 ;
void trig(void)                 ;
void MSDelay(unsigned int)          ;
unsigned char get_range(void)           ;
void main()
{ TMOD=0x09                 ;
  TR0=1                     ;
  TH0=0x00                  ;
  TL0=0x00                  ;
  P3=0x04                   ;
  P2=0x00                   ;
while(1)                        
{ unsigned char R=0             ;
   R=get_range()                ;
if(P0^0==0)
{if(R<=30) 
 {P0=0x01;
  LM_F()                    ;
  MSDelay(4000)                 ;
  }                   
else 
 {RM_F()                    ;
  LM_F()                    ;
  }}
else
{if(R<=30) 
 {P0=0                      ;
  RM_F()                    ;
  MSDelay(4000)                 ;
  }                   
else 
 {RM_F()                    ;
  LM_F()                    ;
  }}}}
unsigned char get_range() 
{unsigned char range                ;
 trig()                     ;
 MSDelay(40)                    ;
 DPH=TH0                    ;
 DPL=TL0                    ; 
 TH0=0xFF                   ;
 TL0=0xFF                   ;
 if(DPTR<35000)
 range=DPTR/59                  ;
 else
 range=0                    ;
 return range                   ; 
}
void trig()
{TH0=0x00                   ;
TL0=0x00                    ; 
US_T=1                      ;
MSDelay(0.01)                   ;                        
US_T=0                      ;
}
void RM_F()
{RM1_X = 1                  ;    //Make positive of motor 1
RM2_X = 1                   ;    //Make positive of motor 1
}
void RM_S()
{RM1_X = 0                  ;    //Make positive of motor 0
RM2_X = 0                   ;    //Make positive of motor 0
}
void LM_F()
{LM1_X = 1                  ;    //Make positive of motor 1
LM2_X = 1                   ;    //Make positive of motor 1
}
void LM_S()
{LM1_X = 0                  ;    //Make positive of motor 0
LM2_X = 0                   ;    //Make positive of motor 0
} 
//Some delay...
void MSDelay(unsigned int itime)
{unsigned int i,j               ;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++)             ;
}

What changes should i make to make this program work. Thanks for helping. PS - I used this blog as a reference for my program http://homemaderobo.blogspot.in/2012/08/ultrasonic-sensor-interfacing-with-8051.html

\$\endgroup\$
  • \$\begingroup\$ Can you describe the ALGORITHM you're using to calculate distance based on the signals the HC-SR04 uses as inputs and outputs? Also, how does your program fail? You need to help us help you \$\endgroup\$ – Scott Seidman Aug 28 '15 at 17:04
  • \$\begingroup\$ @ScottSeidman I've written the algorithm using the link i've provided as reference they've explained it there and for failing it shows no error in compiling but when i dump it on microcontroller and implement it even if there is a wall in front of the sensor it just keeps running both motors. \$\endgroup\$ – devansh Aug 28 '15 at 17:20
2
\$\begingroup\$

I suggest splitting up the ultrasound part and the motor part. Find some way to figure out what your system THINKS the ultrasound is telling it (maybe UART or something), and don't worry about the motor.

Then, write a program that should modulate the speeds of the motor.

If both of those give you answers that make sense to you, then try to put those two things together.

So, debug each of your main systems separately, then join them together.

\$\endgroup\$
  • \$\begingroup\$ what ultrasonic does is that it sends a soundwave after which ECHO pin goes high and when the wave reflects back from a wall and is received the ultrasonic sensor calculates the time it took and depending on that echo pin goes low for some time and becomes high again. So I've connected echo pin to INT0 so when it goes low it should start counting and when it goes high again it should stop. Now this count should go to DPTR after which I know how to convert it to distance and use it in my program. I think the problem is with counting at INT0 and then sending count to DPTR. \$\endgroup\$ – devansh Aug 29 '15 at 9:03
  • \$\begingroup\$ @devansh I've used the module, and echo is high WHILE WAITING FOR THE RETURN. You should start counting when it goes high and stop when it goes low \$\endgroup\$ – Scott Seidman Aug 29 '15 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.