2013年12月27日 星期五

Binary to BCD Conversion Algorithm

Binary to BCD Conversion Algorithm

162 converts to 1-6-2



Purpose:

Conversion of a binary number into separate binary numbers representing digits of the decimal number.
(this example is for 8-bits, other sizes follow the same pattern)

Algorithm:



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


Psuedo-Code:

PsuedoCode

Algorithm In Action:

Algorithm Table


BCD Conversion in Hardware:

Verilog:
Verilog Code



Binary to BCD conversion verilog code (9-bit)
module bin_2_bcd(B,P);
input [8:0] B;
output [10:0] P;
reg [10:0] P;
reg [19:0] z;
integer i;

always @(B)
  begin
    for(i = 0; i <= 19; i = i+1)
     z[i] = 0;
    z[11:3] = B;
    for(i = 0; i <= 5; i = i+1)
    begin
      if(z[12:9] > 4)    
         z[12:9] = z[12:9] + 3;
      
      if(z[16:13] > 4)    
         z[16:13] = z[16:13] + 3;
            
      z[19:1] = z[18:0];
        end
    P = z[19:9];        
  end             
endmodule

沒有留言:

張貼留言

ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中

  一個典型的 IoT(物聯網)與工業自動化系統整合(SCADA/機台連線) 的架構圖。它透過 Node-RED 作為核心資料網關,將前端 ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中。 1. 各模組功能拆解...