Embedded C programming to interface ultrasonic sensor to 89s51 microcontroller

cinterfacemicrocontrollerprogrammingultrasound

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

Best Answer

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.