2015-03-13 26 views
0

我正在使用VHDL的紅外解碼器,我知道IR 1位的寬度爲1.2毫秒,IR 0位爲0.6毫秒,起始位爲2.5毫秒。我正試圖製作一個計數器,它能夠接收50MHz時鐘並轉換爲十分之一毫秒。我怎樣才能做到這一點?你如何使vhdl計數器以十分之一毫秒計數?

entity counter is 
    Port (EN : in STD_LOGIC; 
      RESET : in STD_LOGIC; 
      CLK : in STD_LOGIC; 
      COUNT : out STD_LOGIC_VECTOR (4 downto 0)); 
end counter; 

architecture Behavioral of counter is 

constant max_count : integer := (2); 
begin 
    startCounter: process(EN, RESET, CLK) 
     variable cnt : integer := 0; 
     variable div_cnt : integer := 0; 
    begin 

     if (RESET = '1') then 
     cnt := 0; 
     div_cnt := 0; 
     elsif (EN = '1' and rising_edge(CLK)) then 
     if (cnt = max_count) then 
      cnt := 0; 
      div_cnt:= div_cnt + 1; 
     else 
      cnt := cnt + 1; 
     end if; 
     end if; 
     COUNT <= conv_std_logic_vector(cnt, 5); 
--  COUNT <= temp_count(16 downto 13); 

    end process startCounter; 
end Behavioral; 
+0

請參閱相關鏈接:http://electronics.stackexchange.com/questions/144800/design-up-counter-in-vhdl-using-generate-statement – Barnstokkr 2015-03-13 08:29:39

+0

我瞭解如何進行計數,但我需要幫助轉換從系統時鐘到十分之幾毫秒。 – chanceofthat 2015-03-13 09:07:36

+0

請參閱其他相關信息:http://stackoverflow.com/questions/19708301/making-a-clock-divider – Barnstokkr 2015-03-13 09:28:22

回答

1

由於你有一個50MHz的時鐘,並希望以產生0.1毫秒的脈衝時,可以使用在IEEE庫,math_real,計算的50 MHz的時鐘數來創建一個0.1毫秒的脈衝。這是一個代碼片段。

library ieee; 
use  ieee.math_real.all; 

-- omitting for clarity... 

-- generate one clk cycle pulse with period of 0.1 msec 
gen_0p1mspulse_p : process(Clk) 
    constant CLK_PERIOD  : real := 1/50e6; 
    constant PULSE_PERIOD : real := 0.1e-3; 
    constant MAX_CNT  : integer := INTEGER(PULSE_PERIOD/CLK_PERIOD); 
    variable cnt   : integer range 0 to MAX_CNT-1 := 0; 
begin 
    if rising_edge(Clk) then 
     if reset = '1' then 
      cnt := 0; 
      pulse_0p1msec <= '0';    
     else 
      pulse_0p1msec <= '0'; -- default value 
      if cnt < MAX_CNT-1 then 
       cnt := cnt + 1; 
      else 
       cnt := 0; 
       pulse_0p1msec <= '1'; 
      end if; 
     end if; 
    end if; 
end process; 

-- logic using 0.1 msec pulse 
your_logic_p : process(Clk) 
begin 
    if rising_edge(Clk) then 
     if reset = '1' then 
      your_cnt := 0; 
     else 
      if pulse_0p1msec = '1' then 
       -- insert your logic here 
      end if; 
     end if;   
    end if; 
end process; 

我喜歡分割我的VHDL過程,使它們短。我也更喜歡使用同步重置,因爲它們可以爲Xilinx FPGA提供更少的硬件,並且可以以更高的時鐘速率運行。希望解決您的問題。