PDA

View Full Version : ASCII HEX Word to Decimal conversion....



johnmaetta
- 16th February 2012, 16:58
Hello,

My program reads a string of ASCII characters via a serial port, such as 07DC. This represents the decimal year (2012).

How do I get the decimal equivalent of 07DC? What i get is decimal 236 on the LCD.


Thanks,

John


INCLUDE "BS1DEFS.BAS"
DEFINE HSER_CLROERR 1 'USART CLEAR OVERFLOW ERROR
DEFINE HSER_TXSTA 24H 'SET USART TX STAUS REG
DEFINE HSER_RCSTA 90H 'SET USART RC STAUS REG
DEFINE HSER_BAUD 9600 'SET USART BAUD RATE
define HSER_SPBRG 25 'SET PIC OSC FOR 9600 BAUD

PRITIM var BYTE (16) 'ALLOCATE 16 BYTES FOR USART RX DATA

YEAR VAR WORD

LCDOUT $FE,1 'CLEAR DISPLAY

'----------------------------------------------------------------
MAIN:

GOSUB LCDMAIN
'--------------------------------------------------
LCDMAIN:
HSEROUT [16,142,171,2,16,3] 'TRANSMIT CMD 8EAB STATUS PACKET
HSERIN [WAIT (56,70,65,66),STR PRITIM\16] 'WAIT FOR 8FAB - STORE PRI/TIM PACKET
YEAR = PRITIM [0] + PRITIM [1] + PRITIM [2] + PRITIM [3]

LCDOUT $FE,1 'CLEAR DISPLAY
LCDOUT DEC YEAR

RETURN

spcw1234
- 16th February 2012, 19:31
The way you are displaying the data works, and I have tested it. The data you are receiving must be wrong. Try to display the raw hex data on the LCD to verify what you are receiving.

readitaloud
- 16th February 2012, 19:33
Hi John,

The statement;
YEAR = PRITIM [0] + PRITIM [1] + PRITIM [2] + PRITIM [3}

will yield YEAR = $30 + $37 + $44 + $43 ' ASCII 07DC

This will set YEAR = $EE which equals 238. Are you sure the value you see on the display is 236 ?

YEAR will equal $00EE in memory.

I am not sure if LCDOUT will support sending WORD values.

- Martin

spcw1234
- 16th February 2012, 19:37
I am not sure if LCDOUT will support sending WORD values.




I wasn't sure about that statement, but the LCDOUT part works fine sending the word variable.

mark_s
- 16th February 2012, 19:43
Is this all your code? I do not see any LCD defines.

johnmaetta
- 16th February 2012, 20:13
I wasn't sure about that statement, but the LCDOUT part works fine sending the word variable.

Not sure what you mean by sending the word variable.

readitaloud
- 17th February 2012, 18:01
John,

You need to convert from ASCII to binary value. 4 bytes become 2 bytes.

Add the following:

' New variables
x var byte
y var byte
i var byte

' ************* ASCII to Binary **************
for i = 0 to 2 step 2
if PRITIM[i] > $40 THEN ' Handle MSNibble.
x = PRITIM[i] + 1
x.3 = 1
x = x << 4
else
x = PRITIM[i]
x = PRITIM[i] << 4
endif
if pritim[i + 1] > $40 then ' Handle LSNibble.
y = PRITIM[i + 1] + 1
y.3 =1
y = y & %00001111
else
y = PRITIM[i + 1]
y = y & %00001111
endif
if i = 0 then
YEAR.byte1 = x | y
else
YEAR.BYTE0 = x | y
endif
next i
'************* END of ASCII to Binary *************
YEAR is now ready to be sent to LCD display.

- Martin

mark_s
- 17th February 2012, 21:38
These displays are ASCII. So no need to convert. Try the line below it will send ASCII $32,$30,$31,32 =(2,0,1,2)to the lcd. You may have to reverse the order PRITIM[X]'s ? Depending on the order you receive the data.

LCDOUT $FE, $80, "YEAR =",PRITIM[3],PRITIM[2],PRITIN[1],PRITIM[0]