用单片机独立按键实现了一个小玩意——简易抢答器,这个很简单,涉及到了八段数码管的动态扫描显示,以及独立按键的消抖等。期间遇到了一个小问题,就是抢答器在一个人按下之后需要将数码管显示锁定,这个可以使用一个标志位来实现。
程序:
#include <reg51.h>
#define uchar unsigned char
#define dataport P2
#define control P0
sbit compere = P1^0;
sbit num1 = P1^1;
sbit num2 = P1^2;
sbit num3 = P1^3;
sbit num4 = P1^4;
sbit num5 = P1^5;
sbit num6 = P1^6;
sbit num7 = P1^7;
sbit suo = P0^3;
uchar code seg_code[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x00};
uchar code col_code[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
uchar temp;
uchar flag = 1;
void display(uchar first, uchar num);
void Delay10ms(); //@12.000MHz
void main()
{
while(1)
{
if(!compere)
{
Delay10ms();
if(!compere)
{
while(!compere);
temp = 10;
flag = 1;
display(0, 8);
}
}
if(flag)
{
if(!num1)
{
Delay10ms();
if(!num1)
{
while(!num1);
temp = 1;
display(1, 1);
flag = 0;
}
}
}
if(flag)
{
if(!num2)
{
Delay10ms();
if(!num2)
{
while(!num2);
temp = 2;
display(2, 1);
flag = 0;
}
}
}
if(flag)
{
if(!num3)
{
Delay10ms();
if(!num3)
{
while(!num3);
temp = 3;
display(3, 1);
flag = 0;
}
}
}
if(flag)
{
if(!num4)
{
Delay10ms();
if(!num4)
{
while(!num4);
temp = 4;
display(4, 1);
flag = 0;
}
}
}
if(flag)
{
if(!num5)
{
Delay10ms();
if(!num5)
{
while(!num5);
temp = 5;
display(5, 1);
flag = 0;
}
}
}
if(flag)
{
if(!num6)
{
Delay10ms();
if(!num6)
{
while(!num6);
temp = 6;
display(6, 1);
flag = 0;
}
}
}
if(flag)
{
if(!num7)
{
Delay10ms();
if(!num7)
{
while(!num7);
temp = 7;
display(7, 1);
flag = 0;
}
}
}
}
}
void display(uchar first, uchar num)
{
uchar i, j;
for(i = 0; i < num; i++)
{
dataport = 0x00;
suo = 1;
suo = 0;
dataport = seg_code[temp];
suo = 1;
suo = 0;
control = col_code[i+first];
j = 500;
while(j--);
}
}
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}