蜂鸣器的运用:
蜂鸣器大略设定频率,以20ms即50hz发声
#include
#define uint unsigned int
#define uchar unsigned char
sbit fen = P2^6;
void delay(uint z);
main()
{
while(1)
{
delay(1);//延时10毫秒留意这儿输出的是20毫秒的方波
//由于方波要有正负的跳变才干算一个周期,这儿?
//每隔10毫秒跳变一次,因而周期是200ms,频率是50Hz
fen = ~fen;
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x》0;x–)
for(y=110;y》0;y–);
}
蜂鸣器准确操控发声:运用守时器
#include
#define uint unsigned int
#define uchar unsigned char
sbit fen = P2^6;
uchar num,ben;
void init();
void main()
{
init();
while(1);
}
void init()
{
TMOD = 0x11;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
ET1 = 1;
EA = 1;
TR1 =1;
}
void TIme1()interrupt 3
{
TH1 = (65536-5000)/256;//守时一次是50毫秒
TL1 = (65536-5000)%256;
num++;
if(num==2)
{
num = 0;
fen = ~fen;
}
}
蜂鸣器准确发声(操控其宣布100ms,200ms,300ms,400ms,500ms 的频率
错误代码:
//这个程序调试了一上午吧,感觉有点不对劲,问题是出在
//守时器的打断上,即两个守时器都在作业的时分,会不会打断原本的守时器
//这个程序实习完成的功用原本是使喇叭发声,宣布
//以100ms,200,300,400,500
//原本应该听到5个频率,可是实践仅仅听到3个,阐明中止发生了错位,中止之间发生了彼此的影响
#include
#define uint unsigned int
#define uchar unsigned char
sbit fen = P2^6;
uchar num,flag,tt;
void init();
void main()
{
init();
while(1);
}
void init()
{
TMOD = 0x11;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
ET1 = 1;
ET0 =1;
EA = 1;
TR0 =1;
TR1 =1;
}
void TIme0()interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
tt++;
switch(tt/20)
{
case 0:
flag = 1;
break;
case 1:
flag = 2;
break;
case 2:
flag = 3;
break;
case 3:
flag = 4;
break;
case 4:
tt=0;
flag = 5;
break;
}
}
void TIme1()interrupt 3
{
TH1 = (65536-5000)/256;//守时一次是50毫秒
TL1 = (65536-5000)%256;
num++;
if(num==flag)
{
num = 0;
fen = ~fen;
}
}
正确代码
/*
一起用两个守时器操控蜂鸣器发声,
守时器0操控频率,守时器1操控同个
频率继续的时刻,距离2s顺次输出
1,10,50,100,200,400,800,
1k(hz)的方波?
设晶振频率为12MHz。
*/
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit beep = P2^3;
uchar tt;
uint fre,flag;
void main()
{
fre = 50000;
beep = 0;
TMOD = 0x11;
TH0 = (65536-fre)/256;
TL0 = (65536-fre)%256;
TH1 = (65536- 50000)/256;
TL1 = (65536-50000)%256;
EA = 1;
ET0 = 1;
ET1 =1;
TR0 =1;
TR1 = 1;
while(1);
}
void TImer0()interrupt 1
{
TR0 = 0;//进入中止函数后封闭守时器0,以避免内部程序中止丢掉
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
tt ++;
if(flag《40)
if(tt==10)
{
tt = 0;
fre = 50000;//分别用fre来选取频率守时50ms,所以中止发生的频率为10hz
beep = ~beep;
}
if(flag》=40&&flag《80)
{
tt = 0;
fre = 50000;//分别用fre来选取频率守时50ms,所以中止发生的频率为10hz
beep =~beep;
}
if(flag》=80&&flag《120)
{
tt = 0;
fre = 10000;//守时10ms,周期为20ms,频率为50hz
beep =~beep;
}
if(flag》=120&&flag《160)
{
tt = 0;
fre = 5000;//守时5毫秒,周期为10ms,频率为100zh
beep =~beep;
}
if(flag》=160&&flag《200)
{
tt = 0;
fre = 2500;//守时2.5,周期5ms,频率为200
beep =~beep;
}
if(flag》=200&&flag《240)
{
tt = 0;
fre = 1250;//守时1.25,周期2.5ms,频率400
beep =~beep;
}
if(flag》=240&&flag《280)
{
tt = 0;
fre = 625;//守时2.5,周期5ms,频率为800
beep =~beep;
}
if(flag》=280&&flag《320)
{
tt = 0;
fre = 312;//频率为1600
beep =~beep;
}
if(flag》=320&&flag《360)
{
tt = 0;
fre = 156;//频率为3200
beep =~beep;
}
TR0 = 1;
}
void timer1()interrupt 3
{
TH1 = (65536 – 50000)/256;
TL1 = (65536 – 50000)%256;
flag++;
if(flag == 360)
{
flag = 0;
fre = 50000;
}
}