q3-dsb-labs/ch4/motorcontrol.sv

49 lines
864 B
Systemverilog

`timescale 1ns/1ps
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;
always_comb begin
case (state)
reset:
begin
pwm = 0;
next_state = high;
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
default:
begin
pwm = 0;
next_state = reset;
end
endcase
end
endmodule