在过去的几天里,我一直在尝试解决 VHDL 中的转换错误,这让我很困惑。我的代码附在下面。
它将正确评估 if 语句,但不会将新值分配给 min 或 max 变量。当我注释掉并测试该函数的不同方面时,我知道了这一点。
仅供引用。类型 t_battery_data
包含一个 std_logic_vectors(15 downto 0)
数组,它们是我在下面的函数中比较的电压。
我不确定为什么会这样。我在网上搜索中所能找到的就是包含我已经完成的 ieee.numeric_std 库。
还是一脸懵逼。任何建议将不胜感激。谢谢!
function cell_delta_voltage_counts(
bat_data: t_battery_data
) return integer is
constant POS_INFINITY: integer:= 2 ** 16 - 1;
constant NEG_INFINITY: integer:= 0;
variable min: integer range 0 to 2 ** 16 - 1:= POS_INFINITY-5;
variable max: integer range 0 to 2 ** 16 - 1:= NEG_INFINITY;
begin
for i in 0 to NUM_CELLS-1 loop
if (to_integer(unsigned(bat_data.cell_readings(i).voltage)) < min) then
min := to_integer(unsigned(bat_data.cell_readings(i).voltage));
end if;
if (to_integer(unsigned(bat_data.cell_readings(i).voltage)) > max) then
max := to_integer(unsigned(bat_data.cell_readings(i).voltage));
end if;
end loop;
return max - min;
end function cell_delta_voltage_counts;
最佳答案
我不使用很多函数,但是 IIRC 如果您希望 min 和 max 在调用之间“记住”它们的状态,您需要在函数外部声明它们并将函数声明为不纯的:
variable min: integer range 0 to 2 ** 16 - 1:= POS_INFINITY-5;
variable max: integer range 0 to 2 ** 16 - 1:= NEG_INFINITY;
impure function cell_delta_voltage_counts(
...
关于casting - VHDL 转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12536502/