单片机学习(12)--蜂鸣器

11.1蜂鸣器

1.蜂鸣器

蜂鸣器是一种将电信号转换为声音信号的器件,常用来产生设备的按键音、报警音等提示信号
蜂鸣器按驱动方式可分为有源蜂鸣器和无源蜂鸣器
有源蜂鸣器:内部自带振荡源,将正负极接上直流电压即可持续发声,频率固定
无源蜂鸣器:内部不带振荡源,需要控制器提供振荡脉冲才可发声调整提供振落脉冲的频率,可发出不同频率的声音

2.驱动电路

在这里插入图片描述

3.键盘和音符对照

在这里插入图片描述

4.简谱

在这里插入图片描述
在这里插入图片描述

5.音符和频率对照

在这里插入图片描述
| 在这里插入图片描述
周期取整公式:=1/B2*1000000(B2=262)
重装载值公式:=65536-E2(E2=1908)

11.2蜂鸣器播放提示音和音乐

1.蜂鸣器播放提示音

(1)工程目录

在这里插入图片描述

(2)main.c函数

功能:按下一个按键,伴随一个提示音

#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "Nixie.h"
#include "Buzzer.h"

unsigned char KeyNum;

void main()
{
	Nixie(1,0);
	while(1)
	{
		KeyNum=Key();
		if(KeyNum)
		{
			Buzzer_Time(1000);
			Nixie(1,KeyNum);
		}
	}
}

(3)Buzzer.c函数

#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
//蜂鸣器端口
sbit Buzzer=P2^5;
/**
  * @brief  蜂鸣器私有延时函数,延时500ms
  * @param  无
  * @retval 
  */
 void Buzzer_Delay500us()		//@11.0592MHz
{
	unsigned char data i;

	//_nop_();
	i = 227;
	while (--i);
}

/**
  * @brief  蜂鸣器发声
  * @param  ms 发声时长
  * @retval 无
  */
unsigned int i;
void Buzzer_Time(unsigned int ms)
{
	for(i=0;i<ms*2;i++)
	{
		Buzzer=!Buzzer;
		Buzzer_Delay500us();
	}
}

2.蜂鸣器播放《小星星》

(1)工程目录

在这里插入图片描述

(2)main.c函数

#include <REGX52.H>
#include "Delay.h"
#include "Timer0.h"
sbit Buzzer=P2^5;
#define SPEED  125

unsigned int FreqTable[]={
	63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
	64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
	65049,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,
	0};

/**
	* @brief  音乐《小星星》
  * @param  无
  * @retval 无
  */
code unsigned char Music[]={
	12,4,
	12,4,
	19,4,
	19,4,
	21,4,
	21,4,
	19,4+4,
	//36,4,
	17,4,
	17,4,
	16,4,
	16,4,
	14,4,
	14,4,
	12,4+4,
	17,4,
	 17,4,
	 16,4,
	 16,4,
	 14,4,
	 14,4,
	 12,4+4,
	 19,4,
	 19,4,
	 17,4,
	 17,4,
	 16,4,
	 16,4,
	 14,4+4,
	12,4,
	12,4,
	19,4,
	19,4,
	21,4,
	21,4,
	19,4+4,
	//36,4,
	17,4,
	17,4,
	16,4,
	16,4,
	14,4,
	14,4,
	12,4+4,
	0xFF};
unsigned char FreqSelect,MusicSelect;
void main()
{
	Timer0_Init();
	while(1)
	{
		if(Music[MusicSelect]!=0xFF)
		{
			FreqSelect=Music[MusicSelect];
			MusicSelect++;
			Delay(SPEED*Music[MusicSelect]);
			MusicSelect++;
			TR0=0;
			Delay(5);
			TR0=1;
		}
		else
		{
			TR0=0;
			while(1);
		}

	}
}

void Timer0_Routine() interrupt 1
{
	if(FreqTable[FreqSelect]!=36)
	{
		TH0 =FreqTable[FreqSelect]/256;
		TL0 =FreqTable[FreqSelect]%256;
		Buzzer=!Buzzer;
	}

	
}

2.蜂鸣器播放《天空之城》

(1)工程目录

在这里插入图片描述

(2)main.c函数

#include <REGX52.H>
#include "Delay.h"
#include "Timer0.h"
sbit Buzzer=P2^5;
#define SPEED  125
#define P  36
#define L1  0
#define L1_ 1
#define L2  2
#define L2_ 3
#define L3  4
#define L4  5
#define L4_ 6
#define L5  7
#define L5_ 8
#define L6  9
#define L6_ 10
#define L7  11
#define M1  12
#define M1_ 13
#define M2  14
#define M2_ 15
#define M3  16
#define M4  17
#define M4_ 18
#define M5  19
#define M5_ 20
#define M6  21
#define M6_ 22
#define M7  23
#define H1  24
#define H1_ 25
#define H2  26
#define H2_ 27
#define H3  28
#define H4  29
#define H4_ 30
#define H5  31
#define H5_ 32
#define H6  33
#define H6_ 34
#define H7  35

unsigned int FreqTable[]={
	63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
	64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
	65049,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,
	0};
unsigned char code Music[]={
	//1
	P,4,
	P,4,
	P,4,
	M6,2,
	M7,2,
	
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	
	M7,4+4+4,
	M3,2,
	M3,2,
	
	//2
	M6,4+2,
	M5,2,
	M6,4,
	H1,4,
	
	M5,4+4+4,
	M3,4,
	
	M4,4+2,
	M3,2,
	M4,4,
	H1,4,
	
	//3
	M3,4+4,
	P,2,
	H1,2,
	H1,2,
	H1,2,
	
	M7,4+2,
	M4_,2,
	M4_,4,
	M7,4,
	
	M7,8,
	P,4,
	M6,2,
	M7,2,
	
	//4
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	
	M7,4+4+4,
	M3,2,
	M3,2,
	
	M6,4+2,
	M5,2,
	M6,4,
	H1,4,
	
	//5
	M5,4+4+4,
	M2,2,
	M3,2,
	
	M4,4,
	H1,2,
	M7,2+2,
	H1,2+4,
	
	H2,2,
	H2,2,
	H3,2,
	H1,2+4+4,
	
	//6
	H1,2,
	M7,2,
	M6,2,
	M6,2,
	M7,4,
	M5_,4,
	
	M6,4+4+4,
	H1,2,
	H2,2,
	
	H3,4+2,
	H2,2,
	H3,4,
	H5,4,
	
	//7
	H2,4+4+4,
	M5,2,
	M5,2,
	
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	
	H3,4+4+4+4,
	
	//8
	H3,4+4+4+4,
	
	H6,4+4,
	H5,4+4,
	
	H3,2,
	H2,2,
	H1,4+4,
	P,2,
	H1,2,
	
	//9
	H2,4,
	H1,2,
	H2,2,
	H2,4,
	H5,4,
	
	H3,4+4+4+4,
	
	H6,4+4,
	H5,4+4,
	
	//10
	H3,2,
	H2,2,
	H1,4+4,
	P,2,
	H1,2,
	
	H2,4,
	H1,2,
	H2,2+4,
	H7,4,
	
	H6,4+4+4,
	P,2,
	0xFF
};

unsigned char FreqSelect,MusicSelect;
void main()
{
	Timer0_Init();
	while(1)
	{
		if(Music[MusicSelect]!=0xFF)
		{
			FreqSelect=Music[MusicSelect];
			MusicSelect++;
			Delay(SPEED*Music[MusicSelect]);
			MusicSelect++;
			TR0=0;
			Delay(5);
			TR0=1;
		}
		else
		{
			TR0=0;
			while(1);
		}

	}
}

void Timer0_Routine() interrupt 1
{
	if(FreqTable[FreqSelect]!=36)
	{
		TH0 =FreqTable[FreqSelect]/256;
		TL0 =FreqTable[FreqSelect]%256;
		Buzzer=!Buzzer;
	}

	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值