51单片机抢答器实现

利用共阳极数码管和独立键盘连接单片的P1,P2口实现抢答器

程序如下:

#include
void delay(unsigned char n)
{
unsigned int i,j;
for( i = 255; i > 0; i--)
{
for( j = n; j > 0; j--)
{
}
}
}
void main(void)
{
unsigned char i,flag;
unsigned char arr[16] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
P1 = 0xff;
P2 = 0xff;
flag = 0;
while(1)
{

switch(P2)
{
case 0xfe: 
{
if(flag == 0)
{
P1 = 0xff;
flag = 1;
}
break;
}
case 0xfd: 
{
if(flag == 1)
{
P1 = arr[1];
flag = 0;
}
break;
}
case 0xfb:
{
if(flag == 1)
{
P1 = arr[2];
flag = 0;
}
break;
}
case 0xf7:
{
if(flag == 1)
{
P1 = arr[3];
flag = 0;
}
break;
}
case 0xef:
{
if(flag == 1)
{
P1 = arr[4];
flag = 0;
}
break;
}
case 0xdf:
{
if(flag == 1)
{
P1 = arr[5];
flag = 0;
}
break;
}
case 0xbf:
{
if(flag == 1)
{
P1 = arr[6];
flag = 0;
}
break;
}
case 0x7f:
{
if(flag == 1)
{
P1 = arr[7];
flag = 0;
}
break;
}
default:break;
}
}


}

你可能感兴趣的:(单片机)