Demo entry 6899771

画不玩

   

Submitted by anonymous on Oct 08, 2019 at 13:05
Language: vhdl. Code size: 8.1 kB.

LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
USE IEEE.std_logic_arith.ALL;
ENTITY music_disp_ver3  IS
GENERIC(DIV_2Hz:INTEGER:=2499;
		DIV_0:INTEGER:=60000;
		DIV_C_1:INTEGER:=19;				--in C major
		DIV_C_2:INTEGER:=17;
		DIV_C_3:INTEGER:=15;
		DIV_C_4:INTEGER:=14;
		DIV_C_5:INTEGER:=12;
		DIV_C_6:INTEGER:=11;
		DIV_C_7:INTEGER:=10
		);
PORT(CLK:IN STD_LOGIC;								--CLK: 10KHz
     MUSIC_NUM:IN STD_LOGIC_VECTOR(2 DOWNTO 0);		--MUSIC_NUM: from music_select, choose the right music
     TIMES:IN STD_LOGIC_VECTOR(2 DOWNTO 0);			--TIMES: from time_select, choose the times of playing
     BTN7_IN:IN STD_LOGIC;							--BTN7_IN: if BTN7 is pressed, begin playing
     EN:IN STD_LOGIC;							    --EN: from mode_select, music display enable
     CARTOON_START:OUT STD_LOGIC;					--CARTOON_START: activate matrix_disp 
     BEEP:OUT STD_LOGIC;
     LCD_EN:OUT STD_LOGIC							--LCD_EN: connect to EN of LCD_disp, enable LCD.
     );
END music_disp_ver3;
ARCHITECTURE encoder_arch OF music_disp_ver3 IS
	SIGNAL CLK_2Hz,CLK_BEEP:STD_LOGIC;					--CLK2Hz: 2Hz
	SIGNAL CNT1,CNT2 :INTEGER RANGE 0 TO 2499;
	SIGNAL FREQ :INTEGER RANGE 0 TO 60000:=60000;						--FREQ: use FREQ to play music
	SIGNAL TONE :INTEGER RANGE 1 TO 7;						--TONE: 1-7:DO RE MI FA SO LA SI
	SIGNAL serial:INTEGER RANGE 1 TO 24;					--serial must be used as SIGNAL, because it must be saved outside the process P2
	SIGNAL i:INTEGER RANGE 0 TO 4:=0;						--i: use i to control cycle number
BEGIN
	
	P0:PROCESS(CLK)                    --P0:inner divider
	BEGIN

		IF CLK'EVENT AND CLK='1' THEN
			IF CNT1=DIV_2Hz THEN
				CNT1<=0;
				CLK_2Hz<=NOT CLK_2Hz;
			ELSE
				CNT1<=CNT1+1;
			END IF;															
		END IF;
	END PROCESS P0;

	P1:PROCESS(CLK)              --P1: when FREQ is changed, update the corresponding frequence to FREQ, and then use FREQ as division coefficient
	BEGIN
		IF CLK'EVENT AND CLK='1' THEN
			IF CNT2=FREQ THEN
				CNT2<=0;
				CLK_BEEP<=NOT CLK_BEEP;
			ELSE
				CNT2<=CNT2+1;
			END IF;															
		END IF;
		BEEP<=CLK_BEEP;
	END PROCESS P1;
	
	
	P2:PROCESS(CLK_2Hz,CLK)                			    --P2: time between tones is 0.5s
	variable MUSIC_NUM_TEMP1,MUSIC_NUM_TEMP2 :STD_LOGIC_VECTOR(2 DOWNTO 0);
	BEGIN
		
		IF BTN7_IN='1' THEN							--If BTN7 is pressed before the song is finished, play it from the beginning
			i<=1	;								--Counter i reset
			serial<=1;								--From the beginning to play
			LCD_EN<='1';									--Enable LCD display
		ELSIF RISING_EDGE(CLK_2Hz)  THEN
			MUSIC_NUM_TEMP2:=MUSIC_NUM_TEMP1;
			MUSIC_NUM_TEMP1:=MUSIC_NUM;
			IF EN = '1' AND i<CONV_INTEGER(TIMES)+1 AND i>0 THEN
				IF MUSIC_NUM_TEMP1 = MUSIC_NUM_TEMP2 THEN			--If the song doesn't change, then go on
--					IF serial=20 THEN
--						serial<=1;
--					END IF;
					CASE MUSIC_NUM IS
						WHEN "000"=> 								--music 1
							CASE serial IS
								WHEN 1=>FREQ<=DIV_C_5;		CARTOON_START<='1';		
								WHEN 2=>FREQ<=DIV_C_3;		CARTOON_START<='0';		--give CARTOON_START a rising edge
								WHEN 3=>FREQ<=DIV_C_5;
								WHEN 4=>FREQ<=DIV_C_3;
								WHEN 5=>FREQ<=DIV_C_5;
								WHEN 6=>FREQ<=DIV_C_3;
								WHEN 7=>FREQ<=DIV_C_1;
								WHEN 8=>FREQ<=DIV_C_2;
								WHEN 9=>FREQ<=DIV_C_4;
								WHEN 10=>FREQ<=DIV_C_3;
								WHEN 11=>FREQ<=DIV_C_2;
								WHEN 12=>FREQ<=DIV_C_5;
								WHEN 13=>FREQ<=DIV_0;
								WHEN 14=>FREQ<=DIV_C_5;
								WHEN 15=>FREQ<=DIV_C_3;
								WHEN 16=>FREQ<=DIV_C_5;
								WHEN 17=>FREQ<=DIV_C_3;
								WHEN 18=>FREQ<=DIV_C_5;
								WHEN 19=>FREQ<=DIV_C_3;
								WHEN 20=>FREQ<=DIV_0;
								WHEN 21=>FREQ<=DIV_0;
								WHEN 22=>FREQ<=DIV_0;
								WHEN 23=>FREQ<=DIV_0;
								WHEN 24=>FREQ<=DIV_0;	serial<=1;
								WHEN OTHERS=>FREQ<=0;	serial<=1;
							END CASE;
						WHEN "001"=> 								--music 2
							CASE serial IS
								WHEN 1=>FREQ<=DIV_C_5;		CARTOON_START<='1';	
								WHEN 2=>FREQ<=DIV_C_5;		CARTOON_START<='0';	
								WHEN 3=>FREQ<=DIV_C_6;
								WHEN 4=>FREQ<=DIV_C_5;
								WHEN 5=>FREQ<=DIV_C_1;
								WHEN 6=>FREQ<=DIV_C_7;
								WHEN 7=>FREQ<=DIV_0;
								WHEN 8=>FREQ<=DIV_C_5;
								WHEN 9=>FREQ<=DIV_C_5;
								WHEN 10=>FREQ<=DIV_C_6;
								WHEN 11=>FREQ<=DIV_C_5;
								WHEN 12=>FREQ<=DIV_C_2;
								WHEN 13=>FREQ<=DIV_C_1;
								WHEN 14=>FREQ<=DIV_C_5;
								WHEN 15=>FREQ<=DIV_C_5;
								WHEN 16=>FREQ<=DIV_C_5;
								WHEN 17=>FREQ<=DIV_C_3;
								WHEN 18=>FREQ<=DIV_C_1;
								WHEN 19=>FREQ<=DIV_C_7;
								WHEN 20=>FREQ<=DIV_C_6;
								WHEN 21=>FREQ<=DIV_0;
								WHEN 22=>FREQ<=DIV_0;
								WHEN 23=>FREQ<=DIV_0;
								WHEN 24=>FREQ<=DIV_0;	serial<=1;
								WHEN OTHERS=>FREQ<=0;	serial<=1;
							END CASE;
						WHEN "010"=> 								--music 3
							CASE serial IS
								WHEN 1=>FREQ<=DIV_C_5;		CARTOON_START<='1';	
								WHEN 2=>FREQ<=DIV_C_5;		CARTOON_START<='0';	
								WHEN 3=>FREQ<=DIV_C_6;
								WHEN 4=>FREQ<=DIV_C_5;
								WHEN 5=>FREQ<=DIV_C_1;
								WHEN 6=>FREQ<=DIV_C_7;
								WHEN 7=>FREQ<=DIV_0;
								WHEN 8=>FREQ<=DIV_C_5;
								WHEN 9=>FREQ<=DIV_C_5;
								WHEN 10=>FREQ<=DIV_C_6;
								WHEN 11=>FREQ<=DIV_C_5;
								WHEN 12=>FREQ<=DIV_C_2;
								WHEN 13=>FREQ<=DIV_C_1;
								WHEN 14=>FREQ<=DIV_C_5;
								WHEN 15=>FREQ<=DIV_C_5;
								WHEN 16=>FREQ<=DIV_C_5;
								WHEN 17=>FREQ<=DIV_C_3;
								WHEN 18=>FREQ<=DIV_C_1;
								WHEN 19=>FREQ<=DIV_C_7;
								WHEN 20=>FREQ<=DIV_C_6;
								WHEN 21=>FREQ<=DIV_0;
								WHEN 22=>FREQ<=DIV_0;
								WHEN 23=>FREQ<=DIV_0;
								WHEN 24=>FREQ<=DIV_0;	serial<=1;
								WHEN OTHERS=>FREQ<=0;	serial<=1;
							END CASE;
						WHEN "011"=> 								--music 4
							CASE serial IS
								WHEN 1=>FREQ<=DIV_C_5;		CARTOON_START<='1';	
								WHEN 2=>FREQ<=DIV_C_5;		CARTOON_START<='0';	
								WHEN 3=>FREQ<=DIV_C_6;
								WHEN 4=>FREQ<=DIV_C_5;
								WHEN 5=>FREQ<=DIV_C_1;
								WHEN 6=>FREQ<=DIV_C_7;
								WHEN 7=>FREQ<=DIV_0;
								WHEN 8=>FREQ<=DIV_C_5;
								WHEN 9=>FREQ<=DIV_C_5;
								WHEN 10=>FREQ<=DIV_C_6;
								WHEN 11=>FREQ<=DIV_C_5;
								WHEN 12=>FREQ<=DIV_C_2;
								WHEN 13=>FREQ<=DIV_C_1;
								WHEN 14=>FREQ<=DIV_C_5;
								WHEN 15=>FREQ<=DIV_C_5;
								WHEN 16=>FREQ<=DIV_C_5;
								WHEN 17=>FREQ<=DIV_C_3;
								WHEN 18=>FREQ<=DIV_C_1;
								WHEN 19=>FREQ<=DIV_C_7;
								WHEN 20=>FREQ<=DIV_C_6;
								WHEN 21=>FREQ<=DIV_0;
								WHEN 22=>FREQ<=DIV_0;
								WHEN 23=>FREQ<=DIV_0;
								WHEN 24=>FREQ<=DIV_0;	serial<=1;
								WHEN OTHERS=>FREQ<=0;	serial<=1;
							END CASE;
						WHEN "100"=> 								--music 5
							CASE serial IS
								WHEN 1=>FREQ<=DIV_C_5;		CARTOON_START<='1';	
								WHEN 2=>FREQ<=DIV_C_5;		CARTOON_START<='0';	
								WHEN 3=>FREQ<=DIV_C_6;
								WHEN 4=>FREQ<=DIV_C_5;
								WHEN 5=>FREQ<=DIV_C_1;
								WHEN 6=>FREQ<=DIV_C_7;
								WHEN 7=>FREQ<=DIV_0;
								WHEN 8=>FREQ<=DIV_C_5;
								WHEN 9=>FREQ<=DIV_C_5;
								WHEN 10=>FREQ<=DIV_C_6;
								WHEN 11=>FREQ<=DIV_C_5;
								WHEN 12=>FREQ<=DIV_C_2;
								WHEN 13=>FREQ<=DIV_C_1;
								WHEN 14=>FREQ<=DIV_C_5;
								WHEN 15=>FREQ<=DIV_C_5;
								WHEN 16=>FREQ<=DIV_C_5;
								WHEN 17=>FREQ<=DIV_C_3;
								WHEN 18=>FREQ<=DIV_C_1;
								WHEN 19=>FREQ<=DIV_C_7;
								WHEN 20=>FREQ<=DIV_C_6;
								WHEN 21=>FREQ<=DIV_0;
								WHEN 22=>FREQ<=DIV_0;
								WHEN 23=>FREQ<=DIV_0;
								WHEN 24=>FREQ<=DIV_0;	serial<=1;
								WHEN OTHERS=>FREQ<=0;	serial<=1;
							END CASE;	
				
						WHEN OTHERS=>FREQ<=0;
					END CASE;
					serial<=serial+1;
					IF serial>24 THEN
						serial<=1;
						i<=i+1;		--Loop the program i times
					END IF;
				ELSE serial<=1;	i<=1;					--If the song changes, then play it from the beginning
				END IF;
			ELSE i<=0;	FREQ<=60000;	LCD_EN<='0';		--set a unreachable number to avoid loop and shut down the music, disable the LCD
			END IF;																						
		END IF;
	END PROCESS P2;
	
		
END encoder_arch;

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).