您好,歡迎來電子發(fā)燒友網(wǎng)! ,新用戶?[免費(fèi)注冊]

您的位置:電子發(fā)燒友網(wǎng)>源碼下載>C/C++語言編程>

利用C程序?qū)崿F(xiàn)音樂點(diǎn)唱功能及幾種案例分享

大?。?/span>0.15 MB 人氣: 2017-09-06 需要積分:0
利用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 (j=0;j<125;j++)??//?j循環(huán)?
???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 (j=0;j<125;j++)???// j循環(huán)?
???for (k=0;k<120;k++);?// k循環(huán)?
}?????????// 結(jié)束節(jié)拍函數(shù)?
?
?
?

非常好我支持^.^

(0) 0%

不好我反對

(0) 0%

      發(fā)表評論

      用戶評論
      評價(jià):好評中評差評

      發(fā)表評論,獲取積分! 請遵守相關(guān)規(guī)定!

      ?