Self-Balancing Robot
User Guide
7
www.terasic.com
July 12, 2018
//left motor control
.dc_motor_left_conduit_end_1_pwm (MTR_PWMB), //
dc_motor_left_conduit_end_1.pwm
.dc_motor_left_conduit_end_1_motor_in1 (MTRL_N), // .motor_in1
.dc_motor_left_conduit_end_1_motor_in2 (MTRL_P),
b. Control Rotation Speed
Below is partial code for controlling the rotation speed:
////////////////////////////////////////
// PWM
reg PWM_OUT;
reg [31:0] total_dur;
reg [31:0] high_dur;
reg [31:0] tick;
always @ (posedge clk or negedge reset_n)
begin
if (~reset_n)
begin
tick <= 1;
end
else if (tick >= total_dur)
begin
tick <= 1;
end
else
tick <= tick + 1;
end
always @ (posedge clk)
begin
PWM_OUT <= (tick <= high_dur)?1'b1:1'b0;
end
The
tick
register is the main counter,
total_dur
represents the total_dur register described in
Table
4-2
. When the
tick
value equals to
total_dur
setting value, the whole counter will be reset and