VHDL编码:10位十进制转换为BCD有可能吗?
我最近的任务是将10位十进制(因为10位的最大十进制数为1023)转换为16位BCD。当输入十进制数大于或等于1024时,错误波形将变高。整个模块当然连接到一个时钟。我不知道我怎么能在VHDL实现这个编码,但我有一些建议一些想法,我怎样才能使它发挥作用:
- 首先,我可以实现使用两个模块,其中第一输出块将用相同的时钟连接到第二个块。所述第一模块的输出为输入,其中误差等于1的二进制当十进制输入大于1023
- 第二个是只使用一个模块技术,其中输入的小数被直接转换成16位的BCD其中误差是一个如果输入十进制大于1023
任何人都可以帮助我如何使用VHDL编码十进制到bcd转换。有一点帮助是非常感谢。谢谢
好吧,我的同学弄清楚如何使用MOD功能进行编码的问题。下面的代码:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity dec_to_bcd is
Port (Ina : in STD_LOGIC_VECTOR (9 downto 0);
clk : in STD_LOGIC;
Outa : out STD_LOGIC_VECTOR (15 downto 0);
err : out STD_LOGIC);
end dec_to_bcd;
architecture Behavioral of dec_to_bcd is
begin
process (clk)
begin
if clk='1' and clk'event then
if (conv_integer(Ina) >= 1024) then
err <= '1';
else
Outa(15 downto 12) <= conv_std_logic_vector((conv_integer(Ina)/1000),4);
Outa(11 downto 8) <= conv_std_logic_vector((conv_integer(Ina)/100)MOD 10,4);
Outa(7 downto 4) <= conv_std_logic_vector((conv_integer(Ina)/10)MOD 10,4);
Outa(3 downto 0) <= conv_std_logic_vector((conv_integer(Ina))MOD 10,4);
end if;
end if;
end process;
end Behavioral;
由于我们的介绍在课堂上VHDL只使用模拟,那么我们不知道这是否是“合成的”。有关如何改进此代码的任何建议受到热烈欢迎。谢谢:)
不要使用IEEE.STD_LOGIC_ARITH.ALL;或使用IEEE.STD_LOGIC_UNSIGNED.ALL。你应该'使用ieee.numeric_std.all;',它给你使用signed和unsigned类型的算术,以及'std_logic_vector'和'integer'的强制转换和转换函数。 –
好的,谢谢你的提示。您的帮助非常感谢:) – c2s1
您可以使用Double dabble algorithm达到此目的。 我在博客中写了一个vhdl function,它基本上将8位二进制转换为12位BCD。您也可以对10位二进制数使用相同的概念。
function to_bcd (bin : std_logic_vector(7 downto 0)) return std_logic_vector is
variable i : integer:=0;
variable bcd : std_logic_vector(11 downto 0) := (others => '0');
variable bint : std_logic_vector(7 downto 0) := bin;
begin
for i in 0 to 7 loop -- repeating 8 times.
bcd(11 downto 1) := bcd(10 downto 0); --shifting the bits.
bcd(0) := bint(7);
bint(7 downto 1) := bint(6 downto 0);
bint(0) :='0';
if(i < 7 and bcd(3 downto 0) > "0100") then --add 3 if BCD digit is greater than 4.
bcd(3 downto 0) := bcd(3 downto 0) + "0011";
end if;
if(i < 7 and bcd(7 downto 4) > "0100") then --add 3 if BCD digit is greater than 4.
bcd(7 downto 4) := bcd(7 downto 4) + "0011";
end if;
if(i < 7 and bcd(11 downto 8) > "0100") then --add 3 if BCD digit is greater than 4.
bcd(11 downto 8) := bcd(11 downto 8) + "0011";
end if;
end loop;
return bcd;
end to_bcd;
该代码也是可合成的。
尽管它是可综合的,但您正在使用3个比较器和3个系列的添加,这对时序性能会产生相当大的影响。另外,它似乎你正在使用'std_logic_arith' JHBonarius
那么,我的同学想出了它会怎么做。他在部门使用MODULO功能。我会在这里上传代码,但是因为我们在课堂上介绍VHDL只使用模拟,所以我们不知道它是否是“可合成的”。 – c2s1
一个简单的搜索就会显示你几十个关于BCD转换器的问题。 –
一般作业问题,没有特定的VHDL语言问题,没有研究完成 – EML