赞
踩
蜂鸣器使用
蜂鸣器发声的频率
各个音调的频率
知道了以上上面的知识后,我们还需要知道一个公式T=1/f(f为频率),蜂鸣器是靠高低电信号,方波开发出声音的.
以下是涉及到的原理图;
以下是高低电平持续时间各为1ms,一个周期是2ms,由公式T=1/f(f为频率)=2ms=1000/f,所以f为500HZ
#include <REGX52.H>
#include "DelayXms.H"
sbit BUZZER = P2^5;
void main(){
while(1)
{
BUZZER = ~BUZZER;
DelayXms(1);
}
}
以下是实现两种频率声音
#include <REGX52.H> #include "DelayXms.H" sbit BUZZER = P2^5; void main(){ unsigned int i; while(1) { for(i=0;i<100;i++) { BUZZER = ~BUZZER; DelayXms(1); } for(i=0;i<100;i++) { BUZZER = ~BUZZER; DelayXms(3); } } }
以下是音乐盒仅个人参考(详细有需要私信我)
#ifndef __SOUNDPLAY_H_REVISION_FIRST__ #define __SOUNDPLAY_H_REVISION_FIRST__ #include <REGX51.H> //************************************************************************** #define SYSTEM_OSC 11059200//12000000 //??????12000000HZ #define SOUND_SPACE 4/5 //?????????????,//?4????? sbit BeepIO = P2^5; //?????? unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //????? unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7???????? unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 }; unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //????????? unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //????????? //************************************************************************** unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03, 0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03, 0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。