;******************************************************************* ; Title One Wire temperature reader ; Author Mike Webb ; Date 10th January 2009 ; Pic 10F200 or 12F509 ; ; GG - modified to try and work on 12F629 ; WORKS as at 18h50 Sunday 15th November 2009 !!!! ; ; GG - modified to try and work on 18F4620 ;******************************************************************* errorlevel -302,-224 radix dec list p=18F2620 ; include "p12f629.inc" include "p18f2620.inc" CONFIG MCLRE = ON CONFIG DEBUG = ON CONFIG PWRT = OFF CONFIG OSC = INTIO67 CONFIG BOREN = OFF, BORV = 2 CONFIG WDT = OFF CONFIG CCP2MX = PORTBE CONFIG PBADEN = OFF CONFIG LPT1OSC = OFF CONFIG STVREN = ON, LVP = OFF, XINST = OFF ; CODE PROTECT CONFIG CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF CONFIG CPB = OFF, CPD = OFF CONFIG WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF CONFIG WRTC = OFF, WRTB = OFF, WRTD = OFF CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF CONFIG EBTRB = OFF OW_Bit equ 1 RS_Bit equ 0 #define DataLOW b'11111111' - (1< LastDiscrepency SetMarker movf BitIndex,w movwf Discrepency goto SendRomBit RomIndexEqualLD call SetBit ; RomIndex = LastDiscrepency SendRomBit call GetBit call OWSendCarry incf BitIndex,F btfss BitIndex,6 goto GetBits movf Discrepency,w movwf LastDiscrepency btfsc STATUS,Z bsf b_DONE bsf b_SendROM ; this was a subroutine but due to lack of OWMatch call OWReset ; stack space it was put in line and a flag movlw OWMatchROM ; used to indicate the return address call OWWriteByte movlw ROMBuffer ; =0x20 = 0010 0000 movwf FSR0L SendROM movf INDF0,w call OWWriteByte incf FSR0L,F ; Mike Webb's comment 7654 3210 btfss FSR0L,3 ; dodgey, FSR=0x18 0000 1000 goto SendROM btfss b_SendROM goto Continue ; taken 2nd time around movlw OWConvert call OWWriteByte bcf b_SendROM movlw DataLOW ; setting to output for movwf DS_TRIS ; parasitic power devices bsf DataBIT ; drive data line HIGH by call Delay1 ; wait for conversion bsf DataBIT ; release line movlw DataHIGH ; release data line movwf DS_TRIS ; by setting to input ;---------------------------------------------------------- SendSerial decf FSR0L,F ; FSR=0x28 (was 0x18) when entering ;was btfss FSR,4 ; will be clear when FSR=0x0F btfss FSR0L,5 ; will be clear when FSR=0x0F goto OWMatch swapf INDF0,w call SendNibble ; GG, think this is ROM code movf INDF0,w call SendNibble goto SendSerial Continue movlw OWReadScratchPad call OWWriteByte call SendSpace movlw 0x100-9 movwf FSR0L ;very dodgey - relies on unused bits being 1 ReadLoop call OWReadByte ;but no choice as out of RAM!! swapf OWByte,w call SendNibble ; GG, think this is DATA movf OWByte,w call SendNibble incfsz FSR0L,F goto ReadLoop movlw 0x0D ; send call Send movlw 0x0A call Send ; increment the number of sensors found in EEPROM incf NumberOfSensors,F ; go find another one goto NEXT ; go get next ROM ID ;---------------------------------------------------------- GetBit bsf b_IsGetBit ; Get bit ROMBuffer[BitIndex] goto DoBit ClrBit bsf b_ClearBit ; CLEAR bit ROMBuffer[BitIndex] btfss b_ClearBit SetBit bcf b_ClearBit ; SET bit ROMBuffer[BitIndex] bcf b_IsGetBit DoBit rrcf BitIndex,w movwf OWTemp rrcf OWTemp,F rrcf OWTemp,w andlw 0x07 iorlw ROMBuffer movwf FSR0L movf BitIndex,w andlw 0x07 clrf OWTemp movwf OWCount bsf STATUS,C GetBitLoop rlcf OWTemp,F ; will clear the carry bit decf OWCount,F btfss OWCount,7 goto GetBitLoop movf OWTemp,w btfsc b_IsGetBit goto DoGetBit ; carry must be clear btfsc b_ClearBit goto IsClrBit iorwf INDF0,F retlw 0 IsClrBit xorlw 0xFF andwf INDF0,F retlw 0 DoGetBit andwf INDF0,w btfss STATUS,Z bsf STATUS,C retlw 0 ;---------------------------------------------------------- OWReset movlw DataLOW ; pull line low movwf DS_TRIS bcf DS_PORT,DS_BIT ; pull line low movlw .750/4 ; delay 750uS OWResetDelay movwf temp decfsz temp,w goto OWResetDelay bsf DS_PORT,DS_BIT movlw DataHIGH ; release line movwf DS_TRIS ; movlw .68/4 ; delay 68uS ; movwf temp ; decfsz temp,w ; goto $-2 ; bcf b_OWPresent ; btfss DataBIT ; pulled low? ; bsf b_OWPresent ; yes so chip present ; movlw .480/4 ; delay 480uS movlw .548/4 ; combined times ExitDelay movwf temp decfsz temp,w goto ExitDelay retlw 0 ;---------------------------------------------------------- OWWriteByte movwf OWByte ; store byte to write movlw .8 ; send 8 bits movwf OWCount OWWriteLoop rrcf OWByte,F ; move bit to carry call OWSendCarry ; send it decfsz OWCount,F ; sent all bits goto OWWriteLoop ; no, so loop retlw 0 ;---------------------------------------------------------- OWReadByte movlw .8 ; get 8 bits movwf OWCount OWReadByteLoop call OWReadBit ; returns bit in carry rrcf OWByte,F ; move carry into byte decfsz OWCount,F ; finished? goto OWReadByteLoop ; no, so carry on retlw 0 ;-------------------------------------------------------------------- ;OW_Bit equ 1 ;RS_Bit equ 0 ; DataLOW b'11111111' - (1<0xFFFF) messg "Memory Overflow" endif ;---------------------------------------------------------- ;********************************************************************* ;********************************************************************* END ;********************************************************************* ;*********************************************************************