我正在尝试使用 FPGA 控制 4 个电机。(Verilog HDL)我使用always块和自定义模块编写了一个代码(它控制一个伺服电机,它的输入值-L_CTRL和R_CTRL确定伺服电机是向左还是向右旋转一步)
这是自定义模块代码:
module Servo(CLK, RESETN, L_CTRL, R_CTRL, SERVO);
input CLK;
input RESETN, L_CTRL, R_CTRL;
output SERVO;
integer REG, CNT;
reg L, R;
reg SERVO;
always @(posedge RESETN or posedge CLK)
begin
if (RESETN) CNT = 0;
else
if (CNT >= 199) CNT = 0;
else CNT = CNT + 1;
end
always @(posedge RESETN or posedge CLK)
begin
if (RESETN)
REG = 15;
else
begin
L <= L_CTRL; R <= R_CTRL;
if (L == 0 & L_CTRL & REG > 7)
REG = REG - 1;
else if (R == 0 & R_CTRL & REG < 23)
REG = REG + 1;
end
end
always @(CNT or REG)
begin
if (CNT < REG)
SERVO = 1;
else
SERVO = 0;
end
endmodule
我编写了控制 4 个电机的代码:
module Servo_Motor(direction,CLK,RESETN,SERVO);
input [1:0]direction;
input CLK, RESETN;
reg L_CTRL, R_CTRL;
reg [3:0] SERVO;
output [3:0] SERVO;
//servo0, servo1->x_axis
//servo2, servo3->y_axis
always @(posedge RESETN or posedge CLK)
begin
if(direction==2'b01)//east
begin
L_CTRL<=0; R_CTRL<=1;
Servo S0(CLK, RESETN, L_CTRL, R_CTRL, SERVO[0], enable_0);
Servo S1(CLK, RESETN, L_CTRL, R_CTRL, SERVO[1], enable_1);
SERVO[2]<=0;
SERVO[3]<=0;
end
else if(direction==2'b11)//SOUTH
begin
L_CTRL<=1; R_CTRL<=0;
Servo S2(CLK, RESETN, L_CTRL, R_CTRL, SERVO[2], enable_2);
Servo S3(CLK, RESETN, L_CTRL, R_CTRL, SERVO[3], enable_3);
SERVO[0]<=0;
SERVO[1]<=0;
end
else if(direction==2'b10)//WEST
begin
L_CTRL<=1; R_CTRL<=0;
Servo S0(CLK, RESETN, L_CTRL, R_CTRL, SERVO[0], enable_0);
Servo S1(CLK, RESETN, L_CTRL, R_CTRL, SERVO[1], enable_1);
SERVO[2]<=0;
SERVO[3]<=0;
end
else if(direction==2'b00)//NORTH
begin
L_CTRL<=0; R_CTRL<=1;
Servo S2(CLK, RESETN, L_CTRL, R_CTRL, SERVO[2], enable_2);
Servo S3(CLK, RESETN, L_CTRL, R_CTRL, SERVO[3], enable_3);
SERVO[0]<=0;
SERVO[1]<=0;
end
end
endmodule
我猜在侧面调用模块总是阻塞会导致错误。是否有任何替代算法/方法来解决这个问题?
谢谢。