Created
April 1, 2020 17:26
-
-
Save Laksen/c94372434d8a25b87d8ce5019e133f14 to your computer and use it in GitHub Desktop.
Microstepper prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module pwm_gen #( parameter WIDTH = 8 ) | |
(input clk, | |
input rst, | |
input [WIDTH-1:0] value, | |
output out); | |
reg out = 0; | |
reg [WIDTH-1:0] curr_value = 0; | |
reg [WIDTH:0] counter = 0; | |
wire [WIDTH:0] next_counter = counter + 1; | |
always @(posedge clk) | |
begin | |
out <= (curr_value == {WIDTH{1'b0}}) ? 1'b0 : | |
(curr_value == {WIDTH{1'b1}}) ? 1'b1 : | |
(counter[WIDTH-1:0] < curr_value); | |
counter <= next_counter; | |
if (next_counter[WIDTH] != counter[WIDTH]) | |
curr_value <= value; | |
if (rst) | |
begin | |
counter <= 0; | |
curr_value <= value; | |
end | |
end | |
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module stepper #( parameter USTEPS_PER_STEP = 64 ) | |
(input clk, | |
input rst, | |
// Input | |
input step, | |
input direction, | |
// Output | |
output [3:0] outp); | |
localparam | |
COUNTER_WIDTH = $clog2(USTEPS_PER_STEP*4); | |
reg [31:0] values [0:USTEPS_PER_STEP*4-1]; | |
reg [31:0] currentStep = 0; | |
reg [COUNTER_WIDTH-1:0] counter = 0; | |
genvar i; | |
generate | |
for(i=0; i<4; i=i+1) | |
pwm_gen #(.WIDTH(8)) pwm( | |
.clk(clk), | |
.rst(rst), | |
.value(currentStep[i*8 +: 8]), | |
.out(outp[i])); | |
endgenerate | |
function [7:0] Shorten; | |
input signed [31:0] value; | |
Shorten = value<0 ? 8'd0 : value[7:0]; | |
endfunction | |
genvar i2; | |
generate | |
for(i2=0; i2<USTEPS_PER_STEP*4; i2=i2+1) | |
initial values[i2] <= { | |
Shorten($rtoi($cos(3.141592653589*((i2+0.0*$itor(USTEPS_PER_STEP))/($itor(USTEPS_PER_STEP)*2.0)))*255)), | |
Shorten($rtoi($cos(3.141592653589*((i2+1.0*$itor(USTEPS_PER_STEP))/($itor(USTEPS_PER_STEP)*2.0)))*255)), | |
Shorten($rtoi($cos(3.141592653589*((i2+2.0*$itor(USTEPS_PER_STEP))/($itor(USTEPS_PER_STEP)*2.0)))*255)), | |
Shorten($rtoi($cos(3.141592653589*((i2+3.0*$itor(USTEPS_PER_STEP))/($itor(USTEPS_PER_STEP)*2.0)))*255)) | |
}; | |
endgenerate | |
always @(posedge clk) | |
begin | |
currentStep <= values[counter]; | |
if (step) | |
counter <= direction ? (counter+1) : (counter-1); | |
if (rst) | |
counter <= 0; | |
end | |
endmodule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment