How to check if an output is already high or low

Sean87

Member
Hi all,

I am working on a small project with 20X2. Is there a way to check if an output is already high or low? before actually changing it?

for example I need a logic like this

Code:
if B.0 is high then
 sertxd("is already high")
else
high B.0
Note that I do not want to use TOGGLE, in my application I need to see the actual state of an output on the PC monitor.

Thanks!
 

nick12ab

Senior Member
Your theory is already valid. When you read a pin, it electronically reads the pin rather than a variable which I found out when I accidentally set some pins which have input buttons to outputs - the thing still worked but it draw around 100mA more whenever a button was pressed.

But in the code you can't put 'if pin is high then', you have to put 'if pin = 1 then'.
 
Last edited:

eclectic

Moderator
Have a look at "outpins" in manual 2. (not sure if it applies to 20X2 though:eek:)
It's OK BB, it does. :)

This
Code:
#picaxe 20X2

main:
toggle B.0 ; just to switch pin B.0 on/off

b0 = outpinsB 
if bit0 = 1 then

sertxd ("Pin high ")
else sertxd ("Pin low ")
endif

pause 1000
goto main
both simulates and works for real.

Obviously, it can be tidied.

e
 

jtcurneal

Senior Member
Also check the readoutputs command on page 180 of manual2. This could be used to read the state of the outputs into a variable. or use let var = outpinsX
and then you can do different subrutines based on the status of different pins.

Joel
 

westaust55

Moderator
ed.

But in the code you can't put 'if pin is high then', you have to put 'if pin = 1 then'.
You could have:
IF outpinB.2 is ON THEN . . . . ; "is" and "ON" (also OFF) are predefined within the PE - mainly for younger users

if you want to check multiple pins then
b0 = outpinsB
and then test the various bits bit0 to bit7
 
Last edited:

hippy

Ex-Staff (retired)
Your theory is already valid. When you read a pin, it electronically reads the pin
A few weeks ago I'd have said it was okay, there wasn't any difference in reading how the output pins have been set ( determined via 'outpin' variables or using READOUTPUTS ) and reading that output pin as if it were an input ( assuming the load on the pin wasn't so great it caused a mis-reading, read low while struggling to be high or similar ).

Then I ran into a case where reading an output pin as if it were an input did not deliver the result expected. Reading the very same output via a different pin set for input and results were as expected.

Technical has highlighted that issue in the past but I had never run into it before.

You could have:
IF outpinB.2 is ON THEN . . . . ; "is" and "ON" (also OFF) are predefined within the PE - mainly for younger users
And very useful in the right context for even older and more experienced users. Particularly in an example such as that or for "if BTN_UP is PUSHED" etc.
 
Last edited:

srnet

Senior Member
Is this the read write modify problem ?

I have a document saved called 'readmodifywrite.pdf' which explains an issue that arises when you try to read an output pin.
 

Technical

Technical Support
Staff member
Kind of. You just can't read the status of a pin that is configured as an output by reading the input port pin value. 90% of the time it does appear to work, but not all the time and certainly not reliably, as we have stated on numerous occassions in the past!

On older chips use 'readoutputs', on newer chips read the outpinsX variable. In both cases these are reads of the 'latch copies' of what the outputs should be, not the actual pin itself.
 

jtcurneal

Senior Member
It appears from Manual2 version 7.5 that the readoutputs could be used on the newer chips, but trying to use that command on a 20x2 gives a syntax error that it is not supported.

Joel
 

westaust55

Moderator
Isn't that just the same as tesing the bits outpinB.0 to outpinB.7 except it uses a variable?
It can be.
However if you wanted to test more than 1 but not all pins of a port and at different points in a program then using a variable can have it's place.
 
Top