⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 state.vhd

📁 多功能电子琴 可以实现人性化界面 同时可以根据按键选择播放模式
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity State is
	port(
		Clk_20M:in std_logic;	--20M时钟,用于分频
		Ctrl:in std_logic_vector(2 downto 0);	--模式选择控制,用S1拨盘实现
		Tone:in integer range 0 to 21;  --音符
	    Dout:out std_logic_vector(6 downto 0);	--数码管数据信号
		Leds:out std_logic_vector(5 downto 0);	--数码管选通信号
		Key_Leds:out std_logic_vector(7 downto 0)	--输出音阶的LED显示
		);
end State;

architecture code of State is
signal Clk_div,Clk_free:std_logic;
signal L0,L1,L2,L3,L4,L5,Tem:std_logic_vector(4 downto 0);
signal D45: std_logic_vector(4 downto 0):="10101";
begin
	process(Clk_20M)
	variable cnt:std_logic_vector(10 downto 0);
	variable free_t:std_logic_vector(22 downto 0);
	begin
		if Clk_20M'event and Clk_20M='1' then
			cnt:=cnt+'1';
			free_t:=free_t+'1';
			if free_t="11111111111100000000000" then
				Clk_free<='1';
			else 
				if free_t="11111111111111111111111" then
					Clk_free<='0';
				end if;
			end if;
			if cnt="11111111111" then
				Clk_div<='1';
			else 
				if cnt="11111111110" then
					Clk_div<='0';
				end if;
			end if;
		end if;
	end process;
		
	process(Clk_div)
	variable choice:std_logic_vector(2 downto 0);
	begin
		if Clk_div'event and Clk_div='0' then
			case choice is
				when "000" =>Leds<="011111";Tem<=L5;choice:="001";
				when "001" =>Leds<="101111";Tem<=L4;choice:="010";
				when "010" =>Leds<="110111";Tem<=L3;choice:="011";
				when "011" =>Leds<="111011";Tem<=L2;choice:="100";
				when "100" =>Leds<="111101";Tem<=L1;choice:="101";
				when "101" =>Leds<="111110";Tem<=L0;choice:="000";
				when others =>Leds<="111111";
			end case;
			case Ctrl is
				when "001" =>L3<="01010";L2<="00110";L1<="01100";L0<="00111"; --HAND
				when "010" =>L3<="01101";L2<="01011";L1<="00110";L0<="10000"; --PLAY
				when "100" =>L3<="00110";L2<="01111";L1<="01110";L0<="00111"; --AUTO
				when others =>L3<="01001";L2<="00110";L1<="01000";L0<="01000"; --FREE				
			end case;
		end if;	
	end process;
		
	process(Tem)
	begin
		Key_Leds<="00000000";
		case Tem is
			when "00000" =>Dout<="0000001";Key_Leds<="10000001"; --top
			when "00001" =>Dout<="0000010";Key_Leds<="11000011"; --r1
			when "00010" =>Dout<="0000100";Key_Leds<="11100111"; --r2
			when "00011" =>Dout<="0001000";Key_Leds<="11111111"; --bottom
			when "00100" =>Dout<="0010000";Key_Leds<="11100111"; --l2
			when "00101" =>Dout<="0100000";Key_Leds<="11000011"; --l1
			when "00110" =>Dout<="1110111"; --A,R
			when "00111" =>Dout<="0111111"; --D,O
			when "01000" =>Dout<="1111001"; --E
			when "01001" =>Dout<="1110001"; --F
			when "01010" =>Dout<="1110110"; --H
			when "01011" =>Dout<="0111000"; --L
			when "01100" =>Dout<="0110111"; --N
			when "01101" =>Dout<="1110011"; --P
			when "01110" =>Dout<="0000111"; --T
			when "01111" =>Dout<="0111110"; --U
			when "10000" =>Dout<="1101110"; --Y
			when "10001" =>Dout<="0000001"; --HIGH
			when "10010" =>Dout<="1000000"; --MIDDLE
			when "10011" =>Dout<="0001000"; --LOW
			when "10100" =>Dout<="0000110";Key_Leds<="00000011"; --1
			when "10101" =>Dout<="1011011";Key_Leds<="00000111"; --2
			when "10110" =>Dout<="1001111";Key_Leds<="00001111"; --3
			when "10111" =>Dout<="1100110";Key_Leds<="00011111"; --4
			when "11000" =>Dout<="1101101";Key_Leds<="00111111"; --5
			when "11001" =>Dout<="1111101";Key_Leds<="01111111"; --6
			when "11010" =>Dout<="0000111";Key_Leds<="11111111"; --7
			when others =>Dout<="0000000";Key_Leds<="00000000";
		end case;
	end process;
	
	process(Tone,Clk_free,Ctrl)
	begin
		if(Ctrl="001" or Ctrl="010" or Ctrl="100") then
		case Tone is
			when 1 =>L4<="10100";L5<="10011"; --1
			when 2 =>L4<="10101";L5<="10011"; --2
			when 3 =>L4<="10110";L5<="10011"; --3
			when 4 =>L4<="10111";L5<="10011"; --4
			when 5 =>L4<="11000";L5<="10011"; --5
			when 6 =>L4<="11001";L5<="10011"; --6
			when 7 =>L4<="11010";L5<="10011"; --7
			when 8 =>L4<="10100";L5<="10010"; --8
			when 9 =>L4<="10101";L5<="10010"; --9
			when 10 =>L4<="10110";L5<="10010"; --10
			when 11 =>L4<="10111";L5<="10010"; --11
			when 12 =>L4<="11000";L5<="10010"; --12
			when 13 =>L4<="11001";L5<="10010"; --13
			when 14 =>L4<="11010";L5<="10010"; --14
			when 15 =>L4<="10100";L5<="10001"; --15
			when 16 =>L4<="10101";L5<="10001"; --16
			when 17 =>L4<="10110";L5<="10001"; --17
			when 18 =>L4<="10111";L5<="10001"; --18
			when 19 =>L4<="11000";L5<="10001"; --19
			when 20 =>L4<="11001";L5<="10001"; --20
			when 21 =>L4<="11010";L5<="10001"; --21				
			when others =>L4<="11111";L5<="11111"; --OTHERS
		end case;
		else
			if Clk_free'event and Clk_free='1' then
			if(D45="00101") then
				D45<="00000";
			else
				D45<=D45+1;
			end if;
			L4<=D45;L5<=D45;
			end if;
		end if;
	end process;	
end code;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -