首页 DS18B20 程序时序详解

DS18B20 程序时序详解

举报
开通vip

DS18B20 程序时序详解DS18B20  Datasheet时序完全解读DS18B20时序     本文档参照Maxim公司DS18B20的Datasheet而完成。其中,加了灰框的部分为原文档内容,其中英文部分为官方文档内容,中文部分为本人的翻译。限于本人水平,本文档可能存在错误或者让人误解的内容,对于因此引起的一切问题作者(exploer,CIEE-CAU)概不负责。 INITIALIZATION PROCEDURE—RESET AND PRESENCE PULSES 初始化序列——复位和存在脉冲     All communicat...

DS18B20 程序时序详解
DS18B20  Datasheet时序完全解读DS18B20时序     本文档参照Maxim公司DS18B20的Datasheet而完成。其中,加了灰框的部分为原文档内容,其中英文部分为官方文档内容,中文部分为本人的翻译。限于本人水平,本文档可能存在错误或者让人误解的内容,对于因此引起的一切问题作者(exploer,CIEE-CAU)概不负责。 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)。下图(插图13,即如下截图)阐述了这一点。当发出应答复位脉冲的存在脉冲后,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)     /*     ************************************     函数:Delayxus_DS18B20     功能:DS18B20延时函数     参数:t为定时时间长度     返回:无     说明: 延时公式:15n+15(近似),晶振12Mhz     ******************************************     */     void Delayxus_DS18B20(unsigned int t) //15us*t+15us     {           for(t;t>0;t--)           {                  _nop_();_nop_();_nop_();_nop_();           }               _nop_(); _nop_();     }     延时函数反汇编代码(方便 分析 定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析 延时公式)          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)     /*     ************************************     函数:RST_DS18B20     功能:复位DS18B20,读取存在脉冲并返回     参数:无     返回:1:复位成功 ;0:复位失败     说明: 拉低总线至少480us ;可用于检测DS18B20工作是否正常     ******************************************     */     bit RST_DS18B20()     {                       bit ret="1";           DQ=0;/*拉低总线 */           Delayxus_DS18B20(32);/*为保险起见,延时495us */           DQ=1;/*释放总线 ,DS18B20检测到上升沿后会发送存在脉冲*/           Delayxus_DS18B20(4);/*需要等待15~60us,这里延时75us后可以保证接受到的是存在脉冲(如果通信正常的话) */           ret=DQ;           Delayxus_DS18B20(14);/*延时495us,让ds18b20释放总线,避免影响到下一步的操作 */        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的恢复时间。两种写时隙都通过主机拉低总线产生(见插图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).          为产生写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。     满足上述要求的写函数为:     /*     ************************************     函数:WR_Bit     功能:向DS18B20写一位数据     参数:i为待写的位     返回:无     说明: 总线从高拉到低产生写时序     ******************************************     */     void WR_Bit(bit i)     {           DQ=0;//产生写时序           _nop_();           _nop_();//总线拉低持续时间要大于1us           DQ=i;//写数据 ,0和1均可           Delayxus_DS18B20(3);//延时60us,等待ds18b20采样读取           DQ=1;//释放总线     }     /*     ***********************************     函数:WR_Byte     功能:DS18B20写字节函数,先写最低位     参数:dat为待写的字节数据     返回:无     说明:无     ******************************************     */     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.     插图16显示了最大化系统时间宽限的方法:让TINIT 和TRC尽可能的短,把主机采样总线放到15μs这一时间段的尾部。               由文档可知,DS18B20只有在主机发出读时隙时才能发送数据到主机。因此,主机必须在BE命令,B4命令后立即产生读时隙以使DS18B20提供相应的数据。另外,在44命令,B8命令后也要产生读时隙。     所有的读时隙必须至少有60us的持续时间。相邻两个读时隙必须要有最少1us的恢复时间。所有的读时隙都由拉低总线,持续至少1us后再释放总线(由于上拉电阻的作用,总线恢复为高电平)产生。DS18B20输出的数据在下降沿产生后15us后有效。因此,释放总线和主机采样总线等动作要在15us内完成。     满足以上要求的函数为:     /*     ***********************************     函数:Read_Bit     功能:向DS18B20读一位数据     参数:无     返回:bit i     说明: 总线从高拉到低,持续至1us以上,再释放总线为高电平空闲状态产生读时序     ******************************************     */     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; //返回读取到的数据     }          /*     ************************************     函数:Read_Byte     功能:DS18B20读一个字节函数,先读最低位     参数:无     返回:读取的一字节数据     说明: 无     ******************************************     */      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);     }     /*     ************************************     函数:Start_DS18B20     功能:启动温度转换     参数:无     返回:无     说明: 复位后写44H命令     ******************************************     */     void Start_DS18B20()     {           DQ=1;           RST_DS18B20();           WR_Byte(0xcc);// skip           WR_Byte(0x44);//启动温度转换     }          /*     ************************************     函数:Read_Tem     功能:读取温度     参数:无     返回:int型温度数据,高八位为高八位温度数据,低八位为低八位温度数据     说明: 复位后写BE命令     ******************************************     */     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 程序时序详解】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_828375
暂无简介~
格式:doc
大小:48KB
软件:Word
页数:8
分类:互联网
上传时间:2011-07-12
浏览量:30