Thursday, 4 July 2013

Demux module debugged

I checked out the ftdemux module and found that I forgot how I assigned the MCP23017 output pins and the HC137 enable pins.  After a bit of tidying up things. The demux is now working OK.

So I can now use MCP23S17 to select 1 of 8 devices at 3V3, and at the same time 1 of another 8 SPI devices at 5V0.

.END

# *** Demux ***
ftdemux.TestDemux(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 6)

def TestDemux(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(1.0)

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

    DisableDemux(spiChannel, spiIoxSubAddress)

    spiChannel.close()

def DisableDemux(spiChannel, spiIoxSubAddress):

    DisableHC137ControlByte = 0x20

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

def EnableDemux(spiChannel, spiIoxSubAddress):

    EnableHC137ControlByte  = 0x10 # (bit 5 HC137 ~E1 = Low, bit 4 HC137 E2 = High)

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

.END

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

# fongtoy v2.x 2013

ProgramTitle = "FongToy v2.1 tlfong01 2013jul04"

import sys 
import time 
import smbus 
import pdb 
import spidev 
import wiringpi
import wiringpi2
import RPIO as GPIO  
from RPIO import PWM 
from enum import Enum 
from subprocess import call

import ftgpio
import ftprint
import ftspi
import ftiox
import fteeprom
import ftguzuntypi
import ftdemux
import fttest
import ftadc
import ftwebcam

# *** Troubleshooting functions ***

# *** GPIO tests v1.3 tlfong01 2013may23 ***
# ftgpio.TestLed()
# ftgpio.TestBuzzer()
# ftgpio.TestButtonEchoBuzzer()
# ftgpio.TestButtonEchoLed()

# *** SPI Tests v1.3 tlfong01 2013may23 ***

# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 2, testStartAddress = 0x0123, testWriteDataByte = 0x5a)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)

# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0411, testWriteDataByte = 0x4c)
# fttest.TestDemuxGuzuntyClock(mcp23s17SubAddress = 0, guzuntyClockDemuxAddress = 2, secondCount = 10)

# *** Old tests ***

# *** SPI loop back ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60000)

# *** ADC ***
# ftadc.TestMcp3208v03() 
# ftadc.TestMcp3201v04()

# *** EEPROM ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x4100, testDataByte = 0x55)

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

# *** Guzunty Pi Step Motor ***
# ... test functions already moved to ftguzunti.py tlfong01 2013jun10 ...

# *** Webcam ***

# ftwebcam.TestWebcam()
# ftwebcam.TestWebcam01()
# ftwebcam.TestWebcam03(imageHoldSeconds = 10)
# ftwebcam.TestWebcamCamera04()
# ftwebcam.TestWebcamCamera05()
# ftwebcam.TestWebcamCamera06()

# ftwebcam.TestWebcamCamera07()

# ftwebcam.TestWebcamCamera08(cameraNumber = 0, imageSize = (640, 480), imageCount = 10, timerSecond = 1)
# ftwebcam.TestWebcamCamera08(cameraNumber = 1, imageSize = (640, 480), imageCount = 10, timerSecond = 1)
# ftwebcam.TestWebcamCamera08(cameraNumber = 0, imageSize = (1280, 720), imageCount = 10, timerSecond = 1)
# ftwebcam.TestWebcamCamera08(cameraNumber = 1, imageSize = (1280, 720), imageCount = 10, timerSecond = 1)

# ftwebcam.TestWebcamCamera09(cameraNumber = 0, imageSize = (1280, 720), imageCount = 120, timerSecond = 0.5, fileName = "file2013jun24hkt1044.bmp")

# *** Main program ***

# *** Start program message ***
ftprint.StartProgram(ProgramTitle)

# *** SPI loop back ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60)

# *** EEPROM ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 0, startAddress = 0x4100, testDataByte = 0x55)

# *** Mcp23s17 ***
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)

# *** Demux ***
ftdemux.TestDemux(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 6)


# *** Stop program message ***
ftprint.StopProgram()

# .END



# *****************************************************************************
# 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 - last modified 2013jul04
# *****************************************************************************

# *****************************************************************************
# 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. Loop select and hold n second HC137 output Y0 to Y7
#   5. Select and hold n second HC137 selected output 
#   6. Disable HC137 (all outputs High)   
#   7. Close SPI channel

# Sample call - 
#   ftdemux.TestDemux(spiChannelNumber = 0, spiChipEnableNumber = 0, 
#                     spiIoxSubAddress = 0, spiSlaveDeviceNumber = 6)
# *****************************************************************************
  
def TestDemux(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(1.0)

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

    DisableDemux(spiChannel, spiIoxSubAddress)

    spiChannel.close()

# *****************************************************************************
# Demux Basic functions - last modified 2013jul04
# *****************************************************************************

def SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveSubAddress):

    EnableHC137ControlByte  = 0x10 # (bit 5 HC137 ~E1 = Low, bit 4 HC137 E2 = High)
                      
    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 # (bit 5 HC137 ~E1 = Low, bit 4 HC137 E2 = High)

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

# .END

.END


No comments:

Post a Comment