Simulator surprise

cpedw

Senior Member
I simulated a short program to try to find what I can do with pinsB:
Code:
#picaxe 18m2

dirsb=255
pinsb = 5
b1=pinsb
pinsb= pinsb+1
pinsb=pinsb+2
b2=pinsb
At first I was disappointed to see that pinsB stayed=0 according to the view in Code Explorer/System Variables but b1 and b2 came out as expected, 5 and 8. However, watching pinsB while stepping through the simulation showed that pinsB changes as expected but reverts to 0 when the program finishes. But dirsB retains its value after the program has completed.

However, my initial question seems to be answered: I can use pinsB on either side of an assignment. Is that going to hold true in hardware?
 

cpedw

Senior Member
You prompted me to check what the difference is between pins and outpins. In Manual 2, under "Readouputs" (never noticed that command before!) I found:
Information:
The current state of the output pins can be read into a variable using the
readoutputs command. Note that this is not the same as ‘let var = pins’, as this let
command reads the status of the input (not output) pins.
This command is not normally used with M2, X1 or X2 parts as the outputs can
be read directly with ‘let var = outpinsX’


Further study of the manual has confused me again. Section 2, page 18:
PICAXE-14M2 / 18M2 / 20M2 Special Function Registers
pinsB - the portB input pins
outpinsB - the portB output pins

.....
When used on the left of an assignment ‘pins’ applies to the ‘output’ pins e.g.
let outpinsB = %11000000
will switch outputs 7,6 high and the others low.
When used on the right of an assignment ‘pins’ applies to the input pins e.g.
let b1 = pinsB
will load b1 with the current state of the input pin on portB.


Does this mean that pins and outpins are synonymous when on the left of = but are either inputs or outputs when on the right of = ?
 

AllyCat

Senior Member
Hi,

Yes, it's on the Right-Hand-Side that you need to be "careful". Remember that b1 = pinb.0 is not the same as b1 = b.0 (but both pass a Syntax check) !

Normally, when the pin / port is on the Right Hand Side of an assignment, the PICaxe automatically switches the pin(s) to be input(s), (if currently set as output) and then reads the input level(s). If there is nothing connected on the pin(s), then "stray" capacitance will probably hold each output level until the command reads it. However, the Simulator cannot "know" what is connected on the pin(s) so the result may be unpredictable. The OUTPINs variable contains what was previously WRITTEN to the port/pin by the program.

So, if you want to set/change the output levels, I believe the correct syntax is of the form: PINs = OUTPINs.... , but OUTPINs = OUTPINs.... appears to work and is perhaps more "understandable".

Cheers, Alan.
 

Technical

Technical Support
Staff member
Reading 'pinsX' reads the real life port, but reading an output pin at the silicon level is unreliable ie you can't directly read any pin that is set as an output. So basically 'pinsX' only works for input pins. Reading 'outpinsX' reads the latch register of the outputs, which is the state the output pin is currently driven to. This is reliable for reading what output pins are currently set to, but does not read input pins..

Writing to 'pinsX' or 'outpinsX' both do the same thing - both write to the latch register.
 
Top