【问题标题】:VHDL Coding: 10 bit Decimal conversion to BCD is it possible?VHDL编码:可以将10位十进制转换为BCD吗?
【发布时间】:2017-08-23 01:28:15
【问题描述】:

美好的一天,

我最近的任务是将 10 位十进制数(因为 10 位的最大十进制数是 1023)转换为 16 位 BCD。当输入小数大于等于 1024 时,误差波形会变高。整个模块当然连接到一个时钟。我不知道如何在 VHDL 编码中实现这一点,但我有一些关于如何使其工作的建议:

  • 首先,我可以实现两个模块的使用,其中第一个模块的输出将连接到具有相同时钟的第二个模块。第一个块的输出是输入的二进制,当十进制输入大于 1023 时,误差等于 1。

  • 第二种是只使用一个模块技术,将输入的十进制直接转换为 16 位 BCD,如果输入的十进制大于 1023,则错误为 1。

任何人都可以帮助我了解如何使用 VHDL 将小数点编码为 bcd 转换。非常感谢您的帮助。谢谢

【问题讨论】:

  • 好吧,我的同学想知道它会怎么做。他使用带除法的 MODULO 函数。我会在这里上传代码,但是由于我们在课堂上对VHDL的介绍只使用了模拟,所以我们不知道它是否“可合成”。
  • 一个简单的搜索就会显示许多关于 BCD 转换器的现有问题。
  • 一般作业问题,没有特定的 VHDL 语言问题,没有做过研究
  • 很抱歉,这是不可能的。为什么要费心去解决这个问题呢?

标签: decimal vhdl xilinx-ise bcd


【解决方案1】:

好吧,我的同学解决了如何使用 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的介绍只使用了仿真,所以我们不知道它是否“可综合”。热烈欢迎任何有关如何改进此代码的建议。谢谢:)

【讨论】:

  • 不要use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL。你应该use ieee.numeric_std.all;,它为你提供了使用有符号和无符号类型的算术,以及std_logic_vectorinteger 的强制转换和转换函数。
  • 好的,谢谢你的提示。非常感谢您的帮助:)
【解决方案2】:

您可以为此目的使用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_arithunsigned 类型。请修正你的缩进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多