ChriX
Member
I posted a long time ago about the DS1820 sensors, I could never get the timing right so gave up on them. I managed to get the things to respond this time around though, and have been firing the byte that comes from the DS1820 into the serial port of my pc so I can see it on hyperterminal.
A problem has arisen when trying to convert the byte given by the sensor into ascii characters to send back to the PC. For now I have been rotating right to cut off the bit that represents .5 for simplicity, but I still can't get my head around how to do it. I've been trying to convert to BCD, then adding 30h to each nibble to convert the ascii character. I can find loads of code, but nothing that actually explains how to do it, I find reading other people's assembly really hard for some reason (unless commented well).
I'll try and explain the way I have been doing it. I've got a loop with a count (starting at 8 ), and bit 0 is tested. If it is set, subroutines are called with data tables that contain the amount of ones and the amount of tens for the value of that bit. I.e. 1 in the tens table and 6 in the ones table for bit 4, then I use the wcount value to jump to the appropriate position in the table, and add up all the ones and all the tens in the byte.
The routine BTOBCD should be called with the byte you want to change in BCDTEMP. The routine is incomplete because I can't get it to respond how I want even removing all the subtract ten from the ones and add to the tens section. If I put the binary for one in BCDTEMP, the sub returns with the ascii for 0 and 7 the first time it's called, then 0 and 8 the second time.
Hope I have provided enough info for some help, as I am really running out of ideas! If my idea of doing it is complete and utter rubbish let me know!
Thanks.
A problem has arisen when trying to convert the byte given by the sensor into ascii characters to send back to the PC. For now I have been rotating right to cut off the bit that represents .5 for simplicity, but I still can't get my head around how to do it. I've been trying to convert to BCD, then adding 30h to each nibble to convert the ascii character. I can find loads of code, but nothing that actually explains how to do it, I find reading other people's assembly really hard for some reason (unless commented well).
I'll try and explain the way I have been doing it. I've got a loop with a count (starting at 8 ), and bit 0 is tested. If it is set, subroutines are called with data tables that contain the amount of ones and the amount of tens for the value of that bit. I.e. 1 in the tens table and 6 in the ones table for bit 4, then I use the wcount value to jump to the appropriate position in the table, and add up all the ones and all the tens in the byte.
Code:
ADDONES
movf WCOUNT,W
addwf PCL,F
dt 8,4,2,6,8,4,2,1 ; amount of ones for each bit value (backwards)
; 1,2,4,8,16,32,64,128
ADDTENS
movf WCOUNT,W
addwf PCL,F
dt 2,6,3,1,0,0,0,0 ; amount of tens for each bit value (backwards)
BTOBCD
movlw 0x08
movwf WCOUNT ; Move 8 into wcount to limit the amount of times we go round
movlw 0x00
BTOBCDL
btfsc BCDTEMP,0 ; Test bit in BCDTEMP
call ADDONES ; if it is set, go to the addones thing and get the amount of ones
addwf BCDONES,F ; add the returned table value to the bcdones reg
movlw 0x00
btfsc BCDTEMP,0 ; do the same for the tens
call ADDTENS
addwf BCDTENS,F
movlw 0x00
rrf BCDTEMP,1 ; rotate bcdtemp right one, chop off the bit we've done
decfsz WCOUNT,1 ; decrement wcount, so that a different table value is returned
goto BTOBCDL ; if not 0 all bits not done so go back
movf BCDONES,W
movwf TEMP
movlw 0x0A ; subtract 10 from bcdones, if it goes negative don't increment
subwf BCDONES,F
btfss STATUS,C
incf BCDTENS
btfss STATUS,C ;
clrf BCDONES
movlw 0x30 ; add 30h to each register to turn it into ascii
addwf BCDONES,F
movlw 0x30
addwf BCDTENS,F
return
The routine BTOBCD should be called with the byte you want to change in BCDTEMP. The routine is incomplete because I can't get it to respond how I want even removing all the subtract ten from the ones and add to the tens section. If I put the binary for one in BCDTEMP, the sub returns with the ascii for 0 and 7 the first time it's called, then 0 and 8 the second time.
Hope I have provided enough info for some help, as I am really running out of ideas! If my idea of doing it is complete and utter rubbish let me know!
Thanks.