#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
char code xuanwei[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit beep=P2^5;
sbit DSPORT=P3^7;
sbit moto=P1^0;
int q,w;
float Y,M,Z;
bit fg=1; //溫度正負標志
unsigned int sdata;//測量到的溫度的整數(shù)部分
unsigned char xiaoshu1;//小數(shù)第一位
uchar charac[4];
void delay(uint xms)
{
xms=xms-M;
while(xms--);
for(w=0;w<M;w++)
moto=1;moto=0;
if(q==5)
{beep=~beep;q=0;}
}
void datapros(int temp) //溫度讀取處理轉(zhuǎn)換函數(shù)
{
float tp;
if(temp< 0) //當溫度值為負數(shù)
{
charac[0]=0x40;
temp=temp-1;//因為讀取的溫度是實際溫度的補碼,所以減1,再取反求出原碼
temp=~temp;
tp=temp;
temp=tp*0.0625*10+0.5;
//留兩個小數(shù)點就*100,+0.5是四舍五入,因為C語言浮點數(shù)轉(zhuǎn)換為整型的時候把小數(shù)點
//后面的數(shù)自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進1了,小于0.5的就
//算加上0.5,還是在小數(shù)點后面。
Y=temp;
if(Y*(-1)/10<-10)
q=q+1;
}
else
{
charac[0]=0x00;
tp=temp;//因為數(shù)據(jù)處理有小數(shù)點所以將溫度賦給一個浮點型變量
//如果溫度是正的那么,那么正數(shù)的原碼就是補碼它本身
temp=tp*0.0625*10+0.5;
//留兩個小數(shù)點就*100,+0.5是四舍五入,因為C語言浮點數(shù)轉(zhuǎn)換為整型的時候把小數(shù)點
//后面的數(shù)自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進1了,小于0.5的就
//算加上0.5,還是在小數(shù)點后面。
Y=temp;Z=temp;Z=Z/10;
if(Y/10>30)
q=q+1;
if(Z>=10&&Z<=30)
{M=(Z-10)*10;}
}
charac[1]=xuanwei[temp/100];
charac[2]=xuanwei[temp%100/10]+0x80;
charac[3]=xuanwei[temp%10];
}
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Init() //初始化函數(shù)
{
uchar i;
DSPORT = 0; //將總線拉低480us~960us
i = 80;
while(i--);//延時642us
DSPORT = 1; //然后拉高總線,如果DS18B20做出反應會將在15us~60us后總線拉低
i = 0;
while(DSPORT) //等待DS18B20拉低總線
{
Delay1ms(1);
i++;
if(i>5)//等待>5MS
{
return 0;//初始化失敗
}
}
return 1;//初始化成功
}
void Ds18b20WriteByte(uchar dat)//寫入函數(shù)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; //每寫入一位數(shù)據(jù)之前先把總線拉低1us
i++;
DSPORT = dat & 0x01; //然后寫入一個數(shù)據(jù),從最低位開始
i=6;
while(i--); //延時68us,持續(xù)時間最少60us
DSPORT = 1; //然后釋放總線,至少1us給總線恢復時間才能接著寫入第二個數(shù)值
dat >>= 1;
}
}
uchar Ds18b20ReadByte()//讀取函數(shù)
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;//先將總線拉低1us
i++;
DSPORT = 1;//然后釋放總線
i++;
i++;//延時6us等待數(shù)據(jù)穩(wěn)定
bi = DSPORT; //讀取數(shù)據(jù),從最低位開始讀取
//將byte左移一位,然后與上右移7位后的bi,注意移動之后移掉那位補0。
byte = (byte >> 1) | (bi << 7);
i = 4; //讀取完之后等待48us再接著讀取下一個數(shù)
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()//轉(zhuǎn)換
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0x44); //溫度轉(zhuǎn)換命令
//Delay1ms(25); //等待轉(zhuǎn)換成功,而如果你是一直刷著的話,就不用這個延時了
}
void Ds18b20ReadTempCom()//讀取溫度
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0xbe); //發(fā)送讀取溫度命令
}
int Ds18b20ReadTemp()//ds18b20讀取溫度總函數(shù)
{
uint temp=0;
uchar tmh, tml;
Ds18b20ChangTemp(); //先寫入轉(zhuǎn)換命令
Ds18b20ReadTempCom(); //然后等待轉(zhuǎn)換完后發(fā)送讀取溫度命令
tml = Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節(jié)
tmh = Ds18b20ReadByte(); //再讀高字節(jié)
temp = tmh;
temp <<= 8;
temp |= tml;
sdata = tml/16+tmh*16; //整數(shù)部分
return temp;
}
void DigDisplay()
{
LSA=0;LSB=0;LSC=0; //顯示第0位
P0=charac[3];
delay(1000); //間隔一段時間掃描
P0=0x00;//消隱
LSA=1;LSB=0;LSC=0;//顯示第1位
P0=charac[2];
delay(1000); //間隔一段時間掃描
P0=0x00;//消隱
LSA=0;LSB=1;LSC=0; //顯示第2位
P0=charac[1];
delay(1000); //間隔一段時間掃描
P0=0x00;//消隱
LSA=1;LSB=1;LSC=0; //顯示第3位
P0=charac[0];//發(fā)送段碼
delay(1000); //間隔一段時間掃描
P0=0x00;//消隱
}
int te;
void main()//主函數(shù)
{
q=0;w=0;
while(1)
{
beep=0;moto=0;
te=Ds18b20ReadTemp();
datapros(te);
DigDisplay();
}
}
|