蜂鳴器是一種一體化結(jié)構(gòu)的電子訊響器,采用直流電壓供電,廣泛應(yīng)用于計(jì)算機(jī)、打印機(jī)、復(fù)印機(jī)、報(bào)警器、電子玩具、汽車電子設(shè)備、電話機(jī)、定時(shí)器等電子產(chǎn)品中作發(fā)聲器件。蜂鳴器主要分為壓電式蜂鳴器和電磁式蜂鳴器兩種類型。蜂鳴器在電路中用字母“H”或“HA”(舊標(biāo)準(zhǔn)用“FM”、“ZZG”、“LB”、“JD”等)表示。本文為大家介紹基于vhdl蜂鳴器程序設(shè)計(jì)。
實(shí)驗(yàn)步驟
1、設(shè)置端口
1)輸入端口
CLK:40MHZ系統(tǒng)時(shí)鐘輸入端口。
2)輸出端口
device:樂曲的聲音輸出端口,輸出的是對應(yīng)各音符頻率的方波信號。 2、設(shè)置模塊 1)自動演奏模塊
自動演奏模塊可以自動播放電子琴內(nèi)置樂曲,按節(jié)拍讀取內(nèi)置樂譜。將鍵盤輸入的音符信號輸出。因此,本模塊是向Tone模塊提供音符信息。
首先,對40MHz系統(tǒng)時(shí)鐘進(jìn)行10M的分頻,得到4Hz的信號,這樣一秒中就可以按照四拍進(jìn)行。然后依照此頻率進(jìn)行地址累計(jì)。
音頻發(fā)生器模塊
根據(jù)自動演奏模塊的信號輸出,不同的信號被翻譯為不同的頻率。
蜂鳴器驅(qū)動模塊
根據(jù)音頻發(fā)生器發(fā)出音頻的不同,蜂鳴器得到的驅(qū)動也不同。首先,對系統(tǒng)時(shí)鐘進(jìn)行40分頻,再對1mhz的脈沖再次分頻,得到所需要的音符頻率,然后再進(jìn)行2分頻。
實(shí)驗(yàn)代碼
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity tone is port( index: in std_logic_vector(15 downto 0); --音符輸入信號
tone0: out integer range 0 to 2047 --音符的分頻系數(shù) );
end tone;
architecture behavioral of tone is
begin
search :process(index) --此進(jìn)程完成音符到音符的分頻系數(shù)譯碼,音符的顯示,高低音階
begin
case index is
when “0000000000000001” => tone0<=1433;
when “0000000000000010” => tone0<=1277;
when “0000000000000100” => tone0<=1138;
when “0000000000001000” => tone0<=1074;
when “0000000000010000” => tone0<=960;
when “0000000000100000” => tone0<=853;
when “0000000001000000” => tone0<=759;
when “0000000010000000” => tone0<=716;
when “0000000100000000” => tone0<=358;
when “0000001000000000” => tone0<=319;
when “0000010000000000” => tone0<=284;
when “0000100000000000” => tone0<=268;
when “0001000000000000” => tone0<=239;
when “0010000000000000” => tone0<=213;
when “0100000000000000” => tone0<=190;
when “1000000000000000” => tone0<=638;
when others => tone0<=0;
end case;
end process;
end behavioral;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity speaker is port( clk1: in std_logic; --系統(tǒng)時(shí)鐘12mhz
tone1: in integer range 0 to 2047; --音符分頻系數(shù)
spks: out std_logic --驅(qū)動揚(yáng)聲器的音頻信號 );
end speaker;
architecture behavioral of speaker is
signal preclk, fullspks:std_logic;
begin
p1:process(clk1)--此進(jìn)程對系統(tǒng)時(shí)鐘進(jìn)行16分頻
variable count: integer range 0 to 16;
begin
if clk1‘event and clk1=’1‘ then count:=count+1;
if count=8 then
preclk<=‘1’;
elsif count=16 then preclk<=‘0’;
count:=0;
end if;
end if;
end process p1;
p2:process(preclk,tone1)--對0.75mhz的脈沖再次分頻,得到所需要的音符頻率
variable count11:integer range 0 to 2047;
begin
if preclk‘event and preclk=’1‘ then
if count11
count11:=count11+1;
fullspks<=’1‘;
else
count11:=0;
fullspks<=’0‘;
end if;
end if;
end process p2;
p3:process(fullspks)--此進(jìn)程對fullspks進(jìn)行2分頻
variable count2: std_logic:=’0‘;
begin
if fullspks’event and fullspks=‘1’ then
count2:=not count2;
if count2=‘1’ then
spks<=‘1’;
else spks<=‘0’;
end if;
end if;
end process p3;
end behavioral;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity laohu is port( clk: in std_logic;--系統(tǒng)時(shí)鐘;鍵盤輸入/自動演奏
tone_key_0: buffer std_logic_vector(15 downto 0)--音符信號輸出
);
end laohu;
評論