以下是一個0到99度的測溫程序,只要把數(shù)據(jù)處理程序改一下就可以改變測量范圍和精度。 #INCLUDE <P16F877A.INC> 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 $+3 BCF DELAY_TEMP1,1 ;not respondence GOTO $+2 BSF DELAY_TEMP1,1 MOVLW D'160' ;delay 480us MOVWF DELAY_TEMP0 DECFSZ DELAY_TEMP0,1 GOTO $-1 BSF DELAY_TEMP1,1 ;the slave device responded and set the bit of flag RETURN WRITE_18B20 MOVLW 08H MOVWF DELAY_TEMP0 BCF STATUS,C WR_LOOP1 BCF PORTA,2 BANKSEL TRISA BCF TRISA,2 BANKSEL PORTA NOP NOP RRF PASS_TEMP,1 BTFSS STATUS,C GOTO WR_ZERO BANKSEL TRISA BSF TRISA,2 BANKSEL PORTA WR_ZERO MOVLW D'25' ;delay 75us MOVWF DELAY_TEMP1 DECFSZ DELAY_TEMP1 GOTO $-1 BANKSEL TRISA BSF TRISA,2 BANKSEL PORTA NOP NOP DECFSZ DELAY_TEMP0,1 GOTO WR_LOOP1 RETURN READ_18B20 MOVLW D'16' MOVWF DELAY_TEMP0 READ_LOOP2 BCF PORTA,2 BANKSEL TRISA BCF TRISA,2 BANKSEL PORTA NOP NOP NOP NOP NOP BANKSEL TRISA BSF TRISA,2 BANKSEL PORTA NOP NOP NOP NOP BTFSS PORTA,2 GOTO $+3 BSF STATUS,C GOTO $+2 BCF STATUS,C RRF INPUT_HIGH_8,1 RRF INPUT_LOW_8,1 MOVLW D'20' ;delay 60us MOVWF DELAY_TEMP1 DECFSZ DELAY_TEMP1,1 GOTO $-1 DECFSZ DELAY_TEMP0,1 GOTO READ_LOOP2 RETURN BCD_CONVERSION MOVLW 08H MOVWF COUNT_16 MOVLW 0FH ANDWF INPUT_HIGH_8,1 SWAPF INPUT_HIGH_8,1 MOVLW 0F0H ANDWF INPUT_LOW_8,1 SWAPF INPUT_LOW_8,1 MOVFW INPUT_LOW_8 ADDWF INPUT_HIGH_8,1 ;save 8bits data in input_high_8 CLRF INPUT_LOW_8 BCF STATUS,C LOOP_8TIMES RLF INPUT_HIGH_8,1 RLF INPUT_LOW_8,1 DECFSZ COUNT_16 GOTO $+2 GOTO SEPARATE_2RS MOVLW INPUT_LOW_8 MOVWF FSR CALL ADJ_BCD GOTO LOOP_8TIMES ADJ_BCD MOVLW 03H ADDWF INDF,W MOVWF TENS_DIGIT BTFSC TENS_DIGIT,3 MOVWF INDF ;if the low four bits plus 3 is more than 7, MOVLW 30H ;add 3,if less than 7,do not change it ADDWF INDF,W ;if the high four bits plus 3 is more than 7,than MOVWF TENS_DIGIT BTFSC TENS_DIGIT,7 ;add 3,if less than 7,do not change it MOVWF INDF RETURN SEPARATE_2RS MOVFW INPUT_LOW_8 MOVWF UNITS_ORDER MOVWF TENS_DIGIT MOVLW 0FH ANDWF UNITS_ORDER,1 MOVLW 0F0H ANDWF TENS_DIGIT,1 SWAPF TENS_DIGIT,1 RETURN END
|