Need help sending a hex serial output


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2009
    Posts
    3

    Default Need help sending a hex serial output

    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

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    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)
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    Do you want hex or ASCII hex?

    Ioannis

  4. #4
    Join Date
    Nov 2009
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    I'm pretty sure that I want to send out an ASCII hex.

  5. #5
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    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]
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    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:

    Code:
    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

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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]]

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. Serial Output - for an ASM numb
    By scomi85 in forum Serial
    Replies: 0
    Last Post: - 1st March 2009, 11:13
  2. PIC18F4680 to PC via MAX232 (RS232 serial) no output
    By opticsteam1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th April 2008, 21:39
  3. Serious Serial Situation Setbacks...
    By Dansdog in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th February 2007, 04:46
  4. HSEROUT Newbie question/problem
    By Mark Scotford in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 11th July 2006, 15:44
  5. Replies: 2
    Last Post: - 20th January 2006, 22:09

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts