当前位置:   article > 正文

4-C51-蜂鸣器_蜂鸣器发出不同声音的程序

蜂鸣器发出不同声音的程序

概要

蜂鸣器使用

技术解释

蜂鸣器发声的频率
在这里插入图片描述
各个音调的频率
在这里插入图片描述
知道了以上上面的知识后,我们还需要知道一个公式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);
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

以下是实现两种频率声音

#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);
		}
		
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

以下是音乐盒仅个人参考(详细有需要私信我)

#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/554403
推荐阅读
相关标签
  

闽ICP备14008679号