蜂鸣器播放音乐
时间:10-02
整理:3721RD
点击:
全部程序是这样的:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P1^6; //蜂鸣器控制引脚
//生日快乐歌的音符频率表,不同频率由不同的延时来决定
uchar code pinlv[]={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};
//生日快乐歌节拍表,节拍决定每个音符的演奏长短
uchar code jiepai[]={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 DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<113;t++);
}
//播放函数
void PlayMusic()
{
uint i=0,j,k;
while(pinlv[i]!=0||jiepai[i]!=0)
{ //播放各个音符,jiepai为拍子长度
for(j=0;j<jiepai[i]*20;j++)
{
BEEP=~BEEP;
//pinlv 延时表决定了每个音符的频率
for(k=0;k<pinlv[i]/3;k++);
}
DelayMS(10);
i++;
}
}
void main()
{
BEEP=0;
while(1)
{
PlayMusic(); //播放生日快乐
DelayMS(500); //播放完后暂停一段时间
}
}
for(k=0;k<pinlv[i]/3;k++);是什么意思
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P1^6; //蜂鸣器控制引脚
//生日快乐歌的音符频率表,不同频率由不同的延时来决定
uchar code pinlv[]={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};
//生日快乐歌节拍表,节拍决定每个音符的演奏长短
uchar code jiepai[]={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 DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<113;t++);
}
//播放函数
void PlayMusic()
{
uint i=0,j,k;
while(pinlv[i]!=0||jiepai[i]!=0)
{ //播放各个音符,jiepai为拍子长度
for(j=0;j<jiepai[i]*20;j++)
{
BEEP=~BEEP;
//pinlv 延时表决定了每个音符的频率
for(k=0;k<pinlv[i]/3;k++);
}
DelayMS(10);
i++;
}
}
void main()
{
BEEP=0;
while(1)
{
PlayMusic(); //播放生日快乐
DelayMS(500); //播放完后暂停一段时间
}
}
for(k=0;k<pinlv[i]/3;k++);是什么意思