Wednesday, 3 July 2013

Test Demux module refreshing memory notes

Now I am reading my old python modules to refresh my memory on how did I do demux some weeks ago.

I found that I used the following function to select slave SPI device #5, and the LED 5 should be switched on.

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


One slight problem is that the TestSelectSpiSlaveDevice() seems changed to TestDemuxV01().  So I will try this first.

.END




# ftdemux.py v1.3 tlfong01 2013may29

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

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

import time
import spidev

import ftprint
import ftiox
import ftspi

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

# *****************************************************************************
# Function - TestDemuxV01
# Description - 
#   Disable HC137 all outputs High
#   Enable HC137 any 1 output Low
#   Set HC137 selected output Low
#   Set HC137 output Y0 to Y7 in sequence, hold 1 second
# Sample call - 
#   TestDemuxV01(spiChannelNumber = 0, spiChipEnableNumber = 0, 
#   mcp23s17SubAddress = 0, spiSlaveDeviceNumber = 5)
# *****************************************************************************
  
def TestDemuxV01(spiChannelNumber, spiChipEnableNumber, spiIoxSubAddress, spiSlaveDeviceNumber):

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

    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


# fttest.py v1.21 tlfong01 2013may22

import time
import spidev

import ftprint
import ftiox
import ftspi
import fteeprom
import ftdemux
import ftguzuntypi
import ftadc

# *****************************************************************************
# Function - TestDemuxEeprom()
# *****************************************************************************

def TestDemuxEeprom(mcp23s17SubAddress, eepromDemuxAddress, testStartAddress, testWriteDataByte):

    # *** Setup SPI channels MCP23s17Demux and EEPROM ***

    spiChannelDemux = spidev.SpiDev() 
    spiChannelDemux.open(0, 0)

    spiChannelEeprom = spidev.SpiDev() 
    spiChannelEeprom.open(0, 1)

    # *** Select EEPROM #1 ***

    ftdemux.SelectSpiSlaveDevice(spiChannelDemux, mcp23s17SubAddress, eepromDemuxAddress)
    print "\n\n*** EEPROM demux address = ", eepromDemuxAddress, " ***"

    # *** Test EEPROM ***

    fteeprom.WriteEepromDataByte(spiChannelEeprom, testStartAddress, testWriteDataByte)
    print "\nWrite start address = ", hex(testStartAddress)
    print "Write data byte = ", hex(testWriteDataByte)

    readBackDataByte = fteeprom.ReadEepromDataByte(spiChannelEeprom, testStartAddress)
    print "\nRead back start address = ", hex(testStartAddress)
    print "Read back data byte  = ", hex(readBackDataByte)

    if (testWriteDataByte == readBackDataByte):
      print "\n ### EEPROM good. ###"
    else:
      print "\n ### EEPROM bad!!! ###"

    # *** Reset demux, close channels ***
### ftdemux.ResetDemux(spiChannelDemux, mcp23s17SubAddress)
    spiChannelDemux.close() 
    spiChannelEeprom.close() 

# *****************************************************************************
# Function - TestDemuxGuzuntyClock()
# *****************************************************************************

def TestDemuxGuzuntyClock(mcp23s17SubAddress, guzuntyClockDemuxAddress, secondCount):

    # *** Setup SPI channels for MCP23s17Demux and Guzunty clock ***

    spiChannelDemux = spidev.SpiDev() 
    spiChannelDemux.open(0, 0)

    spiChannelGuzuntyClock = spidev.SpiDev() 
    spiChannelGuzuntyClock.open(0, 1)

    # *** Select GuzuntyClock ***

    ftdemux.SelectSpiSlaveDevice(spiChannelDemux, mcp23s17SubAddress, guzuntyClockDemuxAddress)
    print "\n\n*** Guzunty Clcok demux address = ", guzuntyClockDemuxAddress, " ***"

    # *** Test Guzunty Clock ***

    ftguzuntypi.TestGuzuntyClock(spiChannelGuzuntyClock, secondCount)

    # *** Reset demux, close channels ***

### ftdemux.ResetDemux(spiChannelDemux, mcp23s17SubAddress)
    spiChannelDemux.close() 
    spiChannelGuzuntyClock.close()


# *****************************************************************************
# Function - TestMcp320102()
# *****************************************************************************

def TestMcp320101():

    # *** Setup SPI channel ***

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 1)

    # *** Test Device ***

    print "Testing MCP3201, ... "
    ftadc.TestMcp320102(spiChannel)


# *****************************************************************************
# Function - TestMcp320103()
# *****************************************************************************

def TestMcp320103(testTime, testCount):

    # *** Setup SPI channel ***

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 1)

    # *** Test Device ***

    print "Testing MCP3201, ... "
    ftadc.TestMcp320103(spiChannel, testTime, testCount)

# .END

# fongtoy v1.x 2013

ProgramTitle = "FongToy v1.15 tlfong01 2013jun24"

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)

# *** Main program ***

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

# *** Main 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)

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

# *** 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")

# *** Stop program message ***

ftprint.StopProgram()

# .END

.END

No comments:

Post a Comment