【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《蜂鸣器播放生日快乐歌曲》,欢迎阅读!
//-------------------------------------------------
//以下程序为用蜂鸣器播放生日快乐歌程序,播放完一遍
//之后间隔一段时间再重复播放,请同学们自行分析
//------------------------------------------------
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit BUZ = P2^5;
uchar code music_tone[] = {212,212,190,212,159,169,212,212,190,212,142,159,
212,212,106,126,159,169,190,119,119,126,159,142,159,0};
//以上为生日快乐歌音符频率表,频率表最后为0表示播放完成
uchar code music_long[] = {9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,
12,12,9,3,12,12,12,24,0};
//以上为生日快乐歌节拍表(即每个音符演奏长短)
//----------------------------------------------
//延时子程序
//-----------------------------------------------
void delay(uint x)
{
uchar t;
while(x--) for(t = 0; t < 120;t++);
}
//------------------------------------------------
//播放歌曲子程序
//------------------------------------------------
void playmusic()
{
uint i = 0,j,k;
while(music_long[i] !=0 || music_tone[i] !=0)
{
for(j = 0; j < music_long[i]*20;j++)
{
BUZ = ~BUZ;
for(k = 0; k < music_tone[i]/3 ; k++);
}
delay(10);
i++; //下一个音符
}
}
//--------------------------------------
//主程序
//---------------------------------------
void main()
{
while(1)
{
playmusic();
delay(500);//播放完后暂停一会后继续播放
}
}