注意:8位二进制可表示的十进制数为0~255
我这里用个位,十位,百位来表示输出的十进制数,为了方便都用了4位二进制数表示.
module BCD (IN,OUTHundred,OUTTen,OUTOne);
input [7:0]IN;
output [3:0] OUTHundred;
output [3:0] OUTTen;
output [3:0] OUTOne;
reg[3:0] OUTHundred;
reg[3:0] OUTTen;
reg[3:0] OUTOne;
integer i = 0;
always @(IN)
begin
OUTHundred = 4'd0;
OUTTen = IN[7:4];
OUTOne = IN[3:0];
if(OUTOne > 9)
begin
OUTOne = OUTOne + 6;
OUTTen = OUTTen + 1;
end
if(OUTTen > 9)
begin
OUTTen = OUTTen + 6;
OUTHundred = OUTHundred + 1;
end
end
endmodule