我有一個問題涉及到從numeric_std轉換爲std_logic_vector。我正在使用我在線看到的移動平均濾波器代碼,並過濾我的ADC值以穩定值。從numeric_std無符號轉換爲std_logic_vector在vhdl
過濾包代碼是:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
package filterpack is
subtype number is unsigned(27 downto 0);
type numbers is array(natural range <>) of number;
function slv_to_num(signal slv: in std_logic_vector) return number;
procedure MAF_filter(
signal x: in number;
signal h: inout numbers;
signal y: out number
);
end filterpack;
package body filterpack is
function slv_to_num(signal slv: in std_logic_vector) return number is
variable x: number := (others => '0');
begin
for i in slv'range loop
if slv(i) = '1' then
x(i+4) := '1';
end if;
end loop;
return x;
end function slv_to_num;
procedure MAF_filter(
signal x: in number;
signal h: inout numbers;
signal y: out number
) is
begin
h(0) <= x + h(1); -- h[n] = x[n] + h[n-1]
y <= h(0) - h(h'high); -- y[n] = h[n] - h[n-M]
end MAF_filter;
end package body filterpack;
在我的頂層文件,我稱之爲MAF_filter程序。
Asign_x: x <= slv_to_num(adc_dat);
Filter: MAF_filter(x,h,y);
的adc_dat定義爲:
adc_dat : out std_logic_vector (23 downto 0);
我要轉換的MAF_Filter的輸出std_logic_vector(23 DOWNTO 0)。任何人都可以告訴我如何將過濾器輸出'y'轉換爲'std_logic_vector'?
非常感謝!
我使用了MAF已經制作好的代碼。由於函數slv_to_num的定義,我必須使用4個額外的位。 – user3008991
我編寫了一個針對函數'slv_to_num'的建議 – rick