十年專注單片機方案開發的方案公司英銳恩,分享用PIC16F877A和DS18B20的測溫程序。英銳恩現提供服務產品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。
MCD-demouse板做了一個0到99度的測溫程序,只要把數據處理程序改一下就可以改變測量范圍和精度。
#INCLUDE
CBLOCK 20H
INPUT_HIGH_8 ;high 8 bit,don't allow other P use it but temperature conversion p
INPUT_LOW_8 ;low 8 bit,don't allow other P use it but temperature conversion p
DELAY_TEMP0
DELAY_TEMP1
PASS_TEMP
UNITS_ORDER
TENS_DIGIT
COUNT_16
ENDC
ORG 00H
NOP
GOTO INITIAL
ORG 05H
TABLE
ADDWF PCL,1
DT 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,90H,90H,90H ;comment anode
DISPLAY
MOVFW UNITS_ORDER
CALL TABLE
MOVWF PORTC
BCF PORTA,0 ;trigger transistor of NPN than display units_order
CALL DELAY_1MS
BSF PORTA,0 ;close
MOVFW TENS_DIGIT
CALL TABLE
MOVWF PORTC
BCF PORTA,1 ;trigger transistor of NPN than display tens_digit
CALL DELAY_1MS
BSF PORTA,1
RETURN
DELAY_1MS
MOVLW 12H
MOVWF DELAY_TEMP0
MOVLW 12H
MOVWF DELAY_TEMP1
DECFSZ DELAY_TEMP1
GOTO $-1
DECFSZ DELAY_TEMP0
GOTO $-5
RETURN
INITIAL
BANKSEL TRISA
MOVLW 00H
MOVWF TRISA ;porta0..1 to launch led, porta2 18b20 communication port
MOVLW B'00000110' ;set porta as digital port
MOVWF ADCON1
MOVLW 00H
MOVWF TRISC ;8 bits led data bus
MOVLW 0FFH ;portb1 as a command key
MOVWF TRISB
MOVLW 00H
MOVWF OPTION_REG ;portb as week pull up
BANKSEL PORTA
CLRF UNITS_ORDER
CLRF TENS_DIGIT
MAIN
CALL DISPLAY ;press key to start the function
BTFSC PORTB,2
GOTO $-2
BTFSS PORTB,2 ;key release?
GOTO $-1
CALL INITIAL_18B20
BTFSS DELAY_TEMP1,1
GOTO MAIN
MOVLW 0CCH ;skip the rom
MOVWF PASS_TEMP
CALL WRITE_18B20
MOVLW 44H ;release the command of conversion
MOVWF PASS_TEMP
CALL WRITE_18B20
MOVLW D'30'
MOVWF COUNT_16
MOVLW D'90' ;delay 900ms for conversion
MOVWF DELAY_TEMP1
MOVLW D'110'
MOVWF DELAY_TEMP0
DECFSZ DELAY_TEMP0
GOTO $-1
DECFSZ DELAY_TEMP1
GOTO $-5
DECFSZ COUNT_16
GOTO $-9
CALL INITIAL_18B20
BTFSS DELAY_TEMP1,1
GOTO MAIN
MOVLW 0CCH
MOVWF PASS_TEMP
CALL WRITE_18B20 ;release the command of reading
MOVLW 0BEH
MOVWF PASS_TEMP
CALL WRITE_18B20
CALL READ_18B20
CALL INITIAL_18B20 ;end the other data to transmit
BTFSS DELAY_TEMP1,1
GOTO MAIN
CALL BCD_CONVERSION
GOTO MAIN
INITIAL_18B20
BCF PORTB,2
BANKSEL TRISA
BCF TRISA,2
BANKSEL PORTA
MOVLW 0BH
MOVWF DELAY_TEMP0
MOVLW 22H
MOVWF COUNT_16
DECFSZ COUNT_16,1
GOTO $-1 ;delay 586us
DECFSZ DELAY_TEMP0,1
GOTO $-5
BANKSEL TRISA
BSF TRISA,2 ;release the bus
BANKSEL PORTA
MOVFW PORTA
MOVLW D'25' ;delay 76us
MOVWF DELAY_TEMP0
DECFSZ DELAY_TEMP0,1
GOTO $-1
BTFSS PORTA,2
GOTO