传感器资讯网<br>——最新最专业的物联网传感器资讯平台

传感器资讯网
——最新最专业的物联网传感器资讯平台
一起来关注各类工业传感器、工业自动化、工业互联网、物联网

DS18B20温度传感器64位ROM地址读取(STC89C52)

更新时间:2015-10-12 17:36:34人气:878

温度传感器

最近在做一个温度监测系统,采用DS18B20数字温度传感器,要用到64位ROM地址匹配,在网上查了一些资料,编写了DS18b20的64位ROM地址读取的程序,LCD1602显示。分享给大家,有什么不足请大家留言。

 

程序代码:

#include 

#define uchar unsigned char

#define uint unsigned int

 

sbit lcdrw P1^1;  //LCD 1602 读/写选择 H/L

sbit lcdrs P1^0;   //LCD1602 数据/命令选择 H/L

sbit lcden P2^5;   //LCD1602 使能

sbit ds P2^2;      //温度传感器信号线

 

uchar code table[]="NO.1:     C";

uchar code table1[]="             ";

uchar data fCode[8];

 

int temp;  //整型温度数据

float f_temp;  //浮点型温度数据

uchar tflag;//温度正负标志

//*****************LCD1602*******************************

void delay(uint z)  //延时函数 延时z毫秒

{

uint x,y;

for(x z;x>0;x--)

for(y 110;y>0;y--);

}

void write_com (uchar com)  //lcd1602写指令

{

lcdrs 0;

P0 com ;

delay(5);

lcden 1;

delay(5);

lcden 0;

}  //lcd1602写数据

void write_date(uchar date)

{

lcdrs 1;

P0 date;

delay(5);

lcden 1;

delay(5);

lcden 0;

}

void init1602() //初始化LCD1602

 

uchar i;

lcdrw 0;

lcden 0;

write_com(0x38);

write_com(0x0c);

write_com(0x06);

write_com(0x01);

 

write_com(0x80);

for(i 0;i<13;i++)

{

write_date(table[i]);

delay(5);

}

write_com(0x80+0x40);

for(i 0;i<13;i++)

{

write_date(table1[i]);

delay(5);

}

write_com(0x80+11);

write_date(0xdf);

write_com(0x80+0x40+11);

write_date(0xdf);

 

}

//*******************DS18B20************************** **

void dsreset()   //DS18B20复位,初始化函数

{

uint i;

ds 0;

103;

while(i>0)

i--;

ds 1;

4;

while(i>0)

i--;

}

bit tempreadbit()  //读一位数据 函数

{

uint i;

bit dat;

ds 0;

i++;

ds 1;

i++;

i++;

dat ds;

8;

while(i>0)

i--;

return dat;

}

uchar tempread()   //读一字节数据函数

{

uchar i,j,dat;

dat 0;

for(i 1;i<=8;i++)

{

tempreadbit();

dat (j<<7)|(dat>>1);

}

return dat;

}

void tempwrite(uchar dat)   //向DS18B20写一字节数据函数

{

uint i;

uchar j;

bit testb;

for(j 1;j<=8;j++)

{

testb dat&0x01;

dat dat>>1;

if(testb) //写1

{

ds 0;

i++;i++;

ds 1;

8;

while(i>0)

i--;

}

else    //写0

{

ds 0;

8;

while(i>0)

i--;

ds 1;

i++;i++;

}

}

}

 

void tempchange()  //DS18B20开始获取温度并转换

{

dsreset();

delay(1);

    tempwrite(0xcc);   //写跳过读ROM指令

tempwrite(0x44);   //写温度转换指令

}

uint get_temp()

{

uchar a,b;

dsreset();

delay(1);

tempwrite(0xcc);   //写“跳过读ROM”指令

tempwrite(0xbe);   //写“读内部RAM中9字节的温度数据”指令

tempread();    //读高8位

tempread();    //读低8位

temp b;

temp<<=8;

temp temp|a; //两个字节合为一个字节

if(temp<0x0fff)

    tflag="0;  

    else

   {

temp="~temp+1;

tflag="1; //负值标识符

    }

f_temp temp*0.0625; //温度在寄存器中为12位,分辨路为0.0625

temp f_temp*10+0.5; // 乘以10表示小数点后面只取一位,加0.5是四舍五入

//f_temp f_temp+0.05;

return temp; //temp是整型

}

void write_wendu(uchar address,uchar date)  //写温度  

{

write_com(address);

write_date(date);

}

void display()   //显示函数

{

uint w1,w2,w3,w4;

w1 get_temp()/1000;   //百位

w2 get_temp()00/100;   //十位

w3 get_temp()0/10;      //个位

w4 get_temp();  //小数点位

if(tflag="=0)

write_wendu(0x80+5,0x20);  //不显示正号

else

write_wendu(0x80+5,0x2d);  //显示负号

if(w1==0)

{

write_wendu(0x80+6,0x20);//百位为0,不显示

if((w1==0)&&(w2==0))

write_wendu(0x80+7,0x20);  //百位,十位都为0,不显示

else

write_wendu(0x80+7,0x30+w2);  

write_wendu(0x80+8,0x30+w3);

    write_wendu(0x80+10,0x30+w4);

}

else

{

write_wendu(0x80+6,0x30+w1);

    write_wendu(0x80+7,0x30+w2);

    write_wendu(0x80+8,0x30+w3);

write_wendu(0x80+10,0x30+w4);  

}

}

void DispCode()    //读取ROM

{

uint i,xulie;

dsreset();

delay(1);

    tempwrite(0x33);

    for (i="0;i<8;i++)

{

fCode[i]=tempread();

}

write_com(0x80+0x40); 

for (i="0;i<8;i++)

{

     xulie fCode[i]>>4; //显示高四位

xulie xulie*1.0;

     if (xulie<10) 

     write_date(0x30+xulie);

     else

     write_date(0x37+xulie);

xulie fCode[i]&0x0f;//显示低四位

   xulie xulie*1.0;

   if (xulie<10) 

     write_date(0x30+xulie);

   else

     write_date(0x37+xulie);

}

}

//************************************************

void main()  //主函数

{

 

    init1602();

DispCode();

while(1)

{ //searchrom();

 

tempchange();

display();  

}

}

PREVIOUS减少压力变送器测量误差的方法

传感器的种类繁多,每一种传感器都在各种行业发挥举重若轻的重要性。20世纪70年代光纤温度传感器的技术为微波场测温带来很多新的手段。本文具体为大家分析光纤温度传感器在微波场测温中的优点。基于光纤光栅的光纤传感器,其传感过程是通过外界参量对Bragg中心波长的调...

气体智能流量计设计原理和优良性能NEXT

智能气体流量计主要用于工业管道介质流体的流量丈量,如气体、液体、蒸气等多种介质。其特性是压力损失小,量程范围大,精度高,在丈量工况体积流量时简直不受流体密度、压力、温度、粘度等参数的影响。无可动机械零件,因而牢靠性高,维护量小。仪表参数能长期稳定。智能气体流...

closense-els16