Monitor all B ports with one command.

Jaden

Senior Member
An easy one I hope....

I am using a 40x2 and will be looking at four of the 8 ports (B.0 ~ B.7). I am looking at a combination of highs and lows and the only way I know how to do it is such (I will use Symbol values for pins)...

if B.7=1 and B.6=1 and B.5=1 and B.4=0 (output of a BCD switch) then gosub ......

I do not have the other pins connected and will not be using them, is it possible to shorten that with a command ......
if 'something' %11100000 then gosub
, A simple one command that monitors the 8 pins on that particular port? I have 8 states I want to monitor on these four pins to send to various subroutines.

Thanks
 

westaust55

Moderator
You might need a slight variation as:
Code:
b0 = pinsB AND $F0 ; mask off the signals for the unused pins – then does not matter if unused IO are floating or pulled high or used for another purpose
IF b0 = %11100000 THEN GOSUB . . . . .
I am using a 40x2 and will be looking at four of the 8 ports (B.0 ~ B.7).
Note also that for correctness, the term “Port” refers to an entire group of pins with a common microcontroller (or microprocessor) register address. Each of the 8 discrete signals constitutes one IO point and is referred to as a “pin”.
 
Last edited:

hippy

Ex-Staff (retired)
For a BCD switch it may be best to read the pins shifted down to create a binary value representation of what the switch position is -

b0 = pinsB / $10

or, depending on signal polarity -

b0 = pinsB / $10 ^ $0F

Once you have a value 0-9 in b0 or some other variable it will likely be much easier to process its settings or use its value.
 

Jaden

Senior Member
Thanks guys, I can follow what you have told me. Sorry westaust55, my fingers move to fast for my brain! I understand.

Can you explain the / $10 please hippy, I have come across this a few times and do not grasp it properly.:confused: By signal polarity do you mean +V and 0V ? It will be a 1 or 0 returned. It would be handy to give it a shorter reference as I need to also wirelessly transmit this value (if possible). Or tell me where I can go to read up on it.

Thanks again.
 

nick12ab

Senior Member
Can you explain the / $10 please
$10 is hexadecimal for 16, which when you divide another number by it, the result is bits 0 to 3 are lost and bits 4 to 7 are shifted down to where bits 0 to 3 were, so for example, %11010010 / $10 = %00001101.

By signal polarity do you mean +V and 0V ? It will be a 1 or 0 returned. It would be handy to give it a shorter reference as I need to also wirelessly transmit this value (if possible). Or tell me where I can go to read up on it.
In his 'signal inversion' example, Hippy XORs the already shifted value with $0F (15 in hexadecimal, %00001111 in binary) so this just inverts bits 0 to 3 of the byte. For example, %00001101 ^ $0F = %00000010. So that's basically it - this might/will be the way to go if you use the switch as active low so that you can use the internal pull-ups on portB of the PICAXE.
 

westaust55

Moderator
By signal polarity do you mean +V and 0V ? It will be a 1 or 0 returned.
I believe hippy was considering whether the BCD switch was in effect positive/true in that a value was represented by is usual binary pattern or when it was inverted.

You mentioned a BCD switch which will given you the ten binary codes %0000, through %0001 (=1), %0010 (=2) ... to %1001 (=9)
Your example has the high nybble (most significant 4 bits) as %1110 which is not valid as "true" BCD and hence the consideration if the signal is inverted
so that %1110 becomes %0001 (=1)

Once the high nybble is moved to the lower nybble using the "divide by 16" (==> /$10 ), using the XOR (^) command to bit-wise Exclusive-Or the lower 4 bits will invert the state of each bit.

It all depends upon how you have your BCD switch wired or its internal configuration.
 
Top