【问题标题】:Convertidor Binary to BCD将二进制转换为 BCD
【发布时间】:2015-03-11 21:10:28
【问题描述】:

因为没有存储在 BCD1 num_bin me 中?这将正确的语法?

功能说明

操作必须如下:

  1. 激活复位将复位所有内部寄存器,并挂起启动电路 转换。它还将结束“0”。
  2. 当你开始 = '1' 转换开始。第一步是将值存储在那里 在内部寄存器中的 num_bin 中。
  3. 稍后将在第二条记录上滚动此寄存器的位 包含 BCD 格式的数字(在我们的例子中,因为我们有四位注册它是 16 位)。该操作与时钟的上升沿同步。

代码:

entity bin2bcd is
  Port ( clk : in  STD_LOGIC;
         reset : in  STD_LOGIC;
         inicio : in  STD_LOGIC;
         num_bin : in  STD_LOGIC_VECTOR (12 downto 0);
         und : out  STD_LOGIC_VECTOR (3 downto 0);
         dec : out  STD_LOGIC_VECTOR (3 downto 0);
         cen : out  STD_LOGIC_VECTOR (3 downto 0);
         mil : out  STD_LOGIC_VECTOR (3 downto 0);
         fin : out  STD_LOGIC);
end bin2bcd;

architecture Behavioral of bin2bcd is
  signal bcd1: std_logic_vector (12 downto 0);  
  signal bcd2: std_logic_vector (15 downto 0);

begin

  P1: process(reset,clk)   
  begin

    if reset = '1' then
      --fin <= '0';
      bcd1 <= (others => '0');   -- registres to 0
      bcd2 <= (others => '0');   -- registres to 0

    elsif rising_edge(clk) then


      if inicio = '1' then  
        bcd1 <= num_bin; 


        if bcd2(3 downto 0) > "0100" then    -- if >4
          bcd2(3 downto 0) <= bcd2(3 downto 0) or "0011";
        end if;

        if bcd2(7 downto 4) > "0100" then -- if >4
          bcd2(7 downto 4) <= bcd2(7 downto 4) or "0011";
        end if;

        if bcd2(11 downto 8) > "100" then   -- if >4
          bcd2(11 downto 8) <= bcd2(11 downto 8) or "0011";
        end if;

        if bcd2(15 downto 12) > "0100" then      -- if >4
          bcd2(15 downto 12) <= bcd2(15 downto 12) or "0011";
        end if;

        for i in 0 to 12 loop
          bcd2 <= bcd2(14 downto 0) & num_bin(12);
          bcd1 <= bcd1(11 downto 0) & '0';

          --fin <= '1';
        end loop;

        und <= bcd2 (3 downto 0);   -- unidades
        dec <= bcd2 (7 downto 4);   -- decenas
        cen <= bcd2 (11 downto 8);  -- centenas
        mil <= bcd2 (15 downto 12); -- millares


      end if;
    end if;
  end process P1;
end Behavioral;

【问题讨论】:

  • 这毫无意义。你有什么问题?
  • 我遇到的问题是当你假装test_bench我num_bin值没有存储在bcd1中,也让我很好地转换并保存在bcd2中
  • 您的措辞不是很清楚,但我想我看到了您遇到的问题。

标签: vhdl


【解决方案1】:

我遇到的问题是当你假装 test_bench 时我 num_bin 的值没有存储在 bcd1 中,也让我很好地转换并将其保存在 bcd2 中

【讨论】:

    【解决方案2】:

    问题在于您的 for 循环,以及您在该过程的早期分配 bcd1 的事实。您似乎打算创建一个移位寄存器,但这不是您编写的代码实际所做的。

    在 VHDL 中,进程中分配的信号不会立即更改值 - 分配安排在进程结束时(或任何 wait,在具有 wait 语句的进程中,尽管这些是不可合成的)。

    因此,如果您的进程在同一个进程中对同一个信号“执行”多个赋值,那么后面的赋值会覆盖之前的赋值,并且只有最后一个赋值才是真正生效的。所以,既然你有:

    bcd1 <= num_bin;
    
    ...
    
    for i in 0 to 12 loop
      bcd2 <= bcd2(14 downto 0) & num_bin(12);
      bcd1 <= bcd1(11 downto 0) & '0';
    end loop;
    

    初始分配将被 for 循环中的分配覆盖,因此您永远不会使用任何值加载 bcd1。

    您还有第二个问题,即您的 for 循环不会按您的意愿移动寄存器。 每次进程执行时,时钟进程中的 for 循环都会展开,因此:

    for i in 0 to 12 loop
      bcd1 <= bcd1(11 downto 0) & '0';
    end loop;
    

    相当于:

    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    bcd1 <= bcd1(11 downto 0) & '0';
    

    但是,正如我之前所说,由于只有最后一个分配是影响信号的分配,所以您只需移动一次。

    你可能打算这样做:

          if inicio = '1' then  
            bcd1 <= num_bin; 
          else
            -- rest of your code goes here
    

    else 是必需的,以便bcd1 的初始加载不会被后面的逻辑覆盖。您还需要修复您的 for 循环。您的算法可能存在多个其他问题,但这是您需要处理的第一个问题。

    【讨论】:

    • 如果这样做,我仍然无法检测到大于 5 的数字何时会加 3。
    • 为什么检测不到这个?究竟是什么不工作?另外,代码中的 OR 不一定要加 3。如果要真正加 3,则需要使用加法运算。
    • 什么是当我在任何位置 BCD2 的值大于 5 时不加我 3 另一方面,如果我删除并放置 OR + 会给我错误
    • 程序中的数字 Bynary 1101100001,BCD 为:0-1-11-1(错误)
    • 因为"0101" 是5,大于4,但"0101" or "0011" 是"0111",即7。7 不是5 + 3。如果您在进行数字运算时需要帮助,那就是一个单独的问题。您问为什么 bcd1 没有加载,原因是您执行信号分配的方式。
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2015-01-21
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多