#ifndef _TEMP_H_
#define _TEMP_H_
#include<reg51.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
sbit DSPORT=P3^7;
void Delay1ms(uint y);
uchar DS18B20Init();
void DS18B20WriteByte(uchar dat);
uchar DS18B20ReadByte();
void changetemp();
void readtempcom();
int readtemp();
#endif
#include"temp.h"
void Delay1ms(uint y)
{
uint x;
for(;y>0;y--)
{
for(x=110;x>0;x--);
}
}
uchar DS18B20Init()
{
uchar i;
DSPORT=0;
i=70;
while(i--);
DSPORT=1;
i=0;
while(DSPORT)
{
Delay1ms(1);
i++;
if(i>5)
{
return 0;
}
}
return 1;
}
void DS18B20WriteByte(uchar dat)
{
uint i,j;
for(j=0;j<8;j++)
{
DSPORT=0;
i++;
dat=dat|0x01;
i=6;
while(i--);
DSPORT=1;
dat>>=1;
}
}
uchar DS18B20ReadByte()
{
uchar byte,bi;
uint i,j;
for(j=0;j<8;j++)
{
DSPORT=0;
i++;
DSPORT=1;
i++;
i++;
bi = DSPORT;
byte = (byte >> 1) | (bi << 7);
i = 4;
while(i--);
}
return byte;
}
void changetemp()
{
DS18B20Init();
Delay1ms(1);
DS18B20WriteByte(0xcc);
DS18B20WriteByte(0x44);
}
void readtempcom()
{
DS18B20Init();
Delay1ms(1);
DS18B20WriteByte(0xcc);
DS18B20WriteByte(0xbe);
}
int readtemp()
{
int temp=0;
uchar tmh,tml;
changetemp();
readtempcom();
tmh=DS18B20ReadByte();
tml=DS18B20ReadByte();
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
#include<reg51.h>
#include<temp.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
char num=0;
u8 Date[8];
u8 code shuzu[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(u16 i)
{
while(i--);
}
void datprocess(int temp)
{
float tp;
if(temp<0)
{
Date[0]=0x40;
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
}
else
{
Date[0]=0x00;
tp=temp;
temp=tp*0.0625*100+0.5;
}
Date[1]=shuzu[temp/10000];
Date[2]=shuzu[temp%10000/1000];
Date[3]=shuzu[temp%1000/100]|0x80;
Date[4]=shuzu[temp%100/10];
Date[5]=shuzu[temp%10];
}
void showprocess()
{
u8 i;
for(i=0;i<6;i++)
{
switch(i)
{
case 0:LSA=0;LSB=0;LSC=0;break;
case 1:LSA=1;LSB=0;LSC=0;break;
case 2:LSA=0;LSB=1;LSC=0;break;
case 3:LSA=1;LSB=1;LSC=0;break;
case 4:LSA=0;LSB=0;LSC=1;break;
case 5:LSA=1;LSB=0;LSC=1;break;
}
P0=Date[5-i];
delay(100);
P0=0x00;
}
}
void main()
{
while(1)
{
datprocess(readtemp());
showprocess();
}
}
|