Pages

Tuesday, May 28, 2013

fongtoy biscuit 01 - MCP3208 8 channel 12 bit ADC biscuit completed!



Now I have connected MCP3208's inputs to 2.5V, 2.5V, 2.5V, 0V9, 2.5V, 2.5V, 2.5V, 3V3 and it gives the following almost perfect results.

# Analog voltage at channel number  0  =  2.498
# Analog voltage at channel number  1  =  2.499
# Analog voltage at channel number  2  =  2.499
# Analog voltage at channel number  3  =  0.0
# Analog voltage at channel number  4  =  2.498
# Analog voltage at channel number  5  =  2.499
# Analog voltage at channel number  6  =  2.499
# Analog voltage at channel number  7  =  3.303

.END

# *****************************************************************************
# Function - TestAdcMcp3208 
# ***************************************************************************** 

def TestMcp3208v03(): # v0.3 tlfong01 2013may28

    ftprint.PrintDoubleSpaceLine("*** Start testing MCP3208 ADC ***")   

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

    SingleEndMode = 0
    DifferentialMode = 1
    SingleEndModeFirstByte = 0x06
    DifferentialModeFirstByte = 0x04

    controlTripleByte = [0x00, 0x00, 0x00]
    resultTripleByte = [0x00, 0x00, 0x00]

    for channelNumber in range(0, 8, 1):
        controlTripleByte[0] = SingleEndModeFirstByte | (channelNumber >> 2)
        controlTripleByte[1] = channelNumber << 6
        controlTripleByte[2] = 0x00 # don't care, actually
        resultTripleByte = spiChannel.xfer2(controlTripleByte) 

# ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[0])
# ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[1])
        # ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[2])

        resultDecimal =  ((resultTripleByte[1] & 0x0f) * (2 ** 8)) + (resultTripleByte[2])
# print "resultDecimal = ", resultDecimal

        resultVoltage = (float(resultDecimal) / 4096) * 4.096     
        print "Analog voltage at channel number ", channelNumber, " = ", resultVoltage

    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3208 ***")

# pi@raspberrypi ~/fongtoy $ sudo python fongtoy.py
# *** Start Program - FongToy v1.26 tlfong01 2013may28 ***
# *** Start testing MCP3208 ADC ***
# Analog voltage at channel number  0  =  2.498
# Analog voltage at channel number  1  =  2.499
# Analog voltage at channel number  2  =  2.499
# Analog voltage at channel number  3  =  0.0
# Analog voltage at channel number  4  =  2.498
# Analog voltage at channel number  5  =  2.499
# Analog voltage at channel number  6  =  2.499
# Analog voltage at channel number  7  =  3.303

*** Stop testing MCP3208 ***


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

# ftadc.TestMcp3208v01()

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

ftadc.TestMcp3208v03() 

# *** Stop program message ***

ftprint.StopProgram()


#.END


# ftadc.py v1.4 tlfong01 2013may27

import spidev
import time

import ftprint
import ftspi

# *****************************************************************************
# Function - TestAdcMcp3208 
# ***************************************************************************** 

def TestMcp3208v03(): # v0.3 tlfong01 2013may28

    ftprint.PrintDoubleSpaceLine("*** Start testing MCP3208 ADC ***")   

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

    SingleEndMode = 0
    DifferentialMode = 1
    SingleEndModeFirstByte = 0x06
    DifferentialModeFirstByte = 0x04

    controlTripleByte = [0x00, 0x00, 0x00]
    resultTripleByte = [0x00, 0x00, 0x00]

    for channelNumber in range(0, 8, 1):
        controlTripleByte[0] = SingleEndModeFirstByte | (channelNumber >> 2)
        controlTripleByte[1] = channelNumber << 6
        controlTripleByte[2] = 0x00 # don't care, actually
        resultTripleByte = spiChannel.xfer2(controlTripleByte) 

# ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[0])
# ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[1])
        # ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[2])

        resultDecimal =  ((resultTripleByte[1] & 0x0f) * (2 ** 8)) + (resultTripleByte[2])
# print "resultDecimal = ", resultDecimal

        resultVoltage = (float(resultDecimal) / 4096) * 4.096     
        print "Analog voltage at channel number ", channelNumber, " = ", resultVoltage

    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3208 ***")

# pi@raspberrypi ~/fongtoy $ sudo python fongtoy.py
# *** Start Program - FongToy v1.26 tlfong01 2013may28 ***
# *** Start testing MCP3208 ADC ***
# Analog voltage at channel number  0  =  2.498
# Analog voltage at channel number  1  =  2.499
# Analog voltage at channel number  2  =  2.499
# Analog voltage at channel number  3  =  0.0
# Analog voltage at channel number  4  =  2.498
# Analog voltage at channel number  5  =  2.499
# Analog voltage at channel number  6  =  2.499
# Analog voltage at channel number  7  =  3.303

*** Stop testing MCP3208 ***

*** Stop Program ***

pi@raspberrypi ~/fongtoy $ date
Tue May 28 09:26:40 UTC 2013



def TestMcp3208v02(inputMode, channelNumber): # v0.2 tlfong01 2013may28

    # ftprint.PrintDoubleSpaceLine("*** Start testing MCP3208 ADC ***")   

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

    time.sleep(0.1)

    CommonMode = 0
    DifferentialMode = 1
    SingleEndModeFirstByte = 0x06
    DifferentialModeFirstByte = 0x04

    controlTripleByte = [0x00, 0x00, 0x00]
    resultTripleByte = [0x00, 0x00, 0x00]

    controlTripleByte[0] = SingleEndModeFirstByte | (channelNumber >> 2)
    controlTripleByte[1] = channelNumber << 6
    controlTripleByte[2] = 0x00 # don't care, actually

    resultTripleByte = spiChannel.xfer2(controlTripleByte) 

    # ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[0])
    # ftprint.PrintEightBitPattern("ADC output byte 1 = ", resultTripleByte[1])
    # ftprint.PrintEightBitPattern("ADC output byte 2 = ", resultTripleByte[2])

    resultDecimal =  (resultTripleByte[1] * (2 ** 8)) + (resultTripleByte[2])
    resultVoltage = (float(resultDecimal) / 4096) * 4.096     
    print "Analog voltage at channel number ", channelNumber, " = ", resultVoltage

    resultDecimal =  (resultTripleByte[1] * (2 ** 8)) + (resultTripleByte[2])
    resultVoltage = (float(resultDecimal) / 4096) * 4.096     
    print "Analog voltage at channel number ", channelNumber, " = ", resultVoltage
    
    resultDecimal =  (resultTripleByte[1] * (2 ** 8)) + (resultTripleByte[2])
    resultVoltage = (float(resultDecimal) / 4096) * 4.096     
    print "Analog voltage at channel number ", channelNumber, " = ", resultVoltage

    spiChannel.close() 

    time.sleep(0.1)

    # ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3208 ***")


def TestMcp3208v01(): # v0.1 tlfong01 2013may27

    ftprint.PrintDoubleSpaceLine("*** Start testing MCP3208 ADC ***")   

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

    controlTripleByteSingleEndChannel0 = [0x06, 0x00, 0x00]
    resultTripleByte = [0x00, 0x00, 0x00]
    resultTripleByte = spiGuzuntyPi.xfer2(controlTripleByteSingleEndChannel0) 

    ftprint.PrintEightBitPattern("ADC output byte 0 = ", resultTripleByte[0])
    ftprint.PrintEightBitPattern("ADC output byte 1 = ", resultTripleByte[1])
    ftprint.PrintEightBitPattern("ADC output byte 2 = ", resultTripleByte[2])

    resultDecimal =  (resultTripleByte[1] * (2 ** 8)) + (resultTripleByte[2])

    resultVoltage = (float(resultDecimal) / 4096) * 4.096 
    
    print "Analog voltage = ", resultVoltage

    spiGuzuntyPi.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3208 ***") 

# *****************************************************************************
# Function - TestAdcMcp3201() 
# Description - 
# Sample call - 
# ***************************************************************************** 

def TestMcp320101(): #v1.3 tlfong01 2013may23

    ftprint.PrintDoubleSpaceLine("*** Start testing MCP3201 ADC ***")   

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

    DummyDoubleByteList = [0x00, 0x00]
    adcOutputDoubleByteList = [0x55, 0x55]
    adcOutputDoubleByteList = spiGuzuntyPi.xfer2(DummyDoubleByteList) 

    ftprint.PrintEightBitPattern("ADC output byte 1 = ", adcOutputDoubleByteList[0])
    ftprint.PrintEightBitPattern("ADC output byte 2 = ", adcOutputDoubleByteList[1])

    adcDecimalValue = (adcOutputDoubleByteList[1] >> 1) + (adcOutputDoubleByteList[0] * (2 ** 7))

    # adcAnalogVoltage = (float(adcDecimalValue) / 4096) * 4.10 # without half voltage divider
    adcAnalogVoltage = ((float(adcDecimalValue) / 4096) * 4.10) * 2 # with half voltage divider
    
    print "Analog voltage = ", adcAnalogVoltage

    spiGuzuntyPi.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3201 ***") 

# ***************************************************************************** 
# Function - TestAdcMcp3201() 
# Description - 
# Sample call - 
# Sample output -
# *** Start testing MCP3201 ADC ***
# ADC output byte 1 =  00001011
# ADC output byte 2 =  11111111
# Analog voltage   3.07299804687
# *** Stop testing MCP3201 ***
# ***************************************************************************** 

def TestMcp320102(spiChannel):

    ftprint.PrintDoubleSpaceLine("*** Start testing MCP3201 ADC ***")   

    DummyDoubleByteList = [0x00, 0x00]
    adcOutputDoubleByteList = [0x55, 0x55]
    adcOutputDoubleByteList = spiChannel.xfer2(DummyDoubleByteList) 

    ftprint.PrintEightBitPattern("ADC output byte 1 = ", adcOutputDoubleByteList[0])
    ftprint.PrintEightBitPattern("ADC output byte 2 = ", adcOutputDoubleByteList[1])

    adcDecimalValue = (adcOutputDoubleByteList[1] >> 1) + (adcOutputDoubleByteList[0] * (2 ** 7))

    # adcAnalogVoltage = (float(adcDecimalValue) / 4096) * 4.10 # without half voltage divider
    adcAnalogVoltage = ((float(adcDecimalValue) / 4096) * 4.10) * 2 # with half voltage divider
    
    print "Analog voltage = ", adcAnalogVoltage

    ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3201 ***") 


def TestMcp320103(spiChannel, testTime, testCount):

    ftprint.PrintDoubleSpaceLine("*** Start testing MCP3201 ADC ***")   

    for i in range(testCount):
        DummyDoubleByteList = [0x00, 0x00]
        adcOutputDoubleByteList = [0x55, 0x55]
        adcOutputDoubleByteList = spiChannel.xfer2(DummyDoubleByteList) 
time.sleep(testTime)

    ftprint.PrintEightBitPattern("ADC output byte 1 = ", adcOutputDoubleByteList[0])
    ftprint.PrintEightBitPattern("ADC output byte 2 = ", adcOutputDoubleByteList[1])

    adcDecimalValue = (adcOutputDoubleByteList[1] >> 1) + (adcOutputDoubleByteList[0] * (2 ** 7))

    # adcAnalogVoltage = (float(adcDecimalValue) / 4096) * 4.10 # without half voltage divider
    adcAnalogVoltage = ((float(adcDecimalValue) / 4096) * 4.10) * 2 # with half voltage divider
    
    print "Analog voltage = ", adcAnalogVoltage

    ftprint.PrintDoubleSpaceLine("*** Stop testing MCP3201 ***") 

# .END

.END

No comments:

Post a Comment