// ********************************************************************
// The MotXReg controls the stepper motor that moves in the x-axis.
// Given the new touch screen coordinates, it moves the stepper motor
// to the new coordinates by setting its MotorCon output to the correct
// values.  The MotorCon outputs are inputs to the stepper motor driver
// chip and give the chip the correct amount and direction to move.

module MotXReg (TouchX, CLK, RST, MotorX, MotorCon) ;

input [8:0] TouchX ;
input CLK ;
input RST ;

output [15:0] MotorX ;
output [1:0] MotorCon ;

reg [15:0] MotorX;
reg [1:0] MotorCon;
reg [15:0] counter;
reg [3:0] ScaleCount; // 4-bit counter for the scaling factor

`define RESET_VALUE 16'b0000_0001_0000_0000 // 256
`define COUNTER_LIMIT 16'b1001_1100_0100_0000 // set limit at 40,000

// 1st bit indicates CW/CCW 2nd bit indicates clock level
`define DRIVE_CW_H  2'b01
`define DRIVE_CW_L  2'b00
`define DRIVE_CCW_H 2'b11
`define DRIVE_CCW_L 2'b10

// Scaling constant
`define SCALE 4'b1100 // 12 pixels per half step

always@(posedge CLK)
begin

  if(RST)
  begin
   MotorX <= `RESET_VALUE;
 MotorCon <= `DRIVE_CW_L;
 counter <= 0;
 ScaleCount <= 4'b0100;
  end  // end if(RESET)
 
  else
  begin
 counter <= counter + 1;

 // count to limit between steps to give steppers enough time to move
 if(counter == `COUNTER_LIMIT)
 begin

   if(TouchX < MotorX[8:0]) // need to move turret to the left - turn CCW
   begin

    case(MotorCon)
  `DRIVE_CCW_H:
    begin
      MotorCon <= `DRIVE_CCW_L;
    end
  `DRIVE_CCW_L:
    begin
   MotorX <= MotorX - 1;

   if (ScaleCount != 4'b0000)
       ScaleCount <= ScaleCount - 1;
 
     if ( ScaleCount ==  4'b0000 ) begin // move if MotorX is equal to zero
     MotorCon <= `DRIVE_CCW_H;
     ScaleCount <= `SCALE;  // reset the counter
   end
 
    end
  default:  MotorCon <= `DRIVE_CCW_L;
  endcase
   end
 
   else if(TouchX > MotorX[8:0]) // need to move turret to the right - turn CW
   begin

    case(MotorCon)
     `DRIVE_CW_H:
       begin
         MotorCon <= `DRIVE_CW_L;
       end
    `DRIVE_CW_L:
      begin
        MotorX <= MotorX + 1;
        ScaleCount <= ScaleCount + 1;
      if (ScaleCount >= `SCALE) begin
          ScaleCount <= 4'b0000; // counter reload
          MotorCon <= `DRIVE_CW_H;
        end
      end
    default:  MotorCon <= `DRIVE_CW_L;
      endcase
    end

   counter <= 0;
 end // end if counter limit reached
  end // end if(!RESET)
end // end always@(posedge CLK)

endmodule
 

Back to Report