#include<reg51.h>
#ifndef NULL
#define NULL ((void *) 0)
#endif
sbit speaker=P3^7;
struct display
{
char a[2];
struct display *next;
}time,fm,date,alarm,*p;
bit d=0,flag=0,nao=0,flag_2=1;
unsigned int k=0,tiao=0,pp;
unsigned char tiao_zheng=0,mode=0,
code w[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x00,
};
//闹钟用变量及数组声明
bit alarm_switch=0;
unsigned char ii=0;
unsigned char tone_H=0x00,tone_L=0x00;
void beat_125(unsigned char);
unsigned char code song[]={1,1,2,1,4,3,1,1,2,1,5,4,
1,1,8,6,4,3,9,7,7,6,4,5,4,15};
unsigned char code beat[]={4,4,8,8,8,16,4,4,8,8,8,16,
4,4,8,8,8,8,8,4,4,8,8,8,16};
unsigned int code tone[]={2699,2549,2271,2023,1910,1701,//32M晶振标准音阶
1515,1350,1275,1135,1012};
unsigned int alarm_i=0;
//闹钟用变量及数组声明
void delay(unsigned char i)
{
while(i--);
}
void main(void)
{
unsigned char i=0,temp[4];
speaker=0;
time.next=&fm,fm.next=&date,date.next=&alarm,alarm.next=&time;
time.a[1]=12,time.a[0]=0;
fm.a[1]=time.a[0],fm.a[0]=0;
date.a[1]=12,date.a[0]=31;
alarm.a[1]=12,alarm.a[0]=1;
EA=1;
EX0=1;
IT0=1;
ET0=1;
TH0=0xee;
TL0=0x00;
TR0=1;
ET1=1;
TH1=0xee;
TL1=0x00;
TR1=1;
P0=0x8f;
P2=0xff-w[15];
p=&time;
while(1)
{
if(nao==0)
{
if((d==0||mode==2)&&mode!=3)
P0=(1<<i);
else //if(d!=0||mode==3)
P0=(1<<i)+0x80;
}
else
{
if((d==0||mode==2)&&mode!=3)
P0=(1<<i)+0x20;
else //if(d!=0||mode==3)
P0=(1<<i)+0xa0;
}
P2=0xff-w[temp[i]];
delay(100);
P2=0xff;
i++;
if(i>3)
{
i=0;
if(tiao_zheng==1&&flag_2==0)
{
temp[0]=16;
temp[1]=16;
}
else
{
temp[0]=(p->a[1])/10;
temp[1]=(p->a[1])%10;
}
if(tiao_zheng==2&&flag_2==0)
{
temp[2]=16;
temp[3]=16;
}
else
{
temp[2]=(p->a[0])/10;
temp[3]=(p->a[0])%10;
}
}
}
}
void i0(void) interrupt 0//秒中断
{
d=1;
fm.a[0]++;
if(fm.a[0]>59)
{
fm.a[0]=0;
fm.a[1]++;
time.a[0]++;
if(fm.a[1]>59)
{
fm.a[1]=0;
time.a[0]=0;
time.a[1]++;
if(time.a[1]>23)
{
time.a[1]=0;
date.a[0]++;
if(date.a[0]>31)
{
date.a[0]=1;
date.a[1]++;
if(date.a[1]>12)
date.a[1]=1;
}
}
}
}
if(time.a[0]==alarm.a[0]&&time.a[1]==alarm.a[1])
alarm_switch=1;
if(nao==0||(time.a[0]-1==alarm.a[0]&&time.a[1]==alarm.a[1]))
alarm_switch=0;
}
void timer1(void) interrupt 1 //键盘扫描
{
TH0=0xbf;
TL0=0x00;
if(alarm_switch==1)
{
alarm_i++;
if(alarm_i==550)
{
alarm_i=0;
tone_H=(65536-tone[song[ii]])/256;
tone_L=(65536-tone[song[ii]])%256;
TH0=tone_H;
TL0=tone_L;
TR0=1;
ii++;
if(song[ii]==15)
ii=0;
}
}
if(tiao_zheng!=0)
{
tiao++;
if(tiao>300)
{
tiao=0;
if(flag_2==0)
flag_2=1;
else
flag_2=0;
}
}
if(d==1)
{
pp++;
if(pp>400)
{
d=0;
pp=0;
}
}
if(flag==0&&P1!=0xff)
{
flag=1;
if(P1==0xfe) //显示内容转换
{
p=p->next;
mode++;
if(mode>3)
mode=0;
tiao_zheng=0;
}
if(P1==0xfd) //调整
{
tiao_zheng++;
if(tiao_zheng==3)
tiao_zheng=0;
}
if(P1==0xfb&&tiao_zheng!=0) //+
{
p->a[2-tiao_zheng]++;
if(mode==0&&tiao_zheng==2)//time分位调整,同时改变fm分位
fm.a[1]=time.a[0];
if(mode==1&&tiao_zheng==1)//fm分位调整,同时改变time分位
time.a[0]=fm.a[1];
if(mode==0)
{
if(time.a[1]>23)
time.a[1]=0;
if(time.a[0]>59)
time.a[0]=0;
}
else if(mode==1)
{
if(fm.a[1]>59)
fm.a[1]=0;
if(fm.a[0]>59)
fm.a[0]=0;
}
else if(mode==2)
{
if(date.a[1]>12)
date.a[1]=1;
if(date.a[0]>31)
date.a[0]=1;
}
else if(mode==3)
{
if(alarm.a[1]>23)
alarm.a[1]=0;
if(alarm.a[0]>59)
alarm.a[0]=0;
}
}
if(P1==0xf7&&tiao_zheng!=0) //-
{
p->a[2-tiao_zheng]--;
if(mode==0&&tiao_zheng==2)//time分位调整,同时改变fm分位
fm.a[1]=time.a[0];
if(mode==1&&tiao_zheng==1)//fm分位调整,同时改变time分位
time.a[0]=fm.a[1];
if(mode==0)
{
if(time.a[1]<0)
time.a[1]=23;
if(time.a[0]<0)
time.a[0]=59;
}
else if(mode==1)
{
if(fm.a[1]<0)
fm.a[1]=59;
if(fm.a[0]<0)
fm.a[0]=59;
}
else if(mode==2)
{
if(date.a[1]<1)
date.a[1]=12;
if(date.a[0]<1)
date.a[0]=31;
}
else if(mode==3)
{
if(alarm.a[1]<0)
alarm.a[1]=23;
if(alarm.a[0]<0)
alarm.a[0]=59;
}
}
if(P1==0xef)//闹钟开关
{
if(nao==1)
nao=0;
else
nao=1;
}
}
else if(flag==1)
{
k++;
if(k>300)
{
k=0;
flag=0;
}
}
}
void timer2(void) interrupt 3
{
TH1=tone_H;
TL1=tone_L;
if(alarm_switch==1)
speaker=~speaker;
else
speaker=0;
}