#include<reg52.h>
#include<onewire.h>
#include<iic.h>
unsigned mods5=0;
unsigned mods4=0;
unsigned int dat_v;
unsigned int temp=0;
unsigned char temp_p=25;
unsigned char duanma[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned char code duanma_dot[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
sfr P4=0xc0;
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;
sbit C4 = P3^4;
sbit L1=P0^0;
sbit L2=P0^1;
sbit L3=P0^2;
sbit L4=P0^3;
void display();
void hc573(unsigned char n)
{switch(n)
{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;
case 0:P2=(P2&0x1f)|0x00;break;
}}
void wz(unsigned char pos,unsigned char value)
{ hc573(7);
P0=0xff;
hc573(6);
P0=0x01<<pos;
hc573(7);
P0=value;
}
void delay_20(unsigned int a)
{while(a--)
{display();}
}
void Read_Temp ()
{
unsigned char LSB;
unsigned char MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = (MSB << 8) | LSB;
if ((temp & 0xf800) == 0x0000)
{
temp = temp >> 4;
temp=temp*100;
temp=temp+(LSB&0x0f)*6.25;
}
}
void SetDat_DAC (unsigned int dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}void CloseALL()
{
hc573(6);
P0 = 0xff;
hc573(7);
P0 = 0xff;
}
void delay(unsigned int t)
{while(t--);}
void show1()
{wz(0,0xc6);
delay(500);
wz(1,0xff);
delay(500);
wz(2,0xff);
delay(500);
wz(3,0xff);
delay(500);
wz(4,duanma[temp/1000] );
delay(500);
wz(5,duanma_dot[(temp/100)%10] );
delay(500);
wz(6,duanma[(temp/10)%10]);
delay(500);
wz(7,duanma[temp%10]);
delay(500);CloseALL();
}
void show2()
{wz(0,0x8c);
delay(500);
wz(1,0xff);
delay(500);
wz(2,0xff);
delay(500);
wz(3,0xff);
delay(500);
wz(4,0xff );
delay(500);
wz(5,0xff );
delay(500);
wz(6,duanma[temp_p/10]);
delay(500);
wz(7,duanma[temp_p%10]);
delay(500);CloseALL();
}
void show3()
{wz(0,0x88);
delay(500);
wz(1,0xff);
delay(500);
wz(2,0xff);
delay(500);
wz(3,0xff);
delay(500);
wz(4,0xff );
delay(500);
wz(5,duanma_dot[dat_v/100]);
delay(500);
wz(6,duanma[(dat_v/10)%10]);
delay(500);
wz(7,duanma[dat_v%10]);
delay(500);CloseALL();
}
void display()
{if(mods4==0)
show1();
if(mods4==1)
show2();
if(mods4==2)
show3();
}
void key()
{R4=0;
R1=R2=R3=1;
C1=C2=C3=C4=1;
if(C1==0)
{delay(100);
if(C1==0)
{while(C1==0)
{display();}
if(mods4==0)
mods4=1;
else if(mods4==1)
mods4=2;
else if(mods4==2)
mods4=0;
}
}
if(C2==0)
{delay(100);
if(C2==0)
{while(C2==0)
{display();}
if(mods4==1)
{temp_p--;}
}
}
R3=0;
R1=R2=R4=1;
C1=C2=C3=C4=1;
if(C2==0)
{delay(100);
if(C2==0)
{while(C2==0)
{display();}
if(mods4==1)
{temp_p++;}
}
}
if(C1==0)
{delay(100);
if(C1==0)
{while(C1==0)
{display();}
if(mods5==0)
{mods5=1;
if(temp<(temp_p*100))
{ SetDat_DAC (0x00);
dat_v=0; }
else
{ SetDat_DAC (0xff);
dat_v=500;}
}
else if(mods5==1)
{mods5=0;
if(temp<2000)
{dat_v=100;
SetDat_DAC (0x51);
}
else if(2000<temp<4000)
{dat_v=(temp*0.15)-200;
SetDat_DAC (dat_v*0.15);
}
else if(temp>4000)
{dat_v=400;
SetDat_DAC (204);
}
}
}
}
}
void ledrunning()
{
if(mods4==0)
{ hc573(4);P0=0xfd;}
else if(mods4==1)
{hc573(4); P0=0xfb;}
else if(mods4==2)
{hc573(4); P0=0xf7;}
if(mods5==1)
{if(mods4==0)
{hc573(4);
P0=0xfc;
}}
if(mods5==1)
{if(mods4==1)
{hc573(4);
P0=0xfa;
}}
if(mods5==1)
{if(mods4==2)
{hc573(4);
P0=0xf6;
}}
}
void off()
{hc573(4);
P0=0xff;
hc573(5);
P0=0x00;
hc573(0);
}
void main()
{off();
if(mods5==0)
{mods5=1;if(temp<(temp_p*100))
{ SetDat_DAC (0x00);
dat_v=0; }
else
{ SetDat_DAC (0xff);
dat_v=500;}}
while(1)
{Read_Temp ();display();key();ledrunning();}
}