温度测量(单总线)
DS18B20是我生活中以及在单片机开发中用的最多的一个温度传感器。
DS18B20内部结构
这是基本的一个原理分布图,通过Vdd来给DS18B20供电,要写这个传感器还需要知道单总线协议(在蓝桥杯的参考手册中有源码)。
内部结构图:
重要分为两个部分,64位ROM、9字节暂存器(温度传感器,上下限触发,高速暂存器,8位CRC产生器)。
温度寄存器:
温度寄存器由两个字节组成,分为低8位和高8位。
其中,第0位到第3位,存储的是温度值的小数部分。
第4位到第10位存储的是温度值的整数部分。
第11位到第15位为符号位。全0表示是正温度,全1表示是负温度。
表格中的数值,如果相应的位为1,表示存在。如果相应的位为0,表示不存在。
具体实例如下图:
单总线协议
单总线的初始化 (仅需要了解)
位写入时序(仅需要了解)
位读取时序(仅需要了解)
源码分享
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include "reg52.h"
sbit DQ = P1^4;
//unsigned char rd_temperature();
void Delay_OneWire(unsigned int t); //STC89C52RC
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif
onewire.c
注意要是使用13届以前的赛点资源包时注意Delay_OneWire(unsigned int t)。注意12T和1T 的区别。
#include "onewire.h"
void Delay_OneWire(unsigned int t) //STC89C52RC
{
unsigned int n; //?????????
n = t * 12; //??10?????
//IAP15F2K61S2?1T?8051?????
while(n--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
main.c
#include "reg52.h"
#include "onewire.h"
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned char code SMG_Dot[10]={0x40,0x79,
0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
float temp_ds18b20 = 0;
unsigned int smg_ds18b20 = 0;
void DelaySMG(unsigned int t)
{
while(t--);
}
void SelectHC573(unsigned channel)
{
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
case 0:
P2 = (P2 & 0x1f) | 0x00;
break;
}
}
void DisplaySMG_Bit(unsigned char pos, unsigned char value)
{
SelectHC573(6);
P0 = 0x01 << pos;
SelectHC573(7);
P0 = value;
}
void DisplaySMG_All(unsigned char value)
{
SelectHC573(6);
P0 = 0xff;
SelectHC573(7);
P0 = value;
}
void DisplaySMG_Temp()
{
DisplaySMG_Bit(7,SMG_NoDot[smg_ds18b20 % 10]);
DelaySMG(500);
DisplaySMG_Bit(7,0xff);
DisplaySMG_Bit(6,SMG_NoDot[(smg_ds18b20 / 10)]);
DelaySMG(500);
DisplaySMG_Bit(6,0xff);
}
void Read_Dsiplay_DS18B20()
{
unsigned char LSB,MSB;
unsigned int temp = 0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
init_ds18b20();
temp = MSB;
temp = (temp << 8) | LSB;
smg_ds18b20 = temp * 0.0625;
}
void Init_sys()
{
SelectHC573(0);
DisplaySMG_All(0xff);
SelectHC573(5);
P0 = 0x00;
SelectHC573(4);
P0 = 0xff;
}
void main()
{
Init_sys();
while(1)
{
Read_Dsiplay_DS18B20();
DisplaySMG_Temp();
}
}
上述代码主要是温度的整数显示。
如果需要保留以为小数或者两位的时候只需要修改Read_Dsiplay_DS18B20()中添加
smg_ds18b20 = smg_ds18b20*10;或者*100。
数码管显示向左一位,最后一位使用SMG_Dot[10]该数组。