#i nclude <REG51.H>
#i nclude <ABSACC.H>
#define uchar unsigned char
#define uint unsigned int
//============================
#define C8155 XBYTE[0x7000]
#define PA XBYTE[0x7001]
#define PB XBYTE[0x7002]
#define PC XBYTE[0x7003]
//============================
#define sec XBYTE[0x8000]
#define min XBYTE[0x8002]
#define hou XBYTE[0x8004]
#define week XBYTE[0x8006]
#define day XBYTE[0x8007]
#define mon XBYTE[0x8008]
#define year XBYTE[0x8009]
#define cent XBYTE[0x8032]
#define DS12887A XBYTE[0x800a]
#define DS12887B XBYTE[0x800b]
#define DS12887C XBYTE[0x800c]
//===============================
sbit set_inc=P1^0;
sbit set_dec=P1^1;
sbit inc=P1^2;
sbit dec=P1^3;
uchar disbit;
bit flag;
uchar SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
uchar code act_PB[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code act_PC[5]={0xfe,0xfd,0xfb,0xf7,0xef};
char ms=0;
char data ram_year,ram_mon,ram_day,ram_week,ram_hou,ram_min,ram_sec;
void scan_key(void);
void delay(uint k);
void wr_time(void);
void rd_time(void);
/******************************/
void init_timer0(void)
{
TH0=-(2000/256);
TL0=-(2000%256);
TR0=1;
}
//***********************
void init(void)
{ init_timer0();
EA=1;ET0=1;
C8155=0x0f;
DS12887A=0x20;
}
/**********************************/
void time_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10];}break;
case 3:{PA=SEG7[ram_min/10];}break;
case 4:{PA=SEG7[ram_hou%10];}break;
case 5:{PA=SEG7[ram_hou/10];}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1];}break;
case 7:{PA=SEG7[ram_day%10];}break;
case 8:{PA=SEG7[ram_day/10];}break;
case 9:{PA=SEG7[ram_mon%10];}break;
case 10:{PA=SEG7[ram_mon/10];}break;
case 11:{PA=SEG7[ram_year%10];}break;
case 12:{PA=SEG7[ram_year/10];}break;
default:break;
}
}
/****************************************/
void min_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10]&0x7f;}break;
case 3:{PA=SEG7[ram_min/10]&0x7f;}break;
case 4:{PA=SEG7[ram_hou%10];}break;
case 5:{PA=SEG7[ram_hou/10];}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1];}break;
case 7:{PA=SEG7[ram_day%10];}break;
case 8:{PA=SEG7[ram_day/10];}break;
case 9:{PA=SEG7[ram_mon%10];}break;
case 10:{PA=SEG7[ram_mon/10];}break;
case 11:{PA=SEG7[ram_year%10];}break;
case 12:{PA=SEG7[ram_year/10];}break;
default:break;
}
}
/***************************************/
void hou_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10];}break;
case 3:{PA=SEG7[ram_min/10];}break;
case 4:{PA=SEG7[ram_hou%10]&0x7f;}break;
case 5:{PA=SEG7[ram_hou/10]&0x7f;}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1];}break;
case 7:{PA=SEG7[ram_day%10];}break;
case 8:{PA=SEG7[ram_day/10];}break;
case 9:{PA=SEG7[ram_mon%10];}break;
case 10:{PA=SEG7[ram_mon/10];}break;
case 11:{PA=SEG7[ram_year%10];}break;
case 12:{PA=SEG7[ram_year/10];}break;
default:break;
}
}
/***************************************/
void week_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10];}break;
case 3:{PA=SEG7[ram_min/10];}break;
case 4:{PA=SEG7[ram_hou%10];}break;
case 5:{PA=SEG7[ram_hou/10];}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1]&0x7f;}break;
case 7:{PA=SEG7[ram_day%10];}break;
case 8:{PA=SEG7[ram_day/10];}break;
case 9:{PA=SEG7[ram_mon%10];}break;
case 10:{PA=SEG7[ram_mon/10];}break;
case 11:{PA=SEG7[ram_year%10];}break;
case 12:{PA=SEG7[ram_year/10];}break;
default:break;
}
}
/**************************************/
void day_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10];}break;
case 3:{PA=SEG7[ram_min/10];}break;
case 4:{PA=SEG7[ram_hou%10];}break;
case 5:{PA=SEG7[ram_hou/10];}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1];}break;
case 7:{PA=SEG7[ram_day%10]&0x7f;}break;
case 8:{PA=SEG7[ram_day/10]&0x7f;}break;
case 9:{PA=SEG7[ram_mon%10];}break;
case 10:{PA=SEG7[ram_mon/10];}break;
case 11:{PA=SEG7[ram_year%10];}break;
case 12:{PA=SEG7[ram_year/10];}break;
default:break;
}
}
/********************************************/
void mon_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10];}break;
case 3:{PA=SEG7[ram_min/10];}break;
case 4:{PA=SEG7[ram_hou%10];}break;
case 5:{PA=SEG7[ram_hou/10];}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1];}break;
case 7:{PA=SEG7[ram_day%10];}break;
case 8:{PA=SEG7[ram_day/10];}break;
case 9:{PA=SEG7[ram_mon%10]&0x7f;}break;
case 10:{PA=SEG7[ram_mon/10]&0x7f;}break;
case 11:{PA=SEG7[ram_year%10];}break;
case 12:{PA=SEG7[ram_year/10];}break;
default:break;
}
}
/****************************************/
void year_dis(uchar disbit)
{uchar y_week;
switch(disbit)
{
case 0:{PA=SEG7[ram_sec%10];}break;
case 1:{PA=SEG7[ram_sec/10];}break;
case 2:{PA=SEG7[ram_min%10];}break;
case 3:{PA=SEG7[ram_min/10];}break;
case 4:{PA=SEG7[ram_hou%10];}break;
case 5:{PA=SEG7[ram_hou/10];}break;
case 6:{if(ram_week==1)y_week=9;
else y_week=ram_week;PA=SEG7[y_week-1];}break;
case 7:{PA=SEG7[ram_day%10];}break;
case 8:{PA=SEG7[ram_day/10];}break;
case 9:{PA=SEG7[ram_mon%10];}break;
case 10:{PA=SEG7[ram_mon/10];}break;
case 11:{PA=SEG7[ram_year%10]&0x7f;}break;
case 12:{PA=SEG7[ram_year/10]&0x7f;}break;
default:break;
}
}
/****************************************/
void time0(void) interrupt 1 using 1
{static uchar disbit;
TH0=-(2000/256);
TL0=-(2000%256);
PB=0xff;PC=0xff;
switch(ms)
{
case 0:{time_dis(disbit);}break;
case 1:{min_dis(disbit);}break;
case 2:{hou_dis(disbit);}break;
case 3:{week_dis(disbit);}break;
case 4:{day_dis(disbit);}break;
case 5:{mon_dis(disbit);}break;
case 6:{year_dis(disbit);}break;
default:break;
}
disbit++;
if(disbit>12)disbit=0;
if(disbit<8){PB=act_PB[disbit];PC=0xff;}
else {PB=0xff;PC=act_PC[disbit-8];}
}
/***********************************/
void main(void)
{
init();
rd_time();
while(1)
{
scan_key();
if(ms==0){
if(flag){wr_time();flag=0;}
else rd_time();
}
}
}
/*****************************************/
void wr_time(void)
{
DS12887B=0x86;
year=ram_year;mon=ram_mon;day=ram_day;
week=ram_week;hou=ram_hou;min=ram_min;
DS12887B=0x06;
}
/*******************************************/
void rd_time(void)
{
uchar data temp,x;
do{temp=DS12887A;x=temp|0x7f;}while(x==0xff);
ram_year=year;ram_mon=mon;ram_day=day;
ram_week=week;ram_hou=hou;ram_min=min;ram_sec=sec;
}
/*****************************************/
void scan_key(void)
{
if(!set_inc){ms++;delay(200);if(ms>6)ms=0;}
if(!set_dec){ms--;delay(200);if(ms<0)ms=6;}
if(ms==1){flag=1;if(!inc){ram_min++;if(ram_min>59)ram_min=59;delay(200);}
if(!dec){ram_min--;if(ram_min<1)ram_min=1;delay(200);} }
if(ms==2){ if(!inc){ram_hou++;if(ram_hou>23)ram_hou=23;delay(200);}
if(!dec){ram_hou--;if(ram_hou<0)ram_hou=0;delay(200);} }
if(ms==3){ if(!inc){ram_week++;if(ram_week>7)ram_week=7;delay(200);}
if(!dec){ram_week--;if(ram_week<1)ram_week=1;delay(200);} }
if(ms==4){ if(!inc){ram_day++;if(ram_day>31)ram_day=31;delay(200);}
if(!dec){ram_day--;if(ram_day<1)ram_day=1;delay(200);} }
if(ms==5){ if(!inc){ram_mon++;if(ram_mon>12)ram_mon=12;delay(200);}
if(!dec){ram_mon--;if(ram_mon<1)ram_mon=1;delay(200);} }
if(ms==6){flag=1;if(!inc){ram_year++;if(ram_year>99)ram_year=99;delay(200);}
if(!dec){ram_year--;if(ram_year<0)ram_day=0;delay(200);} }
}
/********************************************************/
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++){
for(j=0;j<60;j++)
{;}}
}
/*****************end********************/
|