2015-04-19 31 views
0

我是新來的VHDL,我試圖做一個計數器,它接收來自輸入的值,並計數到給定值,然後輸出1;VHDL如何分配一個輸入向量值到一個整數信號

例如,輸入是一個4位向量 「1011」

我試圖設置一個整數信號=輸入= 1011在十進制= 11,那麼,如果B = A = 11輸出1,否則輸出0和b = b + 1

我知道我可以通過一系列的if語句來實現,但我想知道是否有更好的方法,如直接將輸入向量賦值爲整數信號?感謝任何能夠幫助的人!

+0

我認爲這需要一些澄清。所以你將一個'value'傳遞給一個模塊,它的'count'被初始化爲0,然後使用參考時鐘輸入遞增到'value',然後輸出一個邏輯1?何時重置? –

+0

您可能需要某種形式的if語句,因爲這將允許您使用寄存器定義順序行爲。 –

+0

非常感謝您的快速回復,是的,這就是我想要做的,我知道我可以通過if語句來實現,就像輸入=「0000」,然後a = 0一樣,但我在想的是任何更簡單的方法來這樣做? – tedhan

回答

1

這是未經測試的,但它是一般的體系結構,聽起來像是你之後。在VHDL中使用if語句是不錯的做法;他們有必要定義順序(而不是組合)的邏輯;你只需要明智的使用。

library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.NUMERIC_STD.ALL; 

entity Counter is port (
    enable: in std_logic; -- Used to increment the counter. (Active high.) 
    value: in std_logic_vector(0 to 3); 
    clk:  in std_logic; -- Used to clock the counter. 
    reset: in std_logic; -- Reset the counter. (Active high.) 
    output: out std_logic -- Generates a logic high once the count has been reached. 
); 
end Counter; 

architecture Behavioral of Counter is 

    signal count: unsigned(0 to 3); 

begin 
    process(clk,reset) 
    begin 
     -- If reset goes high, reset the count. 
     if reset='1' then 
      count <= "0000";    -- Reset the counter. 
      output <= '0';     -- Set the output low. 
     elsif(clk'event and clk='1') then -- If not reset, and the rising edge of the input clock... 
      if enable='1' then    -- If the counter is enabled... 
       if count=unsigned(value) then  -- If the count reached the input value... 
        output <= '1';   -- Set the output high. 
       else 
        count <= count + 1; -- Increment the counter. 
       end if; 
      end if; 
     end if; 
    end process; 
end Behavioral; 
+0

這比我第一次做得更好,當我嘗試創建一個整數信號時,它使得它太複雜了,然後我必須使用16條if語句來關聯輸入值和整數值,非常感謝!此外,我發現我們實際上可以使用整數類型作爲輸入,但我不確定這是否可以在合成時起作用,非常感謝您幫助我! – tedhan

+0

不用擔心,很高興我能幫到你。 –

+1

不建議使用std_logic_arith和std_logic_unsigned,請使用std_numeric。 – Paebbels

相關問題