#include "reg51.h"
sbit K1=P3^0;
sbit K2=P3^1;
//sbit geweishumaguan=P2^6;
//sbit shiwei=P2^7;
unsigned char num=0;
unsigned char weixuan[]={0xef,0xbf};//数码管位选P2控制的P27、P26
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsigned char diswei[]={0xef,0xef};//上电显示0
void delay10ms(void) //误差 0us
{
unsigned char a,b,c;
for(c=5;c>0;c--)
for(b=4;b>0;b--)
for(a=248;a>0;a--);
}
void delay2ms(void) //误差 0us
{
unsigned char a,b;
for(b=133;b>0;b--)
for(a=6;a>0;a--);
}
void InitTimer0(void)
{
TMOD = 0x02; //定时器0计时,方式二8位自动重装
TH0 = 0x0D8; //100ms
TL0 = 0x0F0;
EA = 1;
ET0 = 1;
// TR0 = 1;
}
void keyscan() //按键扫描
{
if(K1==0)
{
delay10ms();
if(K1==0)
{
while(!K1);
TR0=1;//启动计时器
}
}
if(K2==0)
{
delay10ms();
if(K2==0)
{
while(!K1);
TR0=0;//停止计时器
}
}
}
void display()
{
unsigned char i;
diswei[0]=num/10;
diswei[1]=num%10;
P0=0X00;
P2=0XFF;
for(i=0;i<2;i++)
{
P0=table[diswei[1]];
P2=weixuan[i];
delay2ms();
P2|=0xff;
}
}
void main(void)
{
InitTimer0();
while(1)
{
keyscan();
display();
}
}
void timeo() interrupt 1
{
static unsigned char i=0;
i++;
if(i==10)
{
i=0;
num++;
if(num==60)
{
num=0;
}
}
} |