.END
# *** Mcp23s17 ***
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# ftiox.py v1.3 tlfong01 2013may29
# *****************************************************************************
# Module - ftiox.py
# Description - IO expander functions using I2C MCP230x, SPI MCP23sxx
# *****************************************************************************
# *****************************************************************************
# Imports
# *****************************************************************************
import time
import spidev
import ftspi
# *****************************************************************************
# Constants and variables
# *****************************************************************************
PortA = 0
PortB = 1
InputOutputDirectionIndex = 0
InputPolarityIndex = 1
InterruptEnableIndex = 2
DefaultValueIndex = 3
CompareModeIndex = 4
BankInterruptPinModeIndex = 5
PullUpIndex = 6
InterruptFlagIndex = 7
InterruptCaptureIndex = 8
PortStatusIndex = 9
OutputLatchIndex = 10
All8pinOutput = 0x00
All8pinInput = 0xff
All8bitOne = 0xff
All8bitZero = 0x00
All8bitPullUp = 0xff
Upper8bitOneLower8bitZero = 0xff00
Upper8bitZeroLower8bitOne = 0x00ff
Upper4bitOneLower4bitZero = 0xf0
Upper4bitZeroLower4bitOne = 0x0f
Upper8pinInputLower8pinOutput = 0xff00
Upper8pinOutputLower8pinInput = 0x00ff
Upper4pinInputLower4pinOutput = 0xf0
Upper4pinOutputLower4pinInput = 0x0f
DataByte0x55 = 0x55
DataByte0xaa = 0xaa
DataByte0x00 = 0x00
DataByte0xff = 0xff
RegisterAddressArrayMcp23s17 = [0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14,
0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x1d, 0x0f, 0x11, 0x13, 0x15]
Mcp23s17WriteCommand = 0b01000000
# *****************************************************************************
# MCP23s17 Test functions
# *****************************************************************************
# *****************************************************************************
# Function - TestMcp23s17BlinkLed
# Description - Blink the 16 LEDs conntected to MCP23s17 Port A and Port B
# Sample call -
# TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber1bit = 0, spiChipSubAddress = 0)
# *****************************************************************************
def TestMcp23s17BlinkLed(spiChannelNumber, spiChipEnableNumber, spiChipSubAddress):
spiChannel = spidev.SpiDev()
spiChannel.open(spiChannelNumber, spiChipEnableNumber)
SetupMcp23s17Ports(spiChannel, spiChipSubAddress, portAsetupByte = All8pinOutput, portBsetupByte = All8pinOutput)
# *** Write Port A and Port B with constants ***
DisableHC137ControlByte = 0x20 # b0010 0000 = CS1 Low, CS2 High, All Yn High
EnableHC137ControlByte = 0x10 # b0001 0000 = CS1 High, CS2 Low, 1 of 8 Yn low
# WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiChipSubAddress, EnableHC137ControlByte)
# WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiChipSubAddress, EnableHC137ControlByte)
# WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiChipSubAddress, DisableHC137ControlByte)
# WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiChipSubAddress, DisableHC137ControlByte)
# while True:
# pass
print "Now blinking LEDs, ... "
# *** Blink Port A and Port B LEDs ***
for i in range (10):
# *** Write 0x55 to Port A and 0xaa to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x55)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0xaa)
time.sleep(0.5)
# *** Write 0xaa to Port A and 0x55 to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0xaa)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0x55)
time.sleep(0.5)
for i in range (10):
# *** Write 0x00 to Port A and 0x00 to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x00)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0x00)
time.sleep(0.5)
# *** Write 0xff to Port A and 0xff to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0xff)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0xff)
time.sleep(0.5)
# *****************************************************************************
# Basic MCP23s17 functions
# *****************************************************************************
def SetupMcp23s17Ports(spiChannel, spiChipSubAddress, portAsetupByte, portBsetupByte):
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, InputOutputDirectionIndex, PortA, portAsetupByte)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, InputOutputDirectionIndex, PortB, portBsetupByte)
def WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiChipSubAddress, dataByte):
mcp23s17WriteCommand = Mcp23s17WriteCommand | (spiChipSubAddress << 1)
mcp23s17RegisterAddress = GetRegisterAddress(RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA)
spiWriteList = [mcp23s17WriteCommand, mcp23s17RegisterAddress, dataByte]
ftspi.SpiWrite(spiChannel, spiWriteList)
def WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiChipSubAddress, dataByte):
mcp23s17WriteCommand = Mcp23s17WriteCommand | (spiChipSubAddress << 1)
mcp23s17RegisterAddress = GetRegisterAddress(RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB)
spiWriteList = [mcp23s17WriteCommand, mcp23s17RegisterAddress, dataByte]
ftspi.SpiWrite(spiChannel, spiWriteList)
def WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, registerAddressArray, registerIndex, portType, dataByte):
mcp23s17WriteCommand = Mcp23s17WriteCommand | (spiChipSubAddress << 1)
mcp23s17RegisterAddress = GetRegisterAddress(registerAddressArray, registerIndex, portType)
mcp23s17WriteDataByte = dataByte
# PrintEightBitPattern("mcp23s17WriteCommand = ", mcp23s17WriteCommand)
# PrintEightBitPattern("mcp23s17RegisterAddress = ", mcp23s17RegisterAddress)
# PrintEightBitPattern("mcp23s17WriteDataByte = ", mcp23s17WriteDataByte)
spiWriteList = [mcp23s17WriteCommand, mcp23s17RegisterAddress, mcp23s17WriteDataByte]
ftspi.SpiWrite(spiChannel, spiWriteList)
def GetRegisterAddress(registerAddressArray, registerIndex, portType):
if (portType == PortA):
registerAddress = registerAddressArray[registerIndex]
if (portType == PortB):
registerAddress = registerAddressArray[registerIndex + 11]
return registerAddress
# .END
No comments:
Post a Comment