#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define delayNOP() {_nop_();_nop_();_nop_();_nop_();}
sbit DQ = P1^0;
sbit dn= P1^1;
sbit cn = P1^2;
sbit led = P3^0;
sbit f1=P3^1;
sbit f2=P3^2;
sbit z3=P3^3;
sbit z4=P3^4;
unsigned char biao[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar code df_Table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
uchar Temp_Value[]={0x00,0x00};
uchar Display_Digit[]={0,0,0,0};
uchar y=25,CurrentT = 0;
bit DS18B20_IS_OK = 1;
uchar ng=0;
//延时子函数
void DelayXus(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<200;i++);
}
}
//延时子函数
void Delay(uint x)
{
while(--x);
}
//延时子函数
void delay()
{
unsigned int i;
for(i=100;i>0;i--);
}
//-------------------------------------------------------------------------
// 显示子程序
//------------------------------------------------------------------------
void xianshi()
{
{
P2=0X01;if(ng==1)P0=biao[10];else P0=biao[11];
delay();
P2=0X02;P0=biao[Display_Digit[3]];
delay();
P2=0X04;P0=biao[Display_Digit[2]];
delay();
P2=0X08;P0=biao[Display_Digit[1]];
delay();
P2=0X10;if(ng==1)P0=biao[10];else P0=biao[11];
delay();
P2=0X20;P0=biao[y/100%10];
delay();
P2=0X40;P0=biao[y/10];
delay();
P2=0X80;P0=biao[y%10];
delay();
}
}
//------------------------------------------------------------------------
// DS18B20初始化
//------------------------------------------------------------------------
uchar Init_DS18B20()
{
uchar status;
DQ = 1;
Delay(8);
DQ = 0;
Delay(90); //时间(480~960us)
DQ = 1;
Delay(15);
status=DQ;
Delay(100);
DQ = 1;
return status;
}
//-----------------------------------------------------------------------
// DS18B20读取
//------------------------------------------------------------------------
uchar ReadOneByte()
{
uchar i,dat=0;
DQ = 1;
_nop_();
for(i=0;i<8;i++)
{
DQ = 0;
_nop_();
_nop_();
dat >>= 1;
DQ = 1;
if(DQ)
dat |= 0X80;
Delay(30);
DQ = 1;
}
return dat;
}
//---------------------------------------------------------------------
// DS18B20操作命令
//--------------------------------------------------------------------
uchar WriteOneByte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat& 0x01;
Delay(15);
DQ = 1;
dat >>= 1;
}
return dat;
}
//------------------------------------------------------------------------------
// DS18B20读取写入
//------------------------------------------------------------------------------
void Read_Temperature()
{
if(Init_DS18B20()==1)
{DS18B20_IS_OK=0;}
else
{
WriteOneByte(0xcc);
WriteOneByte(0x44);
Init_DS18B20();
WriteOneByte(0xcc);
WriteOneByte(0xbe);
Temp_Value[0] = ReadOneByte();
Temp_Value[1] = ReadOneByte();
DS18B20_IS_OK=1;
}
}
//---------------------------------------------------------------------------
// LCD温度显示
//---------------------------------------------------------------------------
void Display_Temperature()
{ uchar i;
ng = 0;
if((Temp_Value[1]&0xf8)==0xf8)
{
Temp_Value[1] = ~Temp_Value[1];
Temp_Value[0] = ~Temp_Value[0]+1;
if(Temp_Value[0]==0x00)
Temp_Value[1]++;
ng = 1;
}
Display_Digit[0] = df_Table[Temp_Value[0]&0x0f];
CurrentT = ((Temp_Value[0]&0xf0)>>4) | ((Temp_Value[1]&0x07)<<4);
Display_Digit[3] = CurrentT/100;
Display_Digit[2] = CurrentT%100/10;
Display_Digit[1] = CurrentT%10;
}
//---------------------------------------------------------------------
// ???
//----------------------------------------------------------------------
void main()
{
Read_Temperature();
while(1)
{
Read_Temperature();
if(DS18B20_IS_OK)
Display_Temperature();
xianshi();
if(dn==0)
{
delay();
if(dn==0)
{y--;
if(y<1)
{y=999;}
}
while(dn==0);
}
if(cn==0)
{
delay();
if(cn==0)
{y++;
if(y>999)
{y=1;}
}
while(cn==0);
}
if(CurrentT<y)
{led=1;
z4=1;f1=1; z3=0;f2=0;
}
else {led=0;
z4=0;f1=0;z3=1;f2=1;}
}} |