2021-06-10 51单片机设计一个蜂鸣器报警电路每秒

缘由求助一下谢谢啦51单片机_嵌入式-CSDN问答设计一个蜂鸣器报警电路,按下K1,蜂鸣器响一声,按下K2,蜂鸣器响三声,按下K3,蜂鸣器长鸣。要求响声和间隔的时间均为1秒,长鸣不限时,但是此时应设置一个按键能够使长鸣结束

#include "REG52.h"
sbit K1 = P1^5;
sbit K2 = P1^6;
sbit K3 = P1^7;
sbit BUZ1=P1^0;
bit k=0;
unsigned char code SmZiFu[]={63,6,91,79,102,109,125,7,127,111,128};//0-9.
unsigned char Js=0,kz=0;//中断计时
void smxs(unsigned char mz,unsigned char w)
{
	unsigned char Xd=0;
	P0=255;
	P0=255-SmZiFu[mz];
	P2=w;
	while(++Xd);
}
void JiShi(/*时间计算*/)
{
		if(Js>=20){Js=0;kz=0;}
}
void ZhongDuanSheZhi()
{
	TH0+=0X4c;/*定时器赋初值,定时50ms触发中断,自动补偿方式*/
	TL0+=0X00; 
	TMOD=0X01;
	TR0=1; 
	ET0=1; //开启定时器
	EA=1; //全局中断开关
}
void main()
{
	unsigned char Xd=200,ss=0;
	unsigned int wei=0;
	ZhongDuanSheZhi();
	while(1)
	{
		if(K1==0&&++Xd==0){ss=1*2;while(K1==0)smxs(ss,8);kz=1;Xd=200;}
		if(K2==0&&++Xd==0){ss=3*2;while(K2==0)smxs(ss,8);kz=1;Xd=200;}
		if(K3==0&&++Xd==0){ss=0;k=~k;while(K3==0)smxs(k?1:0,8);Xd=200;}
		if(ss&&!kz)
		{
			++kz;
			k=~k;
			--ss;
			smxs(ss,8);
		}
		if(k&&++Xd==0)BUZ1=~BUZ1;
		JiShi();//一定要放在最后
	}
}
void ZhongDuan() interrupt 1
{
	++Js;
	TH0+=0X4c;
	TL0+=0X00;        
}
#include "REG52.h"
sbit K1 = P1^5;
sbit K2 = P1^6;
sbit K3 = P1^7;
sbit BUZ1=P1^0;
bit k=0;
unsigned char code SmZiFu[]={63,6,91,79,102,109,125,7,127,111,128};//0-9.
void smxs(unsigned char mz,unsigned char w)
{
	unsigned char Xd=0;
	P0=255;
	P0=255-SmZiFu[mz];
	P2=w;
	while(++Xd);
}
void main()
{
	unsigned char Xd=0,ss=0;
	unsigned int wei=0;
	while(1)
	{
		if(K1==0&&++Xd==0){ss=1*2;while(K1==0)smxs(ss,8);}
		if(K2==0&&++Xd==0){ss=3*2;while(K2==0)smxs(ss,8);}
		if(K3==0&&++Xd==0){ss=0;k=!k;while(K3==0);}
		if(ss&&!++wei)
		{
			k=!k;
			--ss;smxs(ss,8);
		}
		if(k&&++Xd==0)BUZ1=~BUZ1;
	}
}

2021-06-10 51单片机设计一个蜂鸣器报警电路每秒_第1张图片

你可能感兴趣的:(51单片机,单片机,嵌入式硬件)