2016-04-30 364 views
0
library ieee; 
use ieee.std_logic_unsigned.all; 
use ieee.numeric_std.all; 
use ieee.std_logic_1164.all; 
entity DistanceCal is 
    port(timeIn : in integer; 
     Distance : out std_logic_vector(15 downto 0)); 
end DistanceCal; 

architecture behav of DistanceCal is 
    signal conv_int : std_logic_vector(to_unsigned(timeIn, conv_int'length)); 
begin 
    process(timeIn) 
    begin 
    conv_int <= std_logic_vector(to_unsigned((timeIn*340/2), conv_int'length)); 
    end process; 
    Distance <= conv_int; 
end behav; 

我需要將整數轉換爲二進制表示,但我不知道整數的值。我該怎麼辦?如何將整數轉換爲VHDL中的二進制表示?

+0

現在,我已經可以把它轉換。非常感謝您的建議。 –

回答

1

您聲明的信號conv_int無效。起初,您不能在右側的子類型指示中使用conv_int,因爲conv_int尚未定義。您可以使用其他信號(或對象),例如Distance,這是之前聲明的。而將你必須指定用todownto,而不僅僅是std_logic_vector的長度,例如範圍:

signal conv_int : std_logic_vector(to_unsigned(timeIn, Distance'length)-1); 

但這行不通要麼,因爲現在該區域未確立時限制,因爲timeIn不是一個常數。這意味着,您必須在「編譯」時間指定數組類型std_logic_vector的範圍。

因爲您稍後將conv_int指定爲Distance,因此conv_intDistance具有相同的範圍。此聲明有效:

signal conv_int : std_logic_vector(Distance'range); 

通過此更改,您的代碼將進行分析和詳細說明(編譯/合成)。現在你的整數在此線

conv_int <= std_logic_vector(to_unsigned((timeIn*340/2), conv_int'length)); 

將作爲「二進制」的轉換如下:整數表達式timeIn*340/2將在模擬時間/評價在運行時,則轉換爲unsigned而截斷二進制表示到conv_int'length位,最後將其轉換爲std_logic_vector。請注意,對於大於樓層(2 ** 16/170)= 101的timeIn值,截斷將會/可能導致意外的Distance


的代碼可以進一步改善:

  1. 應避免非標準Synopsys包std_logic_unsigned。請僅使用標準IEEE包numeric_std

  2. 您的過程將等同於編寫爲併發語句的單行conv_int <= ...。因爲變體將在timeIn更改(以及啓動後一次)時執行。

  3. 如果conv_int僅分配給輸出Distance,則此處不需要中間信號。

  4. 只要timeIn小於2 ** 31/170,乘以340/2將相當於乘以170。由於上述關於截斷的要求,情況就是這樣。

因此,你的架構可以簡化爲:

architecture behav of DistanceCal is 
begin 
    Distance <= std_logic_vector(to_unsigned(timeIn*170, Distance'length)); 
end behav; 
相關問題