"); //-->
电子钟.rar#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit s_set=P3^5;
sbit m_set=P3^6;
sbit h_set=P3^7;
sbit SPK=P1^0;
bit flag=1;
bit flag_clock=1;
uchar clock[3]={0,0,0};
uchar second,minute,hour,t_count,key1_count,key2_count;
uchar code Tab[]=
{
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF
};
uchar code weitable[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
void delay(uint x)
{
uchar i;
while(x--)
for(i=0; i<130; i++);
}
void Alarm(uchar t)
{
uchar i,j;
for(i=0;i<200;i++)
{
SPK = ~SPK;
for(j=0;j<t;j++);
}
}
void time_set()
{
if(s_set==0)
{
while(!s_set);
second++;
if(second==60)
second=0;
}
if(m_set==0)
{
while(!m_set);
minute++;
if(minute==60)
minute=0;
}
if(h_set==0)
{
while(!h_set);
hour++;
if(hour==24)
hour=0;
}
}
void clock_set()
{
if(s_set==0)
{
while(!s_set);
clock[0]++;
if(clock[0]==60)
clock[0]=0;
}
if(m_set==0)
{
while(!m_set);
clock[1]++;
if(clock[1]==60)
clock[1]=0;
}
if(h_set==0)
{
while(!h_set);
clock[2]++;
if(clock[2]==24)
clock[2]=0;
}
}
void display(uchar dat,uchar wei)
{
uchar m,n;
m=dat/10;
n=dat%10;
P2=0x00;
P0=Tab[n];
P2=weitable[wei];
delay(1);
P2=0x00;
P0=Tab[m];
P2=weitable[wei+1];
delay(1);
}
void main()
{
uchar i;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EX0=1;
IT0=1;
EX1=1;
IT1=1;
EA=1;
hour=23;
minute=59;
second=51;
TR0=1;
while(1)
{
if((clock[0]==second)&&(clock[1]==minute)&&(clock[2]==hour))
{
for(i=0;i<3;i++)
{
Alarm(90);
Alarm(120);
}
}
if(flag&&flag_clock)
{
display(second,0);
display(minute,3);
display(hour,6);
}
else if((flag==0) && flag_clock)
{
while(flag==0)
{
time_set();
display(second,0);
display(minute,3);
display(hour,6);
}
}
else if(flag_clock==0)
{
while((flag_clock==0))
{
display(clock[0],0);
display(clock[1],3);
display(clock[2],6);
clock_set();
}
}
}
}
void settime() interrupt 0
{
key1_count++;
if(key1_count%2 == 1)
{
TR0=0;
flag=0;
}
else
{
TR0=1;
flag=1;
}
}
void timer() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t_count++;
if(t_count==20)
{
t_count=0;
second++;
}
if(second==60)
{
second=0;
minute++;
}
if(minute==60)
{
minute=0;
hour++;
}
if(hour==24)
{
hour=0;
}
}
void setclock() interrupt 2
{
key2_count++;
if(key2_count%2 == 1)
{
flag_clock=0;
}
else
flag_clock=1;
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。