Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

How to make a averager in VHDL

I have just readed a piece of VHDL code. It is used to average the signal value from AD(14 bit).

The code i write below here:

process(CLK80) begin

if(CLK80'event and CLK80='1') then

if(samplingTriggerReg='1') then -- synchronization with the sampling trigger

avCStateReg <= "00000";

else

case avCStateReg is

when "00000"=> if(sigCLatchReg='1') then avCStateReg<="00001"; end if;

when "00001"=> if(sigCLatchReg='1') then avCStateReg<="00011"; end if;

when "00011"=> if(sigCLatchReg='1') then avCStateReg<="00111"; end if;

when "00111"=> if(sigCLatchReg='1') then avCStateReg<="01111"; end if;

when "01111"=> if(sigCLatchReg='1') then avCStateReg<="11111"; end if;

when "11111"=> if(sigCLatchReg='1') then avCStateReg<="11110"; end if;

when "11110"=> if(sigCLatchReg='1') then avCStateReg<="11100"; end if;

when "11100"=> if(sigCLatchReg='1') then avCStateReg<="11000"; end if;

when "11000"=> if(sigCLatchReg='1') then avCStateReg<="10000"; end if;

when "10000"=> if(sigCLatchReg='1') then avCStateReg<="00000"; end if;

when others => avCStateReg<="00000";

end case;

end if;

end if;

end process;

dfTrigCReg <= '1' when avCStateReg="10000" else '0';

process(CLK80) begin

if(CLK80'event and CLK80='1') then

if(samplingTriggerReg='1') then -- synchronization with the sampling trigger

avSStateReg <= "00000";

else

case avSStateReg is --gray counter?

when "00000"=> if(sigSLatchReg='1') then avSStateReg<="00001"; end if;

when "00001"=> if(sigSLatchReg='1') then avSStateReg<="00011"; end if;

when "00011"=> if(sigSLatchReg='1') then avSStateReg<="00111"; end if;

when "00111"=> if(sigSLatchReg='1') then avSStateReg<="01111"; end if;

when "01111"=> if(sigSLatchReg='1') then avSStateReg<="11111"; end if;

when "11111"=> if(sigSLatchReg='1') then avSStateReg<="11110"; end if;

when "11110"=> if(sigSLatchReg='1') then avSStateReg<="11100"; end if;

when "11100"=> if(sigSLatchReg='1') then avSStateReg<="11000"; end if;

when "11000"=> if(sigSLatchReg='1') then avSStateReg<="10000"; end if;

when "10000"=> if(sigSLatchReg='1') then avSStateReg<="00000"; end if;

when others => avSStateReg<="00000";

end case;

end if;

end if;

end process;

dfTrigSReg <= '1' when avSStateReg="10000" else '0';

process(CLK80)

begin

if (CLK80'event and CLK80='1') then

if (sigCLatchReg='1') then

avSigCReg(0)

<= sigCReg(13) & sigCReg(13) & sigCReg(13) & sigCReg(13) & sigCReg;

for I in 1 to 9 loop

avSigCReg(I) <= avSigCReg(I-1)

+ (sigCReg(13) & sigCReg(13) & sigCReg(13) & sigCReg(13) & sigCReg);

end loop;

end if;

end if;

end process;

process(CLK80)

begin

if (CLK80'event and CLK80='1') then

if (sigSLatchReg='1') then

avSigSReg(0)

<= sigSReg(13) & sigSReg(13) & sigSReg(13) & sigSReg(13) & sigSReg;

for I in 1 to 9 loop

avSigSReg(I) <= avSigSReg(I-1)

+ (sigSReg(13) & sigSReg(13) & sigSReg(13) & sigSReg(13) & sigSReg);

end loop;

end if;

end if;

end process;

i dont konw why the designer do this&#65306; avSigCReg(0) <= sigCReg(13) & sigCReg(13) & sigCReg(13) & sigCReg(13) & sigCReg;

avSigCReg is is array (0 to 9) of signed(17 downto 0)&#65292;

I think this code is calculate the average value of 10 signal .

But I dont understand it ? Thanks for giving your idea.

13 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hi kaz&#65281;

    Maybe I can ask you another VHDL code after this one. The data which is summed will be taken into a digital filter. The code is shown below:------------------- FIR FILTER ------------------

    process(CLK80)

    begin

    if (CLK80'event and CLK80='1') then

    if (DFTrigCReg='1') then

    for I in 0 to firLength-1 loop

    CQRealReg(I) <= avSigCReg(9)(17 downto 4) * DFcoeff(I);

    end loop;

    CSRealReg(0) <= CQRealReg(0)(23) & CQRealReg(0)(21 downto 0);

    for I in 1 to firLength-1 loop

    CSRealReg(I) <= CSRealReg(I-1) + (CQRealReg(I)(23) & CQRealReg(I)(21 downto 0));

    end loop;

    end if; --

    end if; -- CLK

    end process;

    I think the DFcoeff is filter coefficient. But why the designer do the process like this" CSRealReg(0) <= CQRealReg(0)(23) & CQRealReg(0)(21 downto 0);"
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    That will be sign extension, ensuring that the slices arrays stay +ve or -ve correctly.

    It reads better if you use the resize() function.

    Looking at that code, it wont build the adder tree as I expect it, it just creates FirLength-1 output values (you usually end up with a single output).
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    That will be sign extension, ensuring that the slices arrays stay +ve or -ve correctly.

    It reads better if you use the resize() function.

    Looking at that code, it wont build the adder tree as I expect it, it just creates FirLength-1 output values (you usually end up with a single output).

    --- Quote End ---

    Yes, but I dont know the resize() function. And maybe the CSRealReg(FirLength-1) is the output which I want . If you're interesting the complete vhdl code you can see it in the attchment, Tricky. Thanks for your help.