Wednesday, 3 July 2013

Testing demux, no luck

So I ran the test demux program.  But the HC137 output LEDs do not show anything.  So next step is troubleshooting.

.END

# *****************************************************************************
# Module - fongtoy.py
# Description - main test program
# *****************************************************************************

# fongtoy v2.x 2013

ProgramTitle = "FongToy v2.0 tlfong01 2013jul03"

...

ftdemux.TestDemuxV01(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)

...


# *****************************************************************************
# Module - ftdemux.py
# Description - HC137 1 of 8 demultiplexer
# *****************************************************************************

# ftdemux.py v2.0 tlfong01 2013jul03

# *****************************************************************************
# Imports
# *****************************************************************************

import time
import spidev

import ftprint
import ftiox
import ftspi

# *****************************************************************************
# Demux Test functions
# *****************************************************************************

# *****************************************************************************
# Function - TestDemuxV01
# Description -
#   This function does the following.
#   1. Set up SPI channel for MCP23S17
#   2. Disable HC137 (all outputs High)
#   3. Enable HC137 (any 1 output Low)
#   4. Set HC137 output Y0 to Y7 in sequence, hold 1 second
#   5. Set HC137 selected output Low
#   6. Disable HC137 (all outputs High)  
#   7. Close SPI channel
# Sample call -
#   TestDemuxV01(spiChannelNumber = 0, spiChipEnableNumber = 0,
#   mcp23s17SubAddress = 0, spiSlaveDeviceNumber = 5)
# *****************************************************************************

def TestDemuxV01(spiChannelNumber, spiChipEnableNumber, spiIoxSubAddress, spiSlaveDeviceNumber):

    spiChannel = spidev.SpiDev()
    spiChannel.open(spiChannelNumber, spiChipEnableNumber)

    print "spiChannelNumber    = ", spiChannelNumber
    print "spiChipEnableNumber = ", spiChipEnableNumber
    print "MCP23S17 Address    = ", spiIoxSubAddress
    print "HC137 Input Address = ", spiSlaveDeviceNumber

    DisableDemux(spiChannel, spiIoxSubAddress)
    time.sleep(0.5)

    EnableDemux(spiChannel, spiIoxSubAddress)
    time.sleep(0.5)

    for i in range(0, 8, 1):
        SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, i)
time.sleep(0.5)

    SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveDeviceNumber)
    time.sleep(1)

    DisableDemux(spiChannel, spiIoxSubAddress)

    spiChannel.close()

# *****************************************************************************
# Demux Basic functions
# *****************************************************************************

def SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveSubAddress):

    EnableHC137ControlByte  = 0x10
                   
    LoadHC137ControlByte   = 0b000000000 # bit 3 = 0 load address
    LatchHC137ControlByte  = 0b000001000 # bit 3 = 1 latch address

    loadAddressControlByte  = (EnableHC137ControlByte | spiSlaveSubAddress) | LoadHC137ControlByte
    latchAddressControlByte = (EnableHC137ControlByte | spiSlaveSubAddress) | LatchHC137ControlByte

    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, loadAddressControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, latchAddressControlByte)

    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, loadAddressControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, latchAddressControlByte)

def DisableDemux(spiChannel, spiIoxSubAddress):

    DisableHC137ControlByte = 0x20

    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)

def EnableDemux(spiChannel, spiIoxSubAddress):

    EnableHC137ControlByte  = 0x10

    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, EnableHC137ControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, EnableHC137ControlByte)

# .END

No comments:

Post a Comment