51单片机中数码管位选,段选都用在P0口怎么写程序啊
时间:10-02
整理:3721RD
点击:
51单片机中数码管位选,段选都用在P0口怎么写程序啊这是一个抢答器程序,位选在P2口,请教把位选改在P0口怎么改程序啊,直接把p2换成p0口好像不行,求大神指导,单片机还在学习中
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit speak=P3^4;
sbit huang=P3^0;
sbit red=P3^1;
sbit sjia=P3^6;
sbit sji=P3^7;
sbit kai=P3^0;
sbit fuwei=P3^1;
sbit kais=P3^2;
uchar num,temp,shijian,shi,ge,ashi,age,aa,tt,bb,i;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(uint z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void bj()
{
for(i=0;i<110;i++)
{
speak=~speak; delay(5);
}
for(i=0;i<230;i++)
{
speak=~speak; delay(2);
}
}
void keyscan()
{
if(sjia==0)
{
delay(5);
if(sjia==0)
{
shijian=shijian+1;
if(shijian==99)
{
shijian=0;
}
}
while(!sjia);
}
if(sji==0)
{
delay(5);
if(sji==0)
{
shijian=shijian-1;
if(shijian==0)
{
shijian=30;
}
}
while(!sji);
}
}
void zhuanhuan()
{
shi=shijian/10;
ge=shijian%10;
ashi=num/10;
age=num%10;
}
void sound()
{
speak=0;
}
void main()
{ huang=0;red=0;
EA=1;
TMOD=0x11;
T2CON=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
TH2=(65536-50000)/256;
TL2=(65536-50000)%256;
ET0=1;
TR0=0;
ET1=1;
TR1=1;
ET2=1;
TR2=0;
IT0=1;
EX0=1;
IT1=1;
EX1=1;
aa=0;
bb=0;
shijian=30;
while(1)
{
keyscan();
}
}
void int_0() interrupt 0
{
huang=1;
TR0=1;
TR2=1;
}
void timer_0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
shijian--;
if(shijian==6)
{
red=1;
huang=0;
sound();
}
if(shijian==0)
{
TR0=0;
TR2=0;
TF2=0;
}
}
}
void int_1() interrupt 2
{
shijian=30;
num=0;
TR0=0;
TR2=0;
}
void timer_1() interrupt 3
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
tt++;
bb++;
if(tt==5)
tt=1;
zhuanhuan();
switch(tt)
{
case 1:P2=0xf4;P0=table[shi];break;
case 2:P2=0xf8;P0=table[ge];break;
case 3:P2=0xf1;P0=table[ashi];break;
case 4:P2=0xf2;P0=table[age];break;
default : ;
}
if(bb==1)
{
bb=0;
if(shijian==5)
{
speak=~speak;
}
if(shijian==4)
{
speak=~speak;
}
if(shijian==3)
{
speak=~speak;
}
if(shijian==2)
{
speak=~speak;
}
if(shijian==1)
{
speak=~speak;
}
}
}
void timer_2() interrupt 5
{
TH2=(65536-50000)/256;
TL2=(65536-50000)%256;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
TR2=0;
switch(temp)
{
case 0xee:{TR0=0;TF2=0;num=1;bj();}break;
case 0xde:{TR0=0;TF2=0;num=2;bj();}break;
case 0xbe:{TR0=0;TF2=0;num=3;bj();}break;
case 0x7e:{TR0=0;TF2=0;num=4;bj();}break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
TR2=0;
switch(temp)
{
case 0xed:{TR0=0;TF2=0;num=5;bj();}break;
case 0xdd:{TR0=0;TF2=0;num=6;bj();}break;
case 0xbd:{TR0=0;TF2=0;num=7;bj();}break;
case 0x7d:{TR0=0;TF2=0;num=8;bj();}break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit speak=P3^4;
sbit huang=P3^0;
sbit red=P3^1;
sbit sjia=P3^6;
sbit sji=P3^7;
sbit kai=P3^0;
sbit fuwei=P3^1;
sbit kais=P3^2;
uchar num,temp,shijian,shi,ge,ashi,age,aa,tt,bb,i;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(uint z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void bj()
{
for(i=0;i<110;i++)
{
speak=~speak; delay(5);
}
for(i=0;i<230;i++)
{
speak=~speak; delay(2);
}
}
void keyscan()
{
if(sjia==0)
{
delay(5);
if(sjia==0)
{
shijian=shijian+1;
if(shijian==99)
{
shijian=0;
}
}
while(!sjia);
}
if(sji==0)
{
delay(5);
if(sji==0)
{
shijian=shijian-1;
if(shijian==0)
{
shijian=30;
}
}
while(!sji);
}
}
void zhuanhuan()
{
shi=shijian/10;
ge=shijian%10;
ashi=num/10;
age=num%10;
}
void sound()
{
speak=0;
}
void main()
{ huang=0;red=0;
EA=1;
TMOD=0x11;
T2CON=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
TH2=(65536-50000)/256;
TL2=(65536-50000)%256;
ET0=1;
TR0=0;
ET1=1;
TR1=1;
ET2=1;
TR2=0;
IT0=1;
EX0=1;
IT1=1;
EX1=1;
aa=0;
bb=0;
shijian=30;
while(1)
{
keyscan();
}
}
void int_0() interrupt 0
{
huang=1;
TR0=1;
TR2=1;
}
void timer_0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
shijian--;
if(shijian==6)
{
red=1;
huang=0;
sound();
}
if(shijian==0)
{
TR0=0;
TR2=0;
TF2=0;
}
}
}
void int_1() interrupt 2
{
shijian=30;
num=0;
TR0=0;
TR2=0;
}
void timer_1() interrupt 3
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
tt++;
bb++;
if(tt==5)
tt=1;
zhuanhuan();
switch(tt)
{
case 1:P2=0xf4;P0=table[shi];break;
case 2:P2=0xf8;P0=table[ge];break;
case 3:P2=0xf1;P0=table[ashi];break;
case 4:P2=0xf2;P0=table[age];break;
default : ;
}
if(bb==1)
{
bb=0;
if(shijian==5)
{
speak=~speak;
}
if(shijian==4)
{
speak=~speak;
}
if(shijian==3)
{
speak=~speak;
}
if(shijian==2)
{
speak=~speak;
}
if(shijian==1)
{
speak=~speak;
}
}
}
void timer_2() interrupt 5
{
TH2=(65536-50000)/256;
TL2=(65536-50000)%256;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
TR2=0;
switch(temp)
{
case 0xee:{TR0=0;TF2=0;num=1;bj();}break;
case 0xde:{TR0=0;TF2=0;num=2;bj();}break;
case 0xbe:{TR0=0;TF2=0;num=3;bj();}break;
case 0x7e:{TR0=0;TF2=0;num=4;bj();}break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
TR2=0;
switch(temp)
{
case 0xed:{TR0=0;TF2=0;num=5;bj();}break;
case 0xdd:{TR0=0;TF2=0;num=6;bj();}break;
case 0xbd:{TR0=0;TF2=0;num=7;bj();}break;
case 0x7d:{TR0=0;TF2=0;num=8;bj();}break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
}