当前位置:首页 > 技术学院 > 热搜器件
[导读]本文档参照Maxim公司DS18B20的Datasheet而完成。

本文档参照Maxim公司DS18B20的Datasheet而完成。其中,加了灰框的部分为原文档内容,其中英文部分为官方文档内容,中文部分为本人的翻译。限于本人水平,本文档可能存在错误或者让人误解的内容,对于因此引起的一切问题作者概不负责。

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.

DS18B20的所有通信都由由复位脉冲组成的初始化序列开始。该初始化序列由主机发出,后跟由DS18B20发出的存在脉冲(presence pulse)。下图(即如下截图)阐述了这一点。当发出应答复位脉冲的存在脉冲后,DS18B20通知主机它在总线上并且准备好操作了。

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).

在初始化步骤中,总线上的主机通过拉低单总线至少480μs来产生复位脉冲。然后总线主机释放总线并进入接收模式

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.

当总线释放后,5kΩ的上拉电阻把单总线上的电平拉回高电平。当DS18B20检测到上升沿后等待15到60us,然后以拉低总线60-240us的方式发出存在脉冲。

如文档所述,主机将总线拉低最短480us,之后释放总线。由于5kΩ上拉电阻的作用,总线恢复到高电平。DS18B20检测到上升沿后等待15到60us,发出存在脉冲:拉低总线60-240us。至此,初始化和存在时序完毕。

根据上述要求编写的复位函数为:

首先是延时函数:(由于DS18B20延时均以15us为单位,故编写了延时单位为15us的延时函数,注意:以下延时函数晶振为12MHz)

void Delayxus_DS18B20(unsigned int t)

{

for(t;t>0;t--)

{

_nop_();_nop_();_nop_();_nop_();

}

_nop_(); _nop_();

}

延时函数反汇编代码(方便分析延时公式)

C:0x0031 7F01 MOV R7,#0x01

C:0x0033 7E00 MOV R6,#0x00

C:0x0035 1206A6 LCALL delayxus(C:06A6)

38: void Delayxus_DS18B20(unsigned int t)

39: {

40: for(t;t>0;t--)

C:0x06A6 D3 SETB C

C:0x06A7 EF MOV A,R7

C:0x06A8 9400 SUBB A,#0x00

C:0x06AA EE MOV A,R6

C:0x06AB 9400 SUBB A,#0x00

C:0x06AD 400B JC C:06BA

41: {

42: _nop_();_nop_();_nop_();_nop_();

C:0x06AF 00 NOP

C:0x06B0 00 NOP

C:0x06B1 00 NOP

C:0x06B2 00 NOP

43: }

C:0x06B3 EF MOV A,R7

C:0x06B4 1F DEC R7

C:0x06B5 70EF JNZ Delayxus_DS18B20 (C:06A6)

C:0x06B7 1E DEC R6

C:0x06B8 80EC SJMP Delayxus_DS18B20 (C:06A6)

44: _nop_(); _nop_();

C:0x06BA 00 NOP

C:0x06BB 00 NOP

45: }

C:0x06BC 22 RET

分析上述反汇编代码,可知延时公式为15*(t+1)

bit RST_DS18B20()

{

bit ret="1";

DQ=0;

Delayxus_DS18B20(32);

DQ=1;

Delayxus_DS18B20(4);

ret=DQ;

Delayxus_DS18B20(14);

DQ=1;

return(~ret);

}

写时序:

READ/WRITE TIME SLOTS

读写时隙

The bus master writes data to the DS18B20 during write time slots and reads data from the DS18B20 during read time slots. One bit of data is transmitted over the 1-Wire bus per time slot.

主机在写时隙向DS18B20写入数据,并在读时隙从DS18B20读入数据。在单总线上每个时隙只传送一位数据。

WRITE TIME SLOTS

写时间隙

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).

有两种写时隙:写“0”时间隙和写“1”时间隙。总线主机使用写“1”时间隙向DS18B20写入逻辑1,使用写“0”时间隙向DS18B20写入逻辑0.所有的写时隙必须有最少60us的持续时间,相邻两个写时隙必须要有最少1us的恢复时间。两种写时隙都通过主机拉低总线产生。

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).

为产生写1时隙,在拉低总线后主机必须在15μs内释放总线。在总线被释放后,由于5kΩ上拉电阻的作用,总线恢复为高电平。为产生写0时隙,在拉低总线后主机必须继续拉低总线以满足时隙持续时间的要求(至少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.

在主机产生写时隙后,DS18B20会在其后的15到60us的一个时间窗口内采样单总线。在采样的时间窗口内,如果总线为高电平,主机会向DS18B20写入1;如果总线为低电平,主机会向DS18B20写入0。

如文档所述,所有的写时隙必须至少有60us的持续时间。相邻两个写时隙必须要有最少1us的恢复时间。所有的写时隙(写0和写1)都由拉低总线产生。

为产生写1时隙,在拉低总线后主机必须在15us内释放总线(拉低的电平要持续至少1us)。由于上拉电阻的作用,总线电平恢复为高电平,直到完成写时隙。

为产生写0时隙,在拉低总线后主机持续拉低总线即可,直到写时隙完成后释放总线(持续时间60-120us)。

写时隙产生后,DS18B20会在产生后的15到60us的时间内采样总线,以此来确定写0还是写1。

满足上述要求的写函数为:

void WR_Bit(bit i)

{

DQ=0;//产生写时序

_nop_();

_nop_();//总线拉低持续时间要大于1us

DQ=i;//写数据 ,0和1均可

Delayxus_DS18B20(3);//延时60us,等待ds18b20采样读取

DQ=1;//释放总线

}

void WR_Byte(unsigned char dat)

{

unsigned char i="0";

while(i++<8)

{

WR_Bit(dat&0x01);//从最低位写起

dat>>=1; //注意不要写成dat>>1

}

}读时序:

READ TIME SLOTS

读时间隙

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

E 2[B8h] commands to find out the status of the operation as explained in the DS18B20 Function Commands section.

DS18B20只有在主机发出读时隙后才会向主机发送数据。因此,在发出读暂存器命令 [BEh]或读电源命令[B4h]后,主机必须立即产生读时隙以便DS18B20提供所需数据。另外,主机可在发出温度转换命令T [44h]或Recall命令E 2[B8h]后产生读时隙,以便了解操作的状态(在 DS18B20操作指令这一节会详细解释)。

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.

所有的读时隙必须至少有60us的持续时间。相邻两个读时隙必须要有最少1us的恢复时间。所有的读时隙都由拉低总线,持续至少1us后再释放总线(由于上拉电阻的作用,总线恢复为高电平)产生。在主机产生读时隙后,DS18B20开始发送0或1到总线上。DS18B20让总线保持高电平的方式发送1,以拉低总线的方式表示发送0.当发送0的时候,DS18B20在读时隙的末期将会释放总线,总线将会被上拉电阻拉回高电平(也是总线空闲的状态)。DS18B20输出的数据在下降沿(下降沿产生读时隙)产生后15us后有效。因此,主机释放总线和采样总线等动作要在15μs内完成

Figure 15 illustrates that the sum of TINIT, TRC, and TSAMPLE must be less than 15μs for a read time slot.

插图15表明了对于读时隙,TINIT(下降沿后低电平持续时间), TRC(上升沿)和TSAMPLE(主机采样总线)的时间和要在15μs以内。

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.

下图显示了最大化系统时间宽限的方法:让TINIT 和TRC尽可能的短,把主机采样总线放到15μs这一时间段的尾部

由文档可知,DS18B20只有在主机发出读时隙时才能发送数据到主机。因此,主机必须在BE命令,B4命令后立即产生读时隙以使DS18B20提供相应的数据。另外,在44命令,B8命令后也要产生读时隙。

所有的读时隙必须至少有60us的持续时间。相邻两个读时隙必须要有最少1us的恢复时间。所有的读时隙都由拉低总线,持续至少1us后再释放总线(由于上拉电阻的作用,总线恢复为高电平)产生。DS18B20输出的数据在下降沿产生后15us后有效。因此,释放总线和主机采样总线等动作要在15us内完成。

满足以上要求的函数为:

unsigned char Read_Bit()

{

unsigned char ret;

DQ=0;//拉低总线

_nop_(); _nop_();

DQ=1;//释放总线

_nop_(); _nop_();

_nop_(); _nop_();

ret=DQ;//读时隙产生7 us后读取总线数据。把总线的读取动作放在15us时间限制的后面是为了保证数据读取的有效性

Delayxus_DS18B20(3);//延时60us,满足读时隙的时间长度要求

DQ=1;//释放总线

return ret; //返回读取到的数据

}

unsigned char Read_Byte()

{

unsigned char i;

unsigned char dat="0";

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

{

dat>>=1;//先读最低位

if(Read_Bit())

dat|=0x80;

}

return(dat);

}

void Start_DS18B20()

{

DQ=1;

RST_DS18B20();

WR_Byte(0xcc);// skip

WR_Byte(0x44);//启动温度转换

}

int Read_Tem()

{

int tem="0";

RST_DS18B20();

WR_Byte(0xcc);// skip

WR_Byte(0xbe);//发出读取命令

tem=Read_Byte();//读出温度低八位

tem|=(((int)Read_Byte())<<8);//读出温度高八位

return tem;

}

注: DS18B20官方文档中没有说明读写数据位的顺序,查了下资料,DS18B20读写数据都是从最低位读写的。

*共阳极4位数码管显示,请根据自己的电路图再定义端口*/

#i nclude

sbit warmer=P1^4;

sbit led_run=P1^0;

//sbit k_power=P3^3;

sbit zf=P2^0;

sbit bai=P2^1;

sbit shi=P2^2;

sbit ge=P2^3;

sbit dots=P2^4;

sbit xs=P2^5;

sbit DQ =P3^3; //定义通信端口

//延时函数

unsigned char tab[]={ 0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3

0x99,0x92,0x82,0xf8,0x80,0x90, 0xff};

//4 5 6 7 8 9

unsigned int t=0;

unsigned char tflag;

unsigned char data disdata[4];

void delay(unsigned char i)

{

while(i--);

}

//初始化函数

Init_DS18B20(void)

{

unsigned char x=0;

DQ = 1; //DQ复位

delay(8); //稍做延时

DQ = 0; //单片机将DQ拉低

delay(80); //精确延时 大于 480us

DQ = 1; //拉高总线

delay(14);

x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败

delay(20);

}

//读一个字节

ReadOneChar(void)

{

unsigned char i=0;

unsigned char dat = 0;

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

{

DQ = 0; // 给脉冲信号

dat>>=1;

DQ = 1; // 给脉冲信号

if(DQ)

dat|=0x80;

delay(4);

}

return(dat);

}

//写一个字节

WriteOneChar(unsigned char dat)

{

unsigned char i=0;

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

{

DQ = 0;

DQ = dat&0x01;

delay(5);

DQ = 1;

dat>>=1;

}

delay(4);

}

//读取温度

ReadTemperature(void)

{

unsigned char a=0;

unsigned char b=0;

Init_DS18B20();

WriteOneChar(0xCC); // 跳过读序号列号的操作 发送指令0xcc

WriteOneChar(0x44); // 启动温度转换 发送指令0x44

Init_DS18B20();

WriteOneChar(0xCC); //跳过读序号列号的操作

WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 发送指令0xbe

a=ReadOneChar(); //读取温度值低位

b=ReadOneChar(); //读取温度值高位

t=b;

t<<=8; //值左移8位

//a=a>>4; //低位右移4位,舍弃小数部分

//t=b<<4; //高位左移4位,舍弃符号位

//t=t|a;

t=t|a; //合并高低位数值

if(t<0xfff)

tflag=1;

else

{

tflag=0;

t=~t+1; //负值换算

}

t=t*(0.625); //温度扩大10倍,精确到1位小数

return(t);

}

void display_temper(unsigned int i)

{

disdata[0]=i/1000; //百位数

disdata[1]=i00/100; //十位数

disdata[2]=i0/10; //个位数

disdata[3]=i; //小数位

if(tflag==1)

tflag=0xff; //正号,不显示

else

tflag=0xbf; //负号,显示-

P2=0xff;

zf=0;

P0=tflag;

delay(150);

P0=0xff;

zf=1;

bai=0;

if(disdata[0]>0)P0=tab[disdata[0]];

delay(150);

bai=1;

shi=0;

P0=tab[disdata[1]];

delay(150);

shi=1;

ge=0;

P0=tab[disdata[2]];

delay(100);

ge=1;

dots=0;

P0=0x7f;

delay(150);

dots=1;

xs=0;

P0=tab[disdata[3]];

delay(150);

P0=0xff;

P2=0xff;

}

void main(void)

{unsigned int temp;

while(1) //主循环

{ temp=ReadTemperature();

display_temper(temp);

}

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

DS18B20是常用的数字温度传感器,其输出的是数字信号,具有体积小,硬件开销低,抗干扰能力强,精度高的特点。

关键字: ds18b20 温度传感器 数字信号

单总线(1-Wire)是DALLAS公司推出的一种单线双向串行总线,仅用一根线即可实现多个器件间的数据传输。目前,常用的单总线接口芯片有数字温度传感器DS18B20、单总线控制器DSIWM和D-A转换器DS2450等。本...

关键字: 单总线 引脚功能 时序

I2C(Inter-Integrated Circuit)总线是Philips公司推出的一种双向二线制同步串行总线,仅用两根线即可实现器件之间的数据传送。目前很多芯片集成了I2C总线接口,如CYGNAL公司的C805IF...

关键字: I2C总线 引脚功能 时序

单片机与片外程序存储器、数据存储器或I/O接口之间进行数据交换时所产生的总线操作被称为总线周期(Bus Cycle)。单片机向外传输数据的总线周期是写总线周期,反之是读总线周期。在总线周期中,单片机总线引脚的状态将按照一...

关键字: 单片机 片外存储空间 时序

时序就是 CPU 总线信号在时间上的顺序关系。CPU 的控制器实质上是一个复杂的同 步时序电路,所有工作都是在时钟信号控制下进行的。每执行一条指令,CPU 的控制器都要发出一系列特定的控制信号,这些控制信号在时间上的相互...

关键字: 单片机 时序 CPU

(全球TMT2022年6月28日讯)2022年6月28日,国微思尔芯面向全球客户正式发布芯神瞳自动原型编译软件Player Pro-7(PPro-7)。新版本针对大规模芯片设计提供了有效的解决方案,拥有更高的编译效率和...

关键字: PLAYER 编译软件 时序 BSP

(全球TMT2022年4月22日讯)新思科技(Synopsys, Inc.)近日宣布与Ansys联合开发的电压时序签核解决方案已获三星采用,用以加速开发其具有理想功耗、性能和面积(PPA)的高能效比设计。该联合解决方案...

关键字: 三星 时序 电压 新思科技

从功能上来看,数字集成电路内部可以分为数据通路(Data-path,也称为数据路径)和控制逻辑两大部分。这两大部分都是由大量的时序逻辑电路集成的,而且绝大部分都是同步的时序电路,因为时序电路被多个触发器或寄存器分成若干节...

关键字: 时序 电路 触发器

1.需要对原有架构、规格、关键电路非常熟悉。多次实践证明不清楚原有设计的情况下做出的方案,往往只会越改越烂。判断是否熟悉的标准是能够讲清楚整个模块代码->能够自己画出整个微架构->能够提出有效的PPA优化方案。其实这一条...

关键字: 时序

#51单片机#DS18B20硬件原理以及通信的工作时序

关键字: ds18b20 通信
关闭
关闭