everest wrote:Is there a simple way to read the current state of an OUTPUT pin in Zbasic?
I began to reply to your original post but it was deleted before I finished.
The AVR chip has three registers associated with each I/O port: DDRX, PinX and PortX where X is {A, B, ...}. The DataDirectionRegister (DDR) contains eight bits that individually control whether a given bit of the port is an input (0) or an output (1). The PortX register controls the output state of a given bit if the DDR register indicates that that bit is, indeed, an output. Otherwise, if the bit is an input the PortX bit controls whether the pull-up resistor is enabled or not. Lastly, the PinX register is the means by which a signal present on the I/O line can be read.
The ZBasic system provides some specialized functions that allow you to determine if a given pin is an input or an output and, if an output, what the current state is. The example functions below show how this can be done:
Code: Select all
'
'' PinIsOutput
'
' Determine if a given pin is an output.
'
Function PinIsOutput(ByVal pin as Byte) as Boolean
PinIsOutput = CBool(Register.DDR(pin) And PortMask(pin))
End Function
'
'' PinOutputState
'
' Determine the current state of a pin (assumed to be an output).
'
Function PinOutputState(ByVal pin as Byte) as Byte
If CBool(Register.Port(pin) And PortMask(pin)) Then
PinOutputState= 1
Else
PinOutputState= 0
End If
End Function
Note that you could also use Register.Pin(pin) instead of Register.Port(pin) in the function above. The advantage of doing so is that the former reads the actual state of the pin (including the effects of external circuitry, if any) while the latter simply indicates what the output
should be irrespective of the fact that external circuitry might be forcing the pin to a different state.