找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 943|回复: 0
打印 上一主题 下一主题
收起左侧

单片机16路抢答器代码

[复制链接]
跳转到指定楼层
楼主
ID:841382 发表于 2021-11-29 18:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x84};
uchar code table0[]={0xf7,0xfe,0xfd,0xfb};
unsigned char code table2[]={0xef,0xdf,0xbf,0x7f};
uchar display[]={0,0,0,0};
void sw1();
void sw2();
void sw3();
void sw4();
void xianshi();
void init();
uchar temp,num2,x,x1,x2,a,count;
int num,num1;
uchar key;
uchar i,j;
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit k4=P1^3;
sbit sp=P1^4;
sbit l1=P1^5;
sbit l2=P1^6;
sbit P3_4=P3^4;
sbit P3_5=P3^5;
sbit P3_6=P3^6;
sbit P3_7=P3^7;
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
{
a=0;
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
if(k1==0)
{
delay(5);
if(k1==0)
{
num++;
if(num==99)
{
num=0;
}
num2=num;
while(k1==0);
}
}
if(k2==0)
{
delay(5);
if(k2==0)
{
num--;
if(num==-1)
{
num=99;
}
num2=num;
while(k2==0);
}
}
if(k3==0)
{
l1=0;
delay(5);
if(k3==0)
{
x++;
if(x==2)
{
x=0;
}
while(k3==0);
}
}
}
void keyboad()
{
while(1)
{
P3=0xff;
P3_4=0;
temp=P3;
temp=temp & 0x0f; //取高四位
if (temp!=0x0f)
{
delay(5);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
{delay(5);num1=4;l2=0;xianshi();} break;
case 0x0d:
{delay(5);num1=3;l2=0;xianshi();}break;
case 0x0b:
{delay(5);num1=2;l2=0;xianshi();}break;
case 0x07:
{delay(5);num1=1;l2=0;xianshi();} break;
}
}
}
l1=1;
P3=0xff;
P3_5=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
delay(5);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
{delay(5);num1=8;l2=0;xianshi();} break;
case 0x0d:
{delay(5);num1=7;l2=0;xianshi();} break;
case 0x0b:
{delay(5);num1=6;l2=0;xianshi();} break;
case 0x07:
{delay(5);num1=5;l2=0;xianshi();} break;
}
}
}
l1=1;
P3=0xff;
P3_6=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
delay(5);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
{delay(5);num1=12;l2=0;xianshi();} break;
case 0x0d:
{delay(5);num1=11;l2=0;xianshi();} break;
case 0x0b:
{delay(5);num1=10;l2=0;xianshi();} break;
case 0x07:
{delay(5);num1=9;l2=0;xianshi();} break;
}
}
}
l1=1;
P3=0xff;
P3_7=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
delay(5);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
{delay(5);num1=16;l2=0;xianshi();} break;
case 0x0d:
{delay(5);num1=15;l2=0;xianshi();} break;
case 0x0b:
{delay(5);num1=14;l2=0;xianshi();} break;
case 0x07:
{delay(5);num1=13;l2=0;xianshi();} break;
}
l1=1;
temp=P3;
P0=table[key];
}
}
}
}
void xianshi()
{
TR0=0;
num=0;
sp=1;
delay(100);
sp=0;
while(1)
{
for(a=0;a<4;a++)
{
delay(5);
P2=table0[a];
P0=table[display[a]];
}
display[0]=num/10;
display[1]=num%10;
display[2]=num1/10;
display[3]=num1%10;
if(k4==0)          //
{
num=num2;
num1=0;
TR0=1;
l1=1;
l2=1;
return;
}
}
}
void main()
{
l1=1;
l2=1;
sp=0;
x=0;
num=num2=30;
while(1)
{
if(x==1)
{
delay(5);
TR0=1;
while(1)
{
keyboad();
if(k4==0)
{
l1=1;
l2=1;
delay(5);
num=num2;
TR0=1;
}
}
}
else
{
for(a=0;a<4;a++)
{
delay(5);
P2=table0[a];
P0=table[display[a]];
}
display[0]=num/10;
display[1]=num%10;
display[2]=num1/10;
display[3]=num1%10;
init();
}
}
}
void time0() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
count++;
if(k4==0)
{
num=num2;
TR0=1;
num1=0;
return;
}
for(a=0;a<4;a++)
{
delay(10);
P2=table0[a];
P0=table[display[a]];
}
display[0]=num/10;
display[1]=num%10;
display[2]=num1/10;
display[3]=num1%10;
if(count==100)
{
count=0;
num--;
if(num<5)
{
l1=~l1;
}
}
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表