/*
闹钟状态下,任意键关闭关闭提示 没实现
闹钟设置完毕 返回时钟界面 会出现提示现象
*/
#include "STC15F2K60S2.h"
#include "ds1302.h"
#include "onewire.h"
#define u8 unsigned char
sbit L1=P0^0;
u8 code T_display[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40,0x00,0x39}; //0-9 - 灭 c
u8 code T_com[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
u8 time[8],mode,mode2,shi=0x23,fen=0x59,miao=0x50,o_shi=0,o_fen=0,o_miao=0,o_alarm_flag=0;
bit s_flag,c_flag;
unsigned char temperature;
u8 Trg,Cont; //按键
void key_btn(void)
{
u8 readdata=P3^0x0f;
Trg=readdata&(Cont^readdata);
Cont=readdata;
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void display_time(void) //全亮
{
time[0]=shi/10;
time[1]=shi%10;
time[3]=fen/10;
time[4]=fen%10;
time[6]=miao/10;
time[7]=miao%10;
time[2]=time[5]=10;
}
void set_shi()
{
time[0]=11;
time[1]=11;
time[3]=fen/10;
time[4]=fen%10;
time[6]=miao/10;
time[7]=miao%10;
}
void set_fen()
{
time[0]=shi/10;
time[1]=shi%10;
time[3]=11;
time[4]=11;
time[6]=miao/10;
time[7]=miao%10;
}
void set_miao()
{
time[0]=shi/10;
time[1]=shi%10;
time[3]=fen/10;
time[4]=fen%10;
time[6]=11;
time[7]=11;
}
void read_time()
{
shi=Read_Ds1302 ( 0x85 );
shi=shi/16*10+shi%16;
fen=Read_Ds1302 ( 0x83 );
fen=fen/16*10+fen%16;
miao=Read_Ds1302 ( 0x81 );
miao=miao/16*10+miao%16;
}
void set_o_time()
{
time[0]=o_shi/10;
time[1]=o_shi%10;
time[3]=o_fen/10;
time[4]=o_fen%10;
time[6]=o_miao/10;
time[7]=o_miao%10;
}
void display()
{ temperature=(unsigned char)read_temperature();
time[2]=time[0]=time[1]=time[3]=time[4]=11;
time[5]=temperature/10%10;
time[6]=temperature%10;
time[7]=12;
}
bit key_flag,temp_flag;
void main(void)
{
P2=0xa0;P0=0x00;P2=0;
P2=0x80;P0=0xff;P2=0;
Timer0Init();
set_sfm(shi,fen,miao);
while(1)
{
if((!c_flag)&&(!temp_flag))
{
switch(mode)
{
case 0:
EA=0;
read_time();
EA=1;
display_time();break;
case 1:{ if(s_flag) display_time();
else set_shi(); break;
}
case 2:{ if(s_flag) display_time();
else set_fen();break;
}
case 3:{ if(s_flag) display_time();
else set_miao();break;
}
default :break;
}
}
if((c_flag)&&(!temp_flag))
{
switch(mode2)
{
case 0:
EA=0;
read_time();
EA=1;
display_time();break;
case 1:{ if(s_flag) set_o_time();
else set_shi(); break;
}
case 2:{ if(s_flag) set_o_time();
else set_fen();break;
}
case 3:{ if(s_flag) set_o_time();
else set_miao();break;
}
default :break;
}
shi=o_shi;
fen=o_fen;
miao=o_miao;
}
if(key_flag)
{
key_flag=0;
key_btn();
if((o_alarm_flag)&&(Trg&0x0f))
{o_alarm_flag=0;P0=0xff;P2=0x80;L1=1;P2=0;}
else{
if((Trg&0x01)&&(!mode2)) //7
{
mode++;
if(mode==4) {mode=0; EA=0;set_sfm((shi/10)*16+shi%10,(fen/10)*16+fen%10,(miao/10)*16+miao%10); EA=1;}
}
if((Trg&0x02)&&(!mode))//6
{
c_flag=1;
mode2++;
if(mode2==4) {c_flag=0;mode2=0;}
}
if((Trg&0x04)&&(mode+mode2)) //5
{
if(mode)
{switch(mode)
{
case 1:shi++;if(shi>=24) shi=0;break;
case 2:fen++;if(fen>=60) {fen=0;shi++;if(shi>=24) shi=0;} break;
case 3:miao++;if(miao>=60) {miao=0;fen++;if(fen>=60){fen=0;shi++;if(shi>=24) shi=0;}}break;
default :break;
}
}
else
{
switch(mode2)
{
case 1:o_shi++;if(o_shi>=24) o_shi=0;break;
case 2:o_fen++;if(o_fen>=60) {o_fen=0;o_shi++;if(o_shi>=24) o_shi=0;} break;
case 3:o_miao++;if(o_miao>=60) {o_miao=0;o_fen++;if(o_fen>=60) {o_fen=0;o_shi++;if(o_shi>=24) o_shi=0;}}break;
default :break;
}
}
}
temp_flag=0;
if((Cont&0x08)&&!(mode+mode2))
{ temp_flag=1;
display();
}
if(Trg&0x08) //4
{
if(mode+mode2)
{
if(mode)
{
switch(mode)
{
case 1:if(!shi)
shi=24;shi--; break;
case 2:if(!fen) {fen=60;if(!shi)shi=24;shi--;}
fen--; break;
case 3:if(!miao) {miao=60;if(!fen){fen=60;if(!shi)shi=24;shi--;} fen--;}
miao--;break;
default :break;
}
}
else
{
switch(mode2)
{
case 1:if(!o_shi)
o_shi=24;o_shi--; break;
case 2:if(!o_fen) {o_fen=60;if(!o_shi)o_shi=24;o_shi--;}
o_fen--; break;
case 3:if(!o_miao) {o_miao=60;if(!o_fen){o_fen=60;if(!o_shi)o_shi=24;o_shi--;} o_fen--;}
o_miao--;break;
default :break;
}
}
}
else
{
P2=0x80;P0=0x00;P2=0;
if(P3&0x0f==0x0f) {P2=0x80;P0=0xff;P2=0;}
}
}
}
}
if((shi==o_shi)&&(fen==o_fen)&&(miao==o_miao)&&(!(mode+mode2)))
o_alarm_flag=1;
}
}
void tm0_isr() interrupt 1
{
static unsigned int time_count,alarm_count;
static u8 i,smg_count,key_count;
smg_count++,key_count++,time_count++,alarm_count++;
if(key_count==10)
{
key_count=0;
key_flag=1;
}
if(time_count==1000)
{
time_count=0;
s_flag=~s_flag;
}
if((alarm_count>200)&&(o_alarm_flag))
{ unsigned char i;
alarm_count=0;
P0=0xff;P2=0x80;
if(i%2)L1=1;
else L1=0;
P2=0;
i++;
if(i==25 ) {i=0;o_alarm_flag=0;P2=0x80;L1=1;P2=0;}
}
if(smg_count==2)
{
smg_count=0;
P2=0xe0;P0=~T_display[time[i]];P2=0;
P2=0xc0;P0=T_com[i];P2=0;
i++;
if(i==8) i=0;
}
}