Display HEX values in terminal window

RonCzap

Member
Forgive me if this has been done before - I am new to the PICAXE.

I wanted to display values in HEX and did not find a solution so I wrote this code to convert and display using sertxd.

If anyone has an alternative, please share.

Thanks

Code:
#picaxe 20m2
#no_data
#com 4
#terminal 4800

Symbol bval     =b2
Symbol wval     =w13
Symbol wremain  =w11
Symbol divisor  =w12
Symbol subx     =b3
Symbol idx      =b4
Symbol aval     =b5    

main:
  sertxd ("Enter number", cr, lf)
  serrxd #wval
  if wval < 256 then
    bval = wval
    sertxd ("Byte Value: ",#bval, cr,lf)
    gosub HexByte
  else      
    sertxd ("Word Value: ",#wval, cr,lf)
    gosub HexWord
  end if  
  sertxd(cr,lf)
  if wval > 0 then
    goto main
  end if
  reconnect  
  sertxd ("end pgm", cr, lf)
  end
  
HexWord:
  sertxd("$")
  wremain=wval
  divisor=4096
  for subx = 0 to 2
    idx=wremain/divisor
    gosub PrtHex    
    wremain=wremain//divisor
    divisor=divisor/16
  next
  idx=wremain
  gosub PrtHex
  return
  
HexByte:
  sertxd("$")
  idx = bval/16
  gosub PrtHex
  idx = bval//16
  gosub PrtHex
  return

PrtHex:  
  lookup idx,("0123456789ABCDEF"),aval
  sertxd(aval)
  return
 

hippy

Technical Support
Staff member
HexByte:
idx = bval / 16 + "0"
If idx > "9" Then : idx = idx + 7 : End If
SerTxd( "$", idx )
idx = bval & 15 + "0"
If idx > "9" Then : idx = idx + 7 : End If
SerTxd( idx )
Return
 

westaust55

Moderator
Taking hippy's subroutine and going a step further for size reduction:

Code:
#picaxe 20M2
#no_data
#com4
#terminal 4800

Symbol bval      =b2
Symbol idx       =b4
Symbol wval      =w13 ; w13 = b27:b26
SYMBOL  wval_lo   = b26 ; low byte of wval
SYMBOL  wval_hi   = b27 ; high byte of wval

main:
  sertxd ("Enter number", cr, lf)
  serrxd #wval

  IF wval > 255 THEN      
    sertxd ("Word Value: ",#wval, " $")
    bval = wval_hi
    gosub HexByte
  ELSE  
    sertxd ("Byte Value: ",#wval_lo ," $")
  ENDIF  
  bval = wval_lo
  gosub HexByte
  sertxd(cr,lf)
  if wval > 0 then goto main
  reconnect  
  sertxd ("end pgm", cr, lf)
  end
  
HexByte:
  idx = bval / 16 + "0"
  GOSUB Nybble
  idx = bval & 15 + "0"
Nybble:
  IF idx > "9" THEN : idx = idx + 7 : ENDIF
  SerTxd( idx )
  RETURN
 

hippy

Technical Support
Staff member
Page 10 of PICAXE manual 2 describes how the byte variables are overlaid on the word variables.

One handy tip for dealing with words and their byte components is to define the word variable and the byte components of them, for example ...

Code:
Symbol wval     = w13 ; b27:b26
Symbol wval.lsb = b26
Symbol wval.msb = b27
Then if you want to use the word, just use "wvar", for the byte parts, "wvar.msb" or "wvar.lsb".

It's also handy to remember when you can that wN == bN*2+1:bN*2, eg, w10 == b21:b20 and so on.
 

RonCzap

Member
hippy,
Thanks again - I appreciate the tips. That makes it easier to read and understand.
Also more similar to the syntax of other microcontrollers I have used.
 
Top