前言
本篇文章属于STC89C52单片机(以下简称单片机)的学习笔记,来源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记,只能做参考,细节方面建议观看视频,肯定受益匪浅。
一、蜂鸣器介绍
1. 简介
注意:“源”指的不是电源,而是振荡源。
2..驱动电路
需要采用驱动电路而不是正接正、负接负的原因是蜂鸣器功率较大,LED灯可以直接正接正、负接负是因为其功率小。
NPN是高电平导通的三极管开关,基级置高电平,给予一个控制信号,右侧线路就会导通,进行能力更强的驱动,不用控制信号进行驱动,因为其驱动能力不足
PNP是低电平导通的三极管开关,基级置低电平,右侧导通
集成电路驱动采用的芯片,这个芯片通常是用来驱动步进电机的
二、基础乐理知识(为蜂鸣器播放音乐作铺垫)
三、实例一(蜂鸣器播放提示音)
main.c
#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
#include "Key.h"
#include "Buzzer.h"
sbit Buzzer = P2^5;
unsigned char KeyNum;
unsigned int i;
void main()
{
Nixie(1,0);
while(1)
{
KeyNum=Key();
if (KeyNum)
{
Buzzer_Time(100);
Nixie(1,KeyNum);
}
}
}
Buzzer.c
#include <REGX52.H>
#include "INTRINS.h"
//蜂鸣器端口
sbit Buzzer = P2^5;
/**
* @brief 蜂鸣器私有延时函数,延时500us
* @param 无
* @retval 无
*/
void Buzzer_Delay500us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 227;
while (--i);
}
/**
* @brief 蜂鸣器发声
* @param ms 发声的时长
* @retval 无
*/
void Buzzer_Time(unsigned int ms)
{
unsigned int i;
for(i=0;i<ms;i++)
{
Buzzer=!Buzzer;
Buzzer_Delay500us();
}
}
四、实例二(蜂鸣器播放音乐)
main.c
#include <REGX52.H>
#include "Delay.h"
#include "Timer0.h"
sbit Buzzer = P2^5;
#define P 0
#define L1 1
#define L1_ 2
#define L2 3
#define L2_ 4
#define L3 5
#define L4 6
#define L4_ 7
#define L5 8
#define L5_ 9
#define L6 10
#define L6_ 11
#define L7 12
#define M1 13
#define M1_ 14
#define M2 15
#define M2_ 16
#define M3 17
#define M4 18
#define M4_ 19
#define M5 20
#define M5_ 21
#define M6 22
#define M6_ 23
#define M7 24
#define H1 25
#define H1_ 26
#define H2 27
#define H2_ 28
#define H3 29
#define H4 30
#define H4_ 31
#define H5 32
#define H5_ 33
#define H6 34
#define H6_ 35
#define H7 36
//此数组是取到全部的音
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,
};
//乐谱,必须使用code,RAM最大128字节
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
M6, 2,
M7, 2,
H1, 4,
M7, 4,
H2, 2,
H2, 2,
H1, 4+2,
M5, 2+4+4,
H4, 4,
H3, 4,
H3, 4,
H1, 4,
//9
H3, 4+4+4,
H3, 4,
H6, 4+4,
H5, 4,
H5, 4,
H3, 2,
H2, 2,
H1, 4+4,
P, 2,
H1, 2,
//10
H2, 4,
H1, 2,
H2, 2,
H2, 4,
H5, 4,
H3, 4+4+4,
H3, 4,
H6, 4+4,
H5, 4+4,
//11
H3, 2,
H2, 2,
H1, 4+4,
P, 2,
H1, 2,
H2, 4,
H1, 2,
H2, 2+4,
M7, 4,
M6, 4+4+4,
P, 4,
0xFF //终止标志
};
unsigned char freselect,Musicselect;
void main()
{
Timer0Init();
while(1)
{
if(Music[Musicselect]!=0xFF)
{
//选择第几个音
freselect=Music[Musicselect];
Musicselect++;
//选择时长并延时实现
Delay(500/4*Music[Musicselect]);
Musicselect++;
//下面这三行是为了体现抬手的功能,否则声音太密集
TR0=0; //关闭定时器
Delay(5); //音符间短暂停顿
TR0=1; //打开定时器
}
else
{
TR0=0;
while(1);
}
}
}
//刚开始1ms会有一次中断
//产生500HZ的频率
void Timer0_Routine() interrupt 1
{
if(FreqTable[freselect])
{
TL0 = FreqTable[freselect]%256; //设置定时初值
TH0 = FreqTable[freselect]/256; //设置定时初值
Buzzer=!Buzzer;
}
}