2个回答
展开全部
#include <reg51.h>
#include <intrins.h>
sbit SPK = P2^7;
unsigned char frq;
unsigned int flag;
void delayms(unsigned char ms);
void main()
{
TMOD = 0x01;
frq = 0x00;
TH0 = 0x00;
TL0 = 0xff;
TR0 = 1;
IE = 0x82;
flag=0;
while(1)
{
switch(flag)
{ case 0:frq++;break; //flag标志置0时,frq递增
case 1:frq--;break; //flag标志置1时,frq递减
}
if (!(frq^0xff)) flag=1; //当frq增加到FFH时,flag置1,准备frq递减
if (!(frq^0x00)) flag=0; //当frq递减到00H时,flag置0,准备frq递增
delayms(15);
}
}
void timer0() interrupt 1 using 1
{
TH0 = 0xfe;
TL0 = frq;
SPK = ~SPK;
}
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
这是蜂鸣器报警的代码
#include <intrins.h>
sbit SPK = P2^7;
unsigned char frq;
unsigned int flag;
void delayms(unsigned char ms);
void main()
{
TMOD = 0x01;
frq = 0x00;
TH0 = 0x00;
TL0 = 0xff;
TR0 = 1;
IE = 0x82;
flag=0;
while(1)
{
switch(flag)
{ case 0:frq++;break; //flag标志置0时,frq递增
case 1:frq--;break; //flag标志置1时,frq递减
}
if (!(frq^0xff)) flag=1; //当frq增加到FFH时,flag置1,准备frq递减
if (!(frq^0x00)) flag=0; //当frq递减到00H时,flag置0,准备frq递增
delayms(15);
}
}
void timer0() interrupt 1 using 1
{
TH0 = 0xfe;
TL0 = frq;
SPK = ~SPK;
}
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
这是蜂鸣器报警的代码
追问
单片机没有反应咧
追答
你看你开发板的电路图,然后照程序把杜邦线结对了才会有反应吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询