View Full Version : Need help sending a hex serial output
cruzn27
- 3rd December 2009, 04:11
HI Guys,
I'm trying to send out a hex serial output using an array. My hex is 4 digits (ex. 0000). Here is part of my program:
TRISA.2 = 1
A2 VAR TRISA.2
COM_CODE VAR Byte[16]
I VAR Byte
COM_CODE[0]=$0000
Loop:
IF A2==1 THEN
I=0
ENDIF
GOSUB Data_file
GOTO Loop
Data_file:
SEROUT2 PORTB.2,19697,[COM_CODE[I]]
Return
I'm using a 16f88 pic and I think i can only run 8 binary lines but i need to run 16. is there a line that i can use to make it run 16? Do I need to change the com_code var line also? I really need some help
Thanks
Kamikaze47
- 3rd December 2009, 10:42
com_code is a byte variable, so it can only store one byte in each array segment:
like this for example:
COM_CODE[0]=$00
COM_CODE[1]=$00
COM_CODE[2]=$FF
COM_CODE[3]=$8E
COM_CODE[4]=$1A
and so on...
to send the whole array via serial use this line
SEROUT2 PORTB.2,19697,[STR COM_CODE\16]
(the \16 signifies that you want to send 16 bytes)
Ioannis
- 3rd December 2009, 11:11
Do you want hex or ASCII hex?
Ioannis
cruzn27
- 5th December 2009, 07:27
I'm pretty sure that I want to send out an ASCII hex.
Kamikaze47
- 5th December 2009, 07:40
To send ascii 0 0 0 0 you would do this:
COM_CODE[0]="0"
COM_CODE[1]="0"
COM_CODE[2]="0"
COM_CODE[3]="0"
SEROUT2 PORTB.2,19697,[STR COM_CODE\4]
Ioannis
- 5th December 2009, 11:48
If you want ASCII Hex then every binary number in the range 0-9 should be added to 48 because ASCII 48 is character zero.
But what about after 9?
Well Melanie has posted a solution while back:
MyVar var word
' Variable containg 16-bit value
' MyVar's contents are destroyed on exit
MyData var byte (4)
' Four byte Array containing ASCII HEX result
' MSB is in MyData(3), LSB is in MyData(0)
CounterA var byte
' General Purpose Counter
For CounterA=0 to 3
MyData(CounterA)=MyVar&$0F
MyData(CounterA)=MyData(CounterA)+48
If MyData(CounterA)>57 then
MyData(CounterA)=MyData(CounterA)+7
endif
MyVar=MyVar>>4
Next CounterA
Hope this helps,
Ioannis
Melanie
- 5th December 2009, 13:15
So instead of this...
SEROUT2 PORTB.2,19697,[COM_CODE[I]]
...you want to send four digits of HEX...
SEROUT2 PORTB.2,19697,[HEX4 COM_CODE[I]]
Ioannis
- 5th December 2009, 13:24
Hi Melanie.
Since he has an array, he needs a way to convert it before sending each array element.
The solution of in-situ conversion I think will not help him.
Also as kamikaze47 stated his array should be word instead of byte.
So the example of yours code I gave is really for byte and needs some modification.
Ioannis
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.