sbit function = P1^0;
sbit hour_add = P1^1;
sbit hour_sub = P1^2;
sbit minute_add = P1^3;
sbit minute_sub = P1^4;
sbit lamp1 = P3^0;
sbit lamp2 = P3^1;
sbit lamp3 = P3^2;
sbit lamp4 = P3^3;
bit EN_COCLK = 1;
bit BEGIN = 0;
unsigned char cnt;
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char half_second;
unsigned int ms50;
struct time{
unsigned char hour;
unsigned char minute;
unsigned char second;
}ds[2];
unsigned char code tab[] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//CA
//unsigned char code choose_bit[] =
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char code choose_bit[] =
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char idata disdata[7];
unsigned char idata dissetup[7];
unsigned char turnon[4] = {0x01,0x02,0x04,0x08};
unsigned char key_min_hour = 0;
unsigned char disp_type = 0;
unsigned char rn;
/****************************************
*****************************************/
void delay1ms(unsigned int i)
{
unsigned int a,b;
for (a=0;a<i;a++)
for (b=0; b< 120 ;b++);
}
/****************************************
*****************************************/
void disp_clock(void)
{
unsigned char display;
unsigned char i;
disdata[0] = hour/10;
disdata[1] = hour%10;
disdata[2] = minute/10;
disdata[3] = minute%10;
disdata[4] = second/10;
disdata[5] = second%10;
for(i=0; i<6; i++)
{
P0 = 0xff;
display = tab[disdata[i]];
if (half_second&&(i==3))
{
display &= 0x7f;
}
P2 = choose_bit[i];
P0 = display;
delay1ms(1);
}
P0 = 0xff;//¹ØµÆ
/****************************************
*****************************************/
void disp_init(void)
{
unsigned char display;
unsigned char i;
dissetup[0] = ds[disp_type-1].hour/10;
dissetup[1] = ds[disp_type-1].hour%10;
dissetup[2] = ds[disp_type-1].minute/10;
dissetup[3] = ds[disp_type-1].minute%10;
dissetup[4] = ds[disp.type-1].second/10;
dissetup[5] = ds[disp_type-1].second%10;
for(i=0;i<6;i++)
{
P0 = 0xff;
display = tab[dissetup[i]];
if(i==3||i==1)
{
display &= 0x7f;
}
P2 = choose_bit[i];
P0 = display;
delay1ms(1);
}
P0 =0xff;
}
/****************************************
*****************************************/
void keyscan(void)
{
unsigned char houradd =1;
unsigned char minadd =1 ;
if (!function)
{
delay1ms(20);
if(function==0){
while(function==0);
EN_COCLK = 0;
if (++disp_type==3)
{
disp_type = 0;
EN_COCLK = 1;
}
}
}
if (!hour_add)
{
delay1ms(20);
if(hour_add==0){
while(hour_add==0);
switch(disp_type)
{
case 0:
if (++hour>=24)
hour = 0;
break;
case 1:
if (++ds[0].hour>=24)
ds[0].hour = 0;
break;
case 2:
if (++ds[1].hour>=24)
ds[1].hour= 0;
break;
default:
break;
}
}
}
if(!hour_sub)
{
delay1ms(20);
if(hour_sub==0)
{
while(hour_sub==0);
switch(disp_type){
case 0:
if(--hour==0xff)
hour = 23;
break;
case 1:
if(--ds[0].hour==0xff)
ds[0].hour = 23;
break;
case 2:
if(--ds[1].hour==0xff)
ds[1].hour = 23;
break;
default:
break;
}
}
}
if(!minute_add)
{
delay1ms(20);
if(minute_add==0)
{
while(minute_add==0);
switch(disp_type)
{
case 0:
if(++minute>=60)
minute = 0;
break;
case 1:
if(++ds[0].minute>=60)
ds[0].minute = 0;
break;
case 2:
if(++ds[1].minute>=60)
ds[1].minute = 0;
break;
default:
break;
}
}
}
if(!minute_sub)
{
delay1ms(20);
if(minute_sub==0)
{
while(minute_sub==0);
switch(disp_type)
{
case 0:
if(--minute==0xff)
minute = 59;
break;
case 1£»
if(--ds[0].minute==0xff)
ds[0].minute = 59;
break;
case 2:
if(--ds[1].minute==0xff)
ds[1].minute = 59;
break;
default:
break;
}
}
}
}
/****************************************
*****************************************/
void main(void)
{
unsigned char i;
hour = 17;
minute = 59;
second = 50;
cnt = 20;
P3 = 0x0 ;
for(i=0; i<2; i++){
ds[i].hour = 0;
ds[i].minute = 0;
ds[i].second = 0;
TMOD = 0x10;
TL1 = 0xb0;
TH1 = 0x3c;
IE = 0x88;
TR1 = 1;
while(1)
{
keyscan()
if(EN_COCLK)
{
disp_clock();
}
else
{
disp_init();
}
conctrl_lamp();
}
}
void Timer1() interrupt 3 using 3
{
ms50++;
if(cnt--==10)
half_second = 1;
if(!cnt){
half_second = 0;
cnt = 20;
second++;
if(second==60){
second = 0
minute++;
if(minute==60){
minute = 0;
hour++;
if(hour==24)
hour = 0;}
}
}
TL1 = 0xb0;
TH1 = 0x3c;
}
/****************************************
END
*****************************************/
sbit hour_add = P1^1;
sbit hour_sub = P1^2;
sbit minute_add = P1^3;
sbit minute_sub = P1^4;
sbit lamp1 = P3^0;
sbit lamp2 = P3^1;
sbit lamp3 = P3^2;
sbit lamp4 = P3^3;
bit EN_COCLK = 1;
bit BEGIN = 0;
unsigned char cnt;
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char half_second;
unsigned int ms50;
struct time{
unsigned char hour;
unsigned char minute;
unsigned char second;
}ds[2];
unsigned char code tab[] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//CA
//unsigned char code choose_bit[] =
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char code choose_bit[] =
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char idata disdata[7];
unsigned char idata dissetup[7];
unsigned char turnon[4] = {0x01,0x02,0x04,0x08};
unsigned char key_min_hour = 0;
unsigned char disp_type = 0;
unsigned char rn;
/****************************************
*****************************************/
void delay1ms(unsigned int i)
{
unsigned int a,b;
for (a=0;a<i;a++)
for (b=0; b< 120 ;b++);
}
/****************************************
*****************************************/
void disp_clock(void)
{
unsigned char display;
unsigned char i;
disdata[0] = hour/10;
disdata[1] = hour%10;
disdata[2] = minute/10;
disdata[3] = minute%10;
disdata[4] = second/10;
disdata[5] = second%10;
for(i=0; i<6; i++)
{
P0 = 0xff;
display = tab[disdata[i]];
if (half_second&&(i==3))
{
display &= 0x7f;
}
P2 = choose_bit[i];
P0 = display;
delay1ms(1);
}
P0 = 0xff;//¹ØµÆ
/****************************************
*****************************************/
void disp_init(void)
{
unsigned char display;
unsigned char i;
dissetup[0] = ds[disp_type-1].hour/10;
dissetup[1] = ds[disp_type-1].hour%10;
dissetup[2] = ds[disp_type-1].minute/10;
dissetup[3] = ds[disp_type-1].minute%10;
dissetup[4] = ds[disp.type-1].second/10;
dissetup[5] = ds[disp_type-1].second%10;
for(i=0;i<6;i++)
{
P0 = 0xff;
display = tab[dissetup[i]];
if(i==3||i==1)
{
display &= 0x7f;
}
P2 = choose_bit[i];
P0 = display;
delay1ms(1);
}
P0 =0xff;
}
/****************************************
*****************************************/
void keyscan(void)
{
unsigned char houradd =1;
unsigned char minadd =1 ;
if (!function)
{
delay1ms(20);
if(function==0){
while(function==0);
EN_COCLK = 0;
if (++disp_type==3)
{
disp_type = 0;
EN_COCLK = 1;
}
}
}
if (!hour_add)
{
delay1ms(20);
if(hour_add==0){
while(hour_add==0);
switch(disp_type)
{
case 0:
if (++hour>=24)
hour = 0;
break;
case 1:
if (++ds[0].hour>=24)
ds[0].hour = 0;
break;
case 2:
if (++ds[1].hour>=24)
ds[1].hour= 0;
break;
default:
break;
}
}
}
if(!hour_sub)
{
delay1ms(20);
if(hour_sub==0)
{
while(hour_sub==0);
switch(disp_type){
case 0:
if(--hour==0xff)
hour = 23;
break;
case 1:
if(--ds[0].hour==0xff)
ds[0].hour = 23;
break;
case 2:
if(--ds[1].hour==0xff)
ds[1].hour = 23;
break;
default:
break;
}
}
}
if(!minute_add)
{
delay1ms(20);
if(minute_add==0)
{
while(minute_add==0);
switch(disp_type)
{
case 0:
if(++minute>=60)
minute = 0;
break;
case 1:
if(++ds[0].minute>=60)
ds[0].minute = 0;
break;
case 2:
if(++ds[1].minute>=60)
ds[1].minute = 0;
break;
default:
break;
}
}
}
if(!minute_sub)
{
delay1ms(20);
if(minute_sub==0)
{
while(minute_sub==0);
switch(disp_type)
{
case 0:
if(--minute==0xff)
minute = 59;
break;
case 1£»
if(--ds[0].minute==0xff)
ds[0].minute = 59;
break;
case 2:
if(--ds[1].minute==0xff)
ds[1].minute = 59;
break;
default:
break;
}
}
}
}
/****************************************
*****************************************/
void main(void)
{
unsigned char i;
hour = 17;
minute = 59;
second = 50;
cnt = 20;
P3 = 0x0 ;
for(i=0; i<2; i++){
ds[i].hour = 0;
ds[i].minute = 0;
ds[i].second = 0;
TMOD = 0x10;
TL1 = 0xb0;
TH1 = 0x3c;
IE = 0x88;
TR1 = 1;
while(1)
{
keyscan()
if(EN_COCLK)
{
disp_clock();
}
else
{
disp_init();
}
conctrl_lamp();
}
}
void Timer1() interrupt 3 using 3
{
ms50++;
if(cnt--==10)
half_second = 1;
if(!cnt){
half_second = 0;
cnt = 20;
second++;
if(second==60){
second = 0
minute++;
if(minute==60){
minute = 0;
hour++;
if(hour==24)
hour = 0;}
}
}
TL1 = 0xb0;
TH1 = 0x3c;
}
/****************************************
END
*****************************************/