Reading digital outputs

Coyoteboy

Senior Member
Im using an 40X2 part (checking in simulator) and trying to execute the following code to bitshift a variable and place its output onto port C pins 4 and 5, bitwise OR in order to keep existing port config:

Code:
b55 = valueOne << 4
pinsC = pinsC | b55
However this wipes the current outputs on the port as if I've just done pinsC = b55.

I tried

b54 = pinsC
pinsC = b54 | b55

but that doesnt cure it.

In fact no manipulation of port C is visible in the pinsC register, even when done with pinsC=%.... rather than high C.x

I've tried using outpinsC instead of pins (as it holds the right value) but that doesnt work either as "b54=outpinsC" returns a zero when outpinsC isnt zero?!

Is this a known issue?
 
Last edited:

hippy

Technical Support
Staff member
When pinsC appears on the left of an assignment the value written is set on the output pins, when on the right of an assignments pinsC returns the value of input pins. To read what has been previously written to the output pins use outpinsC ...

#Picaxe 40x2
dirsC = $FF
pinsC = $AA
b0 = outpinsC

To bit-bang using output pins you will need to mask off the bits previously set in outpinC and set only those bits as required, effectively, for the general case, where 'mask' is the bits to be set ...

pinsC = ( outpinsC & ( mask ^ $FF ) ) | ( bitstoset & mask )

That needs to be written in a form the PICAXE language understands, and may often be simplified.

So if you want bits 1 and 0 of b55 to appear on C.5 and C.4 ...

b54 = b55 << 4 & %00110000
pinsC = outpinsC & %11001111 | b54
 

hippy

Technical Support
Staff member
Here's an alternative method which uses b0, b1 and their bit variable components. It's not as efficient as the above code but can be more easily used when more complicated bit mappings are required ...


Symbol OutputBits = b0
Symbol WantedBits = b1

Symbol OutputBit0 = bit0
Symbol OutputBit1 = bit1
Symbol OutputBit2 = bit2
Symbol OutputBit3 = bit3
Symbol OutputBit4 = bit4
Symbol OutputBit5 = bit5
Symbol OutputBit6 = bit6
Symbol OutputBit7 = bit7

Symbol WantedBit0 = bit8
Symbol WantedBit1 = bit9
Symbol WantedBit2 = bit10
Symbol WantedBit3 = bit11
Symbol WantedBit4 = bit12
Symbol WantedBit5 = bit13
Symbol WantedBit6 = bit14
Symbol WantedBit7 = bit15

OutputBits = outpinsC
WantedBits = b55
OutputBit4 = WantedBit0
OutputBit5 = WantedBit1
pinsC = OutputBits
 

Coyoteboy

Senior Member
Hi Hippy, cheers for the rapid reply, this is driving me nuts. I'm fairly sure I was happily doing what you suggest earlier and then attempted to simplify it into individual steps so I could watch it. The problem remains with the use of outpinsC - while the "outpinsC" register in the simulator contains the value I placed in it earlier with a "high" command on C.3 (%00001000), the command b54 = outpinsC leaves b54 at zero, so I'm scuppered as I can never detect current values in order to not write over them with an update.
 

hippy

Technical Support
Staff member
This works for me ...

#Picaxe 40X2
High C.3
b54 = outpinsC
Do : Loop

b54 ends up with value %00001000 ($08) which is correct. Make sure you have the latest 5.2.6 of the Programming Editor. Maybe post your code which has the problem as there may be some interaction which the simulator isn't handling correctly and I can check it.
 

Coyoteboy

Senior Member
I think I've sussed it, in the simulator at least, outpinsC doesnt return anything whatsoever? I'll double check I have the latest version.

edit:

I have 5.2.5 - will try updating, that'll be very frustrating!
 
Last edited:
Top