DS18B20测量温度数码管显示

DS18B20温度传感器简介

DS18B20是一种数字温度传感器。它输出的是数字信号,同时具有体积小,硬件资源耗费少,抗干扰能力强,精度高等特点。

DS18B20温度传感器特点

1、单线接口:DS18B20仅需一条线可实现与微处理器双向通信。

2、测温范围: DS18B20温度传感器的测温范围可达-55℃~+125℃,在-10℃到+85℃范围内误差为±0.4°。

3、支持多点组网功能:多个DS18B20温度传感器可以并联在一条数据线上,最多可以并联8个,实现多点测温。

4、工作电源: 3.0~5.5V/DC ,DS18B20温度传感器可以采用外部独立电源供电,也可以用数据线寄生电源供电。

5、DS18B20温度传感器在应用过程中不需要任何外围元件。

6、DS18B20测量温度的结果以9~12位数字量方式串行传送。

7、掉电保护功能, DS18B20温度传感器内部含有 EEPROM ,通过配置寄存器可以设定数字转换精度和报警温度。在DS18B20温度传感器掉电以后仍可保存分辨率及报警温度的设定值。

8、DS18B20温度传感器返回16位二进制数代表此刻探测的温度值,其高五位代表正负。如果高五位全部为1,则代表返回的温度值为负值。如果高五位全部为0,则代表返回的温度值为正值。后面的11位数据代表温度的绝对值,将其转换为十进制数值之后,再乘以0.0625即可获得此时的温度值。

DS18B20温度传感器引脚说明

DS18B20温度传感器接口电路

数码管接口电路

DS18B20测量温度程序源代码

/**********************DS18B20测量温度**********************

* 单片机:51单片机

* 开发环境:keil

* 名称:DS18B20测量温度

* 功能: DS18B20测量温度,并在数码管中显示出来

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

#include <reg51.h>

#define uchar unsigned char

#define uint unsigned int

sbit DS=P2^2;

uint temp;

uchar flag1;

unsigned char code table[]=

{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,

0x77,0x7c,0x39,0x5e,0x79,0x71};

unsigned char code table1[]=

{0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};

void delay(uint count)

{

uint i;

while(count){i=200;while(i>0)i--;count--;}

}

void dsreset(void) //send reset and initializationcommand

{

uint i;

DS=0;i=103;while(i>0)i--;

DS=1;i=4;while(i>0)i--;

}

bit tmpreadbit(void) //read a bit

{

uint i;bitdat;

DS=0;i++; //i++ for delay

DS=1;i++;i++;

dat=DS;

i=8;while(i>0)i--;

return(dat);

}

uchar tmpread(void) //read a byte date

{

uchari,j,dat;

dat=0;

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

{

j=tmpreadbit();

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

//读出的数据最低位在最前面,这样刚好一个字节在DAT里

}

return(dat);

}

void tmpwritebyte(uchar dat) //write a byte to ds18b20

{

uint i;

uchar j;

bit testb;

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

{

testb=dat&0x01;

dat=dat>>1;

if(testb) //write 1

{

DS=0;i++;i++;

DS=1;i=8;while(i>0)i--;

}

else //write 0

{

DS=0;i=8;while(i>0)i--;

DS=1;i++;i++;

}

}

}

void tmpchange(void) //DS18B20 begin change

{

dsreset();delay(1);

tmpwritebyte(0xcc); //address all drivers on bus

tmpwritebyte(0x44); //initiates a single temperatureconversion

}

uint tmp() //get the temperature

{

float tt;

uchar a,b;

dsreset();delay(1);

tmpwritebyte(0xcc);

tmpwritebyte(0xbe);

a=tmpread();

b=tmpread();

temp=b;

temp<<=8; //two byte compose a int variable

temp=temp|a;

tt=temp*0.0625;

temp=tt*10+0.5;

returntemp;

}

void display(uint temp) //显示程序

{

ucharA1,A2,A2t,A3;

A1=temp/100;A2t=temp%100;A2=A2t/10;A3=A2t%10;

P0=table[A1]; //显示十位

P3=0xef;delay(3);

P0=table1[A2]; //显示个位,带小数点

P3=0xdf;delay(3);

P0=table[A3]; //显示十分位

P3=0xbf;delay(1);

}

void main()

{

uchar a;

do{tmpchange();for(a=10;a>0;a--){display(tmp());}}

while(1);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式硬件与代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值