Verilog 4—2线优先编码器和十进制加减计数器
4—2线优先编码器:
根据4线—2线优先编码器的逻辑表: 输入 I0 1 × × × I1 0 1 × × I2 0 0 1 ×
I3 0 0 0 1 Y1 0 0 1 1 输出 Y2 0 1 0 1 可以得出输入与输出的逻辑表达式为: Y0=I2+I3
Y1=I1(~I2)+I3
根据此逻辑关系,写出verilog代码: module _4to2(I,Y0,Y1); input [3:0]I; output Y0,Y1; wire no1,no2;
not (no1,I[2]); //对I2取反 and (no2,I[1],no1); // I1&&(~I2)
assign Y1=(no2|I[3]); // Y1=I1(~I2)+I3 assign Y0=(I[2]|I[3]); // Y0=I2+I3 endmodule
仿真波形:
十进制加减计数器:
代码:
module ten_2(rst,initial_value,clk,choose,oSEG,count);
input clk,rst,choose; //50MHZ,复位按钮,加减选择档 input[3:0]initial_value; //初值置数 output[7:0]oSEG //数码管显示; output[3:0]count; //LED灯显示 wire[3:0]count1;
wire cp; //1HZ
devide_f u1(cp,rst,clk); //分频
ADD_SUB u2( cp,rst,choose,initial_value,count); //加减计数 display_LUT u3(oSEG,count1); //数码管译码显示 assign count1=count; endmodule
module display_LUT (oSEG,count1); input [3:0]count1; output[7:0]oSEG; reg [7:0]oSEG1; always @(count1) begin case(count1) 4'h0: oSEG1 = 8'b00111111; //对应译码 4'h1: oSEG1= 8'b00000110; 4'h2: oSEG1 = 8'b01011011; 4'h3: oSEG1 = 8'b01001111; 4'h4: oSEG1 = 8'b01100110; 4'h5: oSEG1 = 8'b01101101; 4'h6: oSEG1 = 8'b01111101; 4'h7: oSEG1 = 8'b00000111; 4'h8: oSEG1 = 8'b01111111; 4'h9: oSEG1 = 8'b01101111; 4'ha: oSEG1 = 8'b01110111; 4'hb: oSEG1 = 8'b01111100; 4'hc: oSEG1 = 8'b00111001; 4'hd: oSEG1 = 8'b01011110; 4'he: oSEG1 = 8'b01111001; 4'hf: oSEG1 = 8'b01110001; endcase end
assign oSEG=~oSEG1; //低电平显示有效,所以取反 endmodule
module ADD_SUB( cp,rst,choose,initial_value,count);
input [3:0]initial_value; //初值置数,通过拨码开关得到 input cp,rst,choose;
output[3:0]count; reg [3:0]count;
always@(posedge cp or negedge rst )begin //检测时钟或者是否复位 if(~rst)begin
if(choose) count<=4'b0000; //加计数复位为0 else count<=initial_value;end //减计数复位为初值 else begin
if(choose)begin
if(count==initial_value) count<=4'b0000; //加计数到置数后,从0开始 else count<=count+4'b0001; end
else begin
if(count==4'b0000) count<=initial_value; //减计数从置数值减到0 else count<=count-4'b0001;end end end endmodule
module devide_f(_1HZ,nCR,_50MHZ); //分频函数 input _50MHZ,nCR; output _1HZ; reg _1HZ; reg[31:0]Q;
always@(posedge _50MHZ )begin
if(~nCR)Q=32'd0;
if(Q>=32'd24999999)begin // 加到24 999999后取反一次,这样每5M刚好为1HZ Q<=32'd0;
_1HZ=~_1HZ;end else
Q<=Q+1'd1; end
endmodule
因篇幅问题不能全部显示,请点此查看更多更全内容