DS18B20数字温度传感器的C语言程序
;---------------------------------; 单线通信的温度IC
; DS1820
; 实用程序
PROCESSOR PIC16F873PORTA EQU 005H ;assembler constants
PORTB EQU 006H
PORTC EQU 007H
STATUS EQU 003H
ADCON1 EQU 09FH
TRISA EQU 085H
TRISB EQU 086H
TRISC EQU 087H
OPTION_REGEQU 081H
INTCON EQU 00BH
PIR1 EQU 00CH
TMR0 EQU 001H
COUNT EQU 020H; ; program variables in RAM
COUNT1 EQU 021H;
COUNT2 EQU 022H;
COUNT3 EQU 023H;
COUNT4 EQU 024H;
COUNT5 EQU 025H;
TEM_DATA EQU 026H;
TEMPDATAL EQU 027H;
TEMPDATAH EQU 028H;
COMDEQU 029H;
PDBYTEEQU 02AH
COUNT0 EQU 02BH
TMPO EQU2CH
INTEGER EQU 02DH
FRACTION EQU 02EH
TEM EQU 02FH
TEM1EQU 03AH
BAI EQU 030H
SHI EQU 031H
GE EQU 032H
XIAOSHU EQU 033H
X_CNT EQU 034H
RcvReg EQU 035H
RevReg EQU 036H
XmtReg EQU 037H
DCNT EQU 038H
R_CNT EQU 039H
;************************************************
ORG 000H ; define program to goto start
GOTOSTART
ORG 004H ; this is the interrupt vector
GOTOINTERRUPT
; Initialisiing Procedure
;----------------------
;********************************
START NOP
BCF STATUS, 005H ;SELECT BANK0
BCF STATUS, 006H
CLRF PORTA
CLRF PORTB
MOVLW 000H
MOVWF INTCON;DISABLE INTERRRUPT
BSF STATUS, 05H ; SELECT BANK1
BCF STATUS, 06H
MOVLW 006H
MOVWF ADCON1;CONFIGURE PORTA AS DIGITAL
MOVLW 000H
MOVWF TRISC ; SET ALL PORTB OUTPUT
MOVLW 000H
MOVWF TRISB ; SET ALL PORTB OUTPUT
MOVLW 000H
MOVWF TRISA ; SET ALL PORTB OUTPUT
BSF TRISA, 005H ; SET PORTA.1 AS INPUT FOR SWITCH
; BSF TRISA, 002H ; SET PORTA.2 AS INPUT FOR SWITCH
BCFSTATUS, 005H; SELECT BANK0
MOVLW 000H ;DISABLE ALL INTERRUPT
MOVWF INTCON
BCF STATUS, 007H; CLEAR IRP
CLRF PORTA
CLRF PORTB
MOVLW 000H
MOVWF PORTC
CLRF TEM_DATA
CLRF TEMPDATAL
CLRF TEMPDATAH
SENSOR
;BSF STATUS, 05H;
;MOVLW001H
;MOVWFTRISA
;BCFSTATUS, 006H
BSFPORTB, 002H
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
BCFPORTB, 002H
CALL CONVERT
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
CALL GETTEMP
MOVLW TEMPDATAL
MOVWFPORTB
CALL DELAY500MS
CALL DELAY500MS
MOVLW TEMPDATAH
MOVWFPORTB
CALL DELAY500MS
CALL DELAY500MS
BSFPORTB, 002H
CALL DISPLAYMAIN
ROUND
NOP
NOP
NOP
NOP
GOTO ROUND
;CALLDISPLAY
;------------------------
GETTEMP
CALL SENSOR_RESET
BTFSS PDBYTE, 000H
GOTOGETTEMP
MOVLW0CCH
MOVWFCOMD
CALL WR1820
CLRF COMD
MOVLW0BEH
MOVWFCOMD
CALL WR1820
CLRF COMD
CALL RE1820
MOVF TEM_DATA, 000H
MOVWFTEMPDATAL
CALL RE1820
MOVF TEM_DATA, 000H
MOVWFTEMPDATAH
RETURN
;------------------------
CONVERT
CALL SENSOR_RESET
BTFSS PDBYTE, 000H
GOTO CONVERT
MOVLW0CCH
MOVWFCOMD
CALL WR1820
CLRF COMD
MOVLW44H
MOVWFCOMD
CALL WR1820
RETURN
;---------DS1820---------
SENSOR_RESET
CALLOW_HIZ
CLRFPDBYTE
CALLOW_LO
CALL DELAY500US
CALLOW_HIZ
CALL DELAY70US
BTFSS PORTA, 005H
INCF PDBYTE, 001H
CALL DELAY400US
RETURN
;-------------------------
WR1820
MOVLW 8
MOVWF COUNT2
WR0
CALL OW_LO
NOP
NOP
NOP
NOP
NOP
BCFSTATUS, 000H
RRFCOMD, 001H
BTFSC STATUS, 000H
BSF PORTA, 005H
CALL DELAY55US
CALLOW_HIZ
NOP
NOP
DECFSZCOUNT2, 001H
GOTOWR0
RETLW 0
;--------------------------
RE1820
CLRF TEM_DATA
MOVLW 8
MOVWF COUNT2
RE0
CALLOW_LO
NOP
NOP
NOP
NOP
NOP
NOP
CALL OW_HIZ
NOP
NOP
NOP
NOP
BCFSTATUS, 000H
BTFSCPORTA, 005H
BSFSTATUS, 000H
RRF TEM_DATA,001H
CALL DELAY50US
DECFSZCOUNT2,001H
GOTORE0
RETLW 0
;-------------------------
OW_HIZBSF STATUS, 005H
BSF TRISA, 005H
BCF STATUS, 005H
RETURN
;****************************
OW_LO BCF STATUS, 005H
BCF PORTA, 005H
BSF STATUS, 005H
BCF TRISA, 005H
BCF STATUS, 005H
RETURN
;-------------------------------
DISPLAYMAIN
BTFSC TEMPDATAH,07H
goto NEGATIVE
;温度是正
CALL EXCHANGE
CALL HEXBDC
CALL DISPLAY
GOTO DISPLAYEND
;温度是负
NEGATIVE
CALL EXCHANGE
COMF INTEGER
MOVLW 01H
ADDWF INTEGER,
MOVLW B'01111111'
ANDWF INTEGER,1H
COMF FRACTION
MOVLW B'00010000'
ADDWF FRACTION,
MOVLW B'11110000'
ANDWF INTEGER,1H
CALL HEXBDC
MOVLW 0AH
MOVWF BAI
CALL DISPLAY
DISPLAYENDNOP
RETURN
;***********************************
DISPLAY
;BSF PORTC,04H
; CALLDELAY1MS;选第一个LED,百位
;CALL DELAY
MOVFBAI,0H
IORLW B'00010000'
MOVWF PORTC
CALLDELAY1MS
NOP
BCF PORTC, 004H
CALLDELAY1MS
NOP
;BSF PORTC,05H
;CALLDELAY1MS ;选第二个LED,十位
MOVFSHI,0H
IORLWB'00100000'
MOVWFPORTC
CALLDELAY1MS
NOP
BCF PORTC, 005H
CALLDELAY1MS
NOP
;BSF PORTC,06H
; CALLDELAY1MS ;选第三个LED,个位
MOVFGE,0H
IORLW B'01000000'
MOVWF PORTC
CALLDELAY1MS
NOP
BCF PORTC, 006H
CALLDELAY1MS
NOP
; BSF PORTC, 007H
; CALL DELAY1MS ;选第四个LED,一位小数
MOVFXIAOSHU,0H
IORLW B'10000000'
MOVWF PORTC
CALLDELAY1MS
NOP
BCF PORTC, 007H
CALLDELAY1MS
NOP
RETURN
;**************************************
EXCHANGE
MOVF TEMPDATAH,0 ;变换位置,INTEGER存放整数,FRACTION存放小数
ANDLW B'00000111'
MOVWF INTEGER
RLF INTEGER,1H
RLF INTEGER,1H
RLF INTEGER,1H
RLF INTEGER,1H
MOVLWB'01110000'
ANDWF INTEGER,1H
SWAPF TEMPDATAL,1H
MOVF TEMPDATAL,0H
ANDLW B'11110000'
MOVWF FRACTION
MOVF TEMPDATAL,0H
ANDLW B'00001111'
ADDWFINTEGER,1H
RETURN
;******************************************
HEXBDC
CLRF BAI
CLRF SHI
CLRF GE
CLRF XIAOSHU
MOVF INTEGER,0H
MOVWF TEM
SHIWEI
MOVLW 0AH
SUBWF TEM,0H
BTFSS STATUS,0H
GOTO GEWEI
MOVWF TEM
MOVLW 01H
INCF SHI,1H
BTFSS SHI,3H
GOTOSHIWEI
BTFSS SHI,01H
GOTOSHIWEI
CLRF SHI
INCF BAI,01H
GOTO SHIWEI
GEWEI
MOVF TEM,0
MOVWF GE
BTFSS FRACTION,7H
GOTO XIAOSHU0
BTFSS FRACTION,6H
GOTO XIAOSHU1
MOVLW 8H
MOVWF XIAOSHU
GOTO JIESU
XIAOSHU0
BTFSS FRACTION,6H
GOTO XIAOSHU2
MOVLW 3H
MOVWF XIAOSHU
GOTO JIESU
XIAOSHU1
MOVLW 5H
MOVWF XIAOSHU
GOTO JIESU
XIAOSHU2
MOVLW 00H
MOVWF XIAOSHU
JIESU
RETURN
;*****************************************************
;-------------------------------
DELAY15USNOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
RETURN
;**************************
DELAY1MS MOVLW 002H ; delay program for 1M sec
MOVWF COUNT2
BRA133 MOVLW 0FFH
MOVWF COUNT1
BRA123 MOVLW 002H
MOVWF COUNT
BRA113 DECF COUNT,1
BTFSS STATUS, 002H
GOTOBRA113
DECF COUNT1,1
BTFSS STATUS, 002H
GOTOBRA123
DECF COUNT2,1
BTFSS STATUS, 002H
GOTOBRA133
RETURN
;--------------------------------------------------
DELAY10MS CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
CALL DELAY1MS
RETURN
;*****************************************
INTERRUPT
GOTO START
;----------------------------
;---------------------------------
DELAY500US
MOVLW(495/5)-1
MOVWF TMPO
CALL WAIT5U
RETURN
;---------------------------
DELAY70US
MOVLW(65/5)
MOVWF TMPO
CALL WAIT5U
RETURN
;------------------------------------
DELAY400US
MOVLW(395/5)
MOVWF TMPO
CALL WAIT5U
RETURN
;-------------------------
DELAY55US
MOVLW(50/5)
MOVWF TMPO
CALL WAIT5U
RETURN
;---------------------
DELAY50US
MOVLW(45/5)
MOVWF TMPO
CALL WAIT5U
RETURN
;-------------------------------------
;**************************************************
WAIT5U NOP
NOP
DECFSZTMPO, 001H
GOTO WAIT5U
RETLW000H
;*---------------------------
DELAY500MS MOVLW 005H ; delay program for 0.5 sec
MOVWF COUNT2
BRA32 MOVLW 0FFH
MOVWF COUNT1
BRA22 MOVLW 0FFH
MOVWF COUNT
BRA12 DECF COUNT,1
BTFSS STATUS, 002H
GOTOBRA12
DECF COUNT1,1
BTFSS STATUS, 002H
GOTOBRA22
DECF COUNT2,1
BTFSS STATUS, 002H
GOTOBRA32
RETURN
;--------------------------------
END
页:
[1]