利用C程序?qū)崿F(xiàn)音樂點(diǎn)唱功能及幾種案例分享
大?。?/span>0.15 MB 人氣: 2017-09-06 需要積分:0
推薦 + 挑錯(cuò) + 收藏(0) + 用戶評論(0)
標(biāo)簽:C語言(117708)音樂(22712)
利用C程序?qū)崿F(xiàn)音樂點(diǎn)唱功能及幾種案例分享1、快樂點(diǎn)唱機(jī)實(shí)驗(yàn)
/* ch09-5-4.c_ 快樂點(diǎn)唱機(jī)實(shí)驗(yàn) */#include?
sbit? buzzer = P3^7;???// 聲明輸出端?
unsigned char? keys,i;???// 聲明按鈕及播放譜變量?
unsigned char? tone_H, tone_L;?// 聲明計(jì)時(shí)量變量?
void? beat_125(unsigned char);?// 聲明節(jié)拍函數(shù)?
unsigned char code song1[]={?1, 1, 2, 1,? 4, 3,? 1, 1, 2, 1,? 5, 4,
????????1, 1, 8, 6,? 4, 3, 2,? 11, 11, 6, 4,? 5, 4,? 15 };
unsigned char code beat1[]={?4, 4, 8, 8,? 8, 16,? 4, 4, 8, 8,? 8, 16,
????????4, 4, 8, 8,? 8, 8, 8,? 4, 4, 8, 8,? 8, 16 };
unsigned char code song2[]={?1, 1, 1,? 3, 2, 1,? 3, 3, 3,? 5, 4, 3,? 5, 4, 3,? 2,
????????2, 1, 0,? 1, 2, 3,? 4, 3, 2,? 3, 4, 5,? 5, 4, 3, 2,? 1, 15};
unsigned char code beat2[]={?4, 4, 4,? 6, 2, 4,? 4, 4, 4,? 6, 2, 4,? 4, 4, 4,? 12 ,
???????? ?8, 2, 2,? 4, 4, 4,? 8, 2, 2,? 4, 4, 4,? 2, 2, 4, 4,? 12 };
unsigned char code song3[]={?8, 7, 6, 5, 5,? 6, 8, 5,? 6, 5, 3, 2, 5,? 3, 12,
???????? ?8, 7, 6, 5, 5,? 6, 8, 5,? 6, 5, 3, 2, 5,? 1, 12,
???????? ?2, 3, 2, 5,?? 6, 5, 6, 8,?? 9, 8, 7, 6, 9,? 5, 12,
???????? ?6, 8, 5, 6,? 3, 5, 6, 5, 3,? 2, 3, 5, 3, 2,? 1, 0,? 15 };
unsigned char code beat3[]={?8, 4, 4, 8, 8,? 8, 8, 16,? 8, 4, 4, 8, 8,? 24, 8,
????????8, 4, 4, 8, 8,? 8, 8, 16,? 8, 4, 4, 8, 8,? 24, 8,
????????8, 4, 4, 16,? 8, 4, 4, 16,? 8, 4, 4, 8, 8,? 24, 8,
????????8, 8, 8, 8,? 8, 4, 4, 8, 8,? 8, 4, 4, 8, 8,? 24, 8 };
unsigned char code song4[]={?2, 2, 3, 5,? 6, 5, 6, 7,? 9, 7, 7, 6, 5,? 6,
????????7, 9, 9, 7, 9,? 5, 6, 6,? 2, 7, 7, 6, 5,? 5,
????????6, 6, 7, 6, 5,? 3, 2, 3, 5,? 3, 5, 6, 7,? 9,
????????9, 9, 10, 9, 7,? 7, 6, 5, 3,? 2, 7, 7, 6, 5,? 5,? 15};
unsigned char code beat4[]={?12, 4, 8, 8,? 8, 4, 4, 16,? 12, 4, 4, 4, 8,? 32,
????????12, 4, 8, 4, 4,? 12, 4, 16,? 12, 4, 8, 4, 4,? 32,?
????????12, 4, 8, 4, 4,? 8, 4, 4, 16,? 12, 8, 8, 8,? 32,?
????????12, 4, 8, 4, 4,? 8, 4, 4, 16,? 12, 4, 8, 4, 4,? 32? };
unsigned int code tone[]={?1012, 956, 852, 759, 716, 638,
???????568, 506, 478, 426, 379,531, 10 };//音階定義?
void play1(void); ????// 聲明play1函數(shù)?
void play2(void); ????// 聲明play2函數(shù)?
void play3(void); ????// 聲明play3函數(shù)?
void play4(void); ????// 聲明play4函數(shù)?
void beat_125(unsigned char);?// 聲明節(jié)拍函數(shù)?
//================================================
main()
{?buzzer=1;?????// 蜂 器初始值?
?IE=0x82; ?????// 啟用Timer 0
?TMOD=0x01;??? ??? ?// 設(shè)定MODE1???
?while (1)?????//?while循環(huán)?
?{?P2=0xff;????// 將Port2規(guī)劃為輸入埠?
??keys=~P2;????// 讀取按鈕
??switch (keys)???// 判讀?
??{?case 0x01: play1(); break;?// 按下S1,播放第一首歌?
???case 0x02: play2(); break;?// 按下S2,播放第二首歌?
???case 0x04: play3(); break;?// 按下S3,播放第三首歌?
???case 0x08: play4(); break;?// 按下S4,播放第四首歌?
??} ?buzzer=1;???// 蜂 器不動(dòng)作?
?}???????//?while循環(huán)結(jié)束?
}????????// 主程序結(jié)束?
//==第一首歌=================================
void play1(void)
{?i=0;
?while(song1[i]!=15)???//?while循環(huán)開始?
?{?tone_H=(65536-tone[song1[i]])/256;// 讀取音階計(jì)數(shù)量之高八位元?
??tone_L=(65536-tone[song1[i]])%256;// 讀取音階計(jì)數(shù)量之低八位元?
??TH0=tone_H;????// 填入音階計(jì)數(shù)量之高八位元?
??TL0=tone_L;????// 填入音階計(jì)數(shù)量之低八位元?
??TR0=1;?????// 啟動(dòng)Timer 0
??beat_125(beat1[i]);??// 指定節(jié)拍?
??i++;?????// 下一個(gè)音?
??TR0=0;?????// 關(guān)閉Timer 0
?}???????// 結(jié)束播放?
}?????
//==第二首歌=================================
void play2(void)
{?i=0;
?while(song2[i]!=15)???// while循環(huán)開始?
?{?tone_H=(65536-tone[song2[i]])/256;// 讀取音階計(jì)數(shù)量之高八位元?
??tone_L=(65536-tone[song2[i]])%256;// 讀取音階計(jì)數(shù)量之低八位元?
??TH0=tone_H;????// 填入音階計(jì)數(shù)量之高八位元?
??TL0=tone_L;????// 填入音階計(jì)數(shù)量之低八位元?
??TR0=1;?????// 啟動(dòng)Timer 0
??beat_125(beat2[i]);??// 指定節(jié)拍?
??i++;?????// 下一個(gè)音?
??TR0=0;?????// 關(guān)閉Timer 0
?}????
}????????// 結(jié)束播放?
//==第三首歌=================================
void play3(void)
{?i=0;
?while(song3[i]!=15)???// while循環(huán)開始?
?{?tone_H=(65536-tone[song3[i]])/256;// 讀取音階計(jì)數(shù)量之高八位元?
??tone_L=(65536-tone[song3[i]])%256;// 讀取音階計(jì)數(shù)量之低八位元?
??TH0=tone_H;????// 填入音階計(jì)數(shù)量之高八位元?
??TL0=tone_L;????// 填入音階計(jì)數(shù)量之低八位元?
??TR0=1;?????// 啟動(dòng)Timer 0
??beat_125(beat3[i]);??// 指定節(jié)拍?
??i++;?????// 下一個(gè)音?
??TR0=0;?????// 關(guān)閉Timer 0
?}????
}????????// 結(jié)束播放?
//==第四首歌=================================
void play4(void)
{?i=0;
?while(song4[i]!=15)???//?while循環(huán)開始?
?{?tone_H=(65536-tone[song4[i]])/256;// 讀取音階計(jì)數(shù)量之高八位元?
??tone_L=(65536-tone[song4[i]])%256;// 讀取音階計(jì)數(shù)量之低八位元?
??TH0=tone_H;????// 填入音階計(jì)數(shù)量之高八位元?
??TL0=tone_L;????// 填入音階計(jì)數(shù)量之低八位元?
??TR0=1;?????// 啟動(dòng)Timer 0
??beat_125(beat4[i]);??// 指定節(jié)拍?
??i++;?????// 下一個(gè)音?
??TR0=0;?????// 關(guān)閉Timer 0
?}????
}????????// 結(jié)束播放?
//====Timer 0中斷子程序==================
void tone_timer(void) interrupt 1?//?Timer 0中斷子程序開始?
{?TH0=tone_H;?????// 填入計(jì)時(shí)量之高八位元?
?TL0=tone_L;?????// 填入計(jì)時(shí)量之低八位元??
?buzzer=~buzzer;????// 蜂 器反相輸出?
}????????// 結(jié)束中斷子程序?
//====節(jié)拍函數(shù)==================
void beat_125(unsigned char x)?// 節(jié)拍函數(shù)開始?
{?unsigned char i,j,k;??// 聲明變量?
?for (i=0;i
???for (k=0;k<120;k++);//?k循環(huán)??
}????????// 結(jié)束節(jié)拍函數(shù)
?
2、生日快樂歌實(shí)驗(yàn)
/* 生日快樂歌實(shí)驗(yàn)(ch09-5-3.c) */#include
sbit? buzzer = P3^7;???// 聲明輸出端?
unsigned char? i=0;????// 聲明變量?
unsigned char? tone_H, tone_L;?// 聲明計(jì)時(shí)量變量?
void? beat_125(unsigned char);?// 聲明節(jié)拍函數(shù)?
unsigned char code song[]={?1, 1, 2, 1,? 4, 3,? 1, 1, 2, 1,? 5, 4,?
???????1, 1, 8, 6,? 4, 3, 2,? 11, 11, 6, 4,? 5, 4,? 15};//歌曲?
unsigned char code beat[]={?4, 4, 8, 8,? 8, 16,? 4, 4, 8, 8,? 8, 16,?
???????4, 4, 8, 8,? 8, 8, 8,? 4, 4, 8, 8,? 8, 16};??//節(jié)拍?
unsigned int code tone[]={?1012, 956, 852, 759, 716, 638,? ????// 中音Si ̄So
???????568, 506, 478, 426, 379, 536, 10 };????//音階定義?
// ========主程式 ===============
main()
{?buzzer=1;???// 蜂鳴器初始值?
?IE=0x82; ???// 啟用Timer 0
?TMOD=0x01;???? ??// 設(shè)定MODE1???
?while(song[i]!=15)?// while循環(huán)開始?
?{?TH0=tone_H=(65536-tone[song[i]])/256;?//填入音階計(jì)數(shù)量之高八位元?
??TL0= tone_L=(65536-tone[song[i]]) % 256;//填入音階計(jì)數(shù)量之低八位元?
??TR0=1;???// 啟動(dòng)Timer 0
??beat_125(beat[i]);?// 指定節(jié)拍?
??i++;???// 下一個(gè)音?
??TR0=0;???// 關(guān)閉T0停止播放?
??buzzer=1;??// 蜂鳴器不動(dòng)作?
?}?????// 結(jié)束播放?
}??????// 主程式結(jié)束?
//====Timer 0中斷副程式==================
void tone_timer(void) interrupt 1?// Timer 0中斷副程式開始?
{?TH0=tone_H;???// 填入計(jì)時(shí)量之高八位元?
?TL0=tone_L;???// 填入計(jì)時(shí)量之低八位元?
?buzzer=~buzzer;??// 蜂 器反相輸出?
}??????// 結(jié)束中斷副程式?
//====節(jié)拍函數(shù)==================
void beat_125(unsigned char x)??// 節(jié)拍函數(shù)開始?
{?unsigned char? i,j,k;???// 聲明變量?
?for (i=0;i
???for (k=0;k<120;k++);?// k循環(huán)?
}?????????// 結(jié)束節(jié)拍函數(shù)?
?
?
?
非常好我支持^.^
(0) 0%
不好我反對
(0) 0%
下載地址
利用C程序?qū)崿F(xiàn)音樂點(diǎn)唱功能及幾種案例分享下載
相關(guān)電子資料下載
- 基于STM32F429芯片的單片機(jī)芯片內(nèi)存映射圖 154
- C語言編寫程序執(zhí)行效率高的原因是什么? 31
- FPGA協(xié)處理的優(yōu)勢有哪些?如何去使用FPGA協(xié)處理? 887
- Python簡單可視化音樂:提取音樂頻譜 81
- 炫酷好玩的像素時(shí)鐘&amp;音樂頻譜,合宙ESP32-C3開發(fā)板創(chuàng)意DIY分享 122
- 辛苦整理的C語言筆記,一學(xué)就會(huì) 56
- 帶參數(shù)宏定義易出現(xiàn)的隱藏bug和定義方式歸納 57
- 快來!炫酷好玩的像素時(shí)鐘&音樂頻譜,合宙ESP32-C3開發(fā)板創(chuàng)意DIY分享 36
- ARM9中斷中(C語言)如何實(shí)現(xiàn)復(fù)位功能,而不是返回中斷前的狀態(tài)? 131
- C語言之GNU C擴(kuò)展語法應(yīng)用 27