1

我正在尝试将 FPGA 用作带有 pwm 的某些 LED 的移位寄存器,但在尝试分配包含移入输出变量的值的 reg 时遇到错误。当我将它上传到 FPGA 时(我正在使用嵌入式 micro 的 mojo),它什么也不做。当我使用模拟器时,它报告所有输出变量都没有被分配并且具有 X 的值,而模块内的所有其他变量都可以正常工作。这是我的移位模块代码:

module shifting(
    input clk,
    input shiftingpin,//data to be shifted in
    input rst,
    output done,
    output [3:0]data//pwm compare value output
    );
reg [2: 0] ctr_d, ctr_q;
reg don;
reg [3:0]datas;
always @(*) begin
     if(ctr_q == 3'b100) begin
        ctr_d[2:0] = 3'b0;
        don = 1'b1;
     end else begin
       ctr_d = ctr_q + 1'b1;
        don = 1'b0;
     end
end
always @(posedge clk) begin
     datas[ctr_q] = shiftingpin;// assign value to the output
    if (rst) begin
        ctr_q <= 1'b0;
    end else begin
        ctr_q <= ctr_d;
    end
end
assign data = datas;
assign done = don;
endmodule 

done 告诉包含模块何时更新并将值分配给 pwm。

4

1 回答 1

1

如果我正确理解了这个问题,那么您在尝试从always块内驱动端口时会出现语法错误。

声明端口时,默认情况下它们通常是有线的,只能由端口驱动或分配。导致下面的代码

module shifting(
    input        clk,
    input        shiftingpin,
    input        rst,
    output       done,
    output [3:0] data
);
reg           don; 
reg [3:0]     datas;
assign done = don;    
assign data = datas;

解决方案

解决方案是将端口定义为reg,如果您可以支持 System Verilog,则首选逻辑。 logic将根据需要有效地在 wire 和 reg 之间切换,以使重构代码更容易。

module shifting(
    input             clk,
    input             shiftingpin,
    input             rst,
    output reg        done,
    output reg  [3:0] data
);
always @(posedge clk) begin
 data[ctr_q] <= shiftingpin; // <-- data port used directly
//...

注意:移位寄存器只需

always @(posedge clk) begin
  datas[3:0] <= {datas[2:0], shiftingpin};
于 2014-10-12T06:14:33.310 回答