Continue to Site

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.

  • 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.

Print HEX

Status
Not open for further replies.

GreyHairOldGuy

New Member
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?
 
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. )
 
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
 
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
 
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:
;*******************************************************************************    
;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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top