微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > vhdl中conv_unsigned函数怎么使用呢?

vhdl中conv_unsigned函数怎么使用呢?

时间:10-02 整理:3721RD 点击:
我定义的cnt变量是unsigned,但是我想它根据n的变化自动变化长度,在复位的时候赋初始值1.怎么做呢?
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity seris_gen is
    generic(n : NATURAL := 3);
    port(clk  : in  std_logic;
         reset: in  std_logic;
         q    : out std_logic);
end seris_gen;
architecture behave of seris_gen is
    signal q_n  : std_logic;
    signal count: std_logic_vector(n-1 downto 0);
begin
    p0: process (clk, reset, count) is
    variable cnt : unsigned(n-1 downto 0);
    begin
if reset = '1' then
--     cnt := (others => '0');  -- or use sxt
--     cnt := "001";  -- or use sxt
--     cnt := conv_unsigned(1,n);
     cnt := conv_unsigned("001",3);
elsif rising_edge(clk) then
     cnt := cnt + 1;
    end if;
    count <= std_logic_vector(cnt);
    end process p0;
    p1: process(clk, reset, count, q_n) is
    begin
        if (reset = '1') then
            q_n <= '1';
        elsif rising_edge(clk) then
            if (count(2) and (not count(0))) = '1' then
                q_n <= '0';
     else
  q_n <= '1';
            end if;
        end if;
    end process p1;
    q <= q_n;
end behave;

已改,你看看。 写VHDL,尽量不要用variable。
----------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity seris_gen is
    generic(n : NATURAL := 3);
    port(clk  : in  std_logic;
         reset: in  std_logic;
         q    : out std_logic);
end seris_gen;
architecture behave of seris_gen is
    signal q_n  : std_logic;
    signal count: std_logic_vector(n-1 downto 0);
begin
    p0: process (clk, reset, count) is
    begin
        if reset = '1' then
              count   <= conv_std_logic_vector(1,n);
        elsif rising_edge(clk) then
              count <= count + 1;
        end if;
    end process p0;
       
    p1: process(clk, reset) is
    begin
        if (reset = '1') then
            q_n <= '1';
        elsif rising_edge(clk) then
            if (count(2) and (not count(0))) = '1' then
                q_n <= '0';
            else
                q_n <= '1';
            end if;
        end if;
    end process p1;
       
    q <= q_n;
end behave;

谢谢你啊,受益匪浅。我现在还弄不清几种类型该用哪一种

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top