DS18B20 (Programmable Resolution 1-Wire Digital Thermometer 可编程分辨率1线数字温度计)驱动理解

前言

本文通过理解DS18B20的驱动程序,更好的理解DS18B20的功能,其中驱动程序为蓝桥杯官方提供的驱动,解释则通过摘录芯片手册中的内容。

一、DS18B20通信流程

//芯片手册的描述
TRANSACTION SEQUENCE
The transaction sequence for accessing the DS18B20 is as follows:
	Step 1. Initialization
	Step 2. ROM Command (followed by any required data exchange)
	Step 3. DS18B20 Function Command (followed by any required data exchange)
It is very important to follow this sequence every time the DS18B20 is accessed, as the DS18B20 will not 
respond if any steps in the sequence are missing or out of order. Exceptions to this rule are the Search 
ROM [F0h] and Alarm Search [ECh] commands. After issuing either of these ROM commands, the 
master must return to Step 1 in the sequence.

二、DS18B20初始化方法

/*
本程序的晶振频率为12MHz
工作在12T模式
蓝桥杯单片机工作在1T模式,速度比12T模式快12倍
*/
#include "reg52.h"
sbit DQ = P1^4; // 单总线接口:单片机的P1.4端连接在DS18B20的DQ端
//延时函数
void Delay_OneWire(unsigned int t)
{
	while(t--); // 运行一次 12µs
}

// DS18B20设备初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	DQ = 1;
  	Delay_OneWire(12);
	// the bus master transmits (TX) the reset pulse by pulling the 1-Wire bus low for a minimum of 480µs
  	DQ = 0; // 拉低 1-wire 数据总线 
  	Delay_OneWire(80); // 拉低最少 480µs
  	// The bus master then releases the bus and goes into receive mode (RX).
  	DQ = 1; // 释放总线
  	// it waits 15µs to 60µs and then transmits a presence pulse by pulling the 1-Wire bus low for 60µs to 240µs.
  	Delay_OneWire(10); // 等待上拉电阻拉低总线电平 >60µs
    initflag = DQ; // 检查 BS18B20 是否拉低总线电平  
  	Delay_OneWire(5); // 初始化结束
  
  	return initflag; // 返回0则表示初始化成功
}

1. DQ为何物?

下图是DS1802不同型号的管脚排列:
PIN CONFIGURATIONS
这里看到(DS18B20)的管脚图,由接地端GND,电源端VDD,和数字信号输入/输出端DQ三个端口组成。下面是芯片手册中的解释:

2. DS18B20设备初始化流程:

//芯片手册的描述
INITIALIZATION PROCEDURE—RESET AND PRESENCE PULSES
All communication with the DS18B20 begins with an initialization sequence that consists of a reset pulse 
from the master followed by a presence pulse from the DS18B20. This is illustrated in Figure 13. When 
the DS18B20 sends the presence pulse in response to the reset, it is indicating to the master that it is on 
the bus and ready to operate. 
During the initialization sequence the bus master transmits (TX) the reset pulse by pulling the 1-Wire bus 
low for a minimum of 480µs. The bus master then releases the bus and goes into receive mode (RX). 
When the bus is released, the 5kΩ pullup resistor pulls the 1-Wire bus high. When the DS18B20 detects 
this rising edge, it waits 15µs to 60µs and then transmits a presence pulse by pulling the 1-Wire bus low 
for 60µs to 240µs.

在这里插入图片描述

三、通过单总线读写DS18B2

1. 写指令介绍

//芯片手册的描述
ROM COMMANDS
After the bus master has detected a presence pulse, it can issue a ROM command. These commands 
operate on the unique 64-bit ROM codes of each slave device and allow the master to single out a specific 
device if many are present on the 1-Wire bus. These commands also allow the master to determine how 
many and what types of devices are present on the bus or if any device has experienced an alarm 
condition. There are five ROM commands, and each command is 8 bits long. The master device must 
issue an appropriate ROM command before issuing a DS18B20 function command. A flowchart for 
operation of the ROM commands is shown in Figure 11.
// 这里就不给出 Figure 11 了,可以查看芯片手册13页了解详情

2. 写时隙介绍

There are two types of write time slots: “Write 1” time slots and “Write 0” time slots. The bus master 
uses a Write 1 time slot to write a logic 1 to the DS18B20 and a Write 0 time slot to write a logic 0 to the 
DS18B20. All write time slots must be a minimum of 60µs in duration with a minimum of a 1µs recovery 
time between individual write slots. Both types of write time slots are initiated by the master pulling the 
1-Wire bus low (see Figure 14).
To generate a Write 1 time slot, after pulling the 1-Wire bus low, the bus master must release the 1-Wire
bus within 15µs. When the bus is released, the 5kΩ pullup resistor will pull the bus high. To generate a 
Write 0 time slot, after pulling the 1-Wire bus low, the bus master must continue to hold the bus low for 
the duration of the time slot (at least 60µs).
The DS18B20 samples the 1-Wire bus during a window that lasts from 15µs to 60µs after the master 
initiates the write time slot. If the bus is high during the sampling window, a 1 is written to the DS18B20. 
If the line is low, a 0 is written to the DS18B20.

3. 读指令介绍

The DS18B20 can only transmit data to the master when the master issues read time slots. Therefore, the 
master must generate read time slots immediately after issuing a Read Scratchpad [BEh] or Read Power 
Supply [B4h] command, so that the DS18B20 can provide the requested data. In addition, the master can 
generate read time slots after issuing Convert T [44h] or Recall E2 [B8h] commands to find out the status 
of the operation as explained in the DS18B20 Function Commands section.

4. 读时序介绍

All read time slots must be a minimum of 60µs in duration with a minimum of a 1µs recovery time 
between slots. A read time slot is initiated by the master device pulling the 1-Wire bus low for a 
minimum of 1µs and then releasing the bus (see Figure 14). After the master initiates the read time slot, 
the DS18B20 will begin transmitting a 1 or 0 on bus. The DS18B20 transmits a 1 by leaving the bus high 
and transmits a 0 by pulling the bus low. When transmitting a 0, the DS18B20 will release the bus by the 
end of the time slot, and the bus will be pulled back to its high idle state by the pullup resister. Output 
data from the DS18B20 is valid for 15µs after the falling edge that initiated the read time slot. Therefore, 
the master must release the bus and then sample the bus state within 15µs from the start of the slot.
Figure 15 illustrates that the sum of TINIT, TRC, and TSAMPLE must be less than 15µs for a read time slot.
Figure 16 shows that system timing margin is maximized by keeping TINIT and TRC as short as possible 
and by locating the master sample time during read time slots towards the end of the 15µs period.
// 这里就不给出 Figure 15、16 了,可以查看芯片手册了解详情

在这里插入图片描述

5. 读写代码

// 写一个字节
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0; // initiated by the master pulling the 1-Wire bus low 
		DQ = dat&0x01; // 传送dat的最低位
		Delay_OneWire(5);  // (at least 60µs).
		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; // initiated by pulling the 1-Wire bus low for a minimum of 1µs
		dat >>= 1; // 右移一位,准备在最高位接受一个bit
		DQ = 1; // then releasing the bus and then sample the bus state within 15µs
		if(DQ)
		{
			dat |= 0x80; // 若 DQ 为 1,则将高位置 1
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

四、案例介绍

// 将温度读出并存储在 LSB (低 8 位)和 MSB (高 8 位)
// 与 DS18B20 交互的一般流程
// 1. Initialization 初始化芯片状态(复位)
// 2. 发送 ROM 指令:读 ROM 数据、指定匹配芯片、跳跃 ROM、芯片搜索、报警芯片搜索
// 4. 发送存储器操作指令



void ReadTemp() 
{
	unsigned char LSB,MSB
	Init_DS18B20();  // 芯片初始化	
	Write_DS18B20(0xCC);  // ROM 指令: 跳过 ROM 操作指令			
	Write_DS18B20(0x44);  // 操作指令: 开始温度转换
	Delay(1000);  // 等待温度转换完成
	Init_DS18B20();	 // 芯片初始化	
	Write_DS18B20(0xCC);  // ROM 指令		
	Write_DS18B20(0xBE);  // 操作指令: 读取高速暂存器(先低 8 位,后高 8 位)
	LSB = Read_DS18B20();		
	MSB = Read_DS18B20();			
	Init_DS18B20();		
	// LSB 比特位从高到低: 2^3 2^2 ... 2^-4
	// MSB 比特位从高到低: S S S S S 2^6 2^5 2^4 
	// 温度为正时 S = 0; 温度为负时 S = 1;
	temp = 0x0000;
	temp = MSB;
	temp <<= 8;
	temp = temp | LSB;
	
	if((temp & 0xf800) == 0x0000)
	{
		temp >>= 4;
		temp = temp*10;
		temp = temp + (LSB&0x0f)*0.625;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值