目录
1. 蜂鸣器
蜂鸣器(Buzzer)是一种将电信号转换为声音的器件,由电信号的周期性脉冲产生震动频率,可以用来产生设备的按键音、报警音等提示信号
蜂鸣器按驱动方式可分为有源蜂鸣器和无源蜂鸣器
- 有源蜂鸣器:内部自带振荡源,通上直流电压即可产生固定频率,持续发声。
- 无源蜂鸣器:内部不带振荡源,需要控制器提供振荡脉冲才可发生震动,进而发 声,调整提供振荡脉冲的频率,可发出不同频率的声音
1.1 无源蜂鸣器驱动方式
(1)三极管启动:三极管可通过根据PN结原理做成的NPN、PNP用小电流控制大电流,完成驱动功能
(2)集成电路驱动:通过ULN2003D芯片驱动P25输入电平到BEEP完成驱动功能。
单片机上蜂鸣模块就是无源Buzzer。
2.实现独立按键音(无源蜂鸣器)
2.1 Buzzer.h
#include <REGX52.H>
#include <INTRINS.H>
#include "Delay.h"
sbit Buzzer_sbit = P2^5;//操作P25口
/*以1000hz响xms毫秒*/
void Buzzer_Time(unsigned int xms)
{
xms = 2*xms;//500us循环一次,循环2次为1ms
while(xms--)
{
Buzzer_sbit=!Buzzer_sbit;
Delay_500us(1);
}
}
2.2 IndKey.h 独立按键模块
#include <REGX52.H>
#include "Delay.h"
#include "Buzzer.h"
/*返回按下的按键,否则返回0,按下同时有提示音*/
unsigned char getIndKey()
{
unsigned char Key = 0;
if(P3_1 == 0) {Delay_ms(20);while(P3_1 == 0);Delay_ms(20);Key = 1;Buzzer_Time(100);}
if(P3_0 == 0) {Delay_ms(20);while(P3_0 == 0);Delay_ms(20);Key = 2;Buzzer_Time(100);}
if(P3_2 == 0) {Delay_ms(20);while(P3_2 == 0);Delay_ms(20);Key = 3;Buzzer_Time(100);}
if(P3_3 == 0) {Delay_ms(20);while(P3_3 == 0);Delay_ms(20);Key = 4;Buzzer_Time(100);}
return Key;
}
2.3 DigitalTube.h 数码管模块
#include <REGX52.H>
#include "Delay.h"
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
/*在第loc个数码管显示num*/
void DigitalTube(unsigned char loc,num) {
switch(loc)
{
case 1: P2_4 = 1;P2_3 = 1;P2_2 = 1;break;
case 2: P2_4 = 1;P2_3 = 1;P2_2 = 0;break;
case 3: P2_4 = 1;P2_3 = 0;P2_2 = 1;break;
case 4: P2_4 = 1;P2_3 = 0;P2_2 = 0;break;
case 5: P2_4 = 0;P2_3 = 1;P2_2 = 1;break;
case 6: P2_4 = 0;P2_3 = 1;P2_2 = 0;break;
case 7: P2_4 = 0;P2_3 = 0;P2_2 = 1;break;
case 8: P2_4 = 0;P2_3 = 0;P2_2 = 0;break;
}
P0 = NixieTable[num];
}
2.4 Delay.h 延时模块
#include <INTRINS.H>
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay500us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 227;
while (--i);
}
void Delay_ms(unsigned int xms)
{
while(xms--) Delay1ms();
}
void Delay_500us(unsigned int x500us)
{
while(x500us--) Delay500us();
}
2.5 main() 主函数入口
#include <REGX52.H>
#include "IndKey.h"
#include "DigitalTube.h"
unsigned char num;
void main()
{
DigitalTube(1,0);
while(1)
{
num = getIndKey();
if(num)//如果按键按下
{
DigitalTube(1,num);
}
}
}