新手上路
- 积分
- 31
- 金钱
- 31
- 注册时间
- 2019-5-15
- 在线时间
- 4 小时
|
#include <reg51.h>//抢答器(还未设置未抢答响铃)
#define key P1
typedef unsigned int u16;
typedef unsigned char u8;
sbit k1=P3^1;
sbit k2=P3^0;
sbit k3=P3^2;
sbit k4=P3^3;
sbit LSA=P2^4;
sbit LSB=P2^3;
sbit LSC=P2^2;
u8 tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
u8 m[8]={10,10,10,10,10,10,10,10};
u8 z,q=10,i;
u8 flag1,flag2,flag3,flag4;
void delay(u16 i){
while(i--);
}
void Time1Config(){
TMOD|=0x01;
TH1=0xd8;
TL1=0xf0;//10ms
TR1=1;//开启定时器
ET1=1;
EA=1;
}
u8 kskan(void)
{
u8 a=0,KeyValue;
key=0x0f;
if(key!=0x0f)//读取按键是否按下
{
delay(1000);//延时10ms进行消抖
if(key!=0x0f)//再次检测键盘是否按下
{
//测试列
key=0X0F;
switch(key)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//测试行
key=0XF0;
switch(key)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(key!=0xf0)) //检测按键松手检测
{
delay(1000);
a++;
}
}
}
return KeyValue;
}
void daojishi(){
if(flag1==1){
m[0]=q;
m[1]=10;
m[2]=10;
m[3]=10;
m[4]=10;
m[5]=10;
m[6]=10;
m[7]=z;
}
if(flag2==1||flag3==1){
m[0]=q;
m[1]=10;
m[2]=10;
m[3]=10;
m[4]=10;
m[5]=10;
m[6]=z%10;
m[7]=z/10;
}
}
void shumaguan(){
u8 i;
for(i=0;i<8;i++){
switch(i){
case 0SA=0;LSB=0;LSC=0;break;
case 1SA=0;LSB=0;LSC=1;break;
case 2SA=0;LSB=1;LSC=0;break;
case 3SA=0;LSB=1;LSC=1;break;
case 4SA=1;LSB=0;LSC=0;break;
case 5SA=1;LSB=0;LSC=1;break;
case 6SA=1;LSB=1;LSC=0;break;
case 7SA=1;LSB=1;LSC=1;break;
}
P0=tab[m[i]];
delay(100);
P0=0x00;
}
}
void duli(){
if(k1==0){
delay(1000);
if(k1==0){
flag1=1;
flag2=0;
flag3=0;
}
}
if(k2==0){
delay(1000);
if(k2==0){
flag1=0;
flag2=1;
flag3=0;
}
}
if(k3==0){
delay(1000);
if(k3==0){
flag1=0;
flag2=0;
flag3=1;
}
}
if(k4==0){
delay(1000);
if(k4==0){
flag4=1;
}
}
if(flag1==1)
z=5;
if(flag2==1)
z=10;
if(flag3==1)
z=20;
}
void main(){
u8 d;
while(1){
duli();
daojishi();
shumaguan();
if(flag4==1){
Time1Config();
d=kskan();
if((d>=0&&d<=3)&&z>0){
q=d;
z=z;
}
}
}
}
void Time1() interrupt 3{
TH1=0xd0;
TL1=0x8f;
i++;
if(i==100){
i=0;
z--;
}
}
|
|