Embedded C programming to interface ultrasonic sensor to 89s51 microcontroller
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.