赞
踩
[6:0] vote
表示7个人,用开关控件控制。开关开启表示支持,输入一个电平信号,否则为零电平。②用条件判断语句对7路信号依次进行判断,给定一个中间信号[2:0] sum
,如果判断为1(高电平)则sum加1,最终sum如果大于等于4,输出高电平。输出pass
信号连接led灯。module vote_7(pass,vote,sum); output pass; output sum; input [6:0] vote; reg [2:0] sum; reg i; reg pass; always @(vote) begin sum = 0; for(i = 0; i<=6; i=i+1) begin if(vote[i])sum=sum+1; if(sum[2])pass=1; else pass=0; end end endmodule
`timescale 1ns/100ps module vote_7_tb; output pass; reg [6:0] vote; wire [2:0] sum; reg i; wire pass; vote_7 t(pass,vote,sum); initial begin vote = 7'b0000000; #10 vote = 7'b0000010; #10 vote = 7'b0001100; #10 vote = 7'b0011100; #10 vote = 7'b0010111; #10 vote = 7'b0100000; #10 vote = 7'b0110000; #10 vote = 7'b0111100; #10 vote = 7'b0010100; #10 vote = 7'b0110101; #10 $stop; end endmodule
[2:0] sum
是以二进制的形式存储,所以不是用[2:0] sum≥4
判断的,而是用最高位是否为高电平作为依据,最高位sum[2]=1表示已有4人支持,输出1通过。这一点要注意,我们在设计代码时要时刻保持用二进制的视角思考问题。clk
作为输入,当时钟处于上升沿时,信号显示端依次变化并产生巴克码的各个位数,最终产生1110010的序列。module bkm(clk,rst,dout);
input clk,rst;
output dout;
reg dout;
reg [2:0] state;
parameter s6 = 3'b110, s5 = 3'b101, s4 = 3'b100, s3 = 3'b011, s2 = 3'b010, s1 = 3'b001, s0 = 3'b000;
always @(posedge clk or negedge rst)
begin
if(!rst)
begin state <= s6; dout<= 1; end
else
begin
case
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。