蜂鸣器通过震荡脉冲控制
1,尝试发声
#include <REGX52.H> #include "Delay.h" sbit buzzer = P2^5; // 蜂鸣器端口
unsigned int i; //演奏时长 void main() { while(1) { for (i=0 ; i < 500; i++) { buzzer =! buzzer; Delay(100); //100*100us = 10000us = 10ms = 0.01s = 100hz } } }
2,演奏音乐(演奏完停止)
#include <REGX52.H> #include "Timer0.h" #include "Delay.h" // 蜂鸣器端口 sbit buzzer = P2^5; # define step 250 //索引与频率对照表 unsigned int FreqTable[]={ 0, 63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64528, 64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030, 65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283, }; // 音符集 unsigned char Music[] = { 13,4, 13,4, 20,4, 20,4, 22,4, 22,4, 20,4+4, 0,4, 18,4, 18,4, 17,4, 17,4, 15,4, 15,4, 13,4+4, 0xff // 截至符 }; // unsigned int FreqSelect,MusicSelect; void main() { Timer0_Init(); // us while(1) { if (Music[MusicSelect] != 0xff) { FreqSelect = Music[MusicSelect]; MusicSelect ++; Delay1ms(step/4*Music[MusicSelect]); MusicSelect ++; TR0 = 0; Delay1ms(5); TR0 = 1; } else { TR0 = 0; while(1); } } } // 定时其中断函数 void Timer0() interrupt 1 { if (FreqTable[FreqSelect]) { TH0 = FreqTable[FreqSelect] / 256; //设置定时初始值 TL0 = FreqTable[FreqSelect] % 256; //设置定时初始值 buzzer =! buzzer; } }
3,演奏音乐(循环播放)
#include <REGX52.H> #include "Timer0.h" #include "Delay.h" // 蜂鸣器端口 sbit buzzer = P2^5; # define step 250 //索引与频率对照表 unsigned int FreqTable[]={ 0, 63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64528, 64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030, 65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283, }; // 音符集 unsigned char Music[] = { 13,4, 13,4, 20,4, 20,4, 22,4, 22,4, 20,4+4, 0,4, 18,4, 18,4, 17,4, 17,4, 15,4, 15,4, 13,4+4, 0xff // 截至符 }; // unsigned int FreqSelect,MusicSelect; void main() { Timer0_Init(); // us while(1) { if (Music[MusicSelect] != 0xff) { FreqSelect = Music[MusicSelect]; MusicSelect ++; Delay1ms(step/4*Music[MusicSelect]); MusicSelect ++; TR0 = 0; Delay1ms(5); TR0 = 1; } else { MusicSelect = 0; } } } // 定时其中断函数 void Timer0() interrupt 1 { if (FreqTable[FreqSelect]) { TH0 = FreqTable[FreqSelect] / 256; //设置定时初始值 TL0 = FreqTable[FreqSelect] % 256; //设置定时初始值 buzzer =! buzzer; } }
音符与频率对照表:
#in
clude <REGX52.H>
#include "Timer0.h"#include "Delay.h"// 蜂鸣器端口sbit buzzer = P2^5;# define step 250
//索引与频率对照表unsigned int FreqTable[]={0,63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64528,64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,};// 音符集unsigned char Music[] = {13,4,13,4,20,4,20,4,22,4,22,4,20,4+4,0,4,18,4,18,4,17,4,17,4,15,4,15,4,13,4+4,0xff // 截至符};// unsigned int FreqSelect,MusicSelect;
void main(){Timer0_Init(); // uswhile(1){if (Music[MusicSelect] != 0xff){FreqSelect = Music[MusicSelect];MusicSelect ++;Delay1ms(step/4*Music[MusicSelect]);MusicSelect ++;TR0 = 0;Delay1ms(5);TR0 = 1;}else{TR0 = 0;while(1);}}}
// 定时其中断函数void Timer0() interrupt 1{if (FreqTable[FreqSelect]){TH0 = FreqTable[FreqSelect] / 256;//设置定时初始值TL0 = FreqTable[FreqSelect] % 256;//设置定时初始值buzzer =! buzzer;}
} 标签:buzzer,FreqTable,蜂鸣器,MusicSelect,51,unsigned,单片机,Music,FreqSelect From: https://www.cnblogs.com/lld76/p/17350293.html