本文主要是介绍51单片机.之蜂鸣器振动播放歌曲,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
蜂鸣器发声是通过喇叭振动发声的,通电产生磁场,磁铁吸收,而振动。不断释放,吸收。
1、蜂鸣器发声,播放不同频率的声音逐渐变尖
#include<reg52.h>sbit BUZZ = P1^6;unsigned char T0RH=0;
unsigned char T0RL=0;
void OpenBuzz(unsigned int frequ);
void StopBuzz();void main()
{unsigned int i;EA =1;TMOD = 0x01;while(1){OpenBuzz(4000);for(i=0; i<40000; i++);StopBuzz();for(i=0; i<40000; i++);OpenBuzz(1000);for(i=0; i<40000; i++);StopBuzz();for(i=0; i<40000; i++);}
}void OpenBuzz(unsigned int frequ)
{unsigned int reload;reload = 65536 - (11059200/12)/(frequ*2); T0RH = (unsigned char)(reload >>8);T0RL = (unsigned char)reload;TH0 = 0xFF;TL0 = 0xFE;ET0 = 1;TR0 = 1;
}void StopBuzz()
{ET0 = 0;TR0 = 0;
}void InterruptTimer0() interrupt 1
{TH0 = T0RH;TL0 = T0RL;BUZZ = ~BUZZ;
}
2、蜂鸣器播放歌曲
#include <reg52.h>sbit BUZZ = P1^6; //蜂鸣器控制引脚unsigned int code NoteFrequ[] = { //中音1-7和高音1-7对应频率列表523, 587, 659, 698, 784, 880, 988, //中音1-71047, 1175, 1319, 1397, 1568, 1760, 1976 //高音1-7
};
unsigned int code NoteReload[] = { //中音1-7和高音1-7对应的定时器重载值65536 - (11059200/12) / (523*2), //中音165536 - (11059200/12) / (587*2), //265536 - (11059200/12) / (659*2), //365536 - (11059200/12) / (698*2), //465536 - (11059200/12) / (784*2), //565536 - (11059200/12) / (880*2), //665536 - (11059200/12) / (988*2), //765536 - (11059200/12) / (1047*2), //高音165536 - (11059200/12) / (1175*2), //265536 - (11059200/12) / (1319*2), //365536 - (11059200/12) / (1397*2), //465536 - (11059200/12) / (1568*2), //565536 - (11059200/12) / (1760*2), //665536 - (11059200/12) / (1976*2), //7
};
bit enable = 1; //蜂鸣器发声使能标志
bit tmrflag = 0; //定时器中断完成标志
unsigned char T0RH = 0xFF; //T0重载值的高字节
unsigned char T0RL = 0x00; //T0重载值的低字节void PlayTwoTiger();void main()
{unsigned int i;EA = 1; //使能全局中断TMOD = 0x01; //配置T0工作在模式1TH0 = T0RH;TL0 = T0RL;ET0 = 1; //使能T0中断TR0 = 1; //启动T0while (1){PlayTwoTiger(); //播放乐曲--两支老虎for (i=0; i<40000; i++); //停止一段时间}
}
/* 两只老虎乐曲播放函数 */
void PlayTwoTiger()
{unsigned char beat; //当前节拍索引unsigned char note; //当前节拍对应的音符unsigned int time = 0; //当前节拍计时unsigned int beatTime = 0; //当前节拍总时间unsigned int soundTime = 0; //当前节拍需发声时间//两只老虎音符表unsigned char code TwoTigerNote[] = {1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,5,6, 5,4, 3, 1, 5,6, 5,4, 3, 1, 1, 5, 1, 1, 5, 1,};//两只老虎节拍表,4表示一拍,1就是1/4拍,8就是2拍unsigned char code TwoTigerBeat[] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8,3,1, 3,1, 4, 4, 3,1, 3,1, 4, 4, 4, 4, 8, 4, 4, 8,};for (beat=0; beat<sizeof(TwoTigerNote); ) //用节拍索引作为循环变量{while (!tmrflag); //每次定时器中断完成后,检测并处理节拍tmrflag = 0;if (time == 0) //当前节拍播完则启动一个新节拍{note = TwoTigerNote[beat] - 1;T0RH = NoteReload[note] >> 8;T0RL = NoteReload[note];//计算节拍总时间,右移2位相当于除4,移位代替除法可以加快执行速度beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2;//计算发声时间,为总时间的0.75,移位原理同上soundTime = beatTime - (beatTime >> 2);enable = 1; //指示蜂鸣器开始发声time++;}else //当前节拍未播完则处理当前节拍{if (time >= beatTime) //当前持续时间到达节拍总时间时归零,{ //并递增节拍索引,以准备启动新节拍time = 0;beat++;}else //当前持续时间未达到总时间时,{time++; //累加时间计数if (time == soundTime) //到达发声时间后,指示关闭蜂鸣器,{ //插入0.25*总时间的静音间隔,enable = 0; //用以区分连续的两个节拍}}}}
}
/* T0中断服务函数,用于控制蜂鸣器发声 */
void InterruptTimer0() interrupt 1
{TH0 = T0RH; //重新加载重载值TL0 = T0RL;tmrflag = 1;if (enable) //使能时反转蜂鸣器控制电平BUZZ = ~BUZZ;else //未使能时关闭蜂鸣器BUZZ = 1;
}
这篇关于51单片机.之蜂鸣器振动播放歌曲的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!