A request for a pointer for binary

Lez T

Member
I am just starting to learn binary for programming ( The number system). I understand how to use binary for interrupts ( a ‘compare with value’ (input)
and an ‘input mask’ (mask)) ,for letpins = to set the pin high or low and letdirs = to set pins to inputs or outputs.
Is it possible to also use binary for comparing input states on pins
i.e.
original code
let dirs = %110 'make pins 1 and 2 , outputs
if input3 = 0 and input4 =0 then goto main
if input3 = 1 and input4 =0 then goto latch
if input4 = 1 and input3 =0 then goto unlatch

is something like this possible
if %00011000 = %00000000 then goto main
if %00011000 = %00001000 then goto latch
if %00011000 = %00010000 then goto unlatch

I have looked through the manuals ( RTFM ) and did not find anything helpful, which probably means it cannot be done.
All comments gratefully received.
Regards.
Lez.
 

Svejk

Senior Member
Assign to a variable the status of input pins:

ie [asuming a 20x2 is used]:

let pinsC = b0

if b0 = %0001100 then ...
 

hippy

Ex-Staff (retired)
You can read all inputs simulatenously using 'pins' and then mask only the pins / bits you are interested in ...

b0 = pins & %00011000
If b0 = %00000000 Then Goto main
If b0 = %00001000 Then Goto latch
If b0 = %00010000 Then Goto unlatch

You could also do it with a SELECT-CASE ...

b0 = pins & %00011000
Select Case b0
Case %00000000 : Goto main
Case %00001000 : Goto latch
Case %00010000 : Goto unlatch
End Select

Or, in this case, you could move the bits you are interested in to bits 0 and 1 so they give a value of 0, 1 or 2 and use ...

b0 = pins & %00011000 / %00001000
On b0 Goto main, latch, unlatch

or

b0 = pins & %00011000 / %00001000
Branch b0, ( main, latch, unlatch )
 

westaust55

Moderator
binary tests and checks

IF (and only IF) you have an X1 or X2 PICAXE chip, then ther is also the opportunity to test bits to see if they are set (=1) or clear (=0)

for example:
Code:
b5 = %00000010
b3 = %11111011
b2 = pins

IF b5 BIT 1 SET THEN
  SEROUT 7, N2400, ("bit 1 is set")
ELSEIF b3 BIT 2 CLEAR THEN
  SEROUT 7, N2400, ("bit 2 is clear")
ELSEIF b2 BIT 2 CLEAR THEN
  SEROUT 7, N2400, ("bit 2 is clear")
ELSE
  SEROUT 7, N2400, ("it is something different")
ENDIF
See PICAXE Manual 2 page 88
 

Lez T

Member
Thank you all for your replies

@Svejk.
I am at present using 18x but will no doubt move on to the 20x2's that are waiting in the wings (well, the chip box really) for another time.
@H, and Westy.
I will check out the commands that you have pointed out and play with the code to see if I can get somewhere with them.
Thanks again.
Regards.
Lez.
:)
 

bpowell

Senior Member
You can read all inputs simulatenously using 'pins' and then mask only the pins / bits you are interested in ...

b0 = pins & %00011000
If b0 = %00000000 Then Goto main
If b0 = %00001000 Then Goto latch
If b0 = %00010000 Then Goto unlatch

You could also do it with a SELECT-CASE ...

b0 = pins & %00011000
Select Case b0
Case %00000000 : Goto main
Case %00001000 : Goto latch
Case %00010000 : Goto unlatch
End Select

Or, in this case, you could move the bits you are interested in to bits 0 and 1 so they give a value of 0, 1 or 2 and use ...

b0 = pins & %00011000 / %00001000
On b0 Goto main, latch, unlatch

or

b0 = pins & %00011000 / %00001000
Branch b0, ( main, latch, unlatch )
Very nice! I love how code can be optimized with the Picaxe...just a wonderful chip!
 

SAborn

Senior Member
A very handy thing to remember is "Windows" has a calculator that will convert Hex, Dec, and Binary to either value.

It is a great tool to check things against if you have a error of doubt wheather you got a figure right.
 

Lez T

Member
@ Westy, Thank you, I have saved and printed a copy of your table to go in my 'very useful info' folder, hope you don't mind, I was using the windows calculator in scientific mode for all of this, much easier how you have laid it out, and with ASCII alongside.
The support here is A1. Hope that one day I will have the know how to give something back.
Regards.
Lez.
 

westaust55

Moderator
@ Westy, Thank you, I have saved and printed a copy of your table to go in my 'very useful info' folder, hope you don't mind, Lez.
Hi Lez,
That was the whole intention of posting the table, as with other tutorials and information I have created and posted here.
That others might find it useful and save a copy for their future reference.


Would also be good if when something is passed on/further afield that the source is acknowledged - may at least then get even more people to look at PICAXE
 

slimplynth

Senior Member
Hello, good afternoon all.

Related but on a slight tangent, I'm getting round to setting up a Charlieplexing breadboard again, thinking 'Egg Timer' idea.

Rather than using binary to represent %00001010... I've been concentrating on using Hex, after reading up and deciding it would make for fewer key strokes :D (and a different slant on mathematics).

so instead of

Let dirs=%00001010

have

Let dirs=$A

Which still compiles to the same program size and works (in simulator).

Are there any advantages / disdavantages of using either system - other than Hex is at first a little taxing, cumbersome to use (hopefully that will change with brain training... never had call to use hex before picaxe)

P.S. I did read manual 2 and note it does say that normal convention for naming pins I/O is to use binary notation, i just wanted to try something different.

Cheers

Lee
 

MartinM57

Moderator
Integer, binary, hex, ascii characters ...use whatever you feel most comfortable with and which self documents the best in each particular instance.

If I read a datasheet and it says "send $FF to ..." then I will use $FF in the code.

If I'm setting port directions then I might use binary as I can eyeball each bit, which equates to a port pin.

If I'm sending a character I use e.g. b0 = 'a'

As a style thing I always use two hex digits e.g.

Let dirs = $0A
 

westaust55

Moderator
it will make no difference to program size, speed or anything else whether you use %00001010 (binary), $0A (hex variant 1), 0x0A (hex variant 2), or 10 (decimal)
They are just different ways to represent the same value.

I guess that for some folks, the binary representation when related to input or output ports gives a one for one correspondence with the port pin and the binary value bit.

with hex, personnally I prefer variant 1 but others do use the second variant and both are valid
 
Last edited:

BeanieBots

Moderator
As far as the PICAXE (or any other computing system) is concerned, there will be absolutely no difference. It is purely a method of representation so the coice comes down solely to personal preference.

Binary gives an instant visual indication.
Personally I tend to prefer Hex as it's quicker to type and once used frequently is just as much 'at a glance' as binary.
IMHO, decimal has almost no place at all when dealing with micros. It was simply invented for humans with 10 fingers.
 

moxhamj

New Member
Hex vs Binary? Let's use the excelllent advice of SAborn, who hails, it would appear, from near me and who has very cleverly suggested using the freebie windows calculator.

Hex Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

and if you want to make up a number like FF in hex, just look up those two binary numbers and put one after the other.

Hex is nifty. And all you have to remember is 16 binary equivalents. And if the binary looks confusing, scan down the last column. 01010101 etc. The second column 00110011 etc. The third column 0000111100001111 etc. There are some very simple patterns here.
 
Last edited:

hippy

Ex-Staff (retired)
The choice between decimal and hex/binary is really a question of what's most appropriate for how the number is being used.

If it's a 'number' as we humans understand them ( temperature, voltage, level, etc ) it usually makes sense to express them in decimal. If it's a 'bit pattern' the hex or binary is often more appropriate.

And it works vice-versa; if you see a hex or binary number it's a good clue that there's something significant about particular bits.

Whether hex or binary depends as much on readability as anything else; sixteen 0's and 1's can be hard to manage in one go and prone to mistakes. As hex is quick to mentally convert to binary it's often convenient and easier to read.

Of course there's no reason not to mix and match as approprite ...

w0 = $FC00 + 10
 

slimplynth

Senior Member
Cheers for that, it was only because of the DS1307 RTC that hex came to my mind as something to tackle, I did this grid yesterday...

Code:
0	0		0	0	0	0
1	256		1	16	1	1
2	512		2	32	2	2
3	768		3	48	3	3
4	1024		4	64	4	4
5	1280		5	80	5	5
6	1536		6	96	6	6
7	1792		7	112	7	7
8	2048		8	128	8	8
9	2304		9	144	9	9
A	2560		A	160	A	10
B	2816		B	176	B	11
C	3072		C	192	C	12
D	3328		D	208	D	13
E	3584		E	224	E	14
F	3840		F	240	F	15
After a little more thinking It seemed to make the penny drop... If you remember your 16 times table and the above series of numbers from the first column (and practise it plenty) then doing maths with Hex notation up to 3840 should get quicker? Then add another column... so on and so on.

NB: did some searching through the archives and found an example by Hippy (cheers) that explained elegantly how to do hex math - but can't find the thread now, in any case it was a rewarding lesson to go through, hence looking for practical ways to use it. Charlieplexing with let dirs = and let pins = sounded feasible but perhaps not, will find out soon enough.
 

Technical

Technical Support
Staff member
Actually the DS1307 uses BCD (binary coded decimal) which is different to hex, and BCD maths is tricky... we'll let google explain the difference...
 

Technical

Technical Support
Staff member
Even big companies can get this wrong - tens of thousands of peoples Sony's PS3 stopped working last week at midnight on 01 March.

The rumour is 2010 was interpreted internally as the 16th year of 2000 (2016), which is a leap year. Hence the PS3 incorrectly thought it was 29th Feb, got very confused and stopped working for a day.

BCD $10 is decimal 10 - year 10
hex $10 is decimal 16 - year 16
 
Last edited:
Top