Using a limit switch

Help!!
how do i use a limit switch to stop a motor at a given point. i need this in autonomous mode

var = GetDigitalInput(1);

if ( var == 1 )
SetPWM( 1, 127); // Stop the motor