基于51單片機(jī)音樂(lè)盒仿真設(shè)計(jì)( proteus仿真+程序+原理圖+PCB+報(bào)告+講解視頻)
仿真圖proteus 7.8及以上
程序編譯器:keil 4/keil 5
編程語(yǔ)言:C語(yǔ)言
設(shè)計(jì)編號(hào):S0030
主要功能:
本次課設(shè)是應(yīng)用單片機(jī)原理和控制理論設(shè)計(jì)音樂(lè)演奏控制器的硬件電路,并利用C語(yǔ)言進(jìn)行程序設(shè)計(jì)。通過(guò)控制單片機(jī)內(nèi)部的定時(shí)器來(lái)產(chǎn)生不同頻率的方波,驅(qū)動(dòng)喇叭發(fā)出不同的音調(diào)的音樂(lè),在利用延遲來(lái)控制發(fā)音時(shí)間的長(zhǎng)短。把樂(lè)譜轉(zhuǎn)化成相應(yīng)的定時(shí)常數(shù)就可以從發(fā)音設(shè)備中演奏出悅耳的音樂(lè)。
2 設(shè)計(jì)說(shuō)明
2.1設(shè)計(jì)要求
(1)利用I/O口產(chǎn)生一定頻率的方波,驅(qū)動(dòng)蜂鳴器,發(fā)出不同的音調(diào),從而演奏樂(lè)曲
(2)可以實(shí)現(xiàn)歌曲順序播放或單曲循環(huán);
(3)存儲(chǔ)三首以上歌曲
2.2 附加功能
(1)用數(shù)碼管顯示當(dāng)前播放曲目;
(2) LED花樣彩燈隨播放音符變換;
(3)通過(guò)按鍵實(shí)現(xiàn)下一首歌的切換;
以下為本設(shè)計(jì)資料展示圖:
仿真
開(kāi)始仿真
開(kāi)始仿真后,用數(shù)碼管顯示當(dāng)前播放曲目, LED花樣彩燈隨播放音符變換,通過(guò)按鍵實(shí)現(xiàn)下一首歌的切換。通過(guò)撥動(dòng)開(kāi)關(guān)控制單循環(huán)還是全部循環(huán)。內(nèi)置音樂(lè)《千年之戀》《寂寞沙洲冷》《七子之歌》《感恩的心》
原理圖PCB圖
程序設(shè)計(jì):
sbit key1=P3^2; /*暫停鍵 */
sbit key2=P3^3; /* 切換歌曲鍵*/
sbit fm=P1^1; /*蜂鳴器口*/
sbit P1_7=P1^7;
uchar count2=1; /*歌曲標(biāo)志*/
uchar timeh,timel,i;
uchar code DSY_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x70};
uint code tone_delay_table[]={ 64021,64103,64260,64400,64524,64580,64684,
64777,64820,64898,64968,65030,65058,65110,65157,65178};/*每個(gè)音符對(duì)應(yīng)的延遲*/
uchar code huayang[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x0f,0xf0,0x00};
void delay_ms(uchar x) /*延遲函數(shù)*/
{
uchar i;
while(x--)
for(i=0;i<120;i++);
}
uchar code qnzl[]={ /*千年之戀*/
0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x63,0x83,0x83,0x91,0x91,
0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff /*結(jié)束標(biāo)志*/
};
uchar code jmszl[]={ /*寂寞沙洲冷*/
0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
0x31,0x81,0x81,0x81,0x61,0x91,0x82,
0x51,0x51,0x51,0x51,0x31,0x61,0x53,
0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,0xff /*結(jié)束標(biāo)志*/
};
uchar code qizige[]={ /*七子之歌*/
0x54,0x32,0x52,0x32,0x54,0x62,0x52,0x32,0x62,0x54,
0x14,0x12,0x22,0x34,0x52,0x32,0x02,0x32,0x58,
0x52,0x52,0x62,0x52,0x32,0x54,0x52,0x62,0x52,0x82,0x62,0x58,
0x14,0x52,0x32,0x22,0x12,0x24,0x32,0x54,0x2,0x22,0x34,0x1f,0x18,
0x04,0x54,0xa4,0x92,0x81,0x62,0x52,0x54,0x62,0x64,0x52,0x62,0x81,0xa2,0x82,0x9c,
0x02,0x52,0xa4,0x92,0x81,0x62,0x52,0x54,0x62,0x64,0x52,0x64,0xa2,0x92,0x9f,
0xa4,0x92,0x81,0x62,0x52,0x54,0x84,0x62,0x52,0x32,0x22,0x14,
0x04,0x22,0x32,0x58,0x58,0x04,0x52,0x62,0x8f,
0xff /*結(jié)束標(biāo)志*/
};
uchar code ganen[]={ /*感恩的心*/
0x12,0x14,0x22,0x32,0x54,0x32,0x84,0x72,0x62,0x54,
0x02,0x62,0x62,0x52,0x54,0x12,0x22,0x32,0x28,
0x12,0x14,0x22,0x32,0x54,0x32,0x84,0x92,0x82,0x54,
0x32,0x24,0x21,0x61,0x54,0x22,0x32,0x12,0x18,
0x22,0x24,0x12,0x24,0x11,0x21,0x34,0x32,0x21,0x31,0x34,
0x12,0x22,0x22,0x22,0x11,0x21,0x22,0x64,0x52,0x52,0x32,0x38,
0x31,0x52,0x51,0x52,0x31,0x51,0x58,0x31,0x82,0x81,0x82,0x31,0x61,0x68,
0x62,0x62,0x62,0x51,0x61,0x64,0x61,0x82,0x61,0x9c,
0x52,0xa4,0x92,0xa4,0x32,0x84,0x72,0x64,0x62,0x94,0x82,0x94,
0x61,0x81,0x92,0x91,0x91,0x92,0x81,0xa1,0xa2,0x92,0x94,
0x52,0xa4,0x92,0xa4,0x32,0x84,0x72,0x64,
0x52,0x62,0x81,0x81,0x82,0x92,0xa2,0x94,0x82,0x72,0x82,0x88,
0xff /*結(jié)束標(biāo)志*/
};
/*簡(jiǎn)譜音調(diào)對(duì)應(yīng)的定時(shí)器初值*/
/*適合11.0592M的晶振*/
uchar code cuzhi[]={
0xff,0xff, /*占位*/
0xFC,0x8E, /*中央C調(diào)1~7*/
0xFC,0xED,
0xFD,0x43,
0xFD,0x6A,
0xFD,0xB3,
0xFD,0xF3,
0xFE,0x2D,
0xFE,0x47, /*高八度1~7*/
0xFE,0x76,
0xFE,0xA1,
0xFE,0xC7,
0xFE,0xD9,
0xFE,0xF9,
0xFF,0x16
};
void delay1(uint z); /* 延時(shí)1MS*/
void delay(uint z); /*延時(shí)165ms,既16分音符*/
void song();
void beep(); /*蜂鳴器叫一聲*/
void main()
{
P0=0xbf;
// KK=0;
EA=1;
EX0=1;
IT0=1;
EX1=1;
IT1=1;
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
while(1)
{
if(P1_7==1)
{
count2++;//歌曲遞增
if(count2==5)
count2=0;
}
if(count2!=0)
{
song();
delay1(1000);
}
}
}
void int0() interrupt 0
{
EA=0;
delay1(1);
if(key1==0)
{
count2=0;/*暫停音樂(lè)*/
TR0=0;
}
EA=1;
}
void int1() interrupt 2
{
TR0 = 0;
delay1(1); /*去抖*/
if(key2==0)
{
i=0;/*從頭開(kāi)始唱*/
count2++;
TR0=~TR0;
if(count2==5)
count2=0;
}
}
void timer0() interrupt 1/*用于產(chǎn)生各種音調(diào)*/
{
/* if(count2==0)
{
TH0 = tone_delay_table[keynum]/256;
TL0 = tone_delay_table[keynum]%256;
fm = ~fm;
} */
if(count2!=0) //放音樂(lè)
{
TH0=timeh;
TL0=timel;
fm=~fm;
}
}
void song()
{
uint temp;
uchar jp;
i=0;
while(1)
{
if(count2==0)
{
break;
}
if(count2==1) //選曲
temp=jmszl[i];
if(count2==2)
temp=qnzl[i];
if(count2==3)
temp=qizige[i];
if(count2==4)
temp=ganen[i];
if(temp==0xff)
break;
jp=temp/16;
P2 = huayang[jp];
P0=DSY_table[count2];
if(jp!=0)
{
timeh=cuzhi[jp*2];
timel=cuzhi[jp*2+1];
}
else
{
TR0=0;
fm=1;
}
delay(temp%16);
TR0=0;
fm=1;
delay1(10);
TR0=1;
i++;
}
TR0=0;
fm=1;
}
設(shè)計(jì)報(bào)告
系統(tǒng)組成框圖
本設(shè)計(jì)采用STC89C516RD+作為主控芯片(仿真可替換為STC89C51,STC89C52,AT89C51,AT89C52D等51單片機(jī)),蜂鳴器作為輸出設(shè)備產(chǎn)生音樂(lè),通過(guò)MOD鍵能實(shí)現(xiàn)播放模式的控制,數(shù)碼管(LED)能夠?qū)崟r(shí)的顯示當(dāng)前的歌曲號(hào)。其中P3.3外接按鍵,P0口用作數(shù)碼管輸出數(shù)據(jù)端口,P2口作LED輸出數(shù)據(jù)端口P1.1蜂鳴器端口。
程序設(shè)計(jì)
音調(diào)、節(jié)拍以及編碼的確定方法
一般說(shuō)來(lái),單片機(jī)演奏音樂(lè)基本都是單音頻率,它不包含相應(yīng)幅度的諧波頻率,也就是說(shuō)不能像電子琴那樣能奏出多種音色的聲音。因此單片機(jī)奏樂(lè)只需弄清楚兩個(gè)概念即可,也就是“音調(diào)”和節(jié)拍表示一個(gè)音符唱多長(zhǎng)的時(shí)間。
音調(diào)的確定
不同音高的樂(lè)音是用C、D、E、F、G、A、B來(lái)表示,這7個(gè)字母就是音樂(lè)的音名,它們一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成簡(jiǎn)譜的1、2、3、4、5、6、7,相當(dāng)于漢字“多來(lái)米發(fā)梭拉西”的讀音,這是唱曲時(shí)樂(lè)音的發(fā)音,所以叫“音調(diào)”,即Tone。把C、D、E、F、G、A、B這一組音的距離分成12個(gè)等份,每一個(gè)等份叫一個(gè)“半音”。兩個(gè)音之間的距離有兩個(gè)“半音”,就叫“全音”。在鋼琴等鍵盤(pán)樂(lè)器上,C–D、D–E、F–G、G–A、A–B兩音之間隔著一個(gè)黑鍵,他們之間的距離就是全音;E–F、B–C兩音之間沒(méi)有黑鍵相隔,它們之間的距離就是半音。通常唱成1、2、3、4、5、6、7的音叫自然音,那些在它們的左上角加上﹟號(hào)或者b號(hào)的叫變化音。﹟叫升記號(hào),表示把音在原來(lái)的基礎(chǔ)上升高半音,b叫降記音,表示在原來(lái)的基礎(chǔ)上降低半音。例如高音DO的頻率(1046Hz)剛好是中音DO的頻率(523Hz)的一倍,中音DO的頻率(523Hz)剛好是低音DO頻率(266 Hz)的一倍;同樣的,高音RE的頻率(1175Hz)剛好是中音RE的頻率(587Hz)的一倍,中音RE的頻率(587Hz)剛好是低音RE頻率(294 Hz)的一倍。
要產(chǎn)生音頻脈沖,只要算出某一音頻的周期(1/頻率),然后將此周期除以2,即為半周期的時(shí)間。利用定時(shí)器計(jì)時(shí)這半個(gè)周期時(shí)間,每當(dāng)計(jì)時(shí)到后就將輸出脈沖的I/O反相,然后重復(fù)計(jì)時(shí)此半周期時(shí)間再對(duì)I/O反相,就可在I/O腳上得到此頻率的脈沖。利用AT89C51的內(nèi)部定時(shí)器使其工作在計(jì)數(shù)器模式MODE1下,改變計(jì)數(shù)值TH0及TL0。此外結(jié)束符和休止符可以分別用代碼00H和FFH來(lái)表示,若查表結(jié)果為00H,則表示曲子終了;若查表結(jié)果為FFH,則產(chǎn)生相應(yīng)的停頓效果。例如頻率為523Hz,其周期T=1/523=1912us,因此只要令計(jì)數(shù)器計(jì)時(shí)956us/1us=956,在每次技術(shù)956次時(shí)將I/O反相,就可得到中音DO(523Hz)。
節(jié)拍的確定
若要構(gòu)成音樂(lè),光有音調(diào)是不夠的,還需要節(jié)拍,讓音樂(lè)具有旋律(固定的律動(dòng)),而且可以調(diào)節(jié)各個(gè)音的快滿度?!肮?jié)拍”,即Beat,簡(jiǎn)單說(shuō)就是打拍子,就像我們聽(tīng)音樂(lè)不自主的隨之拍手或跺腳。若1拍實(shí)0.5s,則1/4 拍為0.125s。至于1拍多少s,并沒(méi)有嚴(yán)格規(guī)定,就像人的心跳一樣,大部分人的心跳是每分鐘72下,有些人快一點(diǎn),有些人慢一點(diǎn),只要聽(tīng)的悅耳就好。音持續(xù)時(shí)間的長(zhǎng)短即時(shí)值,一般用拍數(shù)表示。休止符表示暫停發(fā)音。
一首音樂(lè)是由許多不同的音符組成的,而每個(gè)音符對(duì)應(yīng)著不同頻率,這樣就可以利用不同的頻率的組合,加以與拍數(shù)對(duì)應(yīng)的延時(shí),構(gòu)成音樂(lè)。了解音樂(lè)的一些基礎(chǔ)知識(shí),我們可知產(chǎn)生不同頻率的音頻脈沖即能產(chǎn)生音樂(lè)。對(duì)于單片機(jī)來(lái)說(shuō),產(chǎn)生不同頻率的脈沖是非常方便的,利用單片機(jī)的定時(shí)/計(jì)數(shù)器來(lái)產(chǎn)生這樣的方波頻率信號(hào)。因此,需要弄清楚音樂(lè)中的音符和對(duì)應(yīng)的頻率,以及單片機(jī)定時(shí)計(jì)數(shù)的關(guān)系。
音符編碼
do re mi fa so la si分別編碼為1~7,重音do編為8,重音re編為9,停頓編為0。播放長(zhǎng)度以十六分音符為單位(在本程序中為165ms),一拍即四分音符等于4個(gè)十六分音符,編為4,其它的播放時(shí)間以此類推。音調(diào)作為編碼的高4位,而播放時(shí)間作為低4位,如此音調(diào)和節(jié)拍就構(gòu)成了一個(gè)編碼。以0xff作為曲譜的結(jié)束標(biāo)志。比如,音調(diào)do,發(fā)音長(zhǎng)度為兩拍,即二分音符,將其編碼為0x18。
歌曲播放的設(shè)計(jì)。先將歌曲的簡(jiǎn)譜進(jìn)行編碼,儲(chǔ)存在一個(gè)數(shù)據(jù)類型為unsigned char 的數(shù)組中。程序從數(shù)組中取出一個(gè)數(shù),然后分離出高4位得到音調(diào),接著找出相應(yīng)的值賦給定時(shí)器0,使之定時(shí)操作蜂鳴器,得出相應(yīng)的音調(diào);接著分離出該數(shù)的低4位,得到延時(shí)時(shí)間,接著調(diào)用軟件延時(shí)。
主程序流程圖
實(shí)物圖
資料清單(提供資料清單所有文件):
仿真+代碼+原理圖PCB+設(shè)計(jì)報(bào)告+講解視頻