module wokwi( output a,b, // top level module must be named "wokwi"
input CLK, x, y, // declarations can be on one line
output OUT,
);
servo_test tester( // instantiate module
.mclk (CLK),
.led (a),
.servo (OUT)
);
endmodule
module servo_test (
input mclk,
output led,
output servo
);
reg [14:0] counter;
reg servo_reg;
reg [10:0] control = 0;
reg toggle = 1;
always @(posedge mclk)
begin
// servo algorithm
counter <= counter + 1;
if (counter == 'd19999) // count to 20 ms using 1 MHz clock
counter <= 0;
// if (counter < ('d1460 )) // close to center in wokwi, may be different in real life
if (counter < ('d460 + control)) // __________
servo_reg <= 1; // generate pulse __| |_______
else // that starts when counter == 0
servo_reg <= 0; // and ends when counter == (control + 460)
// test algorithm // this can be omitted
if (control == 'd2000) // sweep value of control between 0 and 2000
toggle <= 0;
if (control == 0)
toggle <= 1;
if (counter == 0)
begin
if (toggle == 0)
control <= control - 10;
else
control <= control + 10;
end
end
assign led = toggle; // outputs the value of toggle to the LED
assign servo = servo_reg; // output control signal to servo
endmodule