before i go ahead with my code....ill explain wht i have to do

I need to control the position of a DC motor[geared],using a potentiometer

I have to first detect the endpoints of a gear assembly.
say, i start with a count of 32500 and the count at one end of the gear assembly is 32600
..now i rotate it to the other side ,say the count at this point is 32400

The above was just the calibration routine.Using the above values ,i have to
scale the 200 counts[32600-32400] to a potentiometer value[0 to 255]

How am i detecting the end points?
..im using the LMD18200T motor driver .Using the current sense feature of the chip,i detect wheteher the gear assembly has reached an end point.The current sense pin is connected to an ADC port..portA.1 namely


my program flow...
1> set counter to 32500
2> move motor in one direction
3> count pulses..if CW,increment.if CCW decrement
...say it is CW and counter increments to 32600 till the end
4>counter_1 has 32600
5>move the motor to the other side,count the pulses..it is decrementing now
6>so counter_2 has 32400
7>now i have to scale the gear assembly range to a potentiometer
difference=counter_1-counter_2
=32600-32400
=200
8> scale to a pot value
pot_val holds the pot value connected to analog portA.0

hence required count value=[difference/256]*pot_val+counter_2
=[difference*pot_val]/256+counter_2 (rearranging)

example...if potvalue in 128,then
required count value=(200*128)/256+32400
=32500

now i dont know where to go from here..how do i get the motor to move to position 32500,when the pot is moved to 128

...this is a long post..but its the best i could do to explain wht im trying to do.
-Gerry