2012年11月1日 星期四

P8- 16bit Binary change into BCD Code 0000-65535 適用於DE2-70


P8-  16bit Binary change into BCD Code 0000-65535  適用於DE2-70


Binary to BCD Conversion Algorithm






module _16bit_BIN2BCD (SW, LEDR, LEDG , CLOCK_27 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 ,HEX4 );

/*
Algorithm:

If any column (10000'S, 1000'S ,100's, 10's, 1's, etc.) is 5 or greater, add 3 to that column.
Shift all #'s to the left 1 position.
If 8 shifts have been performed, it's done! Evaluate each column for the BCD values.
Go to step 1.

*/
input  [17:0] SW; // toggle switches
input  [3:0] KEY;     // Push bottom
input  CLOCK_27; //Clock 27MHz , 50Mhz

output [17:0] LEDR; // red  LEDS   
  output [8:0] LEDG; // green LEDs
    
    output [6:0] HEX0,HEX1,HEX2,HEX3 ,HEX4; //7-segment display

//set original program input , output 

wire [15:0] Binary ;
reg  [3:0] ten_thousand=4'd0;
reg  [3:0] thousand=4'd0;
reg  [3:0] hundred=4'd0;
reg  [3:0] tens=4'd0;
reg  [3:0] ones=4'd0;

wire [7:0] segout0;   //HEX 0
    wire [7:0] segout1;   //HEX 1
wire [7:0] segout2;   //HEX 2
    wire [7:0] segout3;   //HEX 3
wire [7:0] segout4;   //HEX 4
  

assign Binary = SW[15:0];
assign LEDR[15:0]=SW[15:0];

integer i;
always @ (Binary)
begin
 //set 4'd0 
 ten_thousand=4'd0;
 thousand=4'd0;
 hundred=4'd0;
 tens=4'd0;
 ones=4'd0;
   

for (i=15;i>=0;i=i-1)
 begin
  if (ten_thousand>=5)
      ten_thousand = ten_thousand +3;
      
  if (thousand>=5)
      thousand = thousand +3;
   
  if (hundred>=5)
      hundred = hundred +3;
  
  if  (tens>=5)
      tens = tens +3;  
   
  if  (ones>=5)
      ones = ones +3; 
      
  // shift left 1 bit
     ten_thousand = ten_thousand <<1;
     ten_thousand[0]= thousand[3];
     
     thousand = thousand <<1;
     thousand[0]= hundred[3];
     
     hundred = hundred <<1;
     hundred[0]= tens[3];
     
     tens = tens<<1;
     tens[0]= ones[3];
     
     ones = ones<<1;
     ones[0]= Binary[i]; 
     
  
 end


end

_7seg UUT0(.hex(ones),
               .seg(segout0));
    
    _7seg UUT1(.hex(tens),
               .seg(segout1));
    
    _7seg UUT2(.hex(hundred),
               .seg(segout2));
    
    _7seg UUT3(.hex(thousand),
               .seg(segout3));
    
    _7seg UUT4(.hex(ten_thousand),
               .seg(segout4));           
                      
    assign HEX0=segout0[6:0];
    assign HEX1=segout1[6:0];
assign HEX2=segout2[6:0];
    assign HEX3=segout3[6:0];
assign HEX4=segout4[6:0];


endmodule

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...