按下8个独立按键中的一个,数码管显示相应数字,且蜂鸣器响一声。
#include<reg52.h>
#define uchar unsigned char //数据类型宏定义
#define uint unsigned int
sbit K1=P3^2; //定义独立按键K1
sbit K2=P3^3;
sbit K3=P3^4;
sbit K4=P3^5;
sbit K5=P1^4;
sbit K6=P1^5;
sbit K7=P1^6;
sbit K8=P1^7; //定义独立按键K8
sbit BEEP=P3^6; //蜂鸣器控制端口P36
/*延时子函数*/
void delay1(uchar x)
{
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#define uchar unsigned char
#define uint unsigned int
sbit K1=P3^2;
sbit K2=P3^3;
sbit K3=P3^4;
sbit K4=P3^5;
sbit K5=P1^4;
sbit K6=P1^5;
sbit K7=P1^6;
sbit K8=P1^7;
sbit BEEP=P3^6;
void delay1(uchar x)
{
uchar j;
while((x--)!=0)
{
for(j=0;j<125;j++)
{;}
}
}
void delay0(uchar x)
{
unsigned char i;
while(x--)
{
for(i=0;i<13;i++)
{}
}
}
void delay(uint ms)
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++)
;
}
}
void beep()
{
unsigned char i;
for(i=0;i<100;i++)
{
delay0(4);
BEEP=!BEEP;
}
BEEP=1;
delay1(250);
}
void main()
{
P1=0xff;
P2=0x7f;
while(1)
{
P0=0xbf;
if(K1==0)
{
delay(20);
if(K1==0)
{
beep();
P0=0xf9;
delay(1000);
}
}
else if(K2==0)
{
delay(20);
if(K2==0)
{
beep();
P0=0xa4;
delay(1000);
}
}
else if(K3==0)
{
delay(20);
if(K3==0)
{
beep();
P0=0xb0;
delay(1000);
}
}
else if(K4==0)
{
delay(20);
if(K4==0)
{
beep();
P0=0x99;
delay(1000);
}
}
else if(K5==0)
{
delay(20);
if(K5==0)
{
beep();
P0=0x92;
delay(1000);
}
}
else if(K6==0)
{
delay(20);
if(K6==0)
{
beep();
P0=0x82;
delay(1000);
}
}
else if(K7==0)
{
delay(20);
if(K7==0)
{
beep();
P0=0xf8;
delay(1000);
}
}
else if(K8==0)
{
delay(20);
if(K8==0)
{
beep();
P0=0x80;
delay(1000);
}
}
}
}
一周热门 更多>