2015-03-13 74 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提供更少的硬件,并且可以以更高的时钟速率运行。希望解决您的问题。