cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
valiciousx
Newbie
Newbie
696 Views
Registered: ‎01-07-2021

average value of 4 signals

I'm new to VHDL programming and I try to make a module which calculates the average value of 4 signals. Then I have to use a decoder to display the value from binary to decimal on a Basys3 component. Here's my try but I don't know how to continue:

How to use a decoder to display the value on the component in decimal? Also, am I started in a correct way?

 

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity Average is
Port ( a1, a2, a3, a4: in std_logic_vector(7 downto 0);
result: out std_logic_vector(7 downto 0));
end Average;

architecture Behavioral of Average is
signal sum: std_logic_vector(7 downto 0);
begin
sum <= ("00" & a1)+a2+a3+a4;
result <= sum(9 downto 2) + sum(1);
end Behavioral;

 

image_2021-01-07_191331.png
0 Kudos
Reply
4 Replies
drjohnsmith
Teacher
Teacher
651 Views
Registered: ‎07-09-2009

http://freerangefactory.org/

 

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Reply
bruce_karaffa
Scholar
Scholar
633 Views
Registered: ‎06-21-2017

Do your inputs represent unsigned (0 to 255) or signed (-128 to 127) numbers? It makes a difference in how you sign extend before the addition.  Are you using VHDL 2008 or an earlier version?  Have you tried to simulate?  Simulation will help to find syntax and logical errors.  You have a clock on the board.  You should be using it. 

A decoder can be just a look up table.  You only have 256 possible input combinations so it could fit into a BRAM. 

samamherb
Visitor
Visitor
430 Views
Registered: ‎06-11-2018

Please specify the type of input data (signed or unsigned). For converting from binary to BCD the below sample can be used.

process(AVG)
		variable AVG_TEMP : std_logic_vector(6 downto 0);
	begin
		AVG_TEMP := AVG(7 downto 1);
		case AVG_TEMP is
			when "0000000" => BCD(11 downto 1) <= "00000000000";
			when "0000001" => BCD(11 downto 1) <= "00000000001";
			when "0000010" => BCD(11 downto 1) <= "00000000010";
			when "0000011" => BCD(11 downto 1) <= "00000000011";
			when "0000100" => BCD(11 downto 1) <= "00000000100";
			when "0000101" => BCD(11 downto 1) <= "00000001000";
			when "0000110" => BCD(11 downto 1) <= "00000001001";
			when "0000111" => BCD(11 downto 1) <= "00000001010";
			when "0001000" => BCD(11 downto 1) <= "00000001011";
			when "0001001" => BCD(11 downto 1) <= "00000001100";
			when "0001010" => BCD(11 downto 1) <= "00000010000";
			when "0001011" => BCD(11 downto 1) <= "00000010001";
			when "0001100" => BCD(11 downto 1) <= "00000010010";
			when "0001101" => BCD(11 downto 1) <= "00000010011";
			when "0001110" => BCD(11 downto 1) <= "00000010100";
			when "0001111" => BCD(11 downto 1) <= "00000011000";
			when "0010000" => BCD(11 downto 1) <= "00000011001";
			when "0010001" => BCD(11 downto 1) <= "00000011010";
			when "0010010" => BCD(11 downto 1) <= "00000011011";
			when "0010011" => BCD(11 downto 1) <= "00000011100";
			when "0010100" => BCD(11 downto 1) <= "00000100000";
			when "0010101" => BCD(11 downto 1) <= "00000100001";
			when "0010110" => BCD(11 downto 1) <= "00000100010";
			when "0010111" => BCD(11 downto 1) <= "00000100011";
			when "0011000" => BCD(11 downto 1) <= "00000100100";
			when "0011001" => BCD(11 downto 1) <= "00000101000";
			when "0011010" => BCD(11 downto 1) <= "00000101001";
			when "0011011" => BCD(11 downto 1) <= "00000101010";
			when "0011100" => BCD(11 downto 1) <= "00000101011";
			when "0011101" => BCD(11 downto 1) <= "00000101100";
			when "0011110" => BCD(11 downto 1) <= "00000110000";
			when "0011111" => BCD(11 downto 1) <= "00000110001";
			when "0100000" => BCD(11 downto 1) <= "00000110010";
			when "0100001" => BCD(11 downto 1) <= "00000110011";
			when "0100010" => BCD(11 downto 1) <= "00000110100";
			when "0100011" => BCD(11 downto 1) <= "00000111000";
			when "0100100" => BCD(11 downto 1) <= "00000111001";
			when "0100101" => BCD(11 downto 1) <= "00000111010";
			when "0100110" => BCD(11 downto 1) <= "00000111011";
			when "0100111" => BCD(11 downto 1) <= "00000111100";
			when "0101000" => BCD(11 downto 1) <= "00001000000";
			when "0101001" => BCD(11 downto 1) <= "00001000001";
			when "0101010" => BCD(11 downto 1) <= "00001000010";
			when "0101011" => BCD(11 downto 1) <= "00001000011";
			when "0101100" => BCD(11 downto 1) <= "00001000100";
			when "0101101" => BCD(11 downto 1) <= "00001001000";
			when "0101110" => BCD(11 downto 1) <= "00001001001";
			when "0101111" => BCD(11 downto 1) <= "00001001010";
			when "0110000" => BCD(11 downto 1) <= "00001001011";
			when "0110001" => BCD(11 downto 1) <= "00001001100";
			when "0110010" => BCD(11 downto 1) <= "00010000000";
			when "0110011" => BCD(11 downto 1) <= "00010000001";
			when "0110100" => BCD(11 downto 1) <= "00010000010";
			when "0110101" => BCD(11 downto 1) <= "00010000011";
			when "0110110" => BCD(11 downto 1) <= "00010000100";
			when "0110111" => BCD(11 downto 1) <= "00010001000";
			when "0111000" => BCD(11 downto 1) <= "00010001001";
			when "0111001" => BCD(11 downto 1) <= "00010001010";
			when "0111010" => BCD(11 downto 1) <= "00010001011";
			when "0111011" => BCD(11 downto 1) <= "00010001100";
			when "0111100" => BCD(11 downto 1) <= "00010010000";
			when "0111101" => BCD(11 downto 1) <= "00010010001";
			when "0111110" => BCD(11 downto 1) <= "00010010010";
			when "0111111" => BCD(11 downto 1) <= "00010010011";
			when "1000000" => BCD(11 downto 1) <= "00010010100";
			when "1000001" => BCD(11 downto 1) <= "00010011000";
			when "1000010" => BCD(11 downto 1) <= "00010011001";
			when "1000011" => BCD(11 downto 1) <= "00010011010";
			when "1000100" => BCD(11 downto 1) <= "00010011011";
			when "1000101" => BCD(11 downto 1) <= "00010011100";
			when "1000110" => BCD(11 downto 1) <= "00010100000";
			when "1000111" => BCD(11 downto 1) <= "00010100001";
			when "1001000" => BCD(11 downto 1) <= "00010100010";
			when "1001001" => BCD(11 downto 1) <= "00010100011";
			when "1001010" => BCD(11 downto 1) <= "00010100100";
			when "1001011" => BCD(11 downto 1) <= "00010101000";
			when "1001100" => BCD(11 downto 1) <= "00010101001";
			when "1001101" => BCD(11 downto 1) <= "00010101010";
			when "1001110" => BCD(11 downto 1) <= "00010101011";
			when "1001111" => BCD(11 downto 1) <= "00010101100";
			when "1010000" => BCD(11 downto 1) <= "00010110000";
			when "1010001" => BCD(11 downto 1) <= "00010110001";
			when "1010010" => BCD(11 downto 1) <= "00010110010";
			when "1010011" => BCD(11 downto 1) <= "00010110011";
			when "1010100" => BCD(11 downto 1) <= "00010110100";
			when "1010101" => BCD(11 downto 1) <= "00010111000";
			when "1010110" => BCD(11 downto 1) <= "00010111001";
			when "1010111" => BCD(11 downto 1) <= "00010111010";
			when "1011000" => BCD(11 downto 1) <= "00010111011";
			when "1011001" => BCD(11 downto 1) <= "00010111100";
			when "1011010" => BCD(11 downto 1) <= "00011000000";
			when "1011011" => BCD(11 downto 1) <= "00011000001";
			when "1011100" => BCD(11 downto 1) <= "00011000010";
			when "1011101" => BCD(11 downto 1) <= "00011000011";
			when "1011110" => BCD(11 downto 1) <= "00011000100";
			when "1011111" => BCD(11 downto 1) <= "00011001000";
			when "1100000" => BCD(11 downto 1) <= "00011001001";
			when "1100001" => BCD(11 downto 1) <= "00011001010";
			when "1100010" => BCD(11 downto 1) <= "00011001011";
			when "1100011" => BCD(11 downto 1) <= "00011001100";
			when "1100100" => BCD(11 downto 1) <= "00100000000";
			when "1100101" => BCD(11 downto 1) <= "00100000001";
			when "1100110" => BCD(11 downto 1) <= "00100000010";
			when "1100111" => BCD(11 downto 1) <= "00100000011";
			when "1101000" => BCD(11 downto 1) <= "00100000100";
			when "1101001" => BCD(11 downto 1) <= "00100001000";
			when "1101010" => BCD(11 downto 1) <= "00100001001";
			when "1101011" => BCD(11 downto 1) <= "00100001010";
			when "1101100" => BCD(11 downto 1) <= "00100001011";
			when "1101101" => BCD(11 downto 1) <= "00100001100";
			when "1101110" => BCD(11 downto 1) <= "00100010000";
			when "1101111" => BCD(11 downto 1) <= "00100010001";
			when "1110000" => BCD(11 downto 1) <= "00100010010";
			when "1110001" => BCD(11 downto 1) <= "00100010011";
			when "1110010" => BCD(11 downto 1) <= "00100010100";
			when "1110011" => BCD(11 downto 1) <= "00100011000";
			when "1110100" => BCD(11 downto 1) <= "00100011001";
			when "1110101" => BCD(11 downto 1) <= "00100011010";
			when "1110110" => BCD(11 downto 1) <= "00100011011";
			when "1110111" => BCD(11 downto 1) <= "00100011100";
			when "1111000" => BCD(11 downto 1) <= "00100100000";
			when "1111001" => BCD(11 downto 1) <= "00100100001";
			when "1111010" => BCD(11 downto 1) <= "00100100010";
			when "1111011" => BCD(11 downto 1) <= "00100100011";
			when "1111100" => BCD(11 downto 1) <= "00100100100";
			when "1111101" => BCD(11 downto 1) <= "00100101000";
			when "1111110" => BCD(11 downto 1) <= "00100101001";
			when "1111111" => BCD(11 downto 1) <= "00100101010";
			when others => BCD <= (others => '0');
		end case;
		BCD(0) <= AVG(0);
	end process;
I'm a freelance VHDL developer and HDL designer.
0 Kudos
Reply
drjohnsmith
Teacher
Teacher
418 Views
Registered: ‎07-09-2009

If you are gogin to use this format

    then I woudl strongly sugest that you use a slightly different format,

 

e.g

when "0000101" => BCD(11 downto 1) <= "00000001000";

 

become 

when B"000_0101" => BCD( 10 downto 0 )  <= B" 000_0000_1000";

you will make many fewer mistakes.

or if your using 2008,

when 7x"05" => BCD( 10 downto 0 )  <= 11x"008";

 

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>