
求MCS-51单片机四路抢答器的c语言程序设计
2个回答
展开全部
#include<reg51.h>
#defineuint unsigned int
#defineuchar unsigned char
sbitspeaker = P2^7;
sbit key0 =P1^0;
sbit key1 =P1^1;
sbit key2 =P1^2;
sbit key3 =P1^3;
sbit key4 =P1^4;
sbit key5 =P1^5;
sbit key6 =P1^6;
sbit key7 =P1^7;
ucharled[8]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80};
voiddelay_1ms(uint t)
{
uchar m,n;
for(m=t;m>0;m--)
for(n=330;n>0;n--);
}
bitiskeyinput() //判断是否有按键闭合
{
if((P1 & 0xff) ==0xff ) //屏蔽高4位(列线),只检测低4位(行线)
return 0; //没有键闭合,返回0
else
return 1; //有键闭合,返回1
}
ucharkey_identify() //识别键号
{
uchar key;
if( key0==0 )key=0;
if(key1==0 )key=1;
if( key2==0 )key=2;
if( key3==0 )key=3;
if( key4==0 )key=4;
if( key5==0 )key=5;
if( key6==0 )key=6;
if( key7==0 )key=7;
return key; //输出键号
}
voidkeyprocess(uchar keynum)
{
uint i;
P0 = led[keynum];
i=500;
while(i--)
{
speaker = 0;
delay_1ms(10);
speaker = 1;
}
}
void main()
{
uchar keynum;
P0 = 0xFF;
while(1)
{
while (!iskeyinput() ); //如果没有键,则等待
delay_1ms(10); //去除键抖动
if(iskeyinput() ) //当前有按键,需要识别按键并且等待键盘松开
{
keynum= key_identify(); //识别键号
keyprocess(keynum);
while(1); //等待复位
}
}
}
#defineuint unsigned int
#defineuchar unsigned char
sbitspeaker = P2^7;
sbit key0 =P1^0;
sbit key1 =P1^1;
sbit key2 =P1^2;
sbit key3 =P1^3;
sbit key4 =P1^4;
sbit key5 =P1^5;
sbit key6 =P1^6;
sbit key7 =P1^7;
ucharled[8]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80};
voiddelay_1ms(uint t)
{
uchar m,n;
for(m=t;m>0;m--)
for(n=330;n>0;n--);
}
bitiskeyinput() //判断是否有按键闭合
{
if((P1 & 0xff) ==0xff ) //屏蔽高4位(列线),只检测低4位(行线)
return 0; //没有键闭合,返回0
else
return 1; //有键闭合,返回1
}
ucharkey_identify() //识别键号
{
uchar key;
if( key0==0 )key=0;
if(key1==0 )key=1;
if( key2==0 )key=2;
if( key3==0 )key=3;
if( key4==0 )key=4;
if( key5==0 )key=5;
if( key6==0 )key=6;
if( key7==0 )key=7;
return key; //输出键号
}
voidkeyprocess(uchar keynum)
{
uint i;
P0 = led[keynum];
i=500;
while(i--)
{
speaker = 0;
delay_1ms(10);
speaker = 1;
}
}
void main()
{
uchar keynum;
P0 = 0xFF;
while(1)
{
while (!iskeyinput() ); //如果没有键,则等待
delay_1ms(10); //去除键抖动
if(iskeyinput() ) //当前有按键,需要识别按键并且等待键盘松开
{
keynum= key_identify(); //识别键号
keyprocess(keynum);
while(1); //等待复位
}
}
}

2020-01-15 广告
单片机(Microcontrollers)是一种集成电路芯片,是采用超大规模集成电路技术把具有数据处理能力的中央处理器CPU、随机存储器RAM、只读存储器ROM、多种I/O口和中断系统、定时器/计数器等功能(可能还包括显示驱动电路、脉宽调制...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询