2017-08-23 90 views
-3

好日子,VHDL編碼:10位十進制轉換爲BCD有可能嗎?

我最近的任務是將10位十進制(因爲10位的最大十進制數爲1023)轉換爲16位BCD。當輸入十進制數大於或等於1024時,錯誤波形將變高。整個模塊當然連接到一個時鐘。我不知道我怎麼能在VHDL實現這個編碼,但我有一些建議一些想法,我怎樣才能使它發揮作用:

  • 首先,我可以實現使用兩個模塊,其中第一輸出塊將用相同的時鐘連接到第二個塊。所述第一模塊的輸出爲輸入,其中誤差等於1的二進制當十進制輸入大於1023

Here's the photo of the two module

  • 第二個是隻使用一個模塊技術,其中輸入的小數被直接轉換成16位的BCD其中誤差是一個如果輸入十進制大於1023

Here's the photo of one module

任何人都可以幫助我如何使用VHDL編碼十進制到bcd轉換。有一點幫助是非常感謝。謝謝

+0

那麼,我的同學想出了它會怎麼做。他在部門使用MODULO功能。我會在這裏上傳代碼,但是因爲我們在課堂上介紹VHDL只使用模擬,所以我們不知道它是否是「可合成的」。 – c2s1

+0

一個簡單的搜索就會顯示你幾十個關於BCD轉換器的問題。 –

+3

一般作業問題,沒有特定的VHDL語言問題,沒有研究完成 – EML

回答

-1

好吧,我的同學弄清楚如何使用MOD功能進行編碼的問題。下面的代碼:

library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.STD_LOGIC_ARITH.ALL; 
use IEEE.STD_LOGIC_UNSIGNED.ALL; 



entity dec_to_bcd is 
    Port (Ina : in STD_LOGIC_VECTOR (9 downto 0); 
      clk : in STD_LOGIC; 
      Outa : out STD_LOGIC_VECTOR (15 downto 0); 
      err : out STD_LOGIC); 
end dec_to_bcd; 

architecture Behavioral of dec_to_bcd is 

begin 
process (clk) 
begin 
    if clk='1' and clk'event then 
     if (conv_integer(Ina) >= 1024) then 
      err <= '1'; 
     else 
      Outa(15 downto 12) <= conv_std_logic_vector((conv_integer(Ina)/1000),4); 
      Outa(11 downto 8) <= conv_std_logic_vector((conv_integer(Ina)/100)MOD 10,4); 
      Outa(7 downto 4) <= conv_std_logic_vector((conv_integer(Ina)/10)MOD 10,4); 
      Outa(3 downto 0) <= conv_std_logic_vector((conv_integer(Ina))MOD 10,4); 
     end if; 

end if; 
end process; 
end Behavioral; 

由於我們的介紹在課堂上VHDL只使用模擬,那麼我們不知道這是否是「合成的」。有關如何改進此代碼的任何建議受到熱烈歡迎。謝謝:)

+2

不要使用IEEE.STD_LOGIC_ARITH.ALL;或使用IEEE.STD_LOGIC_UNSIGNED.ALL。你應該'使用ieee.numeric_std.all;',它給你使用signed和unsigned類型的算術,以及'std_logic_vector'和'integer'的強制轉換和轉換函數。 –

+0

好的,謝謝你的提示。您的幫助非常感謝:) – c2s1

-1

您可以使用Double dabble algorithm達到此目的。 我在博客中寫了一個vhdl function,它基本上將8位二進制轉換爲12位BCD。您也可以對10位二進制數使用相同的概念。

function to_bcd (bin : std_logic_vector(7 downto 0)) return std_logic_vector is 
variable i : integer:=0; 
variable bcd : std_logic_vector(11 downto 0) := (others => '0'); 
variable bint : std_logic_vector(7 downto 0) := bin; 

begin 
for i in 0 to 7 loop -- repeating 8 times. 
    bcd(11 downto 1) := bcd(10 downto 0); --shifting the bits. 
    bcd(0) := bint(7); 
    bint(7 downto 1) := bint(6 downto 0); 
    bint(0) :='0'; 


    if(i < 7 and bcd(3 downto 0) > "0100") then --add 3 if BCD digit is greater than 4. 
    bcd(3 downto 0) := bcd(3 downto 0) + "0011"; 
    end if; 

    if(i < 7 and bcd(7 downto 4) > "0100") then --add 3 if BCD digit is greater than 4. 
    bcd(7 downto 4) := bcd(7 downto 4) + "0011"; 
    end if; 

    if(i < 7 and bcd(11 downto 8) > "0100") then --add 3 if BCD digit is greater than 4. 
    bcd(11 downto 8) := bcd(11 downto 8) + "0011"; 
    end if; 

end loop; 
return bcd; 
end to_bcd; 

該代碼也是可合成的。

+0

儘管它是可綜合的,但您正在使用3個比較器和3個系列的添加,這對時序性能會產生相當大的影響。另外,它似乎你正在使用'std_logic_arith' < - 不。使用'unsigned'類型。請修理您的縮進。 – JHBonarius

相關問題