; bcf BitMask,3 ; do "convert" (BitMask=00000001)
OwMatch
[COLOR=Blue]OwReset[/COLOR] ; reset all 1-wire devices
[COLOR=Blue]OwWrite[/COLOR](OwMatchRom) ; send "match rom" command
[COLOR=Blue]OwWriteBuffer[/COLOR] ; send + print rom serial number
btfsc BitMask,3 ; conversion complete?
goto OwScratch ; yes, branch, else
[COLOR=Blue]OwWrite[/COLOR](OwConvert) ; send "convert" command
[COLOR=Blue]OwPowerBus[/COLOR] ; power OW pin during conversion
[COLOR=Blue]Send232[/COLOR](0x0D) ; send <cr> char
[COLOR=Blue]Send232[/COLOR](0x0A) ; send <lf> char
bsf BitMask,3 ; indicate conversion complete
goto OwMatch ; do "match rom" for OwScratch
OwScratch
[COLOR=Blue]OwWrite[/COLOR](OwReadScratch) ; send "read scratchpad" command
[COLOR=Blue]Send232[/COLOR](' ') ; send space char
; movlw 9 ; instructions not needed
; movwf BitMask ; BitMask = 9 already from above
RdLoop [COLOR=Blue]OwReadByte[/COLOR] ; read + print scratchpad byte
decfsz BitMask,F ; all 9 bytes read + printed?
goto RdLoop ; no, branch, else
goto OwSearchNext ; search and process next device
OwWriteBuffer macro
local loop
bcf FSR,3 ; reset FSR = &RomBuffer (0x10)
loop movf INDF,W ;
call Ow.WriteByte ; send rom serial number byte
btfsc BitMask,3 ; OwConvert pass? yes, skip, else
call PutHex ; print rom ID byte as hex
incf FSR,F ;
btfss FSR,3 ;
goto loop ;
endm
FracDigit
movlw 0x0f
andwf TempFrac,F
clrc
rlf TempFrac,W ;*2
movwf temp
rlf temp,W ;*4
addwf TempFrac,F ;*5
rlf TempFrac,F ;*10
swapf TempFrac,W
goto PutNybble
I have not seen this sort of byte hording since collage days. Nice work.
OwMatch
OwReset ; reset all 1-wire devices
OwWrite(OwMatchRom) ; send "match rom" command
OwWriteBuffer ; send + print rom serial number
btfsc BitMask,2 ; conversion complete?
goto OwScratch ; yes, branch, else
OwWrite(OwConvert) ; send "convert" command
OwPowerBus ; power OW pin during conversion
bcf DataBit
bsf BitMask,2 ; indicate conversion complete
goto OwMatch ; do "match rom" for OwScratch
OwScratch
Send232(' ') ; send <space> char
OwWrite(OwReadScratch) ; send "read scratchpad" command
OwReadByte
movfw OwByte
movwf TempLo
OwReadByte
movfw OwByte
movwf TempHi
ReadLoop
OwReadByte
decfsz BitMask,F
goto ReadLoop
movf RomBuffer,W ; get Family ID byte
xorlw 0x28 ; is it 12 bit DS18B20?
bz OwTemperature ; yes, branch, else
rlf TempLo,F ; |
rlf TempHi,F ;
rlf TempLo,F ;
rlf TempHi,F ;
rlf TempLo,F ;
rlf TempHi,F ;
movlw 0xF0 ;
andwf TempLo,F ;
movlw 16 ;
movwf temp ;
movf OwByte,W ; Count_Remain
subwf temp,W ;
iorwf TempLo,F ; now DS18B20 12 bit format
movlw 4
subwf TempLo,F
skpc
decf TempHi,F
OwTemperature
OwReadByte
OwReadByte
Send232 " "
btfss TempHi,7 ;is it negative
goto NoNegate
Send232 '-' ;Send minus sign
comf TempLo,F ;and negate temperature
comf TempHi,F
incfsz TempLo,F
goto NoNegate
incf TempHi,F
NoNegate
movfw TempLo ;keep fraction part
movwf TempFrac ;and move it to seperate var
movlw 0x0f
andwf TempHi,F ;move nibbles around so
swapf TempLo,F ;that the temperature is
andwf TempLo,F ;contained in TempLo
swapf TempHi,W
iorwf TempLo,F
clrf TempHi ;will contain tens digit
movlw 0x100-.100
addwf TempLo,W
bnc NoHundreds
movwf TempLo
Send232 '1'
bsf BitMask,0 ;no longer suppressing zeros
NoHundreds
movlw .10
subwf TempLo,F
incf TempHi,F
bc NoHundreds
decfsz TempHi,F ;is it zero
goto PrintIt ;no so print it
btfss BitMask,0 ;are we suppressing zeros
goto SkipZero ;yes so skip it
PrintIt movlw '0'
addwf TempHi,W ;print 10s digit
call Put232
SkipZero movlw '0'+10
addwf TempLo,W
call Put232 ;always print units digit
Send232 '.'
call FracDigit
call FracDigit
call FracDigit
call FracDigit
Send232(0x0D) ; send <cr> char
Send232(0x0A) ; send <lf> char
goto OwSearchNext ; search and process next device
FracDigit
movlw 0x0f
andwf TempFrac,F
clrc
rlf TempFrac,W ;*2
movwf temp
rlf temp,W ;*4
addwf TempFrac,F ;*5
rlf TempFrac,F ;*10
swapf TempFrac,W
goto PutNybble
OwMatch
[COLOR=Blue]OwReset[/COLOR] ; reset all 1-wire devices
[COLOR=Blue]OwWrite[/COLOR](OwSkipRom) ; send "skip rom" command
[COLOR=Blue]OwWrite[/COLOR](OwConvert) ; send "convert" command
[COLOR=Blue]OwPowerBus[/COLOR] ; power OW pin during conversion
[COLOR=Blue]Send232[/COLOR](0x0D) ; send <cr>
[COLOR=Blue]Send232[/COLOR](0x0A) ; send <lf>
[COLOR=Blue]OwReset[/COLOR] ; reset all 1-wire devices
[COLOR=Blue]OwWrite[/COLOR](OwMatchRom) ; send "match rom" command
[COLOR=Blue]OwWriteBuffer[/COLOR] ; send and print rom serial number
[COLOR=Blue]OwWrite[/COLOR](OwReadScratch) ; send "read scratchpad" command
I just though it would be nice if the Host application on the PC could always find the nine character temperature string in the same place; str[x+0] == " " or "-", str[x+1] == " " or "1" (hundreds), str[x+2] == " " or "n" (tens), str[x+3] == "n" (ones), str[x+4] == ".", etc.As for the leading spaces, you're just never satisfied are you?
Your original program occupies a place of honor in my work folder and so I have easy access to it and I see the reference for turning off the comparator. Thank you.BTW, if you use a 10F206 then you need to turn off the comparator. It was in the original code but got optimized.
[COLOR=DarkOrchid] [COLOR=Blue] clrf TempHi ; will contain tens digit
bsf BitMask,5 ; BitMask = 0x20 = " "
Bin2Dec ; print values " 0".."125"
movlw 10 ;
subwf TempLo,F ;
incf TempHi,F ;
bc Bin2Dec ;
addwf TempLo,F ; fix "ones", 0x00..0x09
decf TempHi,F ; fix "tens", 0x00..0x0C
movlw 6 ;
addwf TempHi,W ; packed BCD, 0x00..0x09?
skpndc ; yes, skip, no digit carry, else
movwf TempHi ; packed BCD, 0x10..0x12
swapf TempHi,W ; get hundreds digit
call PutDigit ; prints " " or "1"
movf TempHi,W ; get tens digit
call PutDigit ; prints " " or "0".."9"
movf TempLo,W ; get ones digit
call PutOnes ; always print ones, "0".."9"[/COLOR][/COLOR]
Send232 '.' ;
call PutFraction ;
call PutFraction ;
call PutFraction ;
call PutFraction ;
[COLOR=Black]Send232[/COLOR](0x0D) ; send <cr> char
[COLOR=Black]Send232[/COLOR](0x0A) ; send <lf> char
goto OwSearchNext ; search and process next device
[COLOR=Blue]PutDigit
andlw 0x0F ;
skpz ; zero? yes, skip, else
PutOnes bsf BitMask,4 ; BitMask = 0x30 = "0"
iorwf BitMask,W ; " " or "0".."9"
goto Put232 ; print digit[/COLOR]
;
123.1234 < no space
12.1234
1.1234
- 1.1234
123.1234 < leading space
12.1234
1.1234
-1.1234
123.1234
12.1234
0.1234
- 0.1234
- 1.1234
- 12.1234
123.1234°C 123.1234°C
12.1234°C 12.1234°C
1.1234°C 1.1234°C
-1.1234°C - 1.1234°C
-12.1234°C -12.1234°C
PutFraction
movlw 0x0F ;
andwf TempLo,F ; toss integer, leave remainder
[COLOR=Blue] movf TempLo,W ;
addwf TempLo,F ; *2, C=0
rlf TempLo,F ; *4, C=0
addwf TempLo,F ; *5, C=0
rlf TempLo,F ; *10
[/COLOR] swapf TempLo,W ; integer portion in lo nybble
PutDigit
andlw 0x0F ;
skpz ; zero? yes, skip, else
PutOnes bsf BitMask,4 ; BitMask = 0x30 = "0"
iorwf BitMask,W ; " " or "0".."9"
goto Put232 ; print digit
10SSSSSSSSSSSSCC TTTTTTTTTTTTTTTTCC 125.0000°C
28SSSSSSSSSSSSCC TTTTTTTTTTTTTTTTCC 23.1234°C
28SSSSSSSSSSSSCC TTTTTTTTTTTTTTTTCC 0.1234°C
10SSSSSSSSSSSSCC TTTTTTTTTTTTTTTTCC - 4.1234°C
10SSSSSSSSSSSSCC TTTTTTTTTTTTTTTTCC - 24.1234°C
I've included the code that subtracts 1/4° from the sign extended two's complement temperature value for DS1820/DS18S20 devices but I'm still not convinced it's necessary. This effectively adds 1/4° to a negative temperature or subtracts 1/4° from a positive temperature. Why would this be necessary? Is it because the 1/2° bit in the original 9-bit temperature value is "rounded up" or "rounded down" based on the value in the Count_Remain byte? I'm not using the 1/2° bit, just the 4 bit Count_Remain value, so do you think it's still necessary to subtract 1/4°?
FracDigit
movlw 0x0f
andwf TempFrac,F
movf TempFrac,W
addwf TempFrac,F ;*2 C=0
rlf TempFrac,F ;*4
addwf TempFrac,F ;*5
rlf TempFrac,F ;*10
swapf TempFrac,W
PutDigit bsf BitMask,5 ; BitMask = 0x20 = " "
andlw 0x0F ;
skpnz ; zero? yes, skip, else
goto PutIt
PutOnes btfsc BitMask,4 ;done leading zeros
goto PutIt ;yes so just print it
movwf FSR ;skip zeros over - keep copy of W
movlw '-' ;print minus maybe
btfss STATUS,7 ;was it minus
movlw ' ' ;No, so print space instead
call Put232
movfw FSR
andlw 0x0F ;and out top bits from FSR
bsf BitMask,4 ; BitMask = 0x30 = "0"
PutIt iorwf BitMask,W ; " " or "0".."9"
goto Put232 ; print digit
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?