I've got a big problem with VHDL for a project. I want to see on 7 segment display a number that user sets with switches. For example if the low-order 5 switches are turned on then they will represent the binary number "11111" that is 31 in decimal. So I want to see 31 on 7 segment display.
To do that I plan these steps:
- Insert the 5 value of the switch into an array
- Convert the array into an integer number
- See the integer number into 7 segment display
Point 1) insert into an array
signal first: std_logic_vector (0 to 4);
signal temp: integer range 0 to 9999:=0;
for i in 0 to 4 loop
first(i)<=SW(i);
end loop;
temp<=VEC_TOINT(first);
HEX0<=INT_TO7SEG(temp);
Point 2) Vector to Integer
Function VEC_TOINT(Vector: in std_logic_vector) return integer is
variable temp: bit_vector(Vector'range);
variable result: integer :=0;
Begin
for index in Vector'range loop
result:=result * 2 + bit'pos(temp(index));
end loop;
if Vector(Vector'left) = '1' then
result:=(-result)-1;
end if;
return result;
End VEC_TOINT;
For the third point at the moment I don't have any idea.
Last questions i promise :)
I start to study your code and think about work, and so i decide to insert another input with the last two hex display that i have and sum them
so i create ad insert this part of code
There is a mistake that i can't find "Near text ":"; expecting ")", or "," .....according me the synthax is correct because on input1, input2 i already use the function to_integer....
Best Regards
Michele
To solve 3) you'll have to understand how a 7 segment display works, specifically character display. Basically you'll have to implement an encoder from integer to the hexadecimal encoding of the display. The hexadecimal encoding is provided in the data sheet of the display, e.g. this datasheet shows you which display pins are connected to which segment. An exemplary hexadecimal code is provided at wikipedia.
I've written a small package that does what you want. Given an unsigned input value, it breaks down this value into a series of decimal digits, and generates signals that can drive any number of seven-segments displays.
Here's an example of how you'd use it:
Note that the input value is an unsigned. To convert (actually, "type cast") an std_logic_vector to an unsigned, just use:
If for some reason you decide to convert the value from the switches to an integer, there's a function in ieee.numeric_std that does that. It's a good idea to use it rather than writing your own. You can use it as:
Finally, here's the code for the package. You are welcome to use it or study it and come up with your own solution.