|  | 
 
| 
`timescale 10ns/10ns
×
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册  module data_conv(clk,dat,units,tens,hundreds,thousands);
 
 input  clk;
 input[13:0] dat; //二进制输入数据
 output[3:0] units;
 output[3:0] tens;
 output[3:0] hundreds;
 output[3:0]        thousands;
 
 reg[3:0]  units_r,tens_r,hundreds_r,thousands_r;   //BCD数据输出寄存器
 reg[13:0] dat_r;
 reg[15:0] temp; //中间寄存器
 
 integer i;
 assign units =  units_r;
 assign tens =  tens_r;
 assign hundreds = hundreds_r;
 assign thousands = thousands_r;
 initial temp=16'b0;                //这个地方是该写在for里面还是外面???
 
 always @(posedge clk)
 begin
 dat_r = dat;
 for(i = 0;i < 15;i = i + 1) //循环15次,注意不是15次,因为第15次不需要修正
 begin
 
 temp = {temp[14:0],dat_r[13]}; //左移一位
 
 if(temp[3:0] > 4'd4) //大于4,加三
 temp[3:0] = temp[3:0]+4'd3;
 if(temp[7:4] > 4'd4) //大于4,加三
 temp[7:4] = temp[7:4]+4'd3;
 if(temp[11:8] > 4'd4) //大于4,加三
 temp[11:8] = temp[11:8]+4'd3;
 if(temp[15:12] > 4'd4) //大于4,加三
 temp[15:12] = temp[15:12]+4'd3;
 
 dat_r=dat_r<<1;  //最高变为原来dat_r的第14位
 {thousands_r,hundreds_r,tens_r,units_r}={temp[14:0],dat[0]};//最后一次(第16次)不用修正
 end
 end
 
 
 endmodule
 
 
 
 这是一个二进制数转十进制BCD码的代码,我想实现0~9999的二进制数转换成BCD码,输入是14位的(输出肯定是16位),然后我随便给dat附了几个值,仿真了一下,但是出得结果不正确啊,难道是for语句出了问题,但是实在看不出来呢,还请高手们给以作答,谢谢。
 加注:         initial temp=16'b0;如上红题字该写在for里面还是外面啊,结果好像也不一样啊,但是我感觉应该写在外面呢,可是结果更不对了,每当上升沿的时候个十百千四个数就发生变化了
 | 
 |