串並轉換

1.並轉串

module b2c(clk,ain,rst,bout,load,ready);//並轉串
 input clk,rst,load;
 input [7:0] ain;
 output reg bout;
 output reg ready;
 
 reg [7:0] temp;
 always @(posedge clk or posedge rst)
     begin
	      if(rst)
		     begin
			      temp<=8'dx;
				  bout<=1'bx;
				  ready=1'b1;//復位時可以接收輸入數據
			 end
	      else
		     begin
			      if(load && ready)//置數
				    begin
				      temp<=ain;
					end
                  if(temp[7]||!temp[7])//temp[7]有數
					begin
				      bout<=temp[7];//輸出由temp[7]決定,每次左移1位,並且在最低位補x
					  temp<={temp[6:0],1'bx};
					  ready<=1'b0;
				    end	
                  else
                    begin
					  ready<=1'b1;
					  bout<=1'bx;
                    end					
			 end    
	 end
endmodule

 另一種計數器方法:

module b2c(clk,din,rst,ordy,bout);
 input clk,rst;
 input [3:0] din;
 output  bout;
 output reg ordy;
 
 reg [1:0] count;
 reg [3:0] temp;
 reg ready;
 always @(posedge clk)
   begin
        if(rst)
		  begin
		       count <= 0;
			   temp <= 0;
			  // bout <= 0;
			   ordy <= 0;
			   ready <= 1;//開始置數
		  end
		else 
		  begin
		      if(ready)
			     begin
				    temp <= din;//置數,同時計數歸零
					ready <= 0;
					count <= 0;
				 end
		      else
			     begin
				    temp <= {temp[2:0],1'bx};//左移
					count <= count + 1'b1;
					  if(count < 3)
					     begin
						    ordy <= 1;
						 end
				      else
					     begin
						    ordy <= 0;
							ready <= 1;
						 end
				 end
		  end
   end
 assign bout = temp[3];
endmodule

 

2.串轉並

module b2c(clk,ain,rst,bout,load,ready);//並轉串
 input clk,rst,load;
 input [7:0] ain;
 output reg bout;
 output reg ready;
 
 reg [7:0] temp;
 always @(posedge clk or posedge rst)
     begin
	      if(rst)
		     begin
			      temp<=8'dx;
				  bout<=1'bx;
				  ready=1'b1;//復位時可以接收輸入數據
			 end
	      else
		     begin
			      if(load && ready)//置數
				    begin
				      temp<=ain;
					end
                  if(temp[7]||!temp[7])//temp[7]有數
					begin
				      bout<=temp[7];//輸出由temp[7]決定,每次左移1位,並且在最低位補x
					  temp<={temp[6:0],1'bx};
					  ready<=1'b0;
				    end	
                  else
                    begin
					  ready<=1'b1;
					  bout<=1'bx;
                    end					
			 end    
	 end
endmodule

另一種計數器方法:

module c2b(clk,ain,rst,bout,ready);//串轉並
 input clk,rst;
 input ain;
 output [3:0] bout;
 output reg ready;
 
 reg [3:0] temp;
 reg [1:0] count;
 always @(posedge clk)
   begin
        if(rst)
		  begin
		       temp <= 4'dx;
			   count <= 0;
			   ready <= 0;
		  end
		else 
		  begin
		       temp <= {temp[2:0],ain};//左移
			   count <= count + 1'd1;
			   if(count == 3)
			      begin
				       ready <= 1;
				  end
			   else
			      begin
				       ready <= 0;
				  end
		  end
   end
 assign bout = temp;
endmodule

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章