Pages

Monday, May 27, 2013

MCP3208 test notes






























Now I am doing the following tests.

1.  Test MCP3201 OK.  But the output is not stable.  Instead of giving correct results 2.4V and 3.2V, it often gives wrong results 52 volt something.

2.  Remove MCP3208 from the sockets, and jumper short MCP3208 sockets' MOSI and MISO pins, then test SPI loop back.  SPI loop back reads correct data when MOSI and MISO are shorted, otherwise give all zero results.

3. I also use the scope to display the SPI clock and CE1 to make sure that the signals and voltage level look OK.

Now the time have come to test the real thing MCP3208.

.END

# ftmain v1.23 tlfong01 2013may27

ProgramTitle = "FongToy v1.23 tlfong01 2013may27 "

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.TestMcp320101()

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

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

# *** Stop program message ***

ftprint.StopProgram()

#.END



*** Start Program - FongToy v1.23 tlfong01 2013may27  ***

Testing MCP3201, ...

*** Start testing MCP3201 ADC ***

ADC output byte 1 =  00000000
ADC output byte 2 =  00000000
Analog voltage =  0.0

*** Stop testing MCP3201 ***

Testing SPI loop back, ...
Output data byte list =  0x55
Input  data byte list =  0x55

*** Stop Program ***

# ftspi.py v1.1 tlfong01 2013may21

import spidev
import time

# *****************************************************************************
# Function - SpiWrite()
# Description - Output data byte list and read data byte list
# *****************************************************************************

def SpiWrite(spiChannel, spiWriteList): 
    spiReadList = spiChannel.xfer2(spiWriteList)
    return spiReadList


# *****************************************************************************
# Function - TestSpiLoopBack01()
# Description - 
#   * Echos MOSI on MISO
#   * Write/Read testByte x 4 testCount number of times, display last time write and read
# Hardware setup - connect MOSI to MSIO
# Function call - ftspi.TestSpiLoopBack(spiChannenNumber = 0, spiChipEnableNumber = 1, testByte = 0x55, testCount = 10000, testTime = 0.001)
# Notes -
#   * Use scope to check clock
#   * Only echoes MOSI by MISO, does not bother clock and chip select signals
# *** Start Program - FongToy v1.0 tlfong01 2013may20  ***
# Sample output - 
# Testing SPI loop back, ...
# Output data byte list =  0x55 0x55 0x55 0x55
# Input  data byte list =  0x55 0x55 0x55 0x55
# *****************************************************************************

def TestSpiLoopBackV01(spiChannelNumber, spiChipEnableNumber, testDataByte, testTime, testCount): 

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

    print "Testing SPI loop back, ..."

    outputDataByteList = [testDataByte]
    print "Output data byte list = ", hex(outputDataByteList[0]) 

    inputDataByteList = [0x00]

    for i in range(testCount):
        inputDataByteList = SpiWrite(spiChannel, outputDataByteList)
time.sleep(testTime)

    print "Input  data byte list = ", hex(inputDataByteList[0]) 


# *****************************************************************************
# Function - TestSpiLoopBack()
# Description - 
#   * Echos MOSI on MISO
#   * Write/Read testByte x 4 testCount number of times, display last time write and read
# Hardware setup - connect MOSI to MSIO
# Function call - ftspi.TestSpiLoopBack(spiChannenNumber = 0, spiChipEnableNumber = 1, testByte = 0x55, testCount = 10000, testTime = 0.001)
# Notes -
#   * Use scope to check clock
#   * Only echoes MOSI by MISO, does not bother clock and chip select signals
# *** Start Program - FongToy v1.0 tlfong01 2013may20  ***
# Sample output - 
# Testing SPI loop back, ...
# Output data byte list =  0x55 0x55 0x55 0x55
# Input  data byte list =  0x55 0x55 0x55 0x55
# *****************************************************************************

def TestSpiLoopBack(spiChannelNumber, spiChipEnableNumber, testDataByte, testCount, testTime): # v1.3 tlfong01 2013may23

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

    outputDataByteList = [testDataByte, testDataByte, testDataByte, testDataByte]
    print "Output data byte list = ", hex(outputDataByteList[0]), hex(outputDataByteList[1]), hex(outputDataByteList[2]), hex(outputDataByteList[3]) 

    inputDataByteList = [0x00, 0x00, 0x00, 0x00]
    for i in range(testCount):
        inputDataByteList = SpiWrite(testSpi, outputDataByteList)
time.sleep(testTime)
    print "Input  data byte list = ", hex(inputDataByteList[0]), hex(inputDataByteList[1]), hex(inputDataByteList[2]), hex(inputDataByteList[3]) 

# * WiringPi Python wrapping SPI test functions *******************************

def TestWiringPiSpi():
    print "\n" + "*** Start testing wiringPi SPI, ... ***" + "\n"

    spi = spidev.SpiDev() # create spidev object
    spi.open(0,0) # open SPI0, CE0_N

    SendByteList = [0x55, 0xaa, 0xAA]
    ReadByteList = spi.xfer2(SendByteList)
    print "Bytes read = " + hex(ReadByteList[0]) + " " + hex(ReadByteList[1]) + " " + hex(ReadByteList[2])

    print "\n" + "*** Stop testing wiringPi SPI, ... ***" + "\n"


# * SPI using bit banging (from Arduino/Netduino, not tested here *************

# SpiClockPin = RPiGpioGen11
# SpiMosiPin = RPiGpioGen10
# SpiMisoPin = RPiGpioGen9
# SpiSelect0Pin = RPiGpioGen8
# SpiSelect1Pin = RPiGpioGen7

def TestRfm12b(registerBaseAddress):
    SetupGPIOpins(OutputPinList, InputPinWithNoPullUpList, InputPinWithPullUpList )

def SpiSelectDevice(deviceNumber):
    if (deviceNumber == 0):
        writeOutputPin(SpiSelect0Pin, Low)  
    else:
        writeOutputPin(SpiSelect1Pin, Low)

def SpiDisSelectDevice(deviceNumber):
    if (deviceNumber == 0):
        writeOutputPin(SpiSelect0Pin, High)  
    else:
        writeOutputPin(SpiSelect1Pin, High)

def SpiClockPulse():
    writeOutputPin(SpiClockPin, High)
    time.sleep(1)
    writeOutputPin(SpiClockPin, Low)
    time.sleep(1)

def SpiWriteBit(dataBit):
    if (dataBit == 1):
        writeOutputPin(SpiMosiPin, High)        
    else:
        writeOutputPin(SpiMosiPin, Low)

def SpiReadBit(inputPin):
    dataBit  = readInputPin(inputPin)                
    # print dataBit
    return dataBit

# * Test Spi functions *

def TestSpiSelectDevice(deviceNumber, count):
    print "Now testing SPI device select pin", deviceNumber, ",..."
    for i in range (count):
        SpiSelectDevice(deviceNumber)
        time.sleep(1)
        SpiDisSelectDevice(deviceNumber)
time.sleep(1)

def TestSpiClockPulse(count):
    for i in range (count):
       SpiClockPulse()

def TestSpiWriteBit(count):
    for i in range (count):
        SpiWriteBit(1)
time.sleep(1)
SpiWriteBit(0)
time.sleep(1)

def TestSpiReadBit(inputPin, count):
    for i in range (count):
        dataBit = SpiReadBit(inputPin)    
if (dataBit == True):
   dataLevel = "High"
        else:
   dataLevel = "Low"
        print "dataBitRead at pin number ", inputPin, " = ", dataLevel
        time.sleep(1)

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

.END


No comments:

Post a Comment