【发布时间】:2014-11-19 14:26:06
【问题描述】:
我为此花费了无数个小时,我终于决定我真的需要一些帮助..所以我来了。
基本上我正在做的是从 ADC 获取 8 位输入,然后将此值转换为 BCD 以在七段板上进行 dsiplay。所以这是我到目前为止的代码:
Library IEEE ;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
use IEEE.std_logic_unsigned.all;
entity Voltage_LUT is
Port ( scaled_value : in unsigned(7 downto 0);
true_value : out STD_LOGIC_VECTOR (15 downto 0)
);
end Voltage_LUT;
architecture Behavioral of Voltage_LUT is
function divide (a : UNSIGNED; b : UNSIGNED) return unsigned is
variable a1 : unsigned(a'length-1 downto 0):=a;
variable b1 : unsigned(b'length-1 downto 0):=b;
variable p1 : unsigned(b'length downto 0):= (others => '0');
variable i : integer:=0;
begin
for i in 0 to b'length-1 loop
p1(b'length-1 downto 1) := p1(b'length-2 downto 0);
p1(0) := a1(a'length-1);
a1(a'length-1 downto 1) := a1(a'length-2 downto 0);
p1 := p1-b1;
if(p1(b'length-1) ='1') then
a1(0) :='0';
p1 := p1+b1;
else
a1(0) :='1';
end if;
end loop;
return a1;
end divide;
signal adj: unsigned(7 downto -2); --adjusted to max 90
signal max_value: unsigned(7 downto 0):= "11111111" ; --adjusted to max 90
signal MSB_int: integer; -- integer form of MSB
signal LSB_int: integer; --integer form of LSB
signal adj2: unsigned(15 downto 0); --converted from adjusted integer to binary
signal LSB: STD_LOGIC_VECTOR (3 downto 0); --BCD for LSB
signal MSB: STD_LOGIC_VECTOR (3 downto 0); --BCD for MSB
signal OFF: STD_LOGIC_VECTOR (3 downto 0):="1010"; --defined to be segment OFF
signal V: STD_LOGIC_VECTOR (3 downto 0):="1011"; --defined to be letter V
begin
adj <= divide ( scaled_value , max_value );
adj2 <= adj* "00001001" ;
end Behavioral;
基本上我所做的是从 ADC 获取一个 8 位值,然后将其表示为最大值(即 9)的一小部分,然后我必须将其转换为 BCD...但是当我运行它时代码我收到一条错误消息:
第 38 行:索引值 超出数组范围 [0:2147483647]
我需要除法的答案是二进制数(带小数,例如 11001.110),这样当我将它乘以 9 时它会是准确的...
【问题讨论】:
-
欢迎来到 StackOverflow!请参阅"Should questions include “tags” in their titles?",其中的共识是“不,他们不应该”。
-
还有一种称为 Double Dabble 的算法,它显示在对几乎相同问题的两个答案中,标题 fru1bat 已更改:Convert 8bit binary number to BCD in VHDL。显示的两个答案还显示了如何简化行执行并提供参考。 Double Dabble 使用连续移位而不是除法,并且在硬件上可以更简单。