'---------------------------------------------------------------------- ' '' Module: PinFunc.bas ' ' Copyright 2005 Elba Corp. ' All rights reserved. ' ' Non-exclusive permission to use, copy, modify, and distribute this ' software for any purpose and without fee is hereby granted, provided: ' 1) that the above copyright notice is retained in the source code and, ' 2) that it is understood and agreed that no warranty of any kind, ' express or implied is provided. ' Use of this code in any manner shall be prima facie evidence of ' acceptance of these terms. ' ' You may contact the author by email at dkinzer@zbasic.net. ' '------------------------------------------------------------------------------- ' Overview ' This module provides some routines for manipulating I/O pins for ' the ZX-24. The code could be adapted to other ZX microcontrollers. '------------------------------------------------------------------------------- ' This table, indexed by (pinNumber - 24) gives the PortB bit ' corresponding to pins 25-27. Private pinTbl as ByteVectorData({ 7, 5, 4 }) '---------------------------------------------------------------------- ' '' PinState ' ' Determine the current state of a ZX-24 output pin. If an invalid pin number ' is passed, zero is returned. If the pin number of an input is passed, ' the result is meaningless. ' Function PinState(ByVal pin as Byte) as Byte Select Case pin Case 5 To 12 PinState = GetBit(Register.PortC, 12 - pin) Case 13 To 20 PinState = GetBit(Register.PortA, 20 - pin) Case 25 To 27 PinState = GetBit(Register.PortB, pinTbl(pin - 24)) Case Else PinState = 0 End Select End Function '---------------------------------------------------------------------- ' '' TogglePin ' ' Given a ZX-24 pin number, if it is an output, the output state is ' set to the opposite state. If the an invalid pin or an input pin is ' passed, no action is taken. ' Sub TogglePin(ByVal pin as Byte) If (PinIsOutput(pin) <> 0) Then Call PutPin(pin, PinState(pin) Xor 1) End If End Sub '---------------------------------------------------------------------- ' '' PinIsOutput ' ' Determine if a given ZX-24 pin is an output. If a valid pin is passed ' and that pin is an output, 1 is returned otherwise zero is returned. ' Function PinIsOutput(ByVal pin as Byte) as Byte Select Case pin Case 5 To 12 PinIsOutput = GetBit(Register.DDRC, 12 - pin) Case 13 To 20 PinIsOutput = GetBit(Register.DDRA, 20 - pin) Case 25 To 27 PinIsOutput = GetBit(Register.DDRB, pinTbl(pin - 24)) Case Else PinIsOutput = 0 End Select End Function