PDA

View Full Version : HEX byte to ASCII output...



johnmaetta
- 12th August 2011, 19:57
Hello,

I am using the Dallas 18S20 to measure temperature. My code is working and I can display the decimal temp on the LCD screen.

My problem is:
The 18S20 provides the temp in HEX (example. $31), I convert that to decimal 49. I then divide 49 by 2 and obtain the temprature of 29degs C.

I need to transmit the "2" and "9" in ASCII. How do I strip the "2" and "9" from the decimal byte 29 in (INTTEMP.LOWBYTE / 2)....? :confused:

INTERNALTEMP:
OWOut DQI,1,[$CC,$44] 'REQUEST FOR INT TEMP AD CONVERSION
PAUSE 750 'WAIT FOR CONVERSION TO COMPLETE
OWOut DQI,1,[$CC,$BE] 'REQUEST XMIT OF TEMP DATA
OWIn DQI,0,[INTTEMP.LOWBYTE,INTTEMP.HIGHBYTE] 'REC TEMP DATA
Lcdout $fe, 1, DEC INTTEMP.HIGHBYTE, " ", DEC (INTTEMP.LOWBYTE / 2)
GOTO INTERNALTEMP
end



Thanks,

John

mackrackit
- 12th August 2011, 20:42
Probably a better way...
To strip out place values
code snip from project with four digits.


C_THOS = TempC / 1000
C_HUNS = (TempC - C_THOS * 1000) /100
C_TENS = (TempC-((C_THOS * 1000) + (C_HUNS * 100)))/10
C_ONES = TempC - ((C_THOS * 1000) + (C_HUNS * 100)+ C_TENS * 10)

Then convert to..
Again, snipped from a project using an array.


FAT_src[40] = $30+C_THOS
FAT_src[41] = $30+C_HUNS
FAT_src[42] = $30+C_TENS
FAT_src[43] = $30+C_ONES

Does that help?

johnmaetta
- 12th August 2011, 21:23
Does that help?

Yes it does...thank you!

John

johnmaetta
- 12th August 2011, 21:42
Works great now!



INTERNALTEMP:
OWOut DQI,1,[$CC,$44] 'REQUEST FOR INT TEMP AD CONVERSION
PAUSE 750 'WAIT FOR CONVERSION TO COMPLETE
OWOut DQI,1,[$CC,$BE] 'REQUEST XMIT OF TEMP DATA
OWIn DQI,0,[INTTEMP.LOWBYTE,INTTEMP.HIGHBYTE] 'REC TEMP DATA
IF INTTEMP.HIGHBYTE = $00 THEN INTTEMPSIGN = 43 'CHECK HIGHBYTE SET SIGN +
IF INTTEMP.HIGHBYTE = $FF THEN INTTEMPSIGN = 45 'CHECK HIGHBYTE SET SIGN -
IF INTTEMP.HIGHBYTE = $FF THEN INTTEMP.LOWBYTE = ($FF - INTTEMP.LOWBYTE) 'INVERT LOWBYTE FOR NEG
INTTEMP.LOWBYTE = INTTEMP.LOWBYTE / 2 'DIV BY 2 FOR DEGREES C
INTC_TENS = INTTEMP.LOWBYTE / 10 'CALC TENS DIGIT
INTC_ONES = INTTEMP.LOWBYTE - (INTC_TENS * 10) 'CALC ONES DIGIT
INTTX(1) = $30 + INTC_TENS 'CONVERT TO ASCII
INTTX(2) = $30 + INTC_ONES 'CONVERT TO ASCII
GOTO INTERNALTEMP
end

readitaloud
- 12th August 2011, 22:46
John,
The sensor has .5 degree resolution. You can store INTTEMP.LOWBYTE into a NEW VAR of length WORD then multiply the new variable by 5. In your example 49 X 5 is 245. HEX "31" actually represents +24.5 degrees C. Use "DIG" to isolate the digits of the NEW VAR WORD. WORD size is suitable because the maximum of INTTEMP.LOWBYTE is HEX "AA", 170 X 5 = 850 which represent +85.0 degrees C.

- Martin

johnmaetta
- 13th August 2011, 00:31
Hi Martin,

Thanks for the DIG tip. That eliminates all that math.

Yes, I know I can get the .5 resolution, but I don't need it for this simple project. But, that doesn't mean I won't incorporate it in my code sometime in the future.

Thank again....

John