#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit P0_0=P0^0;
sbit P0_1=P0^1;
sbit P0_2=P0^2;
sbit P0_3=P0^3;
sbit P0_4=P0^4;
sbit P0_5=P0^5;
sbit P0_6=P0^6;
sbit P0_7=P0^7;
sbit P3_5=P3^5;
sbit P3_6=P3^6;
sbit P3_7=P3^7;
sbit E=P2^2;
uint num=0,second,minute,hour,day,month,gewei,shiwei,m=0;
uchar code duanma[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; //定义段码,用以送P2口
uchar code pianxuan[]={0xfe,0xfd,0xfb,0xf7,0xef}; //定义片选码,用以P1片选
void delay(uint i) //延时程序
{
{for(;i>0;i--);}
}
uchar right[10][8]={{0xff,0x81,0x81,0xff,0x00,0x00,0x00,0x00},
{0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x9f,0x91,0x91,0xf1,0x00,0x00,0x00,0x00},
{0xff,0x91,0x91,0x91,0x00,0x00,0x00,0x00},
{0xff,0x08,0x08,0x0f,0x00,0x00,0x00,0x00},
{0xf1,0x91,0x91,0x9f,0x00,0x00,0x00,0x00},
{0xf1,0x91,0x91,0xff,0x00,0x00,0x00,0x00},
{0xff,0x01,0x01,0x01,0x00,0x00,0x00,0x00},
{0xff,0x91,0x91,0xff,0x00,0x00,0x00,0x00},
{0xff,0x91,0x91,0x9f,0x00,0x00,0x00,0x00}};
uchar code left[10][8]={{0x00,0x00,0x00,0x00,0xff,0x81,0x81,0xff},
{0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x9f,0x91,0x91,0xf1},
{0x00,0x00,0x00,0x00,0xff,0x91,0x91,0x91},
{0x00,0x00,0x00,0x00,0xff,0x08,0x08,0x0f},
{0x00,0x00,0x00,0x00,0xf1,0x91,0x91,0x9f},
{0x00,0x00,0x00,0x00,0xf1,0x91,0x91,0xff},
{0x00,0x00,0x00,0x00,0xff,0x01,0x01,0x01},
{0x00,0x00,0x00,0x00,0xff,0x91,0x91,0xff},
{0x00,0x00,0x00,0x00,0xff,0x91,0x91,0x9f}};
//送段码显示
void display(uint n)
{
int i;
shiwei=n/10;
gewei=n%10;
for(i=0;i<8;i++)
{
P2=duanma[i];
P0=left[shiwei][i]|right[gewei][i];
delay(100);
}
}
//用定时中T0定时一秒
void T0_int() interrupt 1 using 0
{
TH0=(65536-50000)/256; //重新装载初值
TL0=(65536-50000)%256;
num++;
if(num==20)
{
second++;
num=0;
if(second>59)
{
minute++;
second=0;
if(minute>59)
{
hour++;
minute=0;
if(hour>23)
{
day++;
hour=0;
if(day>30)
{
month++;
day=1;
if(month>12)
{
month=1;
}
}
}
}
}
}
}
//用外中断INT0进行校准
void int0() interrupt 0 using 1
{
EA=0;
if(P3_5==0) //用P3_5口记录按键次数,选择需要修改的项目
{
delay(6500);
m++;
if(m==5)
{
m=0;
}
}
if(P3_6==0) //用P3_6口使得需要修改的项目数值增加
{
delay(6500);
switch(m)
{
case 0:second++;if(second>59){second=0;}break;
case 1:minute++;if(minute>59){minute=0;}break;
case 2:hour++;if(hour>23){hour=0;}break;
case 3:day++;if(day>30){day=1;}break;
case 4:month++;if(month>12){month=1;}break;
}
}
if(P3_7==0) //用P3_7口使得需要修改的项目数值减少
{
delay(6500);
switch(m)
{
case 0:second--;if(second<0){second=59;}break;
case 1:minute--;if(minute<0){minute=59;}break;
case 2:hour--;if(hour<0){hour=23;}break;
case 3:day--;if(day<1){day=30;}break;
case 4:month--;if(month<1){month=12;}break;
}
}
EA=1;
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
EX0=1;
ET0=1;
TR0=1;
IT0=1;
P3=0xff;
second=0;
minute=0;
hour=0;
day=1;
month=1;
while(1)
{
P1=pianxuan[0];
display(second);
P1=pianxuan[1];
display(minute);
P1=pianxuan[2];
display(hour);
P1=pianxuan[3];
display(day);
P1=pianxuan[4];
display(month);
delay(90);
}
}
|