Testing the status of an output Pin

CudaBlu

New Member
I would like to test the status (off/on - 0/1) of an output Pin without creating an additional variable as a flag. Is there a way of performing an action based on the status of an output Pin?

The following works on an input Pin:

Code:
#PICAXE 18m2

Do
  SerTxd ("B.3 = ",#PinB.3,cr)
  If PinB.3 = 1 Then SerTxd ("B.3.x = ",#PinB.3,cr,cr)
  EndIf
  Pause 3000
Loop


But this does not work as soon as the Pin becomes an output:

Code:
#PICAXE 18m2

Do
  SerTxd("B.3 = ",#PinB.3,cr)
  High B.3
  If PinB.3 = 1 Then SerTxd("B.3.x = ",#PinB.3,cr,cr)
  EndIf
  Low B.3
  Pause 3000
Loop
How (or can) I do what I would like to do? Or should I even be trying? Is there another way?
 

Technical

Technical Support
Staff member
The second code should work in real life even though it doesn't work in the simulator.
No, reading the pin value (internally the portX micro register) is not reliable when the pin is an output. It may work sometimes, but is not reliable.

Reading the outpins value (internally the latX micro register) is correct as Martin suggests.
 

nick12ab

Senior Member
No, reading the pin value (internally the portX micro register) is not reliable when the pin is an output. It may work sometimes, but is not reliable.
Can you please explain?

If it is because a load on the pin can affect the reading, then that could be used intentionally to detect whether there is a (significant) load on the pin or not.
 

westaust55

Moderator
The following may help.

The need to read the output latch register (LAT) is a function of the PIC chips and not a specific PICAXE situation. From the datasheet for the PIC corresponding to the 28X2”

Each port has five registers for its operation. These registers are:
• TRIS register (data direction register)
• PORT register (reads the levels on the pins of the
device)
• LAT register (output latch)
• ANSEL register (analog input control)
• SLRCON register (port slew rate control)
The Data Latch (LAT register) is useful for read-modify-write operations on the value that the I/O pins are driving
For PortA but also for other ports:
PORTA is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISA. Setting
a TRISA bit (= 1 ) will make the corresponding PORTA pin an input (i.e., disable the output driver).
Clearing a TRISA bit (= 0 ) will make the corresponding PORTA pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin).
Reading the PORTA register reads the status of the pins, whereas writing to it, will write to the PORT latch.
The Data Latch (LATA) register is also memory mapped. Read-modify-write operations on the LATA register read and write the latched output value for PORTA
.
 
Top