Shop OBEX P1 Docs P2 Docs Learn Events
Code to help with debugging SX/B programs... — Parallax Forums

Code to help with debugging SX/B programs...

RobotWorkshopRobotWorkshop Posts: 2,307
edited 2007-05-14 19:26 in General Discussion
Hello,

I was working on one of my projects using SX/B and wanted an easy way to send
debugging data to a PC to help see what was going on in my code. I used the built
in RS-232 port on the SX48 DIP module kit to send data to the host PC. Sending a
specific character is easy and has been shown quite a few times on this forum. I
wanted a quick way to send the Hexidecimal representation of each byte to the
Serial Port (followed by a space) and came up with the following code as another
subroutine. It only has one byte passed to it and converts the value to be sent
to the serial port for debugging.

Just define BaudDbg as a constant like "T9600" and define DbgSer as the pin
you want to use to send the debug data one. Also make sure to define the pin
as an output!


TX_ByteDbg SUB 1 ' Transmit character to debug port
TX_ByteDbgHex SUB 1 ' Transmit Hex value to debug port

.
.
.


'
' Use: TX_ByteDbg char
' -- 'Transmits 'char' over serial connection

TX_ByteDbg:
tmpB1 = __PARAM1 ' get the passed byte value
SEROUT DbgSer, BaudDbg, tmpB1 ' Send the byte
RETURN

TX_ByteDbgHex:
tmpB1 = __PARAM1 ' get the passed byte value
tmpB2 = tmpB1 // 16 ' Get the low nybble digit (0-15)
tmpB1 = tmpB1 / 16 ' Get the high nybble digit (0-15)

tmpB1 = tmpB1 +$30
IF tmpB1 > $39 THEN
tmpB1 = tmpB1 + 7
ENDIF

tmpB2 = tmpB2 +$30
IF tmpB2 > $39 THEN
tmpB2 = tmpB2 + 7
ENDIF

SEROUT DbgSer, BaudDbg, tmpB1 ' Send the upper nybble

SEROUT DbgSer, BaudDbg, tmpB2 ' Send the lower nybble

SEROUT DbgSer, BaudDbg, 32 ' Send a trailing space

RETURN


I just wrote this code today as I needed something quick to help me debug a problem
(which it did). I suppose it could be optimized a bit but perhaps some of you will find
it useful.

Best Regards,

Robert

Comments

  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-05-14 17:28
    Robert,

    Thanks for posting your debugging subroutine.

    I noticed that you define TX_ByteDbg but never seem to call it. I assume you meant to use it to save program space and probably meant to replace your three SEROUT calls with something like the following:

    TX_ByteDbg tmpB1     ' Send the upper nybble
    
    TX_ByteDbg tmpB2     ' Send the lower nybble
    
    TX_ByteDbg 32         ' Send a trailing space
    


    This should not affect how the subroutines work, just home much program space they consume.

    - Sparks
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-05-14 18:41
    You might want to encapsulate SEROUT into its own routine to save code space.
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2007-05-14 18:47
    Hello Sparks,

    You are definitely right and I should just call TX_BtyeDbg from within the Hex routine to save space. I hadn't put that in place yet but will as I clean up the program i'm using it in.

    What wasn't shown is that I had called the TX_ByteDbg from a few other places whenever I wanted to send out status. One routine prints stored text strings (to make it look pretty) and other times I just send the ASCII code of the character I wanted to use for status. When I needed to display Hex bytes I then call the TX_ByteDbgHex routine above. I posted that portion since I hoped others would find the conversion to Hex useful.

    Robert
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-05-14 19:26
    Here's a collection of similar routines that I use for sending values to a PC:

    ' Use: TX_BYTE theByte
    ' -- transmit "theByte" at "Baud" on "TX"
    
    SUB TX_BYTE
      tmpB1 = __PARAM1                              ' get byte to send
      SEROUT TX, Baud, tmpB1
      ENDSUB
    
    ' -------------------------------------------------------------------------
    
    ' Use: TX_STR [noparse][[/noparse]string | label]
    ' -- "string" is an embeded string constant
    ' -- "label" is DATA statement label for stored z-String
    
    SUB TX_STR
      tmpW1 = __WPARAM12                            ' get base+offset
      DO
        READINC tmpW1, tmpB1                        ' read a character
        IF tmpB1 = 0 THEN EXIT                      ' if 0, string complete
        TX_BYTE tmpB1                               ' send character
      LOOP
      ENDSUB
    
    ' -------------------------------------------------------------------------
    
    ' Use: TX_BIN8 theByte
    ' -- transmits value of "theByte" in BIN8 format
    
    SUB TX_BIN8
      tmpB2 = __PARAM1                              ' get byte to send
      FOR tmpB3 = 1 TO 8                            ' loop through eight bits
        IF tmpB2.7 = 1 THEN                         ' if MSB is set
          TX_BYTE "1"                               '   send "1"
        ELSE                                        ' else
          TX_BYTE "0"                               '   send "0"
        ENDIF
        tmpB2 = tmpB2 << 1                          ' shift next bit to MSB
      NEXT
      ENDSUB
    
    ' -------------------------------------------------------------------------
    
    ' Use: TX_HEX2 theByte
    ' -- transmits value of "theByte" in HEX2 format
    
    SUB TX_HEX2
      tmpB2 = __PARAM1                              ' get byte to send
      tmpB3 = tmpB2                                 ' make copy
      SWAP tmpB3                                    ' swap hi/lo
      tmpB3 = tmpB3 & $0F                           ' mask to isolate hi
      READ Hex_Digits + tmpB3, tmpB1
      TX_BYTE tmpB1                                 ' send hi nib
      tmpB2 = tmpB2 & $0F                           ' mask to isolate lo
      READ Hex_Digits + tmpB2, tmpB1
      TX_BYTE tmpB1
      ENDSUB
    
    Hex_Digits:
      DATA "0123456789ABCDEF"
    
Sign In or Register to comment.