單片機(jī)音樂盒設(shè)計(jì)_第1頁(yè)
單片機(jī)音樂盒設(shè)計(jì)_第2頁(yè)
單片機(jī)音樂盒設(shè)計(jì)_第3頁(yè)
單片機(jī)音樂盒設(shè)計(jì)_第4頁(yè)
單片機(jī)音樂盒設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、基于單片機(jī)的帶彩燈外觀的音樂盒摘 要:隨著人類社會(huì)的發(fā)展,人們對(duì)視覺、聽覺方面的享受提出了越來(lái)越高的要求。小小的音樂盒可以給人們帶來(lái)美好的回憶,提高人們的精神文化享受。傳統(tǒng)音樂盒多是機(jī)械型的,體積笨重,發(fā)音單調(diào),不能實(shí)現(xiàn)批量生產(chǎn)。本文設(shè)計(jì)的音樂盒是以AT89C51單片機(jī)為核心元件的電子式音樂盒,體積小,重量輕,能演奏音樂,功能多,外觀效果多彩,配有彩燈,使用方便,本音樂盒有三個(gè)按鍵,key1控制彩燈,key2控制音樂,key3為總開關(guān),可同時(shí)關(guān)閉音樂與彩燈。具有一定的商業(yè)價(jià)值。關(guān)鍵字:AT89C51;音樂盒;按鍵;彩燈Abstract: Along with the development o

2、f human society, people of vision, hearing things put forward higher request. Small music box can bring good memories and improve peoples spiritual culture. Traditional music box is heavy mechanical type, size, pronunciation and drab, cannot achieve batch production. Music box designed in this paper

3、 based on AT89C51 microcontroller as the core element of electronic music box, small size, light weight, can play music, multi-function, appearance and colorful, with a lantern,easy to use. The music box with three buttons , The key1 control Lantern, key2 control music, key3 total switch can turn of

4、f the music and lanterns. Have some commercial value.Keywords: AT89C51, music boxes, buttons, Lantern 1 引言1.1設(shè)計(jì)目的隨著電子技術(shù)及計(jì)算機(jī)技術(shù)的發(fā)展,單片機(jī)在日常生活中的應(yīng)用越來(lái)越廣泛,同時(shí)也顯現(xiàn)出了它的重要性,尤其在一些特殊的場(chǎng)合更是不可代替。為熟悉單片機(jī)的應(yīng)用。要求設(shè)計(jì)一個(gè)基于AT89C51單片機(jī)的帶彩燈外觀的音樂盒。1.2設(shè)計(jì)要求音樂盒有三個(gè)按鍵key1,key2,key3,其中key1控制彩燈的樣式,本設(shè)計(jì)有六種彩燈花樣,每按一次變換一種樣式。Key2控制音樂播放,本設(shè)計(jì)有五首音

5、樂,每按一次變換到下一首,同時(shí)八路LED燈隨音調(diào)不同而閃爍。Key3為總開關(guān)用于關(guān)閉音樂跟彩燈。2設(shè)計(jì)方案及原理2.1設(shè)計(jì)原理音調(diào)之所以不同是因?yàn)轭l率的不同。根據(jù)每個(gè)音調(diào)的頻率單片機(jī)內(nèi)部的定時(shí)計(jì)數(shù)器來(lái)產(chǎn)生相應(yīng)的音樂脈沖。只要算出某一音頻的周期(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ù)器工作在方式1下,改變初值TH0及TL0以產(chǎn)生不同頻率。 此外結(jié)束符和休止符可以分別用代碼0xff和0x00來(lái)表示,若查表結(jié)果為0

6、xff,則表示曲子終了;若查表結(jié)果為0x00,則產(chǎn)生相應(yīng)的停頓效果。 例如頻率為523Hz,其周期T=1/523=1912us,定時(shí)時(shí)間為1912/2=956us因此只要令計(jì)數(shù)器計(jì)時(shí)956us/1us=956次(若晶振頻率為6MHZ),在每次計(jì)數(shù)956次時(shí)將I/O反相,就可得到中音DO(523Hz)。根據(jù)計(jì)數(shù)次數(shù)就可算出計(jì)數(shù)初值。要產(chǎn)生音樂光有音調(diào)是不行的,每個(gè)音調(diào)還有持續(xù)一定的時(shí)間。這可以用延時(shí)程序delay()來(lái)實(shí)現(xiàn)。因此每個(gè)音樂代碼有八位數(shù)字組成,高四位控制音調(diào),低四位通過延時(shí)程序?qū)υ撘粽{(diào)進(jìn)行延時(shí)。通過子函數(shù)連續(xù)不斷的調(diào)用音樂代碼就產(chǎn)生了動(dòng)聽的音樂。音調(diào)的頻率與時(shí)間都不同,讓DO、RE、

7、MI、FA、SO、LA、SI分別對(duì)應(yīng)八路LED燈中的一個(gè),這樣播放音樂的同時(shí)就有LED燈對(duì)應(yīng)閃爍。2.2總體設(shè)計(jì)思想圖1思路圖如圖1所示按鍵部分有三個(gè)鍵,分別結(jié)于外部中斷P3.2/INT0,P3.3/INT1,及串行口P3.1/TXD上,P3.2每產(chǎn)生一次中斷count1加一,根據(jù)count1的值來(lái)決定花樣燈的種類。P3.3每產(chǎn)生一次中斷count2加一,根據(jù)count2的數(shù)值來(lái)決定播放哪一首歌曲。P3.1接總開關(guān)的。播放音樂,及花樣燈均通過調(diào)用子程序來(lái)實(shí)現(xiàn)。八路LED燈燈接到P1口。揚(yáng)聲器接到P3.7上。總體設(shè)計(jì)電路,如圖2。 圖2設(shè)計(jì)圖3硬件設(shè)計(jì)電路由AT89C51,八路LED燈,排阻,三

8、個(gè)開關(guān),揚(yáng)聲器及晶振組成,搭建如圖3。圖3硬件圖4軟件設(shè)計(jì)根據(jù)設(shè)計(jì)要求及其原理繪制程序流程圖,如圖4,,根據(jù)流程圖,在Keil中編寫程序,并不斷編譯跟調(diào)試,直至沒有任何錯(cuò)誤和警告。在keil中該程序能夠運(yùn)行證明編寫成功。程序見附錄2。圖4流程圖5系統(tǒng)仿真及實(shí)際調(diào)試按照實(shí)驗(yàn)電路圖在proteus中找到相應(yīng)的元器件,并搭建電路,將相應(yīng)元器件的規(guī)格調(diào)到合適值。在keil中生成HEX文件,裝入單片機(jī)中運(yùn)行,按key1可看到花樣燈閃爍,每按一次變化一種樣式。按key2可聽到動(dòng)聽的音樂,每按一次變化為下一首歌。按key3可以同時(shí)關(guān)閉燈跟歌曲。按鍵均可實(shí)現(xiàn)要求的功能,proteus截圖見附錄1。6總結(jié) 上學(xué)

9、期通過對(duì)單片機(jī)原理及應(yīng)用系統(tǒng)設(shè)計(jì)的學(xué)習(xí),使我對(duì)單片機(jī)的硬件結(jié)構(gòu),指令系統(tǒng),以及系統(tǒng)設(shè)計(jì)有了一定的認(rèn)識(shí),在這基礎(chǔ)之上進(jìn)行單片機(jī)設(shè)計(jì),可以說是輕松了不少,然而在實(shí)際設(shè)計(jì)的過程中依然暴露出許多問題。例如我對(duì)單片機(jī)的中斷系統(tǒng)就了解不夠深,這使得我在設(shè)計(jì)按鍵控制的時(shí)候,面臨很大的問題,最終我通過重新翻閱課本及相關(guān)資料,克服了這一困難。在程序設(shè)計(jì)的過程中,也出現(xiàn)了不少問題,例如在總開關(guān)程序的編寫中,我就無(wú)從下手。最終我通過向老師答疑得以解決。此次課程設(shè)計(jì)也使我對(duì)C編程的有關(guān)知識(shí)得以溫習(xí)和重新學(xué)習(xí)。 總之可以說是收益匪淺。參考文獻(xiàn)1王思明,張金敏,茍軍年等. 單片機(jī)原理及應(yīng)用系統(tǒng)設(shè)計(jì)M. 北京:科學(xué)出版社,

10、2012.2李云鋼,周逢興,龍志強(qiáng). 單片機(jī)原理與系統(tǒng)設(shè)計(jì)M. 北京:中國(guó)水利水電出版社,2008.3譚浩強(qiáng). C語(yǔ)言程序設(shè)計(jì)(第四版) M. 北京:清華大學(xué)出版社,2010.4風(fēng)波親,吳寧. 微型計(jì)算機(jī)原理與接口技術(shù)M. 北京:清華大學(xué)出版社,2008.3.5吳秀清,周荷琴. 微型計(jì)算機(jī)與接口技術(shù)M. 合肥:中國(guó)科學(xué)技術(shù)大學(xué)出版社,2002.6宋戈等. 單片機(jī)應(yīng)用開發(fā)范例大全M. 北京:人民郵電出版社,2010.5.附錄1圖5仿真圖附錄2源程序#include #define uchar unsigned char#define uint unsigned intsbit key1=P32;

11、/按key1可切換花樣sbit key2=P33;/按key2可切換歌曲sbit key3=P31;/總開關(guān),用于總體控制花樣燈跟歌曲。sbit fm=P37;/蜂鳴器連續(xù)到I/O口P37上/-六種花樣燈閃爍代碼-uchar code huayang1=0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf;/花樣燈1uchar code huayang2=0x7f,0xfe,0xbf,0xfd,0xdf,0xfb,0xef,0xf7,0xef,0xfb,0xdf,0xfd,0xbf,0xfe;/花樣燈2uch

12、ar code huayang3=0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff;/花樣燈3uchar code huayang4=0x3f,0x9f,0xcf,0xe7,0xf3,0xf9,0xfc, 0xf9,0xf3,0xe7,0xcf,0x9f,0x3f; /花樣燈4uchar code huayang5=0x3f,0xfc,0x9f,0xf9,0xcf,0xf3,0xe7, 0xcf,0xf3,0x9f,0xf9,0x3f,0xfc; /花樣燈5uchar code huay

13、ang6=0x55,0xaa,0xcc,0x33,0x99,0x66,0x0f,0xf0; /花樣燈6uchar count1;/花樣標(biāo)志uchar count2;/歌曲標(biāo)志uchar timeh,timel,i;/-音樂代碼-/音樂代碼中,字節(jié)高位控制頻率,即為音調(diào)。字節(jié)低位控制該音調(diào)持續(xù)的時(shí)間,/代表多少個(gè)十六分音符。/1-7代表中央C調(diào),8-E代表高八度,0代表停頓/最后的0是結(jié)束標(biāo)志即0xff。uchar code yldbwdx= /月亮代表我的心0x02,0x82,0x16,0x32,0x54,0x02,0x52,0xA6,0x32,0x54,0x02,0x52,0x64,0x74

14、,0xB6,0x64,0x52,0x5C,0x32,0x22,0x16,0x12,0x14,0x32,0x22,0x16,0x12,0x14,0x22,0x32,0x26,0x12,0x94,0x22,0x32,0x2C,0x32,0x52,0x36,0x22,0x14,0x54,0xAC,0x92,0xA2,0x96,0xA2,0x96,0x82,0x3C,0x54,0x36,0x22,0x14,0x54,0xAC,0x92,0xA2,0x16,0x12,0x14,0x22,0x32,0x2C,0x02,0x82,0x16,0x32,0x56,0x12,0xA6,0x32,0x56,0x52

15、,0x66,0x72,0xB6,0x62,0x1C,0xff/結(jié)束標(biāo)志;uchar code kdqg=/康定情歌0x64,0x84,0x92,0x91,0x81,0x93,0x61,0x54,0x62,0x82,0x92,0x91,0x81,0x92,0x64,0x62,0x82,0x92,0x91,0x81,0x93,0x61,0x54,0x82,0x62,0x51,0x61,0x51,0x41,0x52,0x24,0x92,0x54,0x82,0x64,0x52,0x42,0x94,0x82,0x62,0x51,0x61,0x51,0x41,0x52,0x24,0xff;uchar co

16、de lzh=/國(guó)歌0x03,0x04,0x04,0xc2,0x14,0x12,0x12,0x12,0x11,0x11,0xc2,0xd1,0xe1,0x14,0x02,0x32,0x12,0x21,0x31,0x52,0x51,0x51,0x54, 0x32,0x31,0x31,0x12,0x11,0x31,0x52,0x51,0x31,0x24,0x24,0x24,0x64,0x54,0x24,0x34,0x52,0x32,0x21,0x31,0x12,0x11,0x21,0x34,0x04, 0x52,0x51,0xd1,0x12,0x12,0x32,0x31,0x31,0x52,0x5

17、1,0x51,0x22,0x22,0x22,0xd2,0xd1,0x24,0x22,0xc2,0x14,0x12,0x34,0x32,0x32,0x54,0x54,0x54,0x54,0x12,0x11,0x31,0x52,0x51,0x51,0x64,0x54,0x32,0x31,0x11,0x52,0x52,0x52,0x32,0x02,0x12,0x02,0xc4,0x14,0xa2,0xa1,0x11,0x52,0x52,0x52,0x32,0x02,0x12,0x02,0xc4,0x14,0xc4,0x14,0xc4,0x14,0x14,0x04,0x04,0x04,0x04,0x0

18、4,0xff;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

19、,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,0x2

20、1,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;uchar code qnzl= /千年之戀0x12,0x22,0x34,0x84,0

21、x74,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,0x

22、82,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

23、,0x31,0x36, 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53, 0x42,0x82,0x88,0x02,0x74,0x93,0x89, 0xff;/-不同音調(diào)對(duì)應(yīng)的定時(shí)器初值-/適合11.0592M的晶振uchar code chuzhi= 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, 0xF

24、E,0xD9, 0xFE,0xF9, 0xFF,0x16 ;uchar yinyue=0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x0,0x0;/將音調(diào)轉(zhuǎn)化為對(duì)應(yīng)的LED樣式void delay1(uint z);/延時(shí)1MSvoid delay(uint z);/延時(shí)165MS,即十六分音符void song();void beep();/蜂鳴器叫一聲/-主函數(shù)-main()uchar x; count1=0;/流水燈無(wú)花樣count2=1;/唱第一首歌EA=1;/開總中斷EX0=1;/開外部中斷0IT0=1;/外部中斷0下降沿觸發(fā)方式EX1=

25、1;/開外部中斷1IT1=1;/外部中斷1下降沿觸發(fā)方式TMOD=0x01;/定時(shí)器0工作在方式1TH0=0;TL0=0;ET0=1;while(1) if(count1!=0)switch(count1)case 1:for(x=0;x14;x+) if(key3=1)P1=0xff;fm=0;break;P1=huayang1x;beep();delay1(300);if(count1!=1)break;break;case 2:for(x=0;x14;x+)if(key3=1)P1=0xff;fm=0;break;P1=huayang2x;beep();delay1(300);if(co

26、unt1!=2)break;break;case 3:for(x=0;x16;x+)if(key3=1)P1=0xff;fm=0;break;P1=huayang3x;beep();delay1(300);if(count1!=3)break;break;case 4:for(x=0;x13;x+)if(key3=1)P1=0xff;fm=0;break;P1=huayang4x;beep();delay1(300);if(count1!=4)break;break;case 5:for(x=0;x13;x+) if(key3=1)P1=0xff;fm=0;break;P1=huayang5x;beep();delay1(300);if(count1!=5)break;break;case 6:for(x=0;x8;x+) if(key3=1)P1=0xff;fm=0;break;P1=huayang6x;beep();delay1(300);if(count1!=6)break; break;/elseif(count1=0)song();delay1(1000); void int0() interrupt 0EA=0;/關(guān)總中斷delay1(1);/去抖if(k

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論