首页 > 解决方案 > 7段显示器basys3怎么用2位?

问题描述

我知道一点VHDL。

我的目标是有一个 4 位矢量输入并将输出发送到 base4 数字的 7 段显示器。

例如 1111->33、1001->21

我正在努力将 VHDL 中的 base2 数字转换为 base4 数字。如前所述,当我将 base2 转换为 base4 后,我想将其发送到 7 段显示器,请您告诉我该怎么做?

标签: vhdlfpga

解决方案


二进制很容易转换为任何基于 2 的幂的数字系统,如 4 或 8 或 16。只需将位按. 分组即可log2(bigger base)。在 base 4 的情况下,它们按以下方式分组log2(4)=2

00->0
01->1
10->2
11->3
1110->32

您只需通过连接线X将长度向量拆分为长度X/2向量:2

library IEEE;
use IEEE.std_logic_1164.all;

-- 8 binary bit number to 4 base4 numbers
-- binary - is input number
-- d1 is the most significant base-4 digit
-- ...
-- d4 is the least significant base-4 digit

entity BinToBase4 is
port(binary : in STD_LOGIC_VECTOR(7 downto 0);
     d1 : out STD_LOGIC_VECTOR(1 downto 0);
     d2 : out STD_LOGIC_VECTOR(1 downto 0);
     d3 : out STD_LOGIC_VECTOR(1 downto 0);
     d4 : out STD_LOGIC_VECTOR(1 downto 0)
    );
end BinTobase4;

architecture rtl of BinToBase4 is
begin
    d1 <= binary(7 downto 6);
    d2 <= binary(5 downto 4);
    d3 <= binary(3 downto 2);
    d4 <= binary(1 downto 0);
end rtl;

测试台:

library ieee;
use ieee.std_logic_1164.all;

entity testbench is
end testbench; 

architecture tb of testbench is
component BinToBase4 is
port(
    binary : in STD_LOGIC_VECTOR(7 downto 0);
    d1 : out STD_LOGIC_VECTOR(1 downto 0);
    d2 : out STD_LOGIC_VECTOR(1 downto 0);
    d3 : out STD_LOGIC_VECTOR(1 downto 0);
    d4 : out STD_LOGIC_VECTOR(1 downto 0)
);
end component;

    signal num : std_logic_vector(7 downto 0);
    signal d1 : std_logic_vector(1 downto 0);
    signal d2 : std_logic_vector(1 downto 0);
    signal d3 : std_logic_vector(1 downto 0);
    signal d4 : std_logic_vector(1 downto 0);
begin
    b4: BinToBase4 port map(num,d1,d2,d3,d4);
     process
    begin
      num <= "10110001";
      wait for 1 ns;

      assert(d1="10") report "Fail 10" severity error;
      assert(d2="11") report "Fail 11" severity error;
      assert(d3="00") report "Fail 00" severity error;
      assert(d4="01") report "Fail 01" severity error;
      wait;
    end process;
end tb;

您可能希望将d1, d2, d3,扩展d4 为四位以将它们直接连接到 7 段显示器。使用串联&

d1 <= "00" & binary(7 downto 6);
d2 <= "00" & binary(5 downto 4);
d3 <= "00" & binary(3 downto 2);
d4 <= "00" & binary(1 downto 0);

只是不要忘记相应地调整矢量大小。

即 dx 会变成...

signal dx : std_logic_vector(3 downto 0);

推荐阅读