一.蜂鸣器
1.原理图
原理:蜂鸣器有有源和无源两种
区别:一个是给一个电平直接点香,另一个取决于脉冲的宽度,宽度决定音阶,从而模仿出各种声音
sbit BEEP =P3^7;//蜂鸣器
sbit K1 = P1^4;
sbit K2 = P1^5;
sbit K3 = P1^6;
sbit K4 = P1^7;
void DelayMS(uint x)
{
uchar t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void Play(uchar t)
{
uchar i;
for(i=0;i<100;i++)
{
BEEP=~BEEP;
DelayMS(t);//脉宽,导致周期脉宽不同
}
BEEP =0;
}
void main()
{
P1=0xff;//初始化设置
while(1)
{
if(K1==0) Play(1);//点按式
if(K2==0) Play(2);
if(K3==0) Play(3);
if(K4==0) Play(4);
}
}
二.蜂鸣器播放音乐
#include<>
#define uchar unsigned char
#define uint unsigned int
sbit BBEP=P3^7;
uchar code SONG_TONE[]=
//更换里面内容,即可播放不同的音乐
{
212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,129,169,190
};
uchar{
9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0
};
void DelayMS(uint ms)
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
void PlayMusic()
{
uint i =0,j,k;
while(SONG_LONG[i]!=0||SONG_TONG[i]!=0) //对数据不同的延时会播放出不同的声音
{
for(j=0;j<SONG_LONG[i]*20;j++) //将不同的延时做出不同的声音 高电平延时
{
BEEP =~BEEP;
for(k=0;k<SONG_TONG[i]/3;k++); //不同音阶 低电平延时
}
DelayMS(10);
i++;
}
}
void main()
{
while(1)
{
PlayMusic();
DelayMS(500);
}
}
三.8*8LED点阵屏显示数字
8*8的LED组成,上面有8个IO控制横着的IO,竖着的IO控制竖着的IO
设计到数码管的动态原理,眼睛看到的有50ms延时,一个灯一个灯点亮,动态就是使其显示的快点
具体操作,有取模软件
#include<reg52.h>
#include<instring.h>
#define uchar unsigned char
#define uint unsigned int
uchar code Table_OF_Digita[]= //用的取模软件取的每一行的显示
{
0x00,0x3e,0x41,0x41,0x41,0x3e,0x00,0x00,
0x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00,
0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00,
0x00,0x22,0x49,0x49,0x49,0x36,0x00,0x00,
0x00,0x0c,0x14,0x24,0x7f,0x04,0x00,0x00,
0x00,0x72,0x51,0x51,0x51,0x4e,0x00,0x00,
0x00,0x3e,0x49,0x49,0x49,0x26,0x00,0x00,
0x00,0x40,0x40,0x40,0x4f,0x70,0x00 0x00,
0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00,
0x00,0x32,0x49,0x49,0x49,0x3e,0x00,0x00,
};
uchar i =0,t=0,Num_Index=0;
void main()
{
P3=0x00;
Num_Index=0;
TMOD=0x00;
TH0=(8192-2000)/32;
TL0=(8192-2000)%32;
TR0=1;
IE=0x82;
while(1);
}
void LED_Screen_Display() interrupt 1
{
TH0=(8192-2000)/32;
TL0=(8192-2000)%32;
P3=_crol_(P3,1);
P0=~Table_OF_Digits(Num_Index * 8+i);
if(++i==8)i = 0;
if(++t=250)
{
t=0x00;
if(++Num_Index==10)Num_Index=0;
}
}