第一个问题:已经编了记时50ms和20次循环,可是间隔不是1s。第二个问题:因为位选所以要用P2.2和P2.3,由于编写了P2=0xf7和P2=0xfb其他的P2口无法控制。
这是程序:
#include<reg51.h>
sbit s=P1^3;
sbit b=P3^3;
sfr P2M0=0xf3;
sfr P2M1=0xff;
sbit a=P2^4;
sbit e=P2^5;
sbit d=P2^6;
unsigned char code Tab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code c[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char int_time;
unsigned char second;
void delay()
{
unsigned char i;
for(i=0;i<200;i++)
;
}
void DisplaySecond(unsigned char k)
{
P2=0xf7;
P0=Tab[k%10];
delay();
P2=0xfb;
P0=Tab[k/10];
delay();
P0=0xff;
}
void main()
{
P3=0x00;
TMOD=0x90;
TL1=(65536-50000)/256;
TH1=(65536-50000)%256;
EA=1;
ET1=1;
TR1=1;
EX1=1;
IT1=1;
int_time=0;
second=0;
TR1=0;
if(a==0)
{
P1=c[0];
}
if(e==0)
{
P1=c[1];
}
if(d==0)
{
P1=c[2];
}
while(1)
{
DisplaySecond(second);
}
}
void time1()interrupt 3 using 1
{
int_time++;
if(int_time==20)
{
int_time=0;
if(second<30)
second++;
}
if(second==30)
{
b=0;
}
if(b==0)
{
second=0;
}
TL1=(65536-50000)/256;
TH1=(65536-50000)%256;
TR1=1;
}
|