#include <REGX52.H>
#include <AT24C02.H>
#include <Delay.H>
#include <DS18B20.H>
#include <DS1302.H>
#include <AT24C02.H>
#include <LCD1602.H>
#include <Nixie.H>
#include <Key.H>
void Led_on(void);
void Flash(void);
void Read_Temp(void);
void Show_point(int x);
void Time_show(void);
void Password_change(void);
void Timer0Init(void);
int Password[3]={0},KeyNum,Real_Password[3]={0,0,0},i=0,Error_times=0,
State_on=0,mode=1,Pw_mode=0,My_Count=0,PWM,Flag_change=0;
void main(void)
{
DS1302_Init();
DS1302_SetTime();
DS18B20_ConvertT();
// AT24C02_WriteByte(0,0);
// Delay(5);
// AT24C02_WriteByte(10,0);
// Delay(5);
// AT24C02_WriteByte(20,0);
// Delay(5);
Real_Password[0]=AT24C02_ReadByte(0);
Delay(5);
Real_Password[1]=AT24C02_ReadByte(10);
Delay(5);
Real_Password[2]=AT24C02_ReadByte(20);
Delay(5);
Timer0Init();
TR0=0;
while(1)
{
KeyNum=Key();
// Nixie(1,Real_Password[0]);
// Nixie(2,Real_Password[1]);
// Nixie(3,Real_Password[2]);
Nixie(6,Password[0]);
Nixie(7,Password[1]);
Nixie(8,Password[2]);
if(KeyNum==1) Password[0]++;
if(KeyNum==2) Password[1]++;
if(KeyNum==3) Password[2]++;
if(KeyNum==4 && Password[0]==Real_Password[0] && Password[1]==Real_Password[1] &&Password[2]==Real_Password[2])
{
Flash();
State_on=1;
}
else if(KeyNum==4)
{
Error_times++;
Password[0]=0;
Password[1]=0;
Password[2]=0;
}
if(Error_times==5)
{
P2=0x00;
Delay(5000);
P2=0xff;
Error_times=0;
}
Password[0] = (Password[0]>9)? 0: Password[0];
Password[1] = (Password[1]>9)? 0: Password[1];
Password[2] = (Password[2]>9)? 0: Password[2];
if(State_on)
{
while(1)
{
KeyNum=Key();
if(KeyNum==1)mode++;
if(mode==1) {Read_Temp();P2=0xff;P2_5=0;};
if(mode==2) {Time_show();P2=0xff;P2_6=0;};
mode = (mode>3)? 1:mode;
if(mode==3) TR0=1;
else TR0=0;
if(Flag_change)
{
TR0=0;
Password_change();
Flag_change=0;
}
}
}
}
}
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count,T1Count,Flag_pwm=0;
int
TL0 = 0x18;
TH0 = 0xFC;
T0Count++;
T0Count%=10;
T1Count++;
if(T1Count>=1)
{
if(P3_0==0)
{
My_Count++;
PWM=0;
Flag_pwm=1;
}
else
{
if(Flag_pwm)
{
PWM=My_Count/500;
My_Count=0;
Flag_pwm=0;
}
}
T1Count=0;
}
if(T0Count<PWM)
{
P2=0xf0;
}
else
{
P2=0xff;
}
if(PWM>=10) {Flag_change=1;P2=0xff;PWM=0;}
}
void Flash(void)
{
int i=3;
while(i>0)
{
P2=0x00;
Delay(100);
P2=0xff;
Delay(100);
i--;
}
}
void Led_on(void)
{
P2=0x00;
}
void Read_Temp(void)
{
float T;
P3_5=1;
DS18B20_ConvertT();
T=DS18B20_ReadT();
Nixie(5,(int)T/10);
Show_point(6);
Nixie(6,(int)T%10);
Nixie(7,(int)(T*10)%10);
Nixie(8,(int)(T*100)%10);
}
void Show_point(int x)
{
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
switch(x)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;
case 2:P2_4=1;P2_3=1;P2_2=0;break;
case 3:P2_4=1;P2_3=0;P2_2=1;break;
case 4:P2_4=1;P2_3=0;P2_2=0;break;
case 5:P2_4=0;P2_3=1;P2_2=1;break;
case 6:P2_4=0;P2_3=1;P2_2=0;break;
case 7:P2_4=0;P2_3=0;P2_2=1;break;
case 8:P2_4=0;P2_3=0;P2_2=0;break;
}
P0=P0 | 0x80;
Delay(1);
P0=0x00;
}
void Time_show(void)
{
DS1302_ReadTime();
Nixie(3,DS1302_Time[3]/10);
Nixie(4,DS1302_Time[3]%10);
Nixie(5,DS1302_Time[4]/10);
Nixie(6,DS1302_Time[4]%10);
Nixie(7,DS1302_Time[5]/10);
Nixie(8,DS1302_Time[5]%10);
}
void Password_change(void)
{
int flag=1,flag_show0=1;
while(1)
{
if(flag_show0)
{
Password[0]=0;
Password[1]=0;
Password[2]=0;
flag_show0=0;
}
KeyNum=Key();
Nixie(6,Password[0]);
Nixie(7,Password[1]);
Nixie(8,Password[2]);
if(KeyNum==1) Password[0]++;
if(KeyNum==2) Password[1]++;
if(KeyNum==3) Password[2]++;
Password[0] = (Password[0]>9)? 0: Password[0];
Password[1] = (Password[1]>9)? 0: Password[1];
Password[2] = (Password[2]>9)? 0: Password[2];
if(KeyNum==4 && Password[0]==Real_Password[0] && Password[1]==Real_Password[1] &&Password[2]==Real_Password[2])
{
P2=0x00;
Delay(1000);
P2=0xff;
flag_show0=1;
while(1)
{
KeyNum=Key();
if(flag_show0)
{
Password[0]=0;
Password[1]=0;
Password[2]=0;
flag_show0=0;
}
Nixie(6,Password[0]);
Nixie(7,Password[1]);
Nixie(8,Password[2]);
if(KeyNum==1) Password[0]++;
if(KeyNum==2) Password[1]++;
if(KeyNum==3) Password[2]++;
Password[0] = (Password[0]>9)? 0: Password[0];
Password[1] = (Password[1]>9)? 0: Password[1];
Password[2] = (Password[2]>9)? 0: Password[2];
if(KeyNum==4)
{
Real_Password[0]=Password[0];
Real_Password[1]=Password[1];
Real_Password[2]=Password[2];
Flash();
AT24C02_WriteByte(0,Real_Password[0]);
Delay(5);
AT24C02_WriteByte(10,Real_Password[1]);
Delay(5);
AT24C02_WriteByte(20,Real_Password[2]);
Delay(5);
mode=1;
flag=0;
break;
}
}
}
else if(KeyNum==4)
{
Flash();
mode=1;
flag=0;
}
if(!flag) break;
}
}
void Timer0Init(void)
{
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
PT0=0;
}