module MOTYREG (TouchY, RST, CLK, MotorY, MotorCon) ;
input [8:0] TouchY ;
input RST ;
input CLK ;
output [15:0] MotorY ;
output [1:0] MotorCon ;
reg [15:0] MotorY;
reg [1:0] MotorCon;
reg [21: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'b1011 // 11 pixels per half step
always@(posedge CLK)
begin
if(RST)
begin
MotorY <= `RESET_VALUE;
MotorCon <= `DRIVE_CW_L;
counter <= 0;
ScaleCount <= 4'b0101; // intit to 5
end // end if(RESET)
else
begin
counter <= counter + 1;
// count to one million between steps; it's 4 for simulation purposes
if(counter == `COUNTER_LIMIT)
begin
if(TouchY < MotorY[8:0]) // need to move turret to the
top - turn CCW
begin
case(MotorCon)
`DRIVE_CCW_H: MotorCon <= `DRIVE_CCW_L;
`DRIVE_CCW_L:
begin
MotorY <= MotorY - 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(TouchY > MotorY[8:0]) // need to move turret to
the bottom - turn CW
begin
case(MotorCon)
`DRIVE_CW_H: MotorCon <= `DRIVE_CW_L;
`DRIVE_CW_L:
begin
MotorY <= MotorY + 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