用FPGA实现一个开关控制8位流水灯的verilog程序(消除前面不定态)
时间:10-02
整理:3721RD
点击:
大家好,我是一枚小菜鸟,刚刚接触FPGA,VERILOG学得也不好。这几天在试图用FPGA板来实现“一个SWITCH控制8位流水灯”,但是写VERILOG程序无果。另外,我暂时不想用CLK,感觉SWITCH应该可以吧(设想其实就是用SWITCH代替CLK)。在网上看到一些别人写的程序,无一例外地——前面都有几个不定态。我希望能写出前面不含不定态的~下面附上我的程序,没编译成功,请求大家多多指教,万分感谢!module theleds(led,switch);
output[7:0]
led;
input
switch;
reg [7:0] ctl;
reg [7:0] led;
ctl=8'b00000001;
always @(switch)begin
begin
if(ctl<8'b10000000) ctl=ctl<<1;
else ctl=8'b00000001;
end
led=ctl;
end
endmodule
module stimulus;
reg switch;
wire [7:0] led;
initial
begin
switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
end
endmodule
output[7:0]
led;
input
switch;
reg [7:0] ctl;
reg [7:0] led;
ctl=8'b00000001;
always @(switch)begin
begin
if(ctl<8'b10000000) ctl=ctl<<1;
else ctl=8'b00000001;
end
led=ctl;
end
endmodule
module stimulus;
reg switch;
wire [7:0] led;
initial
begin
switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
#5 switch=1;
#5 switch=0;
end
endmodule
看语法!看看你哪里错了!
1.ctl即为 reg型,那么一般只能在always中赋值;如果always之外赋值,此变量需要为net型,如wire;且需要使用assign 语句;led同理;
2.ctl不能即在always外又在always内部赋值;
2.stimulus中没有包含theleds模块;
建议你找一本讲述Verilog的书,认真学习基本语法,代码中有几处错误都是基本语法错误,举例如下:
1、错误:ctl=8'b00000001;
原因:ctl定义为reg型变量,如果想给它赋初值,可以有两种方法:a、直接在端口定义时赋值,即:reg [7:0] ctl = 8'b0000_0001; b、增加复位信号,在always块中复位时赋初值。
2、错误:led=ctl
原因:同1,建议将led改为wire型变量,用assign赋值,即:wire [7:0] led; assign led = ctl;
3、错误:stimulus 模块中没有对theleds模块的例化,具体例化方法建议参考书籍上的说明
以上均为个人建议,仅供参考
多谢前辈们耐心解答,我明白啦。
