英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
DS18B20数字温度传感器的C语言程序
[打印本页]
作者:
winnie
时间:
2009-4-25 15:59
标题:
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_REG EQU 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;
COMD EQU 029H;
PDBYTE EQU 02AH
COUNT0 EQU 02BH
TMPO EQU 2CH
INTEGER EQU 02DH
FRACTION EQU 02EH
TEM EQU 02FH
TEM1 EQU 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
GOTO START
ORG 004H ; this is the interrupt vector
GOTO INTERRUPT
; 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
BCF STATUS, 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 ;
;MOVLW 001H
;MOVWF TRISA
;BCF STATUS, 006H
BSF PORTB, 002H
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
BCF PORTB, 002H
CALL CONVERT
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
CALL DELAY500MS
CALL GETTEMP
MOVLW TEMPDATAL
MOVWF PORTB
CALL DELAY500MS
CALL DELAY500MS
MOVLW TEMPDATAH
MOVWF PORTB
CALL DELAY500MS
CALL DELAY500MS
BSF PORTB, 002H
CALL DISPLAYMAIN
ROUND
NOP
NOP
NOP
NOP
GOTO ROUND
;CALL DISPLAY
;------------------------
GETTEMP
CALL SENSOR_RESET
BTFSS PDBYTE, 000H
GOTO GETTEMP
MOVLW 0CCH
MOVWF COMD
CALL WR1820
CLRF COMD
MOVLW 0BEH
MOVWF COMD
CALL WR1820
CLRF COMD
CALL RE1820
MOVF TEM_DATA, 000H
MOVWF TEMPDATAL
CALL RE1820
MOVF TEM_DATA, 000H
MOVWF TEMPDATAH
RETURN
;------------------------
CONVERT
CALL SENSOR_RESET
BTFSS PDBYTE, 000H
GOTO CONVERT
MOVLW 0CCH
MOVWF COMD
CALL WR1820
CLRF COMD
MOVLW 44H
MOVWF COMD
CALL WR1820
RETURN
;---------DS1820---------
SENSOR_RESET
CALL OW_HIZ
CLRF PDBYTE
CALL OW_LO
CALL DELAY500US
CALL OW_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
BCF STATUS, 000H
RRF COMD, 001H
BTFSC STATUS, 000H
BSF PORTA, 005H
CALL DELAY55US
CALL OW_HIZ
NOP
NOP
DECFSZ COUNT2, 001H
GOTO WR0
RETLW 0
;--------------------------
RE1820
CLRF TEM_DATA
MOVLW 8
MOVWF COUNT2
RE0
CALL OW_LO
NOP
NOP
NOP
NOP
NOP
NOP
CALL OW_HIZ
NOP
NOP
NOP
NOP
BCF STATUS, 000H
BTFSC PORTA, 005H
BSF STATUS, 000H
RRF TEM_DATA,001H
CALL DELAY50US
DECFSZ COUNT2,001H
GOTO RE0
RETLW 0
;-------------------------
OW_HIZ BSF 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
DISPLAYEND NOP
RETURN
;***********************************
DISPLAY
; BSF PORTC,04H
; CALL DELAY1MS ;选第一个LED,百位
; CALL DELAY
MOVF BAI,0H
IORLW B'00010000'
MOVWF PORTC
CALL DELAY1MS
NOP
BCF PORTC, 004H
CALL DELAY1MS
NOP
; BSF PORTC,05H
; CALL DELAY1MS ;选第二个LED,十位
MOVF SHI,0H
IORLW B'00100000'
MOVWF PORTC
CALL DELAY1MS
NOP
BCF PORTC, 005H
CALL DELAY1MS
NOP
; BSF PORTC,06H
; CALL DELAY1MS ;选第三个LED,个位
MOVF GE,0H
IORLW B'01000000'
MOVWF PORTC
CALL DELAY1MS
NOP
BCF PORTC, 006H
CALL DELAY1MS
NOP
; BSF PORTC, 007H
; CALL DELAY1MS ;选第四个LED,一位小数
MOVF XIAOSHU,0H
IORLW B'10000000'
MOVWF PORTC
CALL DELAY1MS
NOP
BCF PORTC, 007H
CALL DELAY1MS
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
MOVLW B'01110000'
ANDWF INTEGER,1H
SWAPF TEMPDATAL,1H
MOVF TEMPDATAL,0H
ANDLW B'11110000'
MOVWF FRACTION
MOVF TEMPDATAL,0H
ANDLW B'00001111'
ADDWF INTEGER,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
GOTO SHIWEI
BTFSS SHI,01H
GOTO SHIWEI
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
;*****************************************************
;-------------------------------
DELAY15US NOP
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
GOTO BRA113
DECF COUNT1,1
BTFSS STATUS, 002H
GOTO BRA123
DECF COUNT2,1
BTFSS STATUS, 002H
GOTO BRA133
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
DECFSZ TMPO, 001H
GOTO WAIT5U
RETLW 000H
;*---------------------------
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
GOTO BRA12
DECF COUNT1,1
BTFSS STATUS, 002H
GOTO BRA22
DECF COUNT2,1
BTFSS STATUS, 002H
GOTO BRA32
RETURN
;--------------------------------
END
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2