Picaxe 18M2 Code To Output A Binary Number

KB5PWL

New Member
I have read posts until I seem to be brain dead. Can someone point me to a sample of Picaxe 18M2 code to output an eight-bit binary number? I intend to load an eight-bit latch.

Edit:
Ahh, found it in the manual and it finally burned through the fog.
let outpinsB = %11000011 would output 195 in binary on port B.
 
Last edited:

westaust55

Moderator
great to see that you found the solution.
And . . albeit you second post, Welcome (again) to the PICAXE forum.

Also keep in mind that virtually all computers (analog computers excepted)and microcontrollers work in binary thus its native/normal for the number to be represented as %11000011 and in this format it is quite clear which bits are set and which are cleared.
Other representation such as hexadecimal and decimal (and even ASCII codes) are available for the greater east of the human operators.
That binary number %11000011 can easily be represented in hexadecimal by taking the two 4-bit nybbles as $C3 which is a bit quicker to type 3 characters rather than 9.
If decimal is your base of choice and you want for example to know what is the binary equivalent of 195 decimal or output as a BCD display, then yes you can use 195 instead.
let outpinsB = %11000011
let outpinsB = $c3, and
let outpinsB = 195
all achieve the same result.
 
Last edited:

Technical

Technical Support
Staff member
And don't forget to make those pins outputs first.

let dirsB = %11111111
let outpinsB = %11000011
 

geoff07

Senior Member
On the subject of outputting a binary number, but this time as a sertxd output. is there an easy way of actually sending a number in binary? So that, for example, instead of the prefix '#' generating printable ascii, use of, say, '%' would send printable binary ?

Yes I know I could code it myself but it must be a common requirement for debugging at least, though I haven't found it. If there isn't, could it be added to the wish list? The modern chips have a lot more memory so maybe this could be squeezed in?. Coding in C (or whatever the firmware is written in) would likely be much more efficient that doing it in basic.
 

hippy

Technical Support
Staff member
Not sure this is exactly what you want but you can do the following ...

Code:
For b13 = %00000000 To %00001111
  b0 = b13
  SerTxd( "%",#bit7,#bit6,#bit5,#bit4,#bit3,#bit2,#bit1,#bit0, CR, LF )
Next
There are no field or number-base format options for output, just # for turning a raw number into printable decimal. Adding full formatting would probably add a lot of firmware code.
 

geoff07

Senior Member
Indeed that is a clever way I can do it. But it uses B0 which is handy in other ways. Though I could push it on the stack these days perhaps.
 

hippy

Technical Support
Staff member
It is a bit of a pain needing a variable but is possible to use any of b0, b1, b2 or b3 and adjust the bit variables accordingly.

These days I tend to keep b0, b1, w0 and bit0-bit15 solely for temporary use and to allow for easier bit access and manipulation. Using SYMBOL definitions rather than raw variable names ( apart from the above ) can also help make things easier.
 
Top