新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STC89C52單片機(jī)蜂鳴器唱歌實(shí)驗(yàn)

STC89C52單片機(jī)蜂鳴器唱歌實(shí)驗(yàn)

作者: 時(shí)間:2016-11-21 來(lái)源:網(wǎng)絡(luò) 收藏
////////////////////////////////////////////////////////////////////////

實(shí)現(xiàn)功能:通過(guò)對(duì)單片機(jī)定時(shí)器的配置,讓其產(chǎn)生不同頻率的方波,蜂鳴器根據(jù)頻
率的不同而發(fā)出不同的聲音,來(lái)達(dá)到唱歌的目的
實(shí)驗(yàn)板型號(hào):KBL-XYD-C52
實(shí)驗(yàn)名稱(chēng): 蜂鳴器唱歌實(shí)驗(yàn)
編寫(xiě)人: 謝應(yīng)東
編寫(xiě)日期: 2012-4-26
////////////////////////////////////////////////////////////////////////
#include

本文引用地址:http://m.butianyuan.cn/article/201611/319236.htm

#define uchar unsigned char
#define uint unsigned int

uchar cCount;

sbit FMQ =P2^4; //定義實(shí)驗(yàn)板上蜂鳴器器控制腳


unsigned char code SONG[] ={ //祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
//路邊的野華不要采
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,

};
////////////////////////////////////////////////////////////////////////
函數(shù)名稱(chēng):Time0_Init
函數(shù)功能:定時(shí)器的初始化配置,使其能中斷,中斷的時(shí)間間隔為10ms
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):注意此時(shí)實(shí)驗(yàn)板上用到的晶振為11.0592M的,要選用其他的,得重新配置
////////////////////////////////////////////////////////////////////////
void Init_Time0()
{
TMOD = 0x01; //設(shè)置定時(shí)器0為模式1(16bit)

TH0 = 0xDC; //給定時(shí)器0高八位賦初值
TL0 = 0x00; //給定時(shí)器0第八位賦初值

ET0 = 1; //允許定時(shí)器0中斷
EA = 1; //打開(kāi)全局中斷
}
////////////////////////////////////////////////////////////////////////
函數(shù)名稱(chēng):Time0_Routine
函數(shù)功能:定時(shí)器0中斷服務(wù)函數(shù),重新給定時(shí)器0賦值,同時(shí)把全局變量cCount加1
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):無(wú)
////////////////////////////////////////////////////////////////////////
void Time0_Routine() interrupt 1 using 1
{
TH0 = 0xDC;//重新給高八位賦新值
TL0 = 0x00;//重新給第八位賦新值
cCount++;//計(jì)數(shù)變量加1
}
////////////////////////////////////////////////////////////////////////
函數(shù)名稱(chēng):DelayUs
函數(shù)功能:延時(shí)函數(shù),通過(guò)示波器測(cè)試,此函數(shù)每執(zhí)行一次大概能延時(shí)55us
參數(shù)介紹:iDelay_Us:執(zhí)行此延時(shí)函數(shù)的次數(shù)
iNumber1:第一個(gè)for語(yǔ)句所用的記錄循環(huán)的變量
iNumber2:第二個(gè)for語(yǔ)句所用的記錄循環(huán)的變量
返回值: 無(wú)
注意事項(xiàng):無(wú)
////////////////////////////////////////////////////////////////////////
void DelayUs(unsigned int iDelay_Us)
{
uint iNumber1,iNumber2;
for(iNumber1=0;iNumber1 {
for(iNumber2=0;iNumber2<3;iNumber2++);
}
}
////////////////////////////////////////////////////////////////////////
函數(shù)名稱(chēng):FMQ_PlaySong
函數(shù)功能:控制實(shí)驗(yàn)板上與蜂鳴器相連的引腳輸出不同的頻率的方波,依次來(lái)控制蜂鳴
器唱歌
參數(shù)介紹:cNumber:歌曲的選擇,0代表“祝你平安”,‘1’代表“路邊的野花不要采”
返回值: 無(wú)
注意事項(xiàng):無(wú)
////////////////////////////////////////////////////////////////////////
void FMQ_PlaySong(unsigned char cNumber)
{
uchar cTemp1,cTemp2;
uint iAddr;
cCount = 0;//中斷計(jì)數(shù)器清0
iAddr = cNumber * 217;//歌曲的地址,可以參看SONG[],你就會(huì)明白
while(1)
{ //把音碼變量數(shù)組里的值賦給臨時(shí)變量
cTemp1 = SONG[iAddr++];
if (cTemp1 == 0xFF) //休止符
{
TR0 = 0;
DelayUs(100); //延時(shí)
}
else if (cTemp1 == 0x00) //判斷是否歌曲結(jié)束
{
return;
}
else
{
cTemp2 = SONG[iAddr++];//判斷完,不是休止符與結(jié)束符后,讓蜂鳴器唱歌
TR0 = 1; //讓定時(shí)器0開(kāi)始工作
while(1)
{
FMQ = ~FMQ; //讓蜂鳴器不停地在開(kāi)與關(guān)之間切換
DelayUs(cTemp1);
if(cTemp2 == cCount)
{
cCount = 0;
break;
}
}
}
}
}
////////////////////////////////////////////////////////////////////////
函數(shù)名稱(chēng):main
函數(shù)功能:完成系統(tǒng)的初始化,讓實(shí)驗(yàn)板上的蜂鳴器不停循環(huán)的唱上面的兩首歌
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):無(wú)
////////////////////////////////////////////////////////////////////////
void main()
{
DelayUs(100); //穩(wěn)定系統(tǒng)
Init_Time0();//定時(shí)器0中斷初始化
while(1)
{
FMQ_PlaySong(0); //播放“祝你平安”
DelayUs(100); //延時(shí)一下,以便播放下一首歌
FMQ_PlaySong(1); //播放“祝你平安”
DelayUs(100); //延時(shí)一下,以便播放下一首歌
}
}



關(guān)鍵詞: STC89C52單片機(jī)蜂鳴

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉