刚刚开始学习单片机,还有很多不懂,请多多指教。
#include <reg52.h>
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
#define key P1
int i,j;
int a,b,c;
int flag=0;
int min,hour,sec,ssec;
unsigned int code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示0-9
unsigned int weixuan[8];
void time0();
void waibu();
void xuanwei();
void date();
void delay(int a);
void time0()
{
TMOD=TMOD|0X01;
TH0=0Xd8;
TL0=0Xf0;
ET0=1;
EA=1;
TR0=1;
}
void waibu()
{
EA=1;
IT0=0;
EX0=1;
}
void xuanwei()
{
unsigned int i;
for(i=0;i<8;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
case(2):
LSA=0;LSB=1;LSC=0; break;
case(3):
LSA=1;LSB=1;LSC=0; break;
case(4):
LSA=0;LSB=0;LSC=1; break;
case(5):
LSA=1;LSB=0;LSC=1; break;
case(6):
LSA=0;LSB=1;LSC=1; break;
case(7):
LSA=1;LSB=1;LSC=1; break;
}
P0=weixuan[i];
delay(100);
P0=0x00;
}
}
void date()
{
weixuan[0]=smgduan[sec%10];
weixuan[1]=smgduan[sec/10];
weixuan[2]=0x40;
weixuan[3]=smgduan[min%10];
weixuan[4]=smgduan[min/10];
weixuan[5]=0x40;
weixuan[6]=smgduan[hour%10];
weixuan[7]=smgduan[hour/10];
}
void delay(int a)
{
while(a--);
}
void main()
{
while(1)
{
time0();
waibu();
while(1)
{
date();
xuanwei();
}
}
}
void keyr() interrupt 0
{
key=0x0f;
if(key!=0xf0)
{
delay(100);
if(key!=0x0F)
{
key=0x0f;
switch(key)
{
case 0x07:c=1;break;
}
key=0xf0;
switch(key)
{
case 0x70:c=c;break;
case 0xb0:c=1+c;break;
case 0xd0:c=c+2;break;
}
if(key3==0&&c==1&&key1==0)
{
if(sec>=60)
{
sec=sec-60;
min=min+1;
if(min>=60)
{
min=min-60;
}
}
sec=sec+1;
date();
xuanwei();
// delay(50000);
}
if(key3==0&&c==1&&key2==0)
{
if(sec<=0)
{
sec=sec+60;
min=min-1;
if(min<=0)
{
min=min+60;
}
}
sec=sec-1;
date();
xuanwei();
// delay(50000);
}
if(key3==0&&c==2&&key1==0)
{
if(min>=60)
{
min=min-60;
hour=hour+1;
if(hour>=24)
{
hour=0;
}
}
min=min+1;
date();
xuanwei();
// delay(50000);
}
if(key3==0&&c==2&&key2==0)
{
if(min<=0)
{
min=min+60;
hour=hour-1;
if(hour<=0)
{
hour=hour+24;
}
}
min=min-1;
date();
xuanwei();
// delay(50000);
}
if(key3==0&&c==3&&key1==0)
{
if(ssec>=60)
{
ssec=ssec-60;
sec=sec+1;
if(sec>=60)
{
sec=sec-60;
}
}
ssec=ssec+1;
date();
xuanwei();
// delay(50000);
}
if(key3==0&&c==3&&key2==0)
{
if(ssec<=0)
{
ssec=ssec+60;
sec=sec-1;
if(sec<=0)
{
sec=sec+60;
}
}
ssec=ssec-1;
date();
xuanwei();
// delay(50000);
}
}
}
}
void timer0() interrupt 1
{
TH0=0Xd8;
TL0=0Xf0;
ssec++;
if(ssec>100)
{
ssec=0;
sec++;
if(sec>=60)
{
sec=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24)
{
hour=0;
}
}
}
}
}
|