1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Print HEX

Discussion in 'Oshonsoft' started by GreyHairOldGuy, Dec 20, 2014.

  1. GreyHairOldGuy

    GreyHairOldGuy New Member

    Joined:
    May 30, 2011
    Messages:
    12
    Likes:
    0
    Location:
    Canada
    DIM XXX AS BYTE
    XXX = 0

    LOOP:
    XXX = XXX + 1

    SEROUT TX, 9600, "Number=", #xxx, 0xd
    '(or LCDOUT)

    goto loop

    Obviously this will print Number= 0-255 and repeat forever.

    How do I make the same program print 00-FF instead of 0-255?

    Or in other words, is there a simple way to display HEX directly?
     
  2. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    270
    Likes:
    15
    Location:
    Helsinki, Finland
    ONLINE
    The new versions of Oshonsoft Basic support string variables and there is a function HexStr() that converts a word variable to a four character string.
    Look at Oshonsoft home page.

    For a byte variable x: y=x / 16 z=x mod 16
    then convert y and z to characters 0-F ( 10= A etc. )
     
  3. GreyHairOldGuy

    GreyHairOldGuy New Member

    Joined:
    May 30, 2011
    Messages:
    12
    Likes:
    0
    Location:
    Canada
    Thank you jjw

    I have not worked much with strings before but I understand what you have suggested - thank you and I will take that approach and see if I can get it working.
    I also upgraded and downloaded the latest versions of the PIC software from Oshonsoft including the String functions and the Structured Language support (which I am
    weak in, being a hardware guy) . Have a Merry Christmas and tks again for your (and others) help.
    Mike
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    Even so... serout using a lookup table is dead easy to implement.....
     
  6. GreyHairOldGuy

    GreyHairOldGuy New Member

    Joined:
    May 30, 2011
    Messages:
    12
    Likes:
    0
    Location:
    Canada
    Tks Ian........

    h8to16a.HB = LookUp("0123456789ABCDEF"), tmp 'Convert to ascii & Store works GREAT

    and (not this time), but sometimes you have to use the following process:

    h8to16a.HB = LookUp (0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46), tmp

    Which also works but I could not find a line continuation character because sometimes the lines get quite long.

    I tried "_ " (underline) as a line continuation character, but that didn't seem to work - I kept getting a compile error on that line when I tried to put it on two lines for readability.

    Mike
     
  7. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,076
    Likes:
    524
    Location:
    Cleveland, OH, USA
    I use a routine called BIN2ASCII from PicList. Remember, internally your numbers are all binary. This routine converts the binary values in a register to the 2 digit hex values. For the majority of people, unfortunately, it is in Assembly. I suspect a search on BIN2ASCII in C would produce the equivalent.

    Code (asm):

    ;*******************************************************************************    
    ;Scott Dattalo (PicList): This works well for 0x00 - 0xFF.  Since    
    ;Char_hi is transmitted first, modified to finish with Char_hi in w.    
    ;Modified version is 13 cycles, 6.5 uS, Char_hi is in w, Char    
    ;is preserved,enter with binary in w.    
    ;*******************************************************************************    
    BIN2ASCII              
                    movwf   Char
                    andlw   0x0f
                    addlw   6
                    btfsc   STATUS,1        ;skpndc
                    addlw   0x07            ;'A'-('9'+1)
                    addlw   0x2A            ;'0'-6
                    movwf   Char_lo
                    swapf   Char,w
                    andlw   0x0f
                    addlw   6
                    btfsc   STATUS,1        ;skpndc
                    addlw   0x07            ;'A'-('9'+1)
                    addlw   0x2A            ;'0'-6
                                            ;   movwf  Char_hi                  ;added so I don't worry about w on call/return        
                                            ;   retlw   0
                    return
     
    John
     

Share This Page