目录
D1~D8流水灯程序中,D8亮时表示一个流水循环结束,修改流水灯程序,实现每个流水循环蜂鸣器短响提示一次
概要
51单片机蜂鸣器学习
蜂鸣器分类
- 有源蜂鸣器 /* 只需要高低电平就可以 两脚不等长*/
- 无源蜂鸣器 /* 需要自己调制脉冲信号 两脚等长*/
名词解释
- 占空比 /* 高电平在一个时间周期内所占时间的比例,可以通过调节占空比来设置蜂鸣器的声音大小*/
- 频率 /* 脉冲周期的倒数,脉冲周期越长,脉冲频率越低。可以通过设置延时周期控制声音音调*/
- 机器周期 /* 单片机完成一个基本操作所需的时间称为机器周期,由12个振荡周期组成*/
- 振荡周期 /* 由单片机内振荡电路OSC产生,定义 为时钟脉冲频率的倒数*/
- 指令周期 /* 指CPU执行一条指令所需的时间。由于机器执行不同的指令所需的时间不同,因此不同指令所包含的机器周期数也不同。*/
例如计算一个机器周期时间:
根据震荡周期我们知道如果是一个12M频率的晶振,它的震荡周期是t = 1/12000kHZ=1/12000000HZ。又因为机器周期 = 12*震荡周期,所以T = 12T = 12 * 1 / 12000000 = 0.000001(10的-6次),也就是1us,所以一个12M频率的晶振,它执行一次的时间大约是1us。
这个机器周期在复习后面定时器时候也有用,定时器一共可以计数2^16 = 65536,大约是65536us ≈ 65ms。
基础练习
-
D1~D8流水灯程序中,D8亮时表示一个流水循环结束,修改流水灯程序,实现每个流水循环蜂鸣器短响提示一次
实现代码:
#include "reg52.h"
#include "intrins.h"
#define led P2
sbit BEEP = P0^1;
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay75us() //@11.0592MHz
{
unsigned char i;
i = 32;
while (--i);
}
void Delay25us() //@11.0592MHz
{
unsigned char i;
i = 9;
while (--i);
}
void main()
{
int i,j=2000; //改变声音持续时间
led = 0xFE;
while(1)
{
for(i = 7;i>0;i--)
{
led = _crol_(led,1); //循环左移 1-8
Delay1000ms();
}
while(--j)
{
BEEP = 1;
Delay75us(); //调整高电平占空比,来调整声音大小
BEEP = 0;
Delay25us();
}
j = 2000;
for(i = 7;i>0;i--)
{
led = _cror_(led,1); //循环右移 8-1
Delay1000ms();
}
while(--j)
{
BEEP = 1;
Delay75us();
BEEP = 0;
Delay25us();
}
j = 2000;
}
}
娱乐代码
起风了音乐实现(大佬实现的代码):
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P0^1; //单片机开发板型号不同需要修改为所对应的蜂鸣器管脚
uchar code sound[]={0xff,
0x2b,0x30,0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,
0x26,0x20,0x2b,0x30,0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x10,0x26,0x10,
0x2b,0x10,0x30,0x10,0x40,0x20,0x40,0x20,0x2b,0x30,0x30,0x10,0x2b,0x30,
0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,0x2b,0x30,0x26,0x10,
0x2b,0x20,0x30,0x20,0x2b,0x20,0x2b,0x20,0xff,0x20,0xff,0x20,0x2b,0x30,
0x30,0x10,0x2b,0x30,0x30,0x10,0x2b,0x20,0x26,0x20,0x20,0x20,0x26,0x20,
0x2b,0x30,0x26,0x10,0x2b,0x20,0x30,0x20,0x39,0x20,0x39,0x20,0x26,0x10,
0x2b,0x10,0x30,0x10,0x2b,0x10,0x30,0x20,0x30,0x20,0x26,0x10,0x2b,0x10,
0x30,0x10,0x2b,0x10,0x30,0x30,0x20,0x10,0x26,0x10,0x2b,0x10,0x30,0x10,
0x2b,0x10,0x30,0x20,0x30,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,0x30,0x20,
0x1c,0x20,0x20,0x10,0x1c,0x10,
0x1c,0x30,0x30,0x10,0x19,0x20,0x1c,0x10,0x19,0x10,0x19,0x20,0x19,0x20,
0x1c,0x10,0x19,0x10,0x19,0x20,0x26,0x20,0x18,0x10,0x15,0x10,0x18,0x10,
0x19,0x10,0x1c,0x20,0x20,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,0x1c,0x10,
0x20,0x10,0x1c,0x10,0x20,0x10,0x1c,0x20,0x20,0x10,0x2b,0x10,0x2b,0x10,
0x20,0x20,0x20,0x10,0x26,0x20,0x26,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,
0x30,0x20,0x1c,0x20,0x20,0x10,0x1c,0x10,0x1c,0x30,0x30,0x10,0x19,0x20,
0x1c,0x10,0x19,0x10,0x19,0x20,0x19,0x20,0x1c,0x10,0x19,0x10,0x19,0x20,
0x26,0x20,0x18,0x10,0x15,0x10,0x18,0x10,0x19,0x10,0x1c,0x20,0x20,0x20,
0x1c,0x20,0x13,0x10,0x13,0x10,0x13,0x20,0x20,0x20,0x1c,0x20,0x13,0x10,
0x13,0x10,0x13,0x10,0x20,0x20,0x1c,0x10,0x1c,0x20,0x1c,0x20,0x1c,0x20,
0x1c,0x20,0xff,0x20,0xff,0x20, 0x18,0x20,0x15,0x20,0x13,0x20,0x0e,
0x10,0x10,0x10,0x10,0x20,0x0e,0x10,
0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x19,0x10,0x18,0x10,
0x18,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,
0x0e,0x10,0x10,0x10,0x10,0x10,0x13,0x30,0x15,0x20,0x18,0x10,0x1c,0x10,
0x1c,0x10,0x18,0x20,0x1c,0x10,0x15,0x10,0x18,0x10,0x1c,0x20,0x1c,0x10,
0x18,0x30,0x13,0x20,0x13,0x20,0x13,0x10,0x12,0x10,0x13,0x20,0x13,0x10,
0x15,0x20,0xff,0x20,0xff,0x20,0x18,0x20,0x15,0x20,0x13,0x20,0x0e,0x10,
0x10,0x10,
0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,
0x15,0x20,0x13,0x20,0x0e,0x10,0x10,0x10,0x10,0x20,0x0e,0x10,0x10,0x10,
0x10,0x20,0x0e,0x10,0x10,0x10,0x10,0x10,0x13,0x30,0x15,0x20,0x18,0x10,
0x1c,0x10,0x1c,0x10,0x13,0x30,0x15,0x20,0x18,0x10,0x1c,0x10,0x1c,0x10,
0x18,0x30,0x18,0x20,0x18,0x20,0xff,0x20,0xff,0x20,0x1c,0x10,0x13,0x10,
0x15,0x20,0x18,0x10,0x1c,0x10,0x1c,0x10,0x13,0x30,0x15,0x20,0x18,0x10,
0x1c,0x10,0x1c,0x10,0x18,0x30,0x18,0x20,0x18,0x20,0x18,0x20,0x18,0x20,0x00,
};
uchar zdjs=0, jp;
del(yj);
void main(void)
{
uint dpjs=0;
uchar yj;
TMOD=0x01,IE=0x82;
TH0=0xd8, TL0=0xef;
TR0=1;
while(1)
{
zdjs=0;
dpjs++; yj=sound[dpjs];
dpjs++; jp=sound[dpjs];
while(zdjs!=jp)
{
if(yj!=0xff)
{
if(yj!=0)
{
voice=!voice;
del(yj);
}
else
{
dpjs=0;
break;
}
}
else
{
voice=0;
del(jp);
}
}
}
}
time0() interrupt 1 using 1
{
TH0=0xd8, TL0=0xef;
zdjs++;
}
del(yj)
{
uchar yj2=2;
while(yj!=0)
{
while(yj2!=0)
{
yj2--;
}
yj2=2;
yj--;
}
}