您的位置:控制工程论坛网论坛 » 教程与手册 » DS18B20温度转换

cqs0088

cqs0088   |   当前状态:在线

总积分:-390  2024年可用积分:0

注册时间: 2008-07-12

最后登录时间: 2012-05-30

空间 发短消息加为好友

DS18B20温度转换

cqs0088  发表于 2008/9/15 14:07:29      601 查看 0 回复  [上一主题]  [下一主题]

手机阅读

DS18B20温度转换
; 以下程序包含两个功能:
; 1) 读 DS18B20 的 64bits ROM,程序中已将此功能注消,需要此功能请取消注释。
; 2) 启动一个 DS18B20 做温度转换(精度为出厂默认值: 0.0625℃),并读取转换值。
; 3) 本程序运行于 AT89C51/52 或 AT89C1051/2051。
; 4) 本程序在 Intel ASM51 VER2.2 编译通过。
;*****************************************

                DQ              BIT     P1.5            ; 1-WIRLE
                STLPW           BIT     P1.4            ; STEAL POWER CONTROL

        READROM        EQU    33H        ; ROM CAMMAND
        MATCHROM    EQU    55H        ;
        SKIPROM        EQU    0CCH        ;
        SEARCHRDH    EQU    0F0H        ;
        ALARMSEARCH    EQU    0ECH        ;

        WRITESCRATCHPAD    EQU    4EH        ; MEMORY COMMAND
        READSCRATCHPAD    EQU    0BEH        ;
        COPYSCRATCHPAD    EQU    48H        ;
        CONVERT        EQU    44H        ;
        RECALLEE    EQU    0B8H        ;
        READPOWER    EQU    0B4H        ;

        FAMILY        EQU    28H        ; SAMPLE 64 BITS ROM
        SN1        EQU    4EH        ;
        SN2        EQU    91H        ;
        SN3        EQU    08H        ;
        SN4        EQU    00H        ;
        SN5        EQU    00H        ;
        SN6        EQU    00H        ;
        CRC        EQU    1EH        ;
;******************************
        ORG    0000H
        LJMP    MAIN


MAIN:

;        LCALL    INIT
;
;        MOV    A,#READROM
;        LCALL    WRWIRE
;        MOV    R0,#20H
;        MOV    R1,#08H
;SCANROM:    LCALL    RDWIRE
;        MOV    @R0,A
;        INC    R0
;        DJNZ    R1,SCANROM
;        NOP            ; INTERRUPTE HERE CHECK SAMPLE DS18B20 64 BITS    ROM.
;
; RAM 27H -> CRC.
; RAM 21H-26H -> 6 BYTES SARIAL NUMBER.
; RAM 20H -> FAMILY NUMBER.(DS18B20 = 28H)



; THE FOLLOWING PROGRAMME IS ONLY USED FOR ANY ONE DS18B20, BUT ONLY ONE.
; YOU CAN MODIFY IT FOR MANY DS18B20, REFER TO DATASHEET OF DS18B20.

SCANTMP:    LCALL    INIT            ; START CONVERT, 0.625‘C/STEP
        MOV    A,#SKIPROM        ;
        LCALL    WRWIRE            ;
        MOV    A,#CONVERT        ;
        LCALL    WRWIRE            ;
        LCALL    T1S            ; WAIT FOR CONVERT COMPLET.

        LCALL    INIT            ; READ TEMPERATURE
        MOV    A,#SKIPROM        ;
        LCALL    WRWIRE            ;
        MOV    A,#READSCRATCHPAD
        LCALL    WRWIRE            ;
        LCALL    RDWIRE            ;
        MOV    DPL,A            ;
        LCALL    RDWIRE            ;
        MOV    DPH,A            ; DPTR IS SAVED DATA OF TEMPERATURE

        MOV    B,#10H            ;
        MOV    A,DPH            ;
        MUL    AB            ;
        MOV    R0,A            ;
        MOV    B,#10H            ;
        MOV    A,DPL            ;
        DIV    AB            ;
        ADD    A,R0            ; EASY TO READ TEMPERATURE FROM ACC
        
        LCALL    INIT
        NOP                ; INTERRUPTE HERE CHECK TEMPERATURE
        AJMP    SCANTMP

;*****SUBSUBSUBSUBSUBSUBSUBSUBSUB*****
;RESET DS18B20.

INIT:
        PUSH    0000H

        CLR    STLPW
        CLR    DQ

        MOV    R0,#0FFH    ; DELAY 512uS
        DJNZ    R0,$

        SETB    DQ
        SETB    STLPW
        MOV    R0,#18H        ; DELAY 50uS
        DJNZ    R0,$
        CLR    STLPW

        JB    DQ,$        ; WAIT FOR DQ LOW
        JNB    DQ,$        ; WAIT FOR DQ HIGHT
        SETB    STLPW

        MOV    R0,#0FFH    ; DELAY 512uS
        DJNZ    R0,$

        POP    0000H        
        REt
;**************************************
; WRITE 1 BYTE TO DS18B20.
; ACC IS SAVED DATA.

WRWIRE:
        PUSH    ACC
        PUSH    0000H
        PUSH    0001H

        MOV    R0,#08H
WRWIRE_1:    RRC    A

        CLR    STLPW    
        CLR    DQ

        MOV    R1,#06H        ; DELAY 14uS
        DJNZ    R1,$        ;

        MOV    DQ,C        ; WRITE DQ

        MOV    R1,#09H        ; DELAY 20uS WAIT FOR DS18B20 SAMPLE
        DJNZ    R1,$        ;

        SETB    DQ
        SETB    STLPW
        MOV    R1,#30H        ; DELAY 100uS FOR RECOVER
        DJNZ    R1,$        ;

        DJNZ    R0,WRWIRE_1

        POP    0001H
        POP    0000H
        POP    ACC
        RET
;***************************************
; READ 1 BYTE TO ACC
; ACC IS USED

RDWIRE:
        PUSH    0000H
        PUSH    0001H

        MOV    R0,#08H

RDWIRE_1:    CLR    STLPW
        CLR    DQ
        MOV    R1,#04H        ; DELAY 10uS
        DJNZ    R1,$        ;
        SETB    DQ        ; READY FOR RECEIVE DS18B20 DQ
        MOV    C,DQ        ; READ DQ TO ACC        
        RRC    A        ;

        SETB    DQ
        SETB    STLPW
        MOV    R1,#30H        ; DELAY 100uS FOR RECOVER
        DJNZ    R1,$

        DJNZ    R0,RDWIRE_1

        POP    0001H
        POP    0000H
        REt
;*****************************************
; DELAY 0.9S-1S

T1S:
        PUSH    0000H
        PUSH    0001H
        PUSH    0002H

        MOV    R2,#07H
T1S_2:        MOV    R1,#0FFH

T1S_1:        MOV    R0,#0FFH    ; 512uS
        DJNZ    R0,$        ;

        DJNZ    R1,T1S_1    ; 514*255+2=131072uS

        DJNZ    R2,T1S_2    ; 131072*7=917504uS

        POP    0002H
        POP    0001H
        POP    0000H
        RET
1楼 0 0 回复