51单片机蜂鸣器生日快乐歌C语言编码

2022-08-26 17:32:13   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《51单片机蜂鸣器生日快乐歌C语言编码》,欢迎阅读!
快乐歌,蜂鸣器,单片机,编码,生日

#include <reg52.h>

#define uchar unsigned char

#define uint unsigned int

sbit beep=P1^5;

uchar code SONG_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};

//生日快乐歌的音符频率表,不同频率由不同的延时来决定

uchar code SONG_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(SONG_TONE[i]!=0||SONG_LONG[i]!=0)

{

for(j=0;j<SONG_LONG[i]*20;j++) //播放个个音符,SONG_LONG为音拍个个长度

{

beep=~beep;

for(k=0;k<SONG_TONE[i]/3;k++); //SONG_TONE延时决定了每个音符的频率

}

delay(10);

i++;

}

}

void main ()

{

beep=0;

while(1)

{

playmusic();

delay(500);

}

}

本文来源:https://www.wddqxz.cn/2ca7ecaecc22bcd126ff0c6b.html

相关推荐