2025-02-20 08:53:45 +01:00
|
|
|
`timescale 1ns/1ps
|
|
|
|
|
2025-02-13 12:19:44 +01:00
|
|
|
module motorcontrol #(parameter N=21)
|
|
|
|
(input logic clk,
|
|
|
|
input logic rst,
|
|
|
|
input logic direction,
|
|
|
|
input logic [N-1:0] count_in,
|
|
|
|
output logic pwm);
|
|
|
|
|
|
|
|
typedef enum logic[1:0] {reset, high, low} pwm_state;
|
|
|
|
pwm_state state, next_state;
|
|
|
|
|
|
|
|
always_ff @(posedge clk)
|
|
|
|
if (rst)
|
|
|
|
state <= reset;
|
|
|
|
else
|
|
|
|
state <= next_state;
|
|
|
|
|
2025-02-14 08:51:17 +01:00
|
|
|
always_comb begin
|
2025-02-13 12:19:44 +01:00
|
|
|
case (state)
|
|
|
|
reset:
|
|
|
|
begin
|
|
|
|
pwm = 0;
|
2025-02-20 10:05:52 +01:00
|
|
|
next_state = high;
|
2025-02-13 12:19:44 +01:00
|
|
|
end
|
|
|
|
high:
|
|
|
|
begin
|
|
|
|
pwm = 1;
|
|
|
|
if (direction == 0 && count_in >= 21'd100_000)
|
|
|
|
next_state = low;
|
|
|
|
else if ( direction == 1 && count_in >= 21'd200_000)
|
|
|
|
next_state = low;
|
|
|
|
else
|
|
|
|
next_state = state;
|
|
|
|
end
|
|
|
|
low:
|
|
|
|
begin
|
|
|
|
pwm = 0;
|
|
|
|
next_state = state;
|
|
|
|
end
|
2025-02-20 08:53:45 +01:00
|
|
|
default:
|
|
|
|
begin
|
|
|
|
pwm = 0;
|
|
|
|
next_state = reset;
|
|
|
|
end
|
2025-02-13 12:19:44 +01:00
|
|
|
endcase
|
|
|
|
end
|
|
|
|
endmodule
|