【Verilog基础】14.编码器和译码器

BCD 七段数码管显示译码器

module decode4_7(decodeout,indec);
    output[6:0] decodeout;
    input[3:0] indec;
    reg[6:0] decodeout;
    always @(indec)
    begin
 case(indec) //用 case 语句进行译码
    4'd0:decodeout=7'b1111110;
    4'd1:decodeout=7'b0110000;
    4'd2:decodeout=7'b1101101;
    4'd3:decodeout=7'b1111001;
    4'd4:decodeout=7'b0110011;
    4'd5:decodeout=7'b1011011;
    4'd6:decodeout=7'b1011111;
    4'd7:decodeout=7'b1110000;
    4'd8:decodeout=7'b1111111;
    4'd9:decodeout=7'b1111011;
    default: decodeout=7'bx;
 endcase
end
endmodule

8-3 编码器

module bianma8_3(i,y);
    input[7:0] i; //信号输入端
    output[2:0] y; //3 位二进制编码输出端
    reg[2:0] y;
always @(i)
    begin
     case(i[7:0])
     8'b00000001:y[2:0]=3'b000;
     8'b00000010:y[2:0]=3'b001;
     8'b00000100:y[2:0]=3'b010;
     8'b00001000:y[2:0]=3'b011;
     8'b00010000:y[2:0]=3'b100;
     8'b00100000:y[2:0]=3'b101;
     8'b01000000:y[2:0]=3'b110;
     8'b10000000:y[2:0]=3'b111;
     default:y[2:0]=3'b000;
     endcase
    end
endmodule

8-3 编码器(优先编码)

module yxbianma8_3(y,eo,gs,i,ei);
    input[7:0] i; //信号输入端
    input ei; //输入使能端
    output[2:0] y; //3 位二进制编码输出端
    output eo,gs; //输出使能端 eo 和优先标志端 gs
    reg[2:0] y;
    reg eo,gs;
always @(i,ei)
    begin
    if(ei==1)
      begin
         y[2:0]<=3'b111;
         gs<=1;
         eo<=1;
      end
     else
      begin
         if(i[7]==0)
         begin 
             y[2:0]<=3'b000;
             gs<=0;
             eo<=1;
         end
     else if(i[6]==0)
      begin
         y[2:0]<=3'b001;
         gs<=0;
         eo<=1;
      end
     else if(i[5]==0)
      begin
         y[2:0]<=3'b010;
         gs<=0;
         eo<=1;
      end
     else if(i[4]==0)
       begin
         y[2:0]<=3'b011;
         gs<=0;
         eo<=1;
       end
     else if(i[3]==0)
       begin
         y[2:0]<=3'b100;
         gs<=0;
         eo<=1;
       end
     else if(i[2]==0)
       begin
          y[2:0]<=3'b101;
          gs<=0;
         eo<=1;
       end
     else if(i[1]==0)
       begin
         y[2:0]<=3'b110;
         gs<=0;
         eo<=1;
         end
     else if(i[0]==0)
       begin
         y[2:0]<=3'b111;
         gs<=0;
         eo<=1;
       end
     else if(i[7:0]=='b11111111)
       begin
         y[2:0]<=3'b111;
         gs<=1;
         eo<=0;
       end
     end
    end
endmodule

3-8 编码器

module decoder3_8(y,a,g1,g2,g3);
    output[7:0] y; //编码输出端
    input[2:0] a; //3 位二进制编码输入端
    input g1,g2,g3; //编码输出端
    reg[7:0] y;

always @(a or g1 or g2 or g3)
    begin
     if(g1==0) y=8'b11111111;
     else if(g2==1) y=8'b11111111;
     else if(g3==1) y=8'b11111111;
     else

   case(a[2:0])
 3'b000:y[7:0]=8'b11111110;
 3'b001:y[7:0]=8'b11111101;
 3'b010:y[7:0]=8'b11111011;
 3'b011:y[7:0]=8'b11110111;
 3'b100:y[7:0]=8'b11101111;
 3'b101:y[7:0]=8'b11011111;
 3'b110:y[7:0]=8'b10111111;
 3'b111:y[7:0]=8'b01111111;
 default:y[7:0]=8'b11111111;
 endcase

end
endmodule

 3-8 译码器


module decoder_38(out,in);
    output[7:0] out;
    input[2:0] in;
    reg[7:0] out;
    always @(in)
    begin
    case(in)
     3'd0: out=8'b11111110;
     3'd1: out=8'b11111101;
     3'd2: out=8'b11111011;
     3'd3: out=8'b11110111;
     3'd4: out=8'b11101111;
     3'd5: out=8'b11011111;
     3'd6: out=8'b10111111;
     3'd7: out=8'b01111111;
    endcase
    end
endmodule

8-3 优先编码器

module encoder8_3(none_on,outcode,a,b,c,d,e,f,g,h);
    output none_on;
    output[2:0] outcode;
    input a,b,c,d,e,f,g,h;
    reg[3:0] outtemp;
    assign {none_on,outcode}=outtemp;
always @(a or b or c or d or e or f or g or h)
    begin
    if(h) outtemp=4'b0111;
    else if(g) outtemp=4'b0110;
    else if(f) outtemp=4'b0101;
    else if(e) outtemp=4'b0100;
    else if(d) outtemp=4'b0011;
    else if(c) outtemp=4'b0010;
    else if(b) outtemp=4'b0001;
    else if(a) outtemp=4'b0000;
    else outtemp=4'b1000;
    end
endmodule

奇偶校验位产生器

module parity(even_bit,odd_bit,input_bus);
    output even_bit,odd_bit;
    input[7:0] input_bus;

    assign odd_bit = ^ input_bus; //产生奇校验位
    assign even_bit = ~odd_bit; //产生偶校验位
endmodule
  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值