ASCII HEX Word to Decimal conversion....
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
Code:
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
Re: ASCII HEX Word to Decimal conversion....
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.
Re: ASCII HEX Word to Decimal conversion....
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
Re: ASCII HEX Word to Decimal conversion....
Quote:
Originally Posted by
readitaloud
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.
Re: ASCII HEX Word to Decimal conversion....
Is this all your code? I do not see any LCD defines.
Re: ASCII HEX Word to Decimal conversion....
Quote:
Originally Posted by
spcw1234
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.
Re: ASCII HEX Word to Decimal conversion....
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
Re: ASCII HEX Word to Decimal conversion....
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]