一、 DS18B20 介绍 1、温范围-55℃~ 125℃,在-10~ 85℃时精度为±0.5℃。 2.可编程的分辨率为 9~12 对应的可分辨温度为 0.5℃、0.25℃、 0.125℃ 和 0.0625℃,可实现高精度测温 例如,我们需要计算 85 度,数据输出16进制 0X0550,因为高字节高 5位为 0,表示检测温度为正温,0X0550 十进制对应 这个值乘以1360 12 位精度 0.0625,所以可以得到 85 度。
二、时序 DS18B20 时序包括以下几种:初始化时序和写作(0 和 1)时序,读(0 和 1)时序。 DS18B20 所有的命令和数据都在字节的低位之前。 DS18B20 典型的温度读取过程是:复位→发 SKIP ROM 命令(0XCC)→开始转换命令(0X44)→延时→复位→发送 SKIP ROM 命令(0XCC)→读取存储器命令(0XBE)→连续读取两个字节数据(即温度)→结束。
1、编写temp.h文件
#ifndef _temp_H_ #define _temp_H_ #include <reg52.h> #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned char #endif sbit DSPORT=P3^7; void Delay1ms(unsigned int ); unsigned char Ds18b20Init(); void Ds18b20WriteByte(unsigned char com); unsigned char Ds18b20ReadByte(); void Ds18b20ChangTemp(); void Ds18b20ReadTempCom(); short Ds18b20ReadTemp(); #endif
2、编写temp.c文件
#include "temp.h" void Delay1ms(uint y) {
uint x; for(y;y>0;y--) for(x=110;x>0;x--);
}
unsigned char Ds18b20Init()
{
uint i;
DSPORT=0;
i=70;
while(i--);//??642us
DSPORT=1;
i=0;
while(DSPORT)
{
i++;
if(i>5000)
return 0;
}
return 1;
}
void Ds18b20WriteByte(uchar dat)
{
uint i,j;
for(j=0;j<8;j++)
{
DSPORT=0;
i++;
DSPORT=dat&0x01;
i=6;
while(i--);
DSPORT=1;
dat>>=1;
}
}
unsigned char Ds18b20ReadByte()
{
uchar byte,bi;
uint i,j;
for(j=8;j>0;j--)
{
DSPORT=0;
i++;
DSPORT=1;
i++;
i++;
bi=DSPORT;
byte=(byte>>1)|(bi<<7);
i=4;
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44);
// Delay1ms(100);
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0xbe);
}
short Ds18b20ReadTemp()
{
uchar temp=0;
uchar tmh,tml;
short tem;
Ds18b20ChangTemp();
Ds18b20ReadTempCom();
tml=Ds18b20ReadByte();
tmh=Ds18b20ReadByte();
if(tmh>7)
{
tmh=~tmh;
tml=~tml;
temp=0;
}
else
{
temp=1;
}
tem=tmh;
tem<<=8;
tem|=tml;
tem=(double)tem*0.625;
if(temp)
return tem;
else
return -tem;
}
3、编写main.c文件
#include "reg52.h"
#include "i2c.h"
#include "intrins.h"
typedef unsigned int ui;
typedef unsigned char uc;
//#define led P2 //×¢Ò⣡£¡²»¼Ó";"
//sbit beep=P1^5;
//sbit led=P2^0;
//sbit moto=P1^0;
//sbit k1=P3^1;
//sbit k2=P3^0;
//sbit k3=P3^2;
//sbit k4=P3^3;
sbit LSA=P2^2;//×îµÍλ
sbit LSB=P2^3;//´ÎµÍλ
sbit LSC=P2^4;//¸ßλ
char num=0;
uc disp[8];
uc code smgduan[10]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uc code smgduan1[10]={
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
void delay(ui i)
{
while(i--);
}
void Datapros(int temp)
{
float tp;
if(temp<0)
{
disp[0] = 0x40;
temp-=1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
}
else{
disp[0] = 0x00;
tp=temp;
temp=tp*0.0625*100+0.5;
}
disp[1]=smgduan[temp%10000/1000];//ǧλ
disp[2]=smgduan1[temp%1000/100];//°Ùλ
disp[3]=smgduan[temp%100/10];//ʮλ
disp[4]=smgduan[temp%10];//¸öλ
}
void DigDisplay()
{
ui i;
for(i=0;i<6;i++)
{
switch(i)
{
case(0):
LSA=1;LSB=1;LSC=1; break;//led-8
case(1):
LSA=0;LSB=1;LSC=1; break;//led-7
case(2):
LSA=1;LSB=0;LSC=1; break;//led-6
case(3):
LSA=0;LSB=0;LSC=1; break;//led-5
case(4):
LSA=1;LSB=1;LSC=0; break;//led-4
case(5):
LSA=0;LSB=1;LSC=0; break;//led-3
}
P0=disp[i];
delay(100);
P0=0x00;
}
}
void main()
{
while(1)
{
Datapros(Ds18b20ReadTemp());
DigDisplay();
}
}