Pages

Tuesday, May 28, 2013

MCP3208 troubleshooting notes

Now I am trying different things to find which timing is critical.  So I insert time delays after each channel, after setting up SPI channels, doing 3 ADCs for the same channel.

One thing I found out is that if a channel not converting correct, then 3 times so.


Analog voltage at channel number  0  =  2.498
Analog voltage at channel number  0  =  2.498
Analog voltage at channel number  0  =  2.498

Analog voltage at channel number  1  =  59.761
Analog voltage at channel number  1  =  59.761
Analog voltage at channel number  1  =  59.761

Analog voltage at channel number  2  =  3.325
Analog voltage at channel number  2  =  3.325
Analog voltage at channel number  2  =  3.325

Analog voltage at channel number  3  =  0.0
Analog voltage at channel number  3  =  0.0
Analog voltage at channel number  3  =  0.0

Analog voltage at channel number  4  =  3.331
Analog voltage at channel number  4  =  3.331
Analog voltage at channel number  4  =  3.331

Analog voltage at channel number  5  =  58.831
Analog voltage at channel number  5  =  58.831
Analog voltage at channel number  5  =  58.831

Analog voltage at channel number  6  =  59.843
Analog voltage at channel number  6  =  59.843
Analog voltage at channel number  6  =  59.843

Analog voltage at channel number  7  =  57.344
Analog voltage at channel number  7  =  57.344
Analog voltage at channel number  7  =  57.344


ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 0) 
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 1) 
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 2) 
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 3) 
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 4) 
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 5)
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 6) 
time.sleep(1)
ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 7) 
time.sleep(1)

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

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

.END

No comments:

Post a Comment