#include<reg52.h>
#include<math.h>
#include<INTRINS.H>
#define uchar unsigned char
#define uint unsigned int;
/******************************************************************/
/* 定义端口 */
/******************************************************************/
sbit LED1 = P2^7;
sbit LED2 = P2^6;
sbit LED3 = P2^5;
sbit LED4 = P2^4;
sbit DQ=P1^3;
/******************************************************************/
/* 全局变量 */
/******************************************************************/
uint temp;
uchar flag_get,count,num,minute,second;
uchar code tab[]={0x28,0xEB,0x32,0xA2,0xE1,0xA4,0x24,0xEA,0x20,0xA0,0x60,0x25,0x3C,0x23,0x34,0x74,0xF7,0xFF};
uchar str[6];
/******************************************************************/
/* 函数声明 */
/******************************************************************/
void delay1(uchar MS);
unsigned int ReadTemperature(void);
void Init_DS18B20(void);
unsigned char ReadOneChar(void);
void WriteOneChar(unsigned char dat);
void delay(unsigned int i);
/******************************************************************/
/* 主函数 */
/******************************************************************/
main()
{
unsigned char TempH,TempL;
TMOD|=0x01;
TH0=0xef;
TL0=0xf0;
IE=0x82;
TR0=1;
count=0;
while(1)
{
str[5]=0x3C;
str[1]=tab[TempH/100];
str[2]=tab[(TempH%100)/10];
str[3]=tab[(TempH%100)%10] & 0xDF;
str[4]=tab[TempL];
if(flag_get==1)
{
temp=ReadTemperature();
if(temp&0x8000)
{
str[0]=0x40;
temp=~temp;
temp +=1;
}
else
str[0]=0;
TempH=temp>>4;
TempL=temp&0x0F;
TempL=TempL*6/10;
flag_get=0;
}
}
}
/******************************************************************/
/* 定时器中断 */
/******************************************************************/
void tim(void) interrupt 1 using 1
{
TH0=0xef;
TL0=0xf0;
num++;
if (num==50)
{
num=0;
flag_get=1;
second++;
if(second>=60)
{
second=0;
minute++;
}
}
count++;
if(count==1)
{
LED4 = 1;
P0 =str[2];
LED1 = 0;
}
if(count==2)
{
LED1 = 1;
P0 = str[3];
LED2 = 0;}
if(count==3)
{
LED2 = 1;
P0 = str[4];
LED3 = 0;
}
if(count==4)
{
LED3 = 1;
P0=str[5];
LED4 = 0;
count = 0;
}
}
/******************************************************************/
/* 延时函数 */
/******************************************************************/
void delay(unsigned int i)
{
while(i--);
}
/******************************************************************/
/* 初始化 */
/******************************************************************/
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1;
delay(8);
DQ = 0;
delay(80);
DQ = 1;
delay(10);
x=DQ;
delay(5);
}
/******************************************************************/
/* 读一个字节 */
/******************************************************************/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay(5);
}
return(dat);
}
/******************************************************************/
/* 写一个字节 */
/******************************************************************/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(5);
}
/******************************************************************/
/* 读取温度 */
/******************************************************************/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
delay(200);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
b<<=8;
t=a+b;
return(t);
}