Pages

Wednesday, May 29, 2013

SPI MCP23S17 based dual HC137 1 of 3 demulti9plexer module refactoring notes




































Now I have grouped the 2 HC137 decoder and the MCP23S17 IOx on a 1/2 A4 size PCB.  So the coupling is only one 5 wire SPI cable.

.END


# fongtoy v1.26 tlfong01 2013may28

ProgramTitle = "FongToy v1.26 tlfong01 2013may28"

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

# *** Main program ***

# *** Start program message ***

ftprint.StartProgram(ProgramTitle)

# *** Troubleshooting functions ***

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

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

# ftspi.TestSpiLoopBack(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testCount = 1000, testTime = 0.001)
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# fteeprom.TestWriteReadEepormDataByte(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x0410, testDataByte = 0x55)
# ftguzuntypi.TestGuzuntyPi4digit7segmentLedModule(spiChannelNumber = 0, spiChipEnableNumber = 1)
# 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)

# *** Current test functions ***

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

# fttest.TestMcp320103(testTime = 0.1, testCount = 10)
# fttest.TestMcp320103(testTime = 0.01, testCount = 100)
# fttest.TestMcp320103(testTime = 0.05, testCount = 50)

# fttest.TestMcp320103(testTime = 0.1, testCount = 1)

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

# ftadc.TestMcp320104()

# ftadc.TestMcp3208v01()

# ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 0) 

# *** ADC tests ***

# ftadc.TestMcp3208v03() 
# ftadc.TestMcp3201v04()

# *** EEPROM tests ***

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

# *** Mcp23s17 tests ***

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

# *** Demux tests ***

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

# *** Stop program message ***

ftprint.StopProgram()

#.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


.END


No comments:

Post a Comment