视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
DS18B20单总线温度传感器
2025-09-28 02:22:32 责编:小OO
文档
DS18B20单总线温度传感器

1·功能特点

※采用单总线技术,与单片机通讯只要一根IO线

※通过比较系列号可以在一根线上挂多个DS18B20

※低压供电,电源范围从3V~5V,也可以直接从数据线上窃取电源

※测温范围-55~125摄氏度,在-10~85摄氏度范围内误差为±0.5度

※数据位可编程9~12位,转换12位温度时间为750ms(最大)

※用户可自设定预警上下限温度

※报警搜索命令可识别和寻址那个器件的温度至超出预定值

2·与单片机接口

3·时序图

 

                          主器件写 0 和 1 时序图

                         主器件读 0 和 1 时序

                          复位脉冲时序

3·DS18B20的命令字

44HConvert T
BEHRead Scratchpad
4EHWrite Scratchpad
48HCopy Scratchpad
B8HRecall E2

B4HRead Power Supply
4·温度转换时间表

数据位                转换时间

9位

                   93.75ms
10位

                   187.5ms
11 位

                    375ms
12 位

                   750 ms
5·DS18B20读温度流程

   A·总线上只有一个器件

复位发CCH SKIP ROM命令发44H开始转换延时复位发CCH SKIP ROM命令发读存储器命令连续读出两个字节数据(即温度)->结束

  B·总线上挂接多个器件

复位发55H MATCH ROM命令发位地址发44H开始转换命令延时复位发55H MATCH ROM命令发位地址发0BE读存储器命令连续读出两个字节数据(即温度) 复位读下个器件温度

6·接口程序

   A:汇编程序,该程序为总线上只有一个器件

;DIN CONNECT TO DS1820'S DA

;读出的温度值在TEMP中

DIN        EQU     P1.0

TEMP    EQU     30H

TEMP1    EQU     31H

TEMP2    EQU     32H

        ORG     0000H

        SJMP     MAIN

        ORG     0030H

MAIN:    CALL     RDTEM

        SJMP     $

RDTEM: LCALL   RESET                   ;复位

        MOV     A,      #0CCH           ;发SKIP ROM 命令

        LCALL   WRITE

        MOV     A,      #44H            ;发开始温度转换命令

        LCALL   WRITE

        LCALL   DEL1000

        LCALL   RESET

        MOV     A,     #0CCH

        LCALL   WRITE

        MOV     A,      #0BEH           ;发读存储器命令

        LCALL   WRITE

        LCALL   READ

        MOV     TEMP1,  TEMP

        LCALL   READ

        MOV     TEMP2,  TEMP

        RET

RESET:  NOP                ;复位子程序

L0:     CLR     DIN

        MOV     R2, #200

L1:     NOP

        DJNZ    R2,     L1

        SETB    DIN

        MOV     R2,     #30

L4:     DJNZ    R2,     L4

        CLR     C

        ORL     C,      DIN

        JC      L3

        MOV     R6,     #80

L5:     ORL     C,      DIN

        JC      L3

        DJNZ    R6,     L5

        SJMP    L0

L3:     MOV     R2,     #250

L2:     DJNZ    R2,     L2

        RET

WRITE:  MOV     R3,     #8        ;写字节子程序

WR1:    SETB    DIN

        MOV     R4,     #8

        RRC     A

        CLR     DIN

WR2:    DJNZ    R4,     WR2

        MOV     DIN,   C

        MOV     R4,     #20

WR3:    DJNZ    R4,     WR3

        DJNZ    R3,     WR1

        SETB    DIN

        RET

READ:   MOV     R6,     #8        ;读子程序

RE1:    CLR     DIN

        MOV     R4,     #6

        NOP

        SETB    DIN

RE2:    DJNZ    R4,     RE2

        MOV     C,      DIN

        RRC     A

        MOV     R5,     #30

RE3:    DJNZ    R5,     RE3

        DJNZ    R6,     RE1

        MOV     TEMP,   A

        SETB    DIN

        RET

DEL1000:MOV     40H,    #18

DEL100: MOV     41H,    #0FFH

DEL10:  MOV     42H,    #0FFH

DEL1:   DJNZ    42H,    DEL1

        DJNZ    41H,    DEL10

        DJNZ    40H,    DEL100

        RET

B:C语言程序--该程序为总线上只有一个器件

;AT  CONNECT TO DS1820'S DA

;读出的温度值在last中

#include

#define uchar unsigned char

sbit AT = P3^6;

/********** FUNCTION **********/

void dmsec (unsigned int count) {      // mSec Delay 11.0592 Mhz

    unsigned int i;               // 1MS 延时

    while (count--) {

for (i=0;i<125;i++){}

        }

   }

void tmreset (void) {                  // Reset TX

    unsigned int i;

    AT = 0;

i = 103; while (i>0) i--; // Approx 900 uS

    AT = 1;

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

}

void tmpre (void) {                    // Wait for Presence RX

    unsigned int i;

    while (AT);

    while (~AT);

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

}

bit tmrbit (void) {                    // read one bit

    unsigned int i;

    bit dat;

    AT = 0; i++;

    AT = 1; i++; i++;

    dat = AT;

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

    return (dat);

}

unsigned char tmrbyte (void) {         // read one byte

    unsigned char i,j,dat;

    dat = 0;

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

        j = tmrbit ();

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

    }

    return (dat);

}

void tmwbyte (unsigned char dat) {               // write one byte

    unsigned int i;

    unsigned char j;

    bit testb;

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

        testb = dat & 0x01;

dat = dat >> 1;

        if (testb) {

            AT = 0;                           // Write 1

            i++; i++;                            

            AT = 1;

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

        }

        else {

            AT = 0;                           // Write 0

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

            AT = 1;

            i++; i++;                          

        }

    }

}

void tmstart (void) {                  // ds1820 start convert

    tmreset ();

    tmpre ();

    dmsec (1);

    tmwbyte (0xcc);                    // skip rom

    tmwbyte (0x44);                    // convert

}

unsigned char tmrtemp (void) {                  // read temp

    unsigned char a,b,y1,y2,y3;

    tmreset ();

    tmpre ();

    dmsec (1);

    tmwbyte (0xcc);                    // skip rom

    tmwbyte (0xbe);                    // convert

    a = tmrbyte ();                    // LSB

    b = tmrbyte ();                    // MSB

y1=a>>4;

y2=b<<4;

    y3=y1 | y2;

    return(y3);

}

/********** MAIN **********/

void main (void)

 {

    unsigned int i;

    unsigned char last;

    uchar lsb,msb;

    dmsec(1);

    tmstart ();                    // ds1820 start convert

    dmsec(1000);

    last=tmrtemp ();                    // read temperature

       msb=last/0x0a+0x30;

       lsb=last%0x0a+0x30;

    while(1){}      

}下载本文

显示全文
专题