SERTXD and displaying in binary on the console

Circuit

Senior Member
When debugging programs that switch eight bits of a port for control purposes, it can be most useful to SERTXD the value to display on the console in binary form. This allows immediate identification of which lines are high or low.
For example;

sertxd ( "controls=", #bit7,#bit6,#bit5,#bit4, #bit3, #bit2, #bit1,#bit0, cr, lf )

My question is, is there a more elegant way of achieving this than the line I have just shown?
Also, is there any way of displaying higher variables, beyond b3, as binary strings without mapping them back to b0 and sertxd-ing the #bit values?
It would be super if one could "sertxd(%b5, cr, lf)" or some similar syntax... and have the binary string show on the console. Am I missing something?
 

Circuit

Senior Member
Thanks for that pointer, Tex; macro based on that herewith. Works nicely.

Code:
[color=Green]'Macro to send binary display of variable via SERTXD to console[/color]
[color=Navy]#PICAXE [/color][color=Black]40X2[/color]

[color=Navy]#MACRO [/color][color=Black]binarydisplay[/color][color=Blue]([/color][color=Black]portswitches[/color][color=Blue])
for [/color][color=Purple]b1[/color][color=DarkCyan]=[/color][color=Navy]7 [/color][color=Blue]to [/color][color=Navy]0 [/color][color=Blue]step [/color][color=DarkCyan]-[/color][color=Navy]1
      [/color][color=Blue]if [/color][color=Black]portswitches [/color][color=DarkCyan]bit [/color][color=Purple]b1 [/color][color=DarkCyan]set [/color][color=Blue]then
            sertxd ([/color][color=Red]"1"[/color][color=Blue])
      else
            sertxd ([/color][color=Red]"0"[/color][color=Blue])
      endif
next
sertxd (cr[/color][color=Black], [/color][color=Blue]lf)[/color]
[color=Navy]#endmacro[/color]

[color=Black]main:[/color]

[color=Blue]let [/color][color=Purple]b12[/color][color=DarkCyan]=[/color][color=Navy]%10010010[/color]
[color=Black]binarydisplay[/color][color=Blue]([/color][color=Purple]b12[/color][color=Blue])

let [/color][color=Purple]b20[/color][color=DarkCyan]=[/color][color=Navy]%00100101[/color]
[color=Black]binarydisplay[/color][color=Blue]([/color][color=Purple]b20[/color][color=Blue])

end[/color]
 

techElder

Well-known member
Nicely done! And using a macro to boot! Hippy will give you a +1 !

EDIT PS. Just add a switch in your macro to make it work for word variables, too.
 

westaust55

Moderator
To highlight, the IF…BIT command is only available for the PICAXE X1 and X2 parts.

Thus Circuit's Macro will work (defines the part as a PICAXE 40X2) but will not work with an M2 part.

Also worth adding a line
sertxd ("%")

before the line
for b1=7 to 0 step -1
 
Top