Last active
December 18, 2024 21:37
-
-
Save Apocryphon-X/27c2ddf543f476852efa6ee59620ef46 to your computer and use it in GitHub Desktop.
Display the selected 4-bits number in the seven segments display. (4-bit numbers can be edited using switches)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- * Author: Dante Mendoza Leyva | |
-- * Group: 4CV13 | |
library ieee; | |
use ieee.std_logic_1164.all; | |
use ieee.std_logic_unsigned.all; | |
use ieee.numeric_std.all; | |
entity seven_segments_driver is Port ( | |
clk: in std_logic; | |
anodes: out std_logic_vector(3 downto 0); | |
display_glyph: out std_logic_vector(6 downto 0); | |
first_number: in std_logic_vector(3 downto 0); -- Input Bits | |
second_number: in std_logic_vector(3 downto 0); -- Input Bits | |
number_switch: in std_logic; | |
first_number_leds: out std_logic_vector(3 downto 0); | |
second_number_leds: out std_logic_vector(3 downto 0); | |
number_switch_led: out std_logic | |
); | |
end seven_segments_driver; | |
architecture Behavioral of seven_segments_driver is | |
-- This function encodes 4-bit values to seven segment display data | |
function as_seven_segment(input_bits: std_logic_vector(3 downto 0)) return std_logic_vector is | |
variable converted_value : std_logic_vector(6 downto 0); | |
begin | |
case input_bits is | |
when "0000" => converted_value := "0000001"; -- 0 | |
when "0001" => converted_value := "1001111"; -- 1 | |
when "0010" => converted_value := "0010010"; -- 2 | |
when "0011" => converted_value := "0000110"; -- 3 | |
when "0100" => converted_value := "1001100"; -- 4 | |
when "0101" => converted_value := "0100100"; -- 5 | |
when "0110" => converted_value := "0100000"; -- 6 | |
when "0111" => converted_value := "0001111"; -- 7 | |
when "1000" => converted_value := "0000000"; -- 8 | |
when "1001" => converted_value := "0000100"; -- 9 | |
when others => converted_value := "1111111"; | |
end case; | |
return converted_value; | |
end as_seven_segment; | |
------------------------------------------------------------ | |
signal DISPLAY_REFRESH_COUNTER: integer range 0 to 6000 := 0; | |
signal ANODE_SELECTOR: std_logic_vector(1 downto 0); | |
signal display_0: std_logic_vector(6 downto 0) := as_seven_segment("0001"); | |
signal display_1: std_logic_vector(6 downto 0) := as_seven_segment("0101"); | |
signal display_2: std_logic_vector(6 downto 0) := as_seven_segment("0001"); | |
signal display_3: std_logic_vector(6 downto 0) := as_seven_segment("0101"); | |
begin | |
number_switch_led <= '1'; | |
-- Clocking Process | |
process (clk) | |
begin | |
if (clk'event and clk = '1') then | |
DISPLAY_REFRESH_COUNTER <= DISPLAY_REFRESH_COUNTER + 1; | |
-- ! Assuming 100MHz clock ! | |
-- Update each 6μs (100_000_000 = 1_000_000μs -> 6_000 = 6μs) | |
if (DISPLAY_REFRESH_COUNTER < 6000) then | |
DISPLAY_REFRESH_COUNTER <= DISPLAY_REFRESH_COUNTER + 1; | |
else | |
-- Dispatch event for: `process (ANODE_SELECTOR)` | |
ANODE_SELECTOR <= ANODE_SELECTOR + 1; | |
DISPLAY_REFRESH_COUNTER <= 0; | |
end if; | |
end if; | |
end process; | |
-- "Rendering" Process | |
process (ANODE_SELECTOR) | |
begin | |
case ANODE_SELECTOR is | |
when "00" => display_glyph <= display_0; | |
when "01" => display_glyph <= display_1; | |
when "10" => display_glyph <= display_2; | |
when "11" => display_glyph <= display_3; | |
when others => display_glyph <= "0000000"; | |
end case; | |
case ANODE_SELECTOR is | |
when "00" => anodes <= "0111"; -- 0 | |
when "01" => anodes <= "1011"; -- 1 | |
when "10" => anodes <= "1101"; -- 2 | |
when "11" => anodes <= "1110"; -- 3 | |
end case; | |
end process; | |
-- Reacting to input switches | |
process (first_number, second_number, number_switch) | |
variable target_number: unsigned(3 downto 0); | |
variable first_digit: unsigned(3 downto 0); | |
variable second_digit: unsigned(3 downto 0); | |
begin | |
if number_switch = '0' then | |
first_number_leds <= "1111"; | |
second_number_leds <= "0000"; | |
target_number := unsigned(first_number); | |
else | |
first_number_leds <= "0000"; | |
second_number_leds <= "1111"; | |
target_number := unsigned(second_number); | |
end if; | |
display_0 <= as_seven_segment("1111"); -- OFF | |
display_1 <= as_seven_segment("1111"); -- OFF | |
second_digit := (target_number / 10) mod 10; | |
first_digit := target_number mod 10; | |
if second_digit = 0 then | |
display_2 <= as_seven_segment("1111"); -- OFF | |
else | |
display_2 <= as_seven_segment(std_logic_vector(second_digit)); | |
end if; | |
display_3 <= as_seven_segment(std_logic_vector(first_digit)); | |
end process; | |
end Behavioral; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set_property -dict { PACKAGE_PIN W7 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[6]}] | |
set_property -dict { PACKAGE_PIN W6 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[5]}] | |
set_property -dict { PACKAGE_PIN U8 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[4]}] | |
set_property -dict { PACKAGE_PIN V8 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[3]}] | |
set_property -dict { PACKAGE_PIN U5 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[2]}] | |
set_property -dict { PACKAGE_PIN V5 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[1]}] | |
set_property -dict { PACKAGE_PIN U7 IOSTANDARD LVCMOS33 } [get_ports {display_glyph[0]}] | |
# set_property -dict { PACKAGE_PIN V7 IOSTANDARD LVCMOS33 } [get_ports dp] | |
set_property -dict { PACKAGE_PIN U2 IOSTANDARD LVCMOS33 } [get_ports {anodes[0]}] | |
set_property -dict { PACKAGE_PIN U4 IOSTANDARD LVCMOS33 } [get_ports {anodes[1]}] | |
set_property -dict { PACKAGE_PIN V4 IOSTANDARD LVCMOS33 } [get_ports {anodes[2]}] | |
set_property -dict { PACKAGE_PIN W4 IOSTANDARD LVCMOS33 } [get_ports {anodes[3]}] | |
set_property -dict { PACKAGE_PIN W5 IOSTANDARD LVCMOS33 } [get_ports clk] | |
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk] | |
# Second Number | |
set_property -dict { PACKAGE_PIN V17 IOSTANDARD LVCMOS33 } [get_ports {second_number[0]}] | |
set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports {second_number[1]}] | |
set_property -dict { PACKAGE_PIN W16 IOSTANDARD LVCMOS33 } [get_ports {second_number[2]}] | |
set_property -dict { PACKAGE_PIN W17 IOSTANDARD LVCMOS33 } [get_ports {second_number[3]}] | |
# First Number | |
set_property -dict { PACKAGE_PIN W2 IOSTANDARD LVCMOS33 } [get_ports {first_number[0]}] | |
set_property -dict { PACKAGE_PIN U1 IOSTANDARD LVCMOS33 } [get_ports {first_number[1]}] | |
set_property -dict { PACKAGE_PIN T1 IOSTANDARD LVCMOS33 } [get_ports {first_number[2]}] | |
set_property -dict { PACKAGE_PIN R2 IOSTANDARD LVCMOS33 } [get_ports {first_number[3]}] | |
# Number switch | |
set_property -dict { PACKAGE_PIN W13 IOSTANDARD LVCMOS33 } [get_ports {number_switch}] | |
## LEDs | |
#set_property -dict { PACKAGE_PIN U16 IOSTANDARD LVCMOS33 } [get_ports {led[0]}] | |
#set_property -dict { PACKAGE_PIN E19 IOSTANDARD LVCMOS33 } [get_ports {led[1]}] | |
#set_property -dict { PACKAGE_PIN U19 IOSTANDARD LVCMOS33 } [get_ports {led[2]}] | |
#set_property -dict { PACKAGE_PIN V19 IOSTANDARD LVCMOS33 } [get_ports {led[3]}] | |
#set_property -dict { PACKAGE_PIN W18 IOSTANDARD LVCMOS33 } [get_ports {led[4]}] | |
#set_property -dict { PACKAGE_PIN U15 IOSTANDARD LVCMOS33 } [get_ports {led[5]}] | |
#set_property -dict { PACKAGE_PIN U14 IOSTANDARD LVCMOS33 } [get_ports {led[6]}] | |
#set_property -dict { PACKAGE_PIN V14 IOSTANDARD LVCMOS33 } [get_ports {led[7]}] | |
#set_property -dict { PACKAGE_PIN V13 IOSTANDARD LVCMOS33 } [get_ports {led[8]}] | |
#set_property -dict { PACKAGE_PIN V3 IOSTANDARD LVCMOS33 } [get_ports {led[9]}] | |
#set_property -dict { PACKAGE_PIN W3 IOSTANDARD LVCMOS33 } [get_ports {led[10]}] | |
#set_property -dict { PACKAGE_PIN U3 IOSTANDARD LVCMOS33 } [get_ports {led[11]}] | |
set_property -dict { PACKAGE_PIN P3 IOSTANDARD LVCMOS33 } [get_ports {first_number_leds[3]}] | |
set_property -dict { PACKAGE_PIN N3 IOSTANDARD LVCMOS33 } [get_ports {first_number_leds[2]}] | |
set_property -dict { PACKAGE_PIN P1 IOSTANDARD LVCMOS33 } [get_ports {first_number_leds[1]}] | |
set_property -dict { PACKAGE_PIN L1 IOSTANDARD LVCMOS33 } [get_ports {first_number_leds[0]}] | |
set_property -dict { PACKAGE_PIN V19 IOSTANDARD LVCMOS33 } [get_ports {second_number_leds[3]}] | |
set_property -dict { PACKAGE_PIN U19 IOSTANDARD LVCMOS33 } [get_ports {second_number_leds[2]}] | |
set_property -dict { PACKAGE_PIN E19 IOSTANDARD LVCMOS33 } [get_ports {second_number_leds[1]}] | |
set_property -dict { PACKAGE_PIN U16 IOSTANDARD LVCMOS33 } [get_ports {second_number_leds[0]}] | |
set_property -dict { PACKAGE_PIN V14 IOSTANDARD LVCMOS33 } [get_ports {number_switch_led}] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment