小细节总结: 改变ds1302的里面的数值时,要重新调要xie函数,因为有写保护
界面的切换都要用到标志位:例如在时钟显示界面按下S4显示温度,那在其他界面就不显示温度,就要一个标志位,温度的显示和温度的不显示也要一个标志位
#include <STC15F2K60S2.H>
#include "intrins.h"
#include "ds1302.h"
#include "onewire.h"
#define uchar unsigned char
#define uint unsigned int
extern uchar time[];
uchar naozhong[]={0,0,0};
uchar stat=0;
uint wendu;
uchar count=0;
uint miao=0;
uchar flag=0;
unsigned char code smgduan[] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0xbf,0xff,0xc6};
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
sbit led=P0^0;
uchar shezhi=0;
uchar led_kai=0;
uchar T=0;
uchar yi,er,san,si,wu,liu,qi,ba;
void Select(unsigned char channel)
{
switch(channel)
{
case 4:P2=(P2&0x1f)|0x80;
break;
case 5:P2=(P2&0x1f)|0xa0;
break;
case 6:P2=(P2&0x1f)|0xc0;
break;
case 7:P2=(P2&0x1f)|0xe0;
break;
}
}
void Delay1ms() //@11.0592MHz
{
unsigned char i,j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Display1(unsigned char yi,unsigned char er)
{
Select(6);
P0=0x01;
Select(7);
P0=smgduan[yi];
Delay1ms();
Select(6);
P0=0x02;
Select(7);
P0=smgduan[er];
Delay1ms();
}
void Display2(unsigned char san,unsigned char si)
{
Select(6);
P0=0x04;
Select(7);
P0=smgduan[san];
Delay1ms();
Select(6);
P0=0x08;
Select(7);
P0=smgduan[si];
Delay1ms();
}
void Display3(unsigned char wu,unsigned char liu)
{
Select(6);
P0=0x10;
Select(7);
P0=smgduan[wu];
Delay1ms();
Select(6);
P0=0x20;
Select(7);
P0=smgduan[liu];
Delay1ms();
}
void Display4(unsigned char qi,unsigned char ba)
{
Select(6);
P0=0x40;
Select(7);
P0=smgduan[qi];
Delay1ms();
Select(6);
P0=0x80;
Select(7);
P0=smgduan[ba];
Delay1ms();
Select(6);
P0=0xff;
Select(7);
P0=0xff;
}
void InitSystem()
{
Select(5);
P0=0x00;
Select(4);
P0=0xff;
Select(6);
P0=0xff;
Select(7);
P0=0xff;
}
void inint()
{
TMOD&=0xf1;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
ET0=1;
TR0=1;
EA=1;
}
void sercive () interrupt 1
{
count++;miao++;
if((count==20)&&(led_kai==1))
{
count=0;
Select(4);
if(flag==0){flag=1;P0=0xfe;Select(4);P2=0;}
else if(flag==1){flag=0;P0=0xff;Select(4);P2=0;}
}
if(miao==500)
{
miao=0;
led_kai=0;
Select(4);
P0=0xff;
}
}
void keyScan()
{
if(S7==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
if(S7==0)
{ if(led_kai==1)led_kai=0;
else{
if(shezhi==0)shezhi=1;
else if(shezhi==1)shezhi=2;
else if(shezhi==2)shezhi=3;
else if(shezhi==3)shezhi=0;
}}while(S7==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
}
}
if(S6==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
if(S6==0)
{ if(led_kai==1)led_kai=0;
else{
if(shezhi==0)shezhi=4;
else if(shezhi==4)shezhi=5;
else if(shezhi==5)shezhi=6;
else if(shezhi==6)shezhi=0;
}}while(S6==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
}
}
if(S5==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
if(S5==0)
{ if(led_kai==1)led_kai=0;
else{
if(shezhi==1)
{
if(time[0]==59)time[0]=0;
else time[0]++;
write_ds1302();
}
else if(shezhi==2)
{
if(time[1]==59)time[1]=0;
else time[1]++;
write_ds1302();
}
else if(shezhi==3)
{
if(time[2]==23)time[2]=0;
else time[2]++;
write_ds1302();
}
else if(shezhi==4)
{
if(naozhong[0]==59)naozhong[0]=0;
else naozhong[0]++;
}
else if(shezhi==5)
{
if(naozhong[1]==59)naozhong[1]=0;
else naozhong[1]++;
}
else if(shezhi==6)
{
if(naozhong[2]==23)naozhong[2]=0;
else naozhong[2]++;
}}
}while(S5==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
}
}
if(S4==0)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
if(S4==0)
{
stat=1;
if(shezhi==0)T=1;
}
if((stat==1)&&(S4==1))
{
stat=0;T=0;
if(led_kai==1)led_kai=0;
else{
if(shezhi==1)
{
if(time[0]==0)time[0]=0;
else time[0]--;
write_ds1302();
}
else if(shezhi==2)
{
if(time[1]==0)time[1]=0;
else time[1]--;
write_ds1302();
}
else if(shezhi==3)
{
if(time[2]==00)time[2]=0;
else time[2]--;
write_ds1302();
}
else if(shezhi==4)
{
if(naozhong[0]==59)naozhong[0]=0;
else naozhong[0]--;
}
else if(shezhi==5)
{
if(naozhong[1]==59)naozhong[1]=0;
else naozhong[1]--;
}
else if(shezhi==6)
{
if(naozhong[2]==23)naozhong[2]=0;
else naozhong[2]--;
}}
}
}
}
void main()
{ write_ds1302();
InitSystem();
inint();
while(1)
{
wendu=temputer();
get_ds1302();
if(T==0)
{
if(shezhi==0)
{
yi=time[2]/16;er=time[2]%16;san=10;si=time[1]/16;wu=time[1]%16;liu=10;qi=time[0]/16;ba=time[0]%16;
}
else if(shezhi==1)
{
if(time[0]%2==0)
{
yi=time[2]/16;er=time[2]%16;san=10;si=time[1]/16;wu=time[1]%16;liu=10;qi=time[0]/16;ba=time[0]%16;
}else{qi=11;ba=11;}
}
else if(shezhi==2)
{
if(time[0]%2==0)
{
yi=time[2]/16;er=time[2]%16;san=10;si=time[1]/16;wu=time[1]%16;liu=10;qi=time[0]/16;ba=time[0]%16;
}else{si=11;wu=11;}
}
else if(shezhi==3)
{
if(time[0]%2==0)
{
yi=time[2]/16;er=time[2]%16;san=10;si=time[1]/16;wu=time[1]%16;liu=10;qi=time[0]/16;ba=time[0]%16;
}else{yi=11;er=11;}
}
else if(shezhi==4)
{
if(time[0]%2==0)
{
yi=naozhong[2]/10;er=naozhong[2]%10;san=10;si=naozhong[1]/10;wu=naozhong[1]%10;liu=10;qi=naozhong[0]/10;ba=naozhong[0]%10;
}else{qi=11;ba=11;}
}
else if(shezhi==5)
{
if(time[0]%2==0)
{
yi=naozhong[2]/10;er=naozhong[2]%10;san=10;si=naozhong[1]/10;wu=naozhong[1]%10;liu=10;qi=naozhong[0]/10;ba=naozhong[0]%10;
}else{si=11;wu=11;}
}
else if(shezhi==6)
{
if(time[0]%2==0)
{
yi=naozhong[2]/10;er=naozhong[2]%10;san=10;si=naozhong[1]/10;wu=naozhong[1]%10;liu=10;qi=naozhong[0]/10;ba=naozhong[0]%10;
}else{yi=11;er=11;}
}
}
else if(T==1)
{
yi=11;er=11;san=11;si=11;wu=11;liu=wendu/10;qi=wendu%10;ba=12;
}
if((naozhong[0]==time[0])&&(naozhong[1]==time[1])&&(naozhong[2]==time[2]))
{
led_kai=1;
count=0;miao=0;
}
if(led_kai==0){P0=0xff;Select(4);}
keyScan();
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
}
}