Forum Discussion
Altera_Forum
Honored Contributor
13 years agoYour approach didn't work for me. I checked the output on the FPGA but it didn't work for me. Basically I am trying to stabilize the ADC output values. That is why I am averaging the incoming data or captured data with ADC. out_val and out_val_2 are the 12 bit ADC outputs. I'll show how I used your approach below. Kindly correct me if I am wrong:
data_in : in std_logic_vector (31 downto 0);
type type1 is array (1 to 31) of std_logic_vector(11 downto 0);
signal stage: type1 := (others => (others => '0'));
signal sub_result: signed(12 downto 0) := (others => '0');
signal sum: signed(16 downto 0) := (others => '0');
signal stage2: type1 := (others => (others => '0'));
signal sub_result2: signed(12 downto 0) := (others => '0');
signal sum2: signed(16 downto 0) := (others => '0');
process (clk, reset)
begin
if (reset = '1') then
state<=idle;
out_val=0;
out_val_2 <= 0;
avg_1 <= 0;
avg_2 <=0;
elsif(rising_edge(clk)) then
case state is
when idle =>
if req='1' then
state= out_1;
end if;
when out_1 =>
if done='1' then
data_out <= addr0 & bits;
out_val<=data_in (11 downto 0);
avg_1 <= '1';
state <= out_2;
endif;
when out_2 =>
if done='1' then
data_out <= addr1 & bits;
out_val_2<=data_in (11 downto 0);
avg_2 <= '1';
state <= done_st;
when done_st =>
ack <='1';
state <= idle;
when others =>
state <= idle;
end case;
end if;
end process;
--Then comes your approach
-- delay input 31 stages
adc_a_avg : process
begin
wait until clk = '1';
if avg_1 = '1' then
stage(1) <= data_in(11 downto 0);
for i in 2 to 31 loop
stage(i) <= stage(i-1);
end loop;
-- subtract last stage from input
sub_result <= signed(data_in(11 downto 0) & '0') - signed(stage(31)); -- I had to append 0 because I was getting error if I don't put it.
-- accumulate
sum <= sum + sub_result;
adc_a_out <= std_logic_vector(sum(16 downto 5));
end if;
end process adc_a_avg;
adc_b_avg : process
begin
wait until clk = '1';
if avg_2 = '1' then
stage2(1) <= data_in(11 downto 0);
for i in 2 to 31 loop
stage2(i) <= stage2(i-1);
end loop;
-- subtract last stage from input
sub_result2 <= signed(data_in(11 downto 0) & '0') - signed(stage2(31));
-- accumulate
sum2 <= sum2 + sub_result2;
adc_b_out <= std_logic_vector(sum2(16 downto 5));
end if;
end process adc_b_avg; I measure the adc values (current and voltage) "adc_a_out and adc_b_out" after using the above approach and I get incorrect values. Kindly let me know where I am doing wrong.