《电子琴程序设计与仿真.doc》由会员分享,可在线阅读,更多相关《电子琴程序设计与仿真.doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、8.18电子琴程序设计与仿真顶层程序与仿真,音阶发生器程序与仿真,数控分频模块程序与仿真,自动演奏模块程序与仿真设计例见随书所附光盘中文件:8.17电子琴VHDL程序与仿真。1.顶层程序与仿真(1)顶层VHDL程序-文件名:top.vhd-功能:顶层文件-最后修改日期:2004.3.20library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity top isPort ( clk32MHz :in std_logic; -32MHz系统时钟
2、handTOauto : in std_logic; -键盘输入/自动演奏code1 :out std_logic_vector(6 downto 0); -音符显示信号index1 :in std_logic_vector(7 downto 0); -键盘输入信号high1 :out std_logic; -高低音节信号spkout :out std_logic); -音频信号end top;architecture Behavioral of top iscomponent automusicPort ( clk :in std_logic; Auto: in std_logic; ind
3、ex2:in std_logic_vector(7 downto 0); index0 : out std_logic_vector(7 downto 0); end component;component tonePort ( index : in std_logic_vector(7 downto 0); code : out std_logic_vector(6 downto 0); high : out std_logic; tone0 : out integer range 0 to 2047);end component;component speakerPort ( clk1 :
4、 in std_logic;tone1 : in integer range 0 to 2047;spks : out std_logic);end component;signal tone2: integer range 0 to 2047;signal indx:std_logic_vector(7 downto 0);beginu0:automusic port map(clk=clk32MHZ,index2=index1,index0=indx,Auto=handtoAuto);u1: tone port map(index=indx,tone0=tone2,code=code1,h
5、igh=high1);u2: speaker port map(clk1=clk32MHZ,tone1=tone2,spks=spkout);end Behavioral;(2)仿真顶层文件仿真图如图8.17.2所示。图8.17.2 顶层文件仿真图2. 音阶发生器程序与仿真(1) 音阶发生器VHDL程序-文件名:tone.vhd。-功能:library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity tone isPort ( index :
6、 in std_logic_vector(7 downto 0); -音符输入信号code : out std_logic_vector(6 downto 0); -音符显示信号high : out std_logic; -高低音显示信号tone0 : out integer range 0 to 2047); -音符的分频系数end tone;architecture Behavioral of tone isbeginsearch :process(index) -此进程完成音符到音符的分频系数译码,音符的显示,高低音阶begincase index iswhen = tone0=773;
7、code=;high tone0=912;code=;high tone0=1036;code=;high tone0=1116;code=;high tone0=1197;code=;high tone0=1290;code=;high tone0=1372;code=;high tone0=1410;code=;high tone0=2047;code=;high=0;end case;end process;end Behavioral;(2)音阶发生器程序仿真音阶发生器程序仿真图如图8.17.3所示。图8.17.3 音阶发生器仿真图3. 数控分频模块程序与仿真(1) 数控分频模块VHD
8、L程序-文件名:speaker.vhd。-功 能:实现数控分频。-最后修改日期:20004.3.19。library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity speaker isPort ( clk1 : in std_logic; -系统时钟tone1 : in integer range 0 to 30624; -音符分频系数spks : out std_logic); -驱动扬声器的音频信号end speaker;archite
9、cture Behavioral of speaker issignal preclk,fullspks:std_logic;beginpulse1:process(clk1) -此进程对系统时钟进行4分频variable count:integer range 0 to 8;beginif clk1event and clk1=1 then count:=count+1;if count=2 then preclk=1; elsif count=4 then preclk=0;count:=0;end if; end if;end process pulse1;genspks:process
10、(preclk,tone1) -此进程按照tone1输入的分频系数对8MHz的脉冲再次分频,得到所需要的音符频率variable count11:integer range 0 to 30624;beginif preclkevent and preclk=1 thenif count11tone1 then count11:=count11+1;fullspks=1; else count11:=0;fullspks=0;end if;end if;end process;delaysps:process(fullspks) -此进程对fullspks进行2分频variable count2
11、 :std_logic:=0;beginif fullspksevent and fullspks=1 then count2:=not count2;if count2=1 then spks=1;else spks=0;end if;end if;end process;end Behavioral;(2) 数控分频模块程序仿真数控分频模块程序仿真图如图8.17.4所示。图8.17.4 数控分频模块仿真图4. 自动演奏模块程序与仿真(1) 自动演奏模块VHDL程序-文件名:automusic.vhd-功 能:实现自动演奏功能。-最后修改日期:2004.3.19。library IEEE;u
12、se IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;entity automusic isPort ( clk,Auto : in std_logic; -系统时钟;键盘输入/自动演奏index2 : in std_logic_vector(7 downto 0); -键盘输入信号index0 : out std_logic_vector(7 downto 0); -音符信号输出end automusic;architecture Behavioral of automu
13、sic issignal count0:integer range 0 to 31;-changesignal clk2:std_logic;beginpulse0:process(clk,Auto) -此进程完成对系统时钟8M的分频,得到4Hz的信号clk2variable count:integer range 0 to ;beginif Auto=1 then count:=0;clk2=0;elsif clkevent and clk=1 then count:=count+1;if count=(4) then clk2=1; elsif count= (8)then clk2=0;
14、count:=0;end if; end if;end process;music:process(clk2) -此进程完成自动演奏部分曲的地址累加beginif clk2event and clk2=1 thenif count0=31 then count0=0;else count0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 index0 null;end case;else index0=index2; -键盘输入音符信号输出end if;end process;end Behavioral;(2)自动演奏模块程序仿真自动演奏模块仿真图如图8.17.5所示。图8.17.5 自动演奏模块仿真图(注:由于输入频率太高,实验条件所限,如按源程序仿真将看不到输出波形,因此将原脉冲的分频点和改为4和8,得到如图的仿真结果,在实际烧制芯片中不作此处理。)