新聞中心

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

STC89C52單片機蜂鳴器唱歌實驗

作者: 時間:2016-11-21 來源:網(wǎng)絡 收藏
////////////////////////////////////////////////////////////////////////

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

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

#define uchar unsigned char
#define uint unsigned int

uchar cCount;

sbit FMQ =P2^4; //定義實驗板上蜂鳴器器控制腳


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ù)名稱:Time0_Init
函數(shù)功能:定時器的初始化配置,使其能中斷,中斷的時間間隔為10ms
參數(shù)介紹:無
返回值:
注意事項:注意此時實驗板上用到的晶振為11.0592M的,要選用其他的,得重新配置
////////////////////////////////////////////////////////////////////////
void Init_Time0()
{
TMOD = 0x01; //設(shè)置定時器0為模式1(16bit)

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

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



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

評論


技術(shù)專區(qū)

關(guān)閉