首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 家电技术 > 单元电路介绍 > 其它电路
DS18B20温度计
来源:本站整理  作者:佚名  2011-02-05 21:44:54



;****DS18B20温度计,注意数据口是P3.7    
;测温范围-55度至+125度 *
        TIMEL          EQU     0E0H        ;20MS,定时器 0 时间常数
        TIMEH        EQU    0B1H        ;
        TEMPHEAD    EQU    36H        ;
CS1     BIT     P2.3
CS2     BIT     P2.4
D_I     BIT     P3.6   ;4
R_W     BIT     P2.5   ;5
E       BIT     P2.6   ;6
DB0_DB7 EQU     P0
X       EQU     22H    ;页地址
Y       EQU     23H    ;列地址  
COM     EQU        24H
DAT     EQU     24H
COUNT   EQU     25H          ;代码倒计数单元            
CODE0   EQU     26H        ;表格起始地址,赋0值后与DPTR合成查表指针地址
MUL_    EQU     26H    ;显示数字时,暂存单元(要显示的数是几就在里面存几)  
        BITST       DATA    20H
        TIME1SOK    BIT    BITST.1
        TEMPONEOK    BIT    BITST.2
        TEMPL        DATA    30H
        TEMPH        DATA    27H
        TEMPHC        DATA    28H
        TEMPLC        DATA    29H
        TEMPDIN     BIT     P3.7            ;
        ORG     0000H
        AJMP    START
        ORG     00BH
        AJMP    T0IT
        ORG     0030H
;       系统初始化
START:           MOV   SP,#60H
                   ACALL INT
                   SETB  CS1
                   SETB  CS2
                   ACALL CLEAR
CLSMEM:          MOV      R0, #20H                ;
                 MOV      R1, #60H                ;
CLSMEM1:         MOV      @R0, #00H               ;
                INC     R0                      ;
                DJNZ    R1, CLSMEM1             ;
                ACALL SY
;
         MOV     TMOD, #00100001B    ; 定时器 0 作方式1 (16BIT)
         MOV     TH0, #TIMEL        ;
         MOV     TL0, #TIMEH        ; 20ms
                SJMP    INIT
;
ERROR:          NOP
                LJMP    START            ;
;
                NOP                ;
INIT:           NOP                ;
        SETB    ET0            ;
        SETB    TR0            ;
         SETB    EA            ;
                 MOV     PSW, #00H        ;
                CLR    TEMPONEOK        ;
                LJMP    MAIN            ;
;       定时器0中断服务程序
T0IT:          PUSH    PSW            ;
         MOV     PSW, #10H        ;
         MOV     TH0, #TIMEH        ;
         MOV     TL0, #TIMEL        ;
        INC    R7            ;
        CJNE    R7, #32H, T0IT1        ;
         MOV     R7, #00H        ;
        SETB    TIME1SOK        ;1S定时到标志
T0IT1:        POP    PSW            ;
        RETI                ;
;    主程序
MAIN:         LCALL    DISP1            ;调用显示子程序
        JNB    TIME1SOK, $        ;
        CLR    TIME1SOK        ;测温每1S一次
                JNB    TEMPONEOK, MAIN2    ;上电时先温度转换一次
                LCALL    READTEMP1        ;读出温度值子程序
                LCALL    CONVTEMP        ;温度BCD 码计算处理子程序
                LCALL   DISPBCD                 ;显示区BCD码温度值涮新子程序
                LCALL   DISP1                   ;消闪烁,显示一次
MAIN2:        LCALL    READTEMP        ;温度转换开始
                SETB    TEMPONEOK        ;
                LJMP    MAIN            ;
;子程序
INITDS1820:    SETB    TEMPDIN            ;
        NOP                ;
        NOP                ;
        CLR    TEMPDIN            ;
         MOV     R6, #0A0H        ; DELAY 480us
        DJNZ    R6, $
                 MOV     R6, #0A0H
                DJNZ    R6, $            ;
        SETB    TEMPDIN            ;
         MOV     R6, #32H        ; DELAY 70us
        DJNZ    R6, $            ;
                 MOV      R6,#3CH
LOOP1820:        MOV     C, TEMPDIN        ;
        JC    INITDS1820OUT        ;
                DJNZ    R6,LOOP1820
                 MOV     R6, #064H        ; DELAY 200us
        DJNZ    R6, $            ;
        SJMP    INITDS1820        ;
        RET                ;
;
INITDS1820OUT:    SETB    TEMPDIN            ;
        RET                ;
;读DS18B20的程序, 从DS18B20中读出一个字节的数据
READDS1820:      MOV     R7, #08H        ;
        SETB    TEMPDIN            ;
        NOP                ;
        NOP                ;
READDS1820LOOP:    CLR    TEMPDIN            ;
        NOP                ;
        NOP                ;
        NOP                ;
        SETB     TEMPDIN            ;
         MOV         R6, #07H        ; DELAY 15us
               DJNZ    R6, $            ;
             MOV      C, TEMPDIN        ;
         MOV     R6, #3CH        ; DELAY 120us
        DJNZ    R6, $            ;
        RRC     A            ;
                SETB     TEMPDIN            ;
        DJNZ    R7, READDS1820LOOP    ;
         MOV     R6, #3CH        ; DELAY 120us
        DJNZ    R6, $            ;
        RET                ;
;写DS18B20的程序, 从DS18B20中写一个字节的数据
WRITEDS1820:     MOV     R7, #08H        ;
        SETB    TEMPDIN            ;
        NOP                ;
        NOP                ;
WRITEDS1820LOP:    CLR    TEMPDIN            ;
         MOV         R6, #07H        ; DELAY 15us
               DJNZ    R6, $            ;
            RRC     A            ;
             MOV     TEMPDIN, C        ;
             MOV     R6, #34H        ; DELAY 104us
            DJNZ    R6, $            ;
            SETB    TEMPDIN            ;
            DJNZ    R7, WRITEDS1820LOP    ;
        RET                ;
;READ TEMP
READTEMP:    LCALL    INITDS1820        ;
         MOV     A, #0CCH        ;
        LCALL    WRITEDS1820        ; SKIP ROM
         MOV     R6, #34H        ; DELAY 104us
        DJNZ    R6, $            ;
         MOV     A, #44H            ;
        LCALL    WRITEDS1820        ; START CONVERSION
         MOV     R6, #34H        ; DELAY 104us
        DJNZ    R6, $        ;
        RET                ;
;
READTEMP1:    LCALL    INITDS1820        ;
         MOV     A, #0CCH        ;
        LCALL    WRITEDS1820        ; SKIP ROM
         MOV     R6, #34H        ; DELAY 104us
        DJNZ    R6, $            ;
         MOV     A, #0BEH        ;
        LCALL    WRITEDS1820        ; SCRATCHPAD
         MOV     R6, #34H        ; DELAY 104us
        DJNZ    R6, $            ;
         MOV     R5, #09H        ;
         MOV     R0, #TEMPHEAD        ;
                 MOV      B,#00H
READTEMP2:    LCALL    READDS1820        ;
         MOV     @R0, A            ;
        INC    R0            ;
READTEMP21:     LCALL    CRC8CAL            ;
        DJNZ    R5, READTEMP2        ;
         MOV     A, B            ;
        JNZ    READTEMPOUT        ;
         MOV     A, TEMPHEAD + 0        ;
         MOV     TEMPL, A        ;
         MOV     A, TEMPHEAD + 1        ;
         MOV     TEMPH, A        ;
READTEMPOUT:    RET                ;
;处理温度 BCD 码子程序
CONVTEMP:     MOV     A, TEMPH        ;
        ANL    A, #80H            ;
        JZ    TEMPC1            ;
        CLR    C            ;
         MOV     A, TEMPL        ;
        CPL    A            ;
        ADD    A, #01H            ;
         MOV     TEMPL, A        ;
         MOV     A, TEMPH        ; -
        CPL    A            ;
        ADDC    A, #00H            ;
         MOV     TEMPH, A        ; TEMPHC HI = 符号位
         MOV     TEMPHC, #0BH        ;
        SJMP    TEMPC11            ;
;
TEMPC1:         MOV     TEMPHC, #0AH        ; +
TEMPC11:         MOV      A,TEMPHC
                SWAP    A
                 MOV      TEMPHC,A
                 MOV     A, TEMPL        ;
        ANL    A, #0FH            ; 乘 0.0625
         MOV     DPTR, #TEMPDOTTAB    ;
         MOV C    A, @A + DPTR        ;
         MOV     TEMPLC, A        ; TEMPLC LOW = 小数部分 BCD
;
         MOV     A, TEMPL        ; 整数部分
        ANL    A, #0F0H        ;
        SWAP    A            ;
         MOV     TEMPL, A        ;
         MOV     A, TEMPH        ;
        ANL    A, #0FH            ;
        SWAP    A            ;
        ORL    A, TEMPL        ;
        LCALL    HEX2BCD1        ;
         MOV     TEMPL, A        ;
        ANL    A, #0F0H        ;
        SWAP    A            ;
        ORL    A, TEMPHC        ; TEMPHC LOW = 十位数 BCD
         MOV     TEMPHC, A        ;
         MOV     A, TEMPL        ;
        ANL    A, #0FH            ;
        SWAP    A            ; TEMPLC HI = 个位数 BCD
        ORL    A, TEMPLC        ;
         MOV     TEMPLC, A        ;
         MOV     A, R7            ;
        JZ    TEMPC12            ;
        ANL    A, #0FH            ;
        SWAP    A            ;
         MOV     R7, A            ;
         MOV     A, TEMPHC        ; TEMPHC HI = 百位数 BCD
        ANL    A, #0FH            ;
        ORL    A, R7            ;
         MOV     TEMPHC, A        ;
TEMPC12:    RET                ;
;小数部分码表
TEMPDOTTAB:    DB    00H, 01H, 01H, 02H, 03H, 03H, 04H, 04H, 05H, 06H
;
        DB    06H, 07H, 08H, 08H, 09H, 09H
;显示区BCD码温度值涮新子程序
DISPBCD:         MOV     A, TEMPLC        ;
        ANL    A, #0FH            ;
         MOV     70H, A                ;
         MOV     A, TEMPLC        ;
        SWAP    A            ;
        ANL    A, #0FH            ;
         MOV     71H, A                ;
         MOV     A, TEMPHC        ;
        ANL    A, #0FH            ;
         MOV     72H, A                ;
         MOV     A, TEMPHC        ;
        SWAP    A            ;
        ANL    A, #0FH            ;
         MOV     73H, A                ;
         MOV     A, TEMPHC        ;
        ANL    A, #0F0H        ;
        CJNE    A, #010H, DISPBCD0    ;
        SJMP    DISPBCD2        ;
;
DISPBCD0:     MOV     A, TEMPHC        ;
        ANL    A, #0FH            ;

        JNZ    DISPBCD2        ; 十位数是零
         MOV     A, TEMPHC        ;
        SWAP    A            ;
        ANL    A, #0FH            ;
         MOV     73H, #0AH            ; 符号位不显示
         MOV     72H, A                ; 十位数显示符号
DISPBCD2:    RET                ;
;单字节 16 进制转 BCD
HEX2BCD1:     MOV     B, #064H        ; 16 进制 -> BCD
        DIV    AB            ; B = A % 100
         MOV     R7, A            ; R7 = 百位数
         MOV     A, #0AH            ;
        XCH    A, B            ;
        DIV    AB            ; B = A % B
        SWAP    A            ;
        ORL    A, B            ;
        RET                ;
;    Calculate CRC-8 Values.  Uses The CCITT-8 Polynomial, Expressed As
;    X^8 + X^5 + X^4 + 1

CRC8CAL:    PUSH    ACC            ;
         MOV     R7, #08H        ; Number Bits In Byte
;
CRC8LOOP1:    XRL    A, B            ; Calculate CRC
        RRC    A            ;  MOV e To Carry
         MOV     A, B            ; Get The Last CRC Value
        JNC    CRC8LOOP2        ; Skip If Data == 0
        XRL    A, #18H            ; Update The CRC Value
;
CRC8LOOP2:    RRC    A            ; Position The New CRC
         MOV     B, A            ; Store The New CRC
        POP    ACC            ; Get The Remaining Bits
        RR    A            ; Position The Next Bit
        PUSH    ACC            ; Save The Remaining Bits
        DJNZ    R7, CRC8LOOP1        ; Repeat For 8 Bits
        POP    ACC            ;
        RET                ;
;显示子程序
;显示数据在70H-73H单元内,用4位LED共阳数码管显示
DISP1:    SETB CS2
         CLR  CS1
          MOV  X,#0BAH                
          MOV  Y,#48H                    
          MOV  DPTR,#DDB 
          MOV  MUL_,72H
         ACALL DISP_
          MOV  X,#0BAH                
          MOV  Y,#50H                    
          MOV  MUL_,71H
         ACALL DISP_
          MOV  X,#0BAH                
          MOV  Y,#60H                    
          MOV  MUL_,70H
         ACALL DISP_

          MOV  X,#0BAH                
          MOV  Y,#58H                    
          MOV  DPTR,#TAB1
          MOV  MUL_,#00H
         ACALL DISP_
         RET
;************************8位字宽显示子程序:  (CODE0待设)**********************
DISP_:   MOV  B,#10H       ;要显示的数是几就用几乘以16,得到基址起始地址  
         MOV  A,MUL_
        MUL AB
         MOV  CODE0,A
         MOV  39H,#02H
         MOV  COUNT,#08H        
LP_:     MOV  COM,X            ;设置页地址
        LCALL INST
         MOV  COM,Y            ;设置列地址
        LCALL INST 
W_:      MOV  A,CODE0                   
         MOV C A,@A+DPTR
         MOV  DAT,A
        LCALL DATA_        
        INC CODE0
        DJNZ COUNT,W_
        DJNZ 39H,LP1_
        RET
LP1_:   INC X
         MOV  COUNT,#08H
        AJMP LP_
;***************************16位字宽显示子程序**********************   
DISP:    MOV  39H,#02H
         MOV  CODE0,#00H
         MOV  COUNT,#10H    ;根据字宽及象素的不同而改变 DISP中的COUNT根据字高及象素改变CODE0    
LP:      MOV  COM,X            ;设置页地址
        LCALL INST
         MOV  COM,Y            ;设置列地址
        LCALL INST 
W:       MOV  A,CODE0                   
         MOV C A,@A+DPTR
         MOV  DAT,A
        LCALL DATA_        
        INC CODE0
        DJNZ COUNT,W
        DJNZ 39H,LP1
        RET
LP1:    INC X
         MOV  COUNT,#10H
        AJMP LP
;***********************以下为字符显示子程序块*************************
SY:        SETB CS1          ;开机   
        CLR  CS2
         MOV  X,#0BAH                   
         MOV  Y,#40H                        
         MOV      DPTR,#TAB2        ;
        CALL    DISP
         MOV  X,#0BAH                       
         MOV  Y,#50H                       
         MOV      DPTR,#TAB3        ;
        CALL    DISP
         MOV  X,#0BAH                
         MOV  Y,#60H                    
         MOV      DPTR,#TAB4       ;
        CALL    DISP
         MOV  X,#0BAH                   
         MOV  Y,#70H                       
         MOV      DPTR,#TAB5        ;   
        CALL    DISP
        CLR CS1
        SETB CS2
         MOV  X,#0BAH                   
         MOV  Y,#40H                        
         MOV      DPTR,#TAB6        ;
        CALL    DISP
         MOV  X,#0BAH                   
         MOV  Y,#70H                        
         MOV      DPTR,#TAB7        ;
        CALL    DISP        
        RET
INST:   LCALL BUSY     ;写指令
        SETB E
        CLR D_I
        CLR R_W
         MOV  DB0_DB7,COM
        CLR E
        RET
DATA_:  LCALL BUSY     ;写数据
        SETB E
        SETB D_I
        CLR R_W
         MOV  DB0_DB7,DAT
        CLR E
        RET
BUSY:   CLR D_I        ;测忙
        SETB R_W
BUSY1:   MOV  DB0_DB7,#0FFH
        SETB E
         MOV  A,DB0_DB7
        CLR E
        JB ACC.7,BUSY1
        RET
CLEAR:      MOV  R4,#00H      ;清屏
CLEAR1:  MOV  A,R4
        ORL A,#0B8H
         MOV  COM,A
        ACALL INST
         MOV  COM,#40H
        ACALL INST
         MOV  R3,#40H
CLEAR2:  MOV  DAT,#00H
        LCALL DATA_
        DJNZ R3,CLEAR2
        INC R4
        CJNE R4,#08H,CLEAR1
        RET
INT:     MOV  COM,#0C0H    ;初始化
        ACALL INST
         MOV  COM,#3FH
        ACALL INST
        RET
;***********************************************
DEL:   MOV  R7,#01H
DEL1:  MOV  R6,#0FFH
DEL2:  MOV  R5,#0FFH
      DJNZ R5,$
      DJNZ R6,DEL2
      DJNZ R7,DEL1
      RET
DDB:
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,010H,010H,0F8H,000H,000H,000H,000H,000H,020H,020H,03FH,020H,020H,000H,000H ; "1" 1
DB 000H,070H,008H,008H,008H,088H,070H,000H,000H,030H,028H,024H,022H,021H,030H,000H ; "2" 2
DB 000H,030H,008H,088H,088H,048H,030H,000H,000H,018H,020H,020H,020H,011H,00EH,000H ; "3" 3
DB 000H,000H,0C0H,020H,010H,0F8H,000H,000H,000H,007H,004H,024H,024H,03FH,024H,000H ; "4" 4
DB 000H,0F8H,008H,088H,088H,008H,008H,000H,000H,019H,021H,020H,020H,011H,00EH,000H ; "5" 5
DB 000H,0E0H,010H,088H,088H,018H,000H,000H,000H,00FH,011H,020H,020H,011H,00EH,000H ; "6" 6
DB 000H,038H,008H,008H,0C8H,038H,008H,000H,000H,000H,000H,03FH,000H,000H,000H,000H ; "7" 7
DB 000H,070H,088H,008H,008H,088H,070H,000H,000H,01CH,022H,021H,021H,022H,01CH,000H ; "8" 8
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,000H,031H,022H,022H,011H,00FH,000H ; "9" 9
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
TAB1:
DB 000H,000H,000H,000H,000H,000H,000H,000H,000H,030H,030H,000H,000H,000H,000H,000H ; "." 0
TAB2:
DB 000H,000H,040H,042H,05CH,048H,040H,040H,07FH,040H,050H,04EH,044H,0C0H,000H,000H ; 
DB 000H,000H,020H,022H,022H,022H,022H,022H,022H,022H,022H,022H,022H,07FH,000H,000H ; "当" 0
TAB3:
DB 008H,008H,0E8H,0A8H,0A9H,0AEH,0EAH,008H,008H,0C8H,00CH,00BH,0EAH,008H,008H,000H ; 
DB 000H,000H,07FH,004H,024H,044H,03FH,000H,000H,01FH,040H,080H,07FH,000H,000H,000H ; "前" 1
TAB4:
DB 000H,010H,02CH,024H,0A4H,064H,025H,026H,024H,024H,0A4H,024H,034H,02CH,004H,000H ; 
DB 040H,040H,048H,049H,049H,049H,049H,07FH,049H,049H,049H,04BH,048H,040H,040H,000H ; "室" 0
TAB5:
DB 010H,021H,086H,070H,000H,07EH,04AH,04AH,04AH,04AH,04AH,07EH,000H,000H,000H,000H ; 
DB 002H,0FEH,001H,040H,07FH,041H,041H,07FH,041H,041H,07FH,041H,041H,07FH,040H,000H ; "温" 0
TAB6:
DB 000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H ; 
DB 000H,000H,036H,036H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H ; ":" 0
TAB7:
DB 000H,000H,0FCH,004H,024H,024H,0FCH,0A5H,0A6H,0A4H,0FCH,024H,024H,024H,004H,000H ; 
DB 080H,060H,01FH,080H,080H,042H,046H,02AH,012H,012H,02AH,026H,042H,0C0H,040H,000H ; "度" 1

        END

关键词:

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)
Copyright © 2007-2017 down.gzweix.Com. All Rights Reserved .
页面执行时间:311,203.10000 毫秒